Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2013 bigbiff/Dees_Troy TeamWin |
| 3 | This file is part of TWRP/TeamWin Recovery Project. |
| 4 | |
| 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <string.h> |
| 20 | |
| 21 | extern "C" { |
| 22 | #include "../twcommon.h" |
| 23 | #include "../minuitwrp/minui.h" |
| 24 | } |
| 25 | |
| 26 | #include "rapidxml.hpp" |
| 27 | #include "objects.hpp" |
| 28 | #include "../data.hpp" |
| 29 | |
that | ae4b12e | 2015-02-06 00:23:05 +0100 | [diff] [blame] | 30 | const int SCROLLING_SPEED_DECREMENT = 6; // friction |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 31 | const int SCROLLING_FLOOR = 10; // minimum pixels for scrolling to start or stop |
that | ae4b12e | 2015-02-06 00:23:05 +0100 | [diff] [blame] | 32 | const int SCROLLING_MULTIPLIER = 1; // initial speed of kinetic scrolling |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 33 | const float SCROLLING_SPEED_LIMIT = 2.5; // maximum number of items to scroll per update |
| 34 | |
| 35 | GUIScrollList::GUIScrollList(xml_node<>* node) : GUIObject(node) |
| 36 | { |
| 37 | xml_attribute<>* attr; |
| 38 | xml_node<>* child; |
| 39 | int header_separator_color_specified = 0, header_separator_height_specified = 0, header_text_color_specified = 0, header_background_color_specified = 0; |
| 40 | |
| 41 | firstDisplayedItem = mItemSpacing = mFontHeight = mSeparatorH = y_offset = scrollingSpeed = 0; |
| 42 | maxIconWidth = maxIconHeight = mHeaderIconHeight = mHeaderIconWidth = 0; |
| 43 | mHeaderSeparatorH = mHeaderIsStatic = mHeaderH = actualItemHeight = 0; |
| 44 | mBackground = mFont = mHeaderIcon = NULL; |
| 45 | mBackgroundW = mBackgroundH = 0; |
| 46 | mFastScrollW = mFastScrollLineW = mFastScrollRectW = mFastScrollRectH = 0; |
| 47 | lastY = last2Y = fastScroll = 0; |
| 48 | mUpdate = 0; |
| 49 | touchDebounce = 6; |
| 50 | ConvertStrToColor("black", &mBackgroundColor); |
| 51 | ConvertStrToColor("black", &mHeaderBackgroundColor); |
| 52 | ConvertStrToColor("black", &mSeparatorColor); |
| 53 | ConvertStrToColor("black", &mHeaderSeparatorColor); |
| 54 | ConvertStrToColor("white", &mFontColor); |
| 55 | ConvertStrToColor("white", &mHeaderFontColor); |
| 56 | ConvertStrToColor("white", &mFastScrollLineColor); |
| 57 | ConvertStrToColor("white", &mFastScrollRectColor); |
| 58 | hasHighlightColor = false; |
| 59 | hasFontHighlightColor = false; |
| 60 | selectedItem = NO_ITEM; |
| 61 | |
| 62 | // Load header text |
| 63 | child = node->first_node("header"); |
| 64 | if (child) |
| 65 | { |
| 66 | attr = child->first_attribute("icon"); |
| 67 | if (attr) |
| 68 | mHeaderIcon = PageManager::FindResource(attr->value()); |
| 69 | |
| 70 | attr = child->first_attribute("background"); |
| 71 | if (attr) |
| 72 | { |
| 73 | std::string color = attr->value(); |
| 74 | ConvertStrToColor(color, &mHeaderBackgroundColor); |
| 75 | header_background_color_specified = -1; |
| 76 | } |
| 77 | attr = child->first_attribute("textcolor"); |
| 78 | if (attr) |
| 79 | { |
| 80 | std::string color = attr->value(); |
| 81 | ConvertStrToColor(color, &mHeaderFontColor); |
| 82 | header_text_color_specified = -1; |
| 83 | } |
| 84 | attr = child->first_attribute("separatorcolor"); |
| 85 | if (attr) |
| 86 | { |
| 87 | std::string color = attr->value(); |
| 88 | ConvertStrToColor(color, &mHeaderSeparatorColor); |
| 89 | header_separator_color_specified = -1; |
| 90 | } |
| 91 | attr = child->first_attribute("separatorheight"); |
| 92 | if (attr) { |
| 93 | string parsevalue = gui_parse_text(attr->value()); |
| 94 | mHeaderSeparatorH = atoi(parsevalue.c_str()); |
| 95 | header_separator_height_specified = -1; |
| 96 | } |
| 97 | } |
| 98 | child = node->first_node("text"); |
| 99 | if (child) mHeaderText = child->value(); |
| 100 | |
| 101 | memset(&mHighlightColor, 0, sizeof(COLOR)); |
| 102 | child = node->first_node("highlight"); |
| 103 | if (child) { |
| 104 | attr = child->first_attribute("color"); |
| 105 | if (attr) { |
| 106 | hasHighlightColor = true; |
| 107 | std::string color = attr->value(); |
| 108 | ConvertStrToColor(color, &mHighlightColor); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Simple way to check for static state |
| 113 | mLastHeaderValue = gui_parse_text(mHeaderText); |
| 114 | if (mLastHeaderValue != mHeaderText) |
| 115 | mHeaderIsStatic = 0; |
| 116 | else |
| 117 | mHeaderIsStatic = -1; |
| 118 | |
| 119 | child = node->first_node("background"); |
| 120 | if (child) |
| 121 | { |
| 122 | attr = child->first_attribute("resource"); |
| 123 | if (attr) |
| 124 | mBackground = PageManager::FindResource(attr->value()); |
| 125 | attr = child->first_attribute("color"); |
| 126 | if (attr) |
| 127 | { |
| 128 | std::string color = attr->value(); |
| 129 | ConvertStrToColor(color, &mBackgroundColor); |
| 130 | if (!header_background_color_specified) |
| 131 | ConvertStrToColor(color, &mHeaderBackgroundColor); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // Load the placement |
| 136 | LoadPlacement(node->first_node("placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH); |
| 137 | SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 138 | |
| 139 | // Load the font, and possibly override the color |
| 140 | child = node->first_node("font"); |
| 141 | if (child) |
| 142 | { |
| 143 | attr = child->first_attribute("resource"); |
| 144 | if (attr) |
| 145 | mFont = PageManager::FindResource(attr->value()); |
| 146 | |
| 147 | attr = child->first_attribute("color"); |
| 148 | if (attr) |
| 149 | { |
| 150 | std::string color = attr->value(); |
| 151 | ConvertStrToColor(color, &mFontColor); |
| 152 | if (!header_text_color_specified) |
| 153 | ConvertStrToColor(color, &mHeaderFontColor); |
| 154 | } |
| 155 | |
| 156 | attr = child->first_attribute("spacing"); |
| 157 | if (attr) { |
| 158 | string parsevalue = gui_parse_text(attr->value()); |
| 159 | mItemSpacing = atoi(parsevalue.c_str()); |
| 160 | } |
| 161 | |
| 162 | attr = child->first_attribute("highlightcolor"); |
| 163 | memset(&mFontHighlightColor, 0, sizeof(COLOR)); |
| 164 | if (attr) |
| 165 | { |
| 166 | std::string color = attr->value(); |
| 167 | ConvertStrToColor(color, &mFontHighlightColor); |
| 168 | hasFontHighlightColor = true; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | // Load the separator if it exists |
| 173 | child = node->first_node("separator"); |
| 174 | if (child) |
| 175 | { |
| 176 | attr = child->first_attribute("color"); |
| 177 | if (attr) |
| 178 | { |
| 179 | std::string color = attr->value(); |
| 180 | ConvertStrToColor(color, &mSeparatorColor); |
| 181 | if (!header_separator_color_specified) |
| 182 | ConvertStrToColor(color, &mHeaderSeparatorColor); |
| 183 | } |
| 184 | |
| 185 | attr = child->first_attribute("height"); |
| 186 | if (attr) { |
| 187 | string parsevalue = gui_parse_text(attr->value()); |
| 188 | mSeparatorH = atoi(parsevalue.c_str()); |
| 189 | if (!header_separator_height_specified) |
| 190 | mHeaderSeparatorH = mSeparatorH; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // Fast scroll colors |
| 195 | child = node->first_node("fastscroll"); |
| 196 | if (child) |
| 197 | { |
| 198 | attr = child->first_attribute("linecolor"); |
| 199 | if(attr) |
| 200 | ConvertStrToColor(attr->value(), &mFastScrollLineColor); |
| 201 | |
| 202 | attr = child->first_attribute("rectcolor"); |
| 203 | if(attr) |
| 204 | ConvertStrToColor(attr->value(), &mFastScrollRectColor); |
| 205 | |
| 206 | attr = child->first_attribute("w"); |
| 207 | if (attr) { |
| 208 | string parsevalue = gui_parse_text(attr->value()); |
| 209 | mFastScrollW = atoi(parsevalue.c_str()); |
| 210 | } |
| 211 | |
| 212 | attr = child->first_attribute("linew"); |
| 213 | if (attr) { |
| 214 | string parsevalue = gui_parse_text(attr->value()); |
| 215 | mFastScrollLineW = atoi(parsevalue.c_str()); |
| 216 | } |
| 217 | |
| 218 | attr = child->first_attribute("rectw"); |
| 219 | if (attr) { |
| 220 | string parsevalue = gui_parse_text(attr->value()); |
| 221 | mFastScrollRectW = atoi(parsevalue.c_str()); |
| 222 | } |
| 223 | |
| 224 | attr = child->first_attribute("recth"); |
| 225 | if (attr) { |
| 226 | string parsevalue = gui_parse_text(attr->value()); |
| 227 | mFastScrollRectH = atoi(parsevalue.c_str()); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // Retrieve the line height |
| 232 | mFontHeight = gr_getMaxFontHeight(mFont ? mFont->GetResource() : NULL); |
| 233 | mHeaderH = mFontHeight; |
| 234 | |
| 235 | if (mHeaderIcon && mHeaderIcon->GetResource()) |
| 236 | { |
| 237 | mHeaderIconWidth = gr_get_width(mHeaderIcon->GetResource()); |
| 238 | mHeaderIconHeight = gr_get_height(mHeaderIcon->GetResource()); |
| 239 | if (mHeaderIconHeight > mHeaderH) |
| 240 | mHeaderH = mHeaderIconHeight; |
| 241 | if (mHeaderIconWidth > maxIconWidth) |
| 242 | maxIconWidth = mHeaderIconWidth; |
| 243 | } |
| 244 | |
| 245 | mHeaderH += mItemSpacing + mHeaderSeparatorH; |
| 246 | actualItemHeight = mFontHeight + mItemSpacing + mSeparatorH; |
| 247 | if (mHeaderH < actualItemHeight) |
| 248 | mHeaderH = actualItemHeight; |
| 249 | |
| 250 | if (actualItemHeight / 3 > 6) |
| 251 | touchDebounce = actualItemHeight / 3; |
| 252 | |
| 253 | if (mBackground && mBackground->GetResource()) |
| 254 | { |
| 255 | mBackgroundW = gr_get_width(mBackground->GetResource()); |
| 256 | mBackgroundH = gr_get_height(mBackground->GetResource()); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | GUIScrollList::~GUIScrollList() |
| 261 | { |
| 262 | delete mHeaderIcon; |
| 263 | delete mBackground; |
| 264 | delete mFont; |
| 265 | } |
| 266 | |
| 267 | void GUIScrollList::SetMaxIconSize(int w, int h) |
| 268 | { |
| 269 | if (w > maxIconWidth) |
| 270 | maxIconWidth = w; |
| 271 | if (h > maxIconHeight) |
| 272 | maxIconHeight = h; |
| 273 | if (maxIconHeight > mFontHeight) { |
| 274 | actualItemHeight = maxIconHeight + mItemSpacing + mSeparatorH; |
| 275 | if (actualItemHeight > mHeaderH) |
| 276 | mHeaderH = actualItemHeight; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | void GUIScrollList::SetVisibleListLocation(size_t list_index) |
| 281 | { |
| 282 | // This will make sure that the item indicated by list_index is visible on the screen |
| 283 | size_t lines = GetDisplayItemCount(), listSize = GetItemCount(); |
| 284 | |
| 285 | if (list_index <= (unsigned)firstDisplayedItem) { |
| 286 | // list_index is above the currently displayed items, put the selected item at the very top |
| 287 | firstDisplayedItem = list_index; |
| 288 | y_offset = 0; |
| 289 | } else if (list_index >= firstDisplayedItem + lines) { |
| 290 | // list_index is below the currently displayed items, put the selected item at the very bottom |
| 291 | firstDisplayedItem = list_index - lines + 1; |
| 292 | if (GetDisplayRemainder() != 0) { |
| 293 | // There's a partial row displayed, set the scrolling offset so that the selected item really is at the very bottom |
| 294 | firstDisplayedItem--; |
| 295 | y_offset = GetDisplayRemainder() - actualItemHeight; |
| 296 | } else { |
| 297 | // There's no partial row so zero out the offset |
| 298 | y_offset = 0; |
| 299 | } |
| 300 | } |
| 301 | scrollingSpeed = 0; // stop kinetic scrolling on setting visible location |
| 302 | mUpdate = 1; |
| 303 | } |
| 304 | |
| 305 | int GUIScrollList::Render(void) |
| 306 | { |
| 307 | if(!isConditionTrue()) |
| 308 | return 0; |
| 309 | |
| 310 | // First step, fill background |
| 311 | gr_color(mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, 255); |
| 312 | gr_fill(mRenderX, mRenderY + mHeaderH, mRenderW, mRenderH - mHeaderH); |
| 313 | |
| 314 | // Next, render the background resource (if it exists) |
| 315 | if (mBackground && mBackground->GetResource()) |
| 316 | { |
| 317 | int mBackgroundX = mRenderX + ((mRenderW - mBackgroundW) / 2); |
| 318 | int mBackgroundY = mRenderY + ((mRenderH - mBackgroundH) / 2); |
| 319 | gr_blit(mBackground->GetResource(), 0, 0, mBackgroundW, mBackgroundH, mBackgroundX, mBackgroundY); |
| 320 | } |
| 321 | |
| 322 | // This tells us how many lines we can actually render |
| 323 | size_t lines = GetDisplayItemCount(); |
| 324 | |
| 325 | size_t listSize = GetItemCount(); |
| 326 | int listW = mRenderW; |
| 327 | |
| 328 | if (listSize <= lines) { |
| 329 | hasScroll = false; |
| 330 | scrollingSpeed = 0; |
| 331 | lines = listSize; |
| 332 | y_offset = 0; |
| 333 | } else { |
| 334 | hasScroll = true; |
| 335 | listW -= mFastScrollW; // space for fast scroll |
| 336 | lines++; |
| 337 | if (lines < listSize) |
| 338 | lines++; |
| 339 | } |
| 340 | |
| 341 | void* fontResource = NULL; |
| 342 | if (mFont) fontResource = mFont->GetResource(); |
| 343 | |
| 344 | int yPos = mRenderY + mHeaderH + y_offset; |
| 345 | int fontOffsetY = (int)((actualItemHeight - mFontHeight) / 2); |
| 346 | |
| 347 | // render all visible items |
| 348 | for (size_t line = 0; line < lines; line++) |
| 349 | { |
| 350 | size_t itemindex = line + firstDisplayedItem; |
| 351 | if (itemindex >= listSize) |
| 352 | break; |
| 353 | |
| 354 | // get item data |
| 355 | Resource* icon; |
| 356 | std::string label; |
| 357 | if (GetListItem(itemindex, icon, label)) |
| 358 | break; |
| 359 | |
| 360 | if (hasHighlightColor && itemindex == selectedItem) { |
| 361 | // Highlight the item background of the selected item |
| 362 | gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, 255); |
| 363 | int HighlightHeight = actualItemHeight; |
| 364 | if (yPos + HighlightHeight > mRenderY + mRenderH) { |
| 365 | HighlightHeight = mRenderY + mRenderH - yPos; |
| 366 | } |
| 367 | gr_fill(mRenderX, yPos, mRenderW, HighlightHeight); |
| 368 | } |
| 369 | |
| 370 | if (hasFontHighlightColor && itemindex == selectedItem) { |
| 371 | // Use the highlight color for the font |
| 372 | gr_color(mFontHighlightColor.red, mFontHighlightColor.green, mFontHighlightColor.blue, 255); |
| 373 | } else { |
| 374 | // Set the color for the font |
| 375 | gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, 255); |
| 376 | } |
| 377 | |
| 378 | if (icon && icon->GetResource()) { |
| 379 | int currentIconHeight = gr_get_height(icon->GetResource()); |
| 380 | int currentIconWidth = gr_get_width(icon->GetResource()); |
| 381 | int currentIconOffsetY = (int)((actualItemHeight - currentIconHeight) / 2); |
| 382 | int currentIconOffsetX = (maxIconWidth - currentIconWidth) / 2; |
| 383 | int rect_y = 0, image_y = (yPos + currentIconOffsetY); |
| 384 | if (image_y + currentIconHeight > mRenderY + mRenderH) |
| 385 | rect_y = mRenderY + mRenderH - image_y; |
| 386 | else |
| 387 | rect_y = currentIconHeight; |
| 388 | gr_blit(icon->GetResource(), 0, 0, currentIconWidth, rect_y, mRenderX + currentIconOffsetX, image_y); |
| 389 | } |
| 390 | |
| 391 | gr_textExWH(mRenderX + maxIconWidth + 5, yPos + fontOffsetY, label.c_str(), fontResource, mRenderX + listW, mRenderY + mRenderH); |
| 392 | |
| 393 | // Add the separator |
| 394 | if (yPos + actualItemHeight < mRenderH + mRenderY) { |
| 395 | gr_color(mSeparatorColor.red, mSeparatorColor.green, mSeparatorColor.blue, 255); |
| 396 | gr_fill(mRenderX, yPos + actualItemHeight - mSeparatorH, listW, mSeparatorH); |
| 397 | } |
| 398 | |
| 399 | // Move the yPos |
| 400 | yPos += actualItemHeight; |
| 401 | } |
| 402 | |
| 403 | // Render the Header (last so that it overwrites the top most row for per pixel scrolling) |
| 404 | // First step, fill background |
| 405 | gr_color(mHeaderBackgroundColor.red, mHeaderBackgroundColor.green, mHeaderBackgroundColor.blue, 255); |
| 406 | gr_fill(mRenderX, mRenderY, mRenderW, mHeaderH); |
| 407 | |
| 408 | // Now, we need the header (icon + text) |
| 409 | yPos = mRenderY; |
| 410 | { |
| 411 | Resource* headerIcon; |
| 412 | int mIconOffsetX = 0; |
| 413 | |
| 414 | // render the icon if it exists |
| 415 | headerIcon = mHeaderIcon; |
| 416 | if (headerIcon && headerIcon->GetResource()) |
| 417 | { |
| 418 | gr_blit(headerIcon->GetResource(), 0, 0, mHeaderIconWidth, mHeaderIconHeight, mRenderX + ((mHeaderIconWidth - maxIconWidth) / 2), (yPos + (int)((mHeaderH - mHeaderIconHeight) / 2))); |
| 419 | mIconOffsetX = maxIconWidth; |
| 420 | } |
| 421 | |
| 422 | // render the text |
| 423 | gr_color(mHeaderFontColor.red, mHeaderFontColor.green, mHeaderFontColor.blue, 255); |
| 424 | gr_textExWH(mRenderX + mIconOffsetX + 5, yPos + (int)((mHeaderH - mFontHeight) / 2), mLastHeaderValue.c_str(), fontResource, mRenderX + mRenderW, mRenderY + mRenderH); |
| 425 | |
| 426 | // Add the separator |
| 427 | gr_color(mHeaderSeparatorColor.red, mHeaderSeparatorColor.green, mHeaderSeparatorColor.blue, 255); |
| 428 | gr_fill(mRenderX, yPos + mHeaderH - mHeaderSeparatorH, mRenderW, mHeaderSeparatorH); |
| 429 | } |
| 430 | |
| 431 | // render fast scroll |
| 432 | lines = GetDisplayItemCount(); |
| 433 | if (hasScroll) { |
| 434 | int startX = listW + mRenderX; |
| 435 | int fWidth = mRenderW - listW; |
| 436 | int fHeight = mRenderH - mHeaderH; |
| 437 | |
| 438 | // line |
| 439 | gr_color(mFastScrollLineColor.red, mFastScrollLineColor.green, mFastScrollLineColor.blue, 255); |
| 440 | gr_fill(startX + fWidth/2, mRenderY + mHeaderH, mFastScrollLineW, mRenderH - mHeaderH); |
| 441 | |
| 442 | // rect |
| 443 | int pct = 0; |
| 444 | if (GetDisplayRemainder() != 0) { |
| 445 | // Properly handle the percentage if a partial line is present |
| 446 | int partial_line_size = actualItemHeight - GetDisplayRemainder(); |
| 447 | pct = ((firstDisplayedItem*actualItemHeight - y_offset)*100)/(listSize*actualItemHeight-((lines + 1)*actualItemHeight) + partial_line_size); |
| 448 | } else { |
| 449 | pct = ((firstDisplayedItem*actualItemHeight - y_offset)*100)/(listSize*actualItemHeight-lines*actualItemHeight); |
| 450 | } |
| 451 | int mFastScrollRectX = startX + (fWidth - mFastScrollRectW)/2; |
| 452 | int mFastScrollRectY = mRenderY+mHeaderH + ((fHeight - mFastScrollRectH)*pct)/100; |
| 453 | |
| 454 | gr_color(mFastScrollRectColor.red, mFastScrollRectColor.green, mFastScrollRectColor.blue, 255); |
| 455 | gr_fill(mFastScrollRectX, mFastScrollRectY, mFastScrollRectW, mFastScrollRectH); |
| 456 | } |
| 457 | mUpdate = 0; |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | int GUIScrollList::Update(void) |
| 462 | { |
| 463 | if(!isConditionTrue()) |
| 464 | return 0; |
| 465 | |
| 466 | if (!mHeaderIsStatic) { |
| 467 | std::string newValue = gui_parse_text(mHeaderText); |
| 468 | if (mLastHeaderValue != newValue) { |
| 469 | mLastHeaderValue = newValue; |
| 470 | mUpdate = 1; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | // Handle kinetic scrolling |
| 475 | int maxScrollDistance = actualItemHeight * SCROLLING_SPEED_LIMIT; |
| 476 | if (scrollingSpeed == 0) { |
| 477 | // Do nothing |
| 478 | return 0; |
| 479 | } else if (scrollingSpeed > 0) { |
| 480 | if (scrollingSpeed < maxScrollDistance) |
| 481 | y_offset += scrollingSpeed; |
| 482 | else |
| 483 | y_offset += maxScrollDistance; |
| 484 | scrollingSpeed -= SCROLLING_SPEED_DECREMENT; |
| 485 | } else if (scrollingSpeed < 0) { |
| 486 | if (abs(scrollingSpeed) < maxScrollDistance) |
| 487 | y_offset += scrollingSpeed; |
| 488 | else |
| 489 | y_offset -= maxScrollDistance; |
| 490 | scrollingSpeed += SCROLLING_SPEED_DECREMENT; |
| 491 | } |
| 492 | if (abs(scrollingSpeed) < SCROLLING_FLOOR) |
| 493 | scrollingSpeed = 0; |
| 494 | HandleScrolling(); |
| 495 | mUpdate = 1; |
| 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | size_t GUIScrollList::HitTestItem(int x, int y) |
| 501 | { |
| 502 | // We only care about y position |
| 503 | if (y < mRenderY || y - mRenderY <= mHeaderH || y - mRenderY > mRenderH) |
| 504 | return NO_ITEM; |
| 505 | |
| 506 | int startSelection = (y - mRenderY - mHeaderH); |
| 507 | |
| 508 | // Locate the correct item |
| 509 | size_t actualSelection = firstDisplayedItem; |
| 510 | int selectY = y_offset; |
| 511 | while (selectY + actualItemHeight < startSelection) { |
| 512 | selectY += actualItemHeight; |
| 513 | actualSelection++; |
| 514 | } |
| 515 | |
| 516 | if (actualSelection < GetItemCount()) |
| 517 | return actualSelection; |
| 518 | |
| 519 | return NO_ITEM; |
| 520 | } |
| 521 | |
| 522 | int GUIScrollList::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 523 | { |
| 524 | if(!isConditionTrue()) |
| 525 | return -1; |
| 526 | |
| 527 | switch (state) |
| 528 | { |
| 529 | case TOUCH_START: |
| 530 | if (hasScroll && x >= mRenderX + mRenderW - mFastScrollW) |
| 531 | fastScroll = 1; // Initial touch is in the fast scroll region |
| 532 | if (scrollingSpeed != 0) { |
| 533 | selectedItem = NO_ITEM; // this allows the user to tap the list to stop the scrolling without selecting the item they tap |
| 534 | scrollingSpeed = 0; // stop scrolling on a new touch |
| 535 | } else if (!fastScroll) { |
| 536 | // find out which item the user touched |
| 537 | selectedItem = HitTestItem(x, y); |
| 538 | } |
| 539 | if (selectedItem != NO_ITEM) |
| 540 | mUpdate = 1; |
| 541 | lastY = last2Y = y; |
| 542 | break; |
| 543 | |
| 544 | case TOUCH_DRAG: |
| 545 | if (fastScroll) |
| 546 | { |
| 547 | int pct = ((y-mRenderY-mHeaderH)*100)/(mRenderH-mHeaderH); |
| 548 | int totalSize = GetItemCount(); |
| 549 | int lines = GetDisplayItemCount(); |
| 550 | |
| 551 | float l = float((totalSize-lines)*pct)/100; |
| 552 | if(l + lines >= totalSize) |
| 553 | { |
| 554 | firstDisplayedItem = totalSize - lines; |
| 555 | if (GetDisplayRemainder() != 0) { |
| 556 | // There's a partial row displayed, set the scrolling offset so that the last item really is at the very bottom |
| 557 | firstDisplayedItem--; |
| 558 | y_offset = GetDisplayRemainder() - actualItemHeight; |
| 559 | } else { |
| 560 | // There's no partial row so zero out the offset |
| 561 | y_offset = 0; |
| 562 | } |
| 563 | } |
| 564 | else |
| 565 | { |
| 566 | if (l < 0) |
| 567 | l = 0; |
| 568 | firstDisplayedItem = l; |
| 569 | y_offset = -(l - int(l))*actualItemHeight; |
| 570 | if (GetDisplayRemainder() != 0) { |
| 571 | // There's a partial row displayed, make sure y_offset doesn't go past the max |
| 572 | if (firstDisplayedItem == totalSize - lines - 1 && y_offset < GetDisplayRemainder() - actualItemHeight) |
| 573 | y_offset = GetDisplayRemainder() - actualItemHeight; |
| 574 | } else if (firstDisplayedItem == totalSize - lines) |
| 575 | y_offset = 0; |
| 576 | } |
| 577 | |
| 578 | selectedItem = NO_ITEM; |
| 579 | mUpdate = 1; |
| 580 | scrollingSpeed = 0; // prevent kinetic scrolling when using fast scroll |
| 581 | break; |
| 582 | } |
| 583 | |
| 584 | // Provide some debounce on initial touches |
| 585 | if (selectedItem != NO_ITEM && abs(y - lastY) < touchDebounce) { |
| 586 | mUpdate = 1; |
| 587 | break; |
| 588 | } |
| 589 | |
| 590 | selectedItem = NO_ITEM; // nothing is selected because we dragged too far |
| 591 | // Handle scrolling |
| 592 | if (hasScroll) { |
| 593 | y_offset += y - lastY; // adjust the scrolling offset based on the difference between the starting touch and the current touch |
| 594 | last2Y = lastY; // keep track of previous y locations so that we can tell how fast to scroll for kinetic scrolling |
| 595 | lastY = y; // update last touch to the current touch so we can tell how far and what direction we scroll for the next touch event |
| 596 | |
| 597 | HandleScrolling(); |
| 598 | } else |
| 599 | y_offset = 0; |
| 600 | mUpdate = 1; |
| 601 | break; |
| 602 | |
| 603 | case TOUCH_RELEASE: |
| 604 | fastScroll = 0; |
| 605 | if (selectedItem != NO_ITEM) { |
| 606 | // We've selected an item! |
| 607 | NotifySelect(selectedItem); |
| 608 | mUpdate = 1; |
| 609 | |
| 610 | DataManager::Vibrate("tw_button_vibrate"); |
| 611 | selectedItem = NO_ITEM; |
| 612 | } else { |
| 613 | // Start kinetic scrolling |
| 614 | scrollingSpeed = lastY - last2Y; |
| 615 | if (abs(scrollingSpeed) > SCROLLING_FLOOR) |
| 616 | scrollingSpeed *= SCROLLING_MULTIPLIER; |
| 617 | else |
| 618 | scrollingSpeed = 0; |
| 619 | } |
| 620 | case TOUCH_REPEAT: |
| 621 | case TOUCH_HOLD: |
| 622 | break; |
| 623 | } |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | void GUIScrollList::HandleScrolling() |
| 628 | { |
| 629 | // handle dragging downward, scrolling upward |
| 630 | // the offset should always be <= 0 and > -actualItemHeight, adjust the first display row and offset as needed |
| 631 | while(firstDisplayedItem && y_offset > 0) { |
| 632 | firstDisplayedItem--; |
| 633 | y_offset -= actualItemHeight; |
| 634 | } |
| 635 | if (firstDisplayedItem == 0 && y_offset > 0) |
| 636 | y_offset = 0; // user kept dragging downward past the top of the list, so always reset the offset to 0 since we can't scroll any further in this direction |
| 637 | |
| 638 | // handle dragging upward, scrolling downward |
| 639 | int totalSize = GetItemCount(); |
| 640 | int lines = GetDisplayItemCount(); // number of full lines our list can display at once |
| 641 | int bottom_offset = GetDisplayRemainder() - actualItemHeight; // extra display area that can display a partial line for per pixel scrolling |
| 642 | |
| 643 | // the offset should always be <= 0 and > -actualItemHeight, adjust the first display row and offset as needed |
| 644 | while (firstDisplayedItem + lines + (bottom_offset ? 1 : 0) < totalSize && abs(y_offset) > actualItemHeight) { |
| 645 | firstDisplayedItem++; |
| 646 | y_offset += actualItemHeight; |
| 647 | } |
| 648 | // Check if we dragged too far, set the list at the bottom and adjust offset as needed |
| 649 | if (bottom_offset != 0 && firstDisplayedItem + lines + 1 >= totalSize && y_offset <= bottom_offset) { |
| 650 | firstDisplayedItem = totalSize - lines - 1; |
| 651 | y_offset = bottom_offset; |
| 652 | } else if (firstDisplayedItem + lines >= totalSize && y_offset < 0) { |
| 653 | firstDisplayedItem = totalSize - lines; |
| 654 | y_offset = 0; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | int GUIScrollList::GetDisplayItemCount() |
| 659 | { |
| 660 | return (mRenderH - mHeaderH) / (actualItemHeight); |
| 661 | } |
| 662 | |
| 663 | int GUIScrollList::GetDisplayRemainder() |
| 664 | { |
| 665 | return (mRenderH - mHeaderH) % actualItemHeight; |
| 666 | } |
| 667 | |
| 668 | int GUIScrollList::NotifyVarChange(const std::string& varName, const std::string& value) |
| 669 | { |
| 670 | GUIObject::NotifyVarChange(varName, value); |
| 671 | |
| 672 | if(!isConditionTrue()) |
| 673 | return 0; |
| 674 | |
| 675 | if (!mHeaderIsStatic) { |
| 676 | std::string newValue = gui_parse_text(mHeaderText); |
| 677 | if (mLastHeaderValue != newValue) { |
| 678 | mLastHeaderValue = newValue; |
| 679 | firstDisplayedItem = 0; |
| 680 | y_offset = 0; |
| 681 | scrollingSpeed = 0; // stop kinetic scrolling on variable changes |
| 682 | mUpdate = 1; |
| 683 | } |
| 684 | } |
| 685 | return 0; |
| 686 | } |
| 687 | |
| 688 | int GUIScrollList::SetRenderPos(int x, int y, int w /* = 0 */, int h /* = 0 */) |
| 689 | { |
| 690 | mRenderX = x; |
| 691 | mRenderY = y; |
| 692 | if (w || h) |
| 693 | { |
| 694 | mRenderW = w; |
| 695 | mRenderH = h; |
| 696 | } |
| 697 | SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 698 | mUpdate = 1; |
| 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | void GUIScrollList::SetPageFocus(int inFocus) |
| 703 | { |
| 704 | if (inFocus) { |
| 705 | NotifyVarChange("", ""); // This forces a check for the header text |
| 706 | scrollingSpeed = 0; // stop kinetic scrolling on page changes |
| 707 | mUpdate = 1; |
| 708 | } |
| 709 | } |