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 | 10ec017 | 2015-02-15 23:52:28 +0100 | [diff] [blame] | 30 | const float SCROLLING_SPEED_DECREMENT = 0.9; // friction |
| 31 | const int SCROLLING_FLOOR = 2; // minimum pixels for scrolling to stop |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 32 | const float SCROLLING_SPEED_LIMIT = 2.5; // maximum number of items to scroll per update |
| 33 | |
| 34 | GUIScrollList::GUIScrollList(xml_node<>* node) : GUIObject(node) |
| 35 | { |
| 36 | xml_attribute<>* attr; |
| 37 | xml_node<>* child; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 38 | |
| 39 | firstDisplayedItem = mItemSpacing = mFontHeight = mSeparatorH = y_offset = scrollingSpeed = 0; |
| 40 | maxIconWidth = maxIconHeight = mHeaderIconHeight = mHeaderIconWidth = 0; |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 41 | mHeaderSeparatorH = mHeaderH = actualItemHeight = 0; |
| 42 | mHeaderIsStatic = false; |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 43 | mBackground = mHeaderIcon = NULL; |
| 44 | mFont = NULL; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 45 | mFastScrollW = mFastScrollLineW = mFastScrollRectW = mFastScrollRectH = 0; |
that | a999821 | 2015-02-19 22:51:24 +0100 | [diff] [blame] | 46 | mFastScrollRectCurrentY = mFastScrollRectCurrentH = mFastScrollRectTouchY = 0; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 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; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 59 | selectedItem = NO_ITEM; |
| 60 | |
| 61 | // Load header text |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 62 | child = node->first_node("text"); |
| 63 | if (child) mHeaderText = child->value(); |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 64 | // Simple way to check for static state |
| 65 | mLastHeaderValue = gui_parse_text(mHeaderText); |
| 66 | mHeaderIsStatic = (mLastHeaderValue == mHeaderText); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 67 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 68 | mHighlightColor = LoadAttrColor(FindNode(node, "highlight"), "color", &hasHighlightColor); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 69 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 70 | child = FindNode(node, "background"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 71 | if (child) |
| 72 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 73 | mBackground = LoadAttrImage(child, "resource"); |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 74 | mBackgroundColor = LoadAttrColor(child, "color"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // Load the placement |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 78 | LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 79 | SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 80 | |
| 81 | // Load the font, and possibly override the color |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 82 | child = FindNode(node, "font"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 83 | if (child) |
| 84 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 85 | mFont = LoadAttrFont(child, "resource"); |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 86 | mFontColor = LoadAttrColor(child, "color"); |
| 87 | mFontHighlightColor = LoadAttrColor(child, "highlightcolor", mFontColor); |
| 88 | mItemSpacing = LoadAttrIntScaleY(child, "spacing"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // Load the separator if it exists |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 92 | child = FindNode(node, "separator"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 93 | if (child) |
| 94 | { |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 95 | mSeparatorColor = LoadAttrColor(child, "color"); |
| 96 | mSeparatorH = LoadAttrIntScaleY(child, "height"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 97 | } |
| 98 | |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 99 | // Fast scroll |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 100 | child = FindNode(node, "fastscroll"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 101 | if (child) |
| 102 | { |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 103 | mFastScrollLineColor = LoadAttrColor(child, "linecolor"); |
| 104 | mFastScrollRectColor = LoadAttrColor(child, "rectcolor"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 105 | |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 106 | mFastScrollW = LoadAttrIntScaleX(child, "w"); |
| 107 | mFastScrollLineW = LoadAttrIntScaleX(child, "linew"); |
| 108 | mFastScrollRectW = LoadAttrIntScaleX(child, "rectw"); |
| 109 | mFastScrollRectH = LoadAttrIntScaleY(child, "recth"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // Retrieve the line height |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 113 | mFontHeight = mFont->GetHeight(); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 114 | actualItemHeight = mFontHeight + mItemSpacing + mSeparatorH; |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 115 | |
| 116 | // Load the header if it exists |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 117 | child = FindNode(node, "header"); |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 118 | if (child) |
| 119 | { |
| 120 | mHeaderH = mFontHeight; |
| 121 | mHeaderIcon = LoadAttrImage(child, "icon"); |
| 122 | mHeaderBackgroundColor = LoadAttrColor(child, "background", mBackgroundColor); |
| 123 | mHeaderFontColor = LoadAttrColor(child, "textcolor", mFontColor); |
| 124 | mHeaderSeparatorColor = LoadAttrColor(child, "separatorcolor", mSeparatorColor); |
| 125 | mHeaderSeparatorH = LoadAttrIntScaleY(child, "separatorheight", mSeparatorH); |
| 126 | |
| 127 | if (mHeaderIcon && mHeaderIcon->GetResource()) |
| 128 | { |
| 129 | mHeaderIconWidth = mHeaderIcon->GetWidth(); |
| 130 | mHeaderIconHeight = mHeaderIcon->GetHeight(); |
| 131 | if (mHeaderIconHeight > mHeaderH) |
| 132 | mHeaderH = mHeaderIconHeight; |
| 133 | if (mHeaderIconWidth > maxIconWidth) |
| 134 | maxIconWidth = mHeaderIconWidth; |
| 135 | } |
| 136 | |
| 137 | mHeaderH += mItemSpacing + mHeaderSeparatorH; |
| 138 | if (mHeaderH < actualItemHeight) |
| 139 | mHeaderH = actualItemHeight; |
| 140 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 141 | |
| 142 | if (actualItemHeight / 3 > 6) |
| 143 | touchDebounce = actualItemHeight / 3; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | GUIScrollList::~GUIScrollList() |
| 147 | { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void GUIScrollList::SetMaxIconSize(int w, int h) |
| 151 | { |
| 152 | if (w > maxIconWidth) |
| 153 | maxIconWidth = w; |
| 154 | if (h > maxIconHeight) |
| 155 | maxIconHeight = h; |
| 156 | if (maxIconHeight > mFontHeight) { |
| 157 | actualItemHeight = maxIconHeight + mItemSpacing + mSeparatorH; |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 158 | if (mHeaderH > 0 && actualItemHeight > mHeaderH) |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 159 | mHeaderH = actualItemHeight; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void GUIScrollList::SetVisibleListLocation(size_t list_index) |
| 164 | { |
| 165 | // This will make sure that the item indicated by list_index is visible on the screen |
| 166 | size_t lines = GetDisplayItemCount(), listSize = GetItemCount(); |
| 167 | |
| 168 | if (list_index <= (unsigned)firstDisplayedItem) { |
| 169 | // list_index is above the currently displayed items, put the selected item at the very top |
| 170 | firstDisplayedItem = list_index; |
| 171 | y_offset = 0; |
| 172 | } else if (list_index >= firstDisplayedItem + lines) { |
| 173 | // list_index is below the currently displayed items, put the selected item at the very bottom |
| 174 | firstDisplayedItem = list_index - lines + 1; |
| 175 | if (GetDisplayRemainder() != 0) { |
| 176 | // There's a partial row displayed, set the scrolling offset so that the selected item really is at the very bottom |
| 177 | firstDisplayedItem--; |
| 178 | y_offset = GetDisplayRemainder() - actualItemHeight; |
| 179 | } else { |
| 180 | // There's no partial row so zero out the offset |
| 181 | y_offset = 0; |
| 182 | } |
| 183 | } |
| 184 | scrollingSpeed = 0; // stop kinetic scrolling on setting visible location |
| 185 | mUpdate = 1; |
| 186 | } |
| 187 | |
| 188 | int GUIScrollList::Render(void) |
| 189 | { |
| 190 | if(!isConditionTrue()) |
| 191 | return 0; |
| 192 | |
| 193 | // First step, fill background |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 194 | gr_color(mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, mBackgroundColor.alpha); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 195 | gr_fill(mRenderX, mRenderY + mHeaderH, mRenderW, mRenderH - mHeaderH); |
| 196 | |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 197 | // don't paint outside of the box |
| 198 | gr_clip(mRenderX, mRenderY, mRenderW, mRenderH); |
| 199 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 200 | // Next, render the background resource (if it exists) |
| 201 | if (mBackground && mBackground->GetResource()) |
| 202 | { |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame^] | 203 | int BackgroundW = mBackground->GetWidth(); |
| 204 | int BackgroundH = mBackground->GetHeight(); |
| 205 | int BackgroundX = mRenderX + ((mRenderW - BackgroundW) / 2); |
| 206 | int BackgroundY = mRenderY + ((mRenderH - BackgroundH) / 2); |
| 207 | gr_blit(mBackground->GetResource(), 0, 0, BackgroundW, BackgroundH, BackgroundX, BackgroundY); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 208 | } |
| 209 | |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 210 | // This tells us how many full lines we can actually render |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 211 | size_t lines = GetDisplayItemCount(); |
| 212 | |
| 213 | size_t listSize = GetItemCount(); |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame^] | 214 | int listW = mRenderW; // this is only used for the separators - the list items are rendered in the full width of the list |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 215 | |
| 216 | if (listSize <= lines) { |
| 217 | hasScroll = false; |
| 218 | scrollingSpeed = 0; |
| 219 | lines = listSize; |
| 220 | y_offset = 0; |
| 221 | } else { |
| 222 | hasScroll = true; |
| 223 | listW -= mFastScrollW; // space for fast scroll |
| 224 | lines++; |
| 225 | if (lines < listSize) |
| 226 | lines++; |
| 227 | } |
| 228 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 229 | int yPos = mRenderY + mHeaderH + y_offset; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 230 | |
| 231 | // render all visible items |
| 232 | for (size_t line = 0; line < lines; line++) |
| 233 | { |
| 234 | size_t itemindex = line + firstDisplayedItem; |
| 235 | if (itemindex >= listSize) |
| 236 | break; |
| 237 | |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame^] | 238 | RenderItem(itemindex, yPos, itemindex == selectedItem); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 239 | |
| 240 | // Add the separator |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 241 | gr_color(mSeparatorColor.red, mSeparatorColor.green, mSeparatorColor.blue, mSeparatorColor.alpha); |
| 242 | gr_fill(mRenderX, yPos + actualItemHeight - mSeparatorH, listW, mSeparatorH); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 243 | |
| 244 | // Move the yPos |
| 245 | yPos += actualItemHeight; |
| 246 | } |
| 247 | |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame^] | 248 | // Render the Header (last so that it overwrites the top most row for per pixel scrolling) |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 249 | yPos = mRenderY; |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 250 | if (mHeaderH > 0) { |
| 251 | // First step, fill background |
| 252 | gr_color(mHeaderBackgroundColor.red, mHeaderBackgroundColor.green, mHeaderBackgroundColor.blue, mHeaderBackgroundColor.alpha); |
| 253 | gr_fill(mRenderX, mRenderY, mRenderW, mHeaderH); |
| 254 | |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame^] | 255 | int IconOffsetX = 0; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 256 | |
| 257 | // render the icon if it exists |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame^] | 258 | if (mHeaderIcon && mHeaderIcon->GetResource()) |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 259 | { |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame^] | 260 | gr_blit(mHeaderIcon->GetResource(), 0, 0, mHeaderIconWidth, mHeaderIconHeight, mRenderX + ((mHeaderIconWidth - maxIconWidth) / 2), (yPos + (int)((mHeaderH - mHeaderIconHeight) / 2))); |
| 261 | IconOffsetX = maxIconWidth; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | // render the text |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 265 | gr_color(mHeaderFontColor.red, mHeaderFontColor.green, mHeaderFontColor.blue, mHeaderFontColor.alpha); |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame^] | 266 | gr_textEx(mRenderX + IconOffsetX + 5, yPos + (int)((mHeaderH - mFontHeight) / 2), mLastHeaderValue.c_str(), mFont->GetResource()); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 267 | |
| 268 | // Add the separator |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 269 | gr_color(mHeaderSeparatorColor.red, mHeaderSeparatorColor.green, mHeaderSeparatorColor.blue, mHeaderSeparatorColor.alpha); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 270 | gr_fill(mRenderX, yPos + mHeaderH - mHeaderSeparatorH, mRenderW, mHeaderSeparatorH); |
| 271 | } |
| 272 | |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 273 | // reset clipping |
| 274 | gr_noclip(); |
that | a999821 | 2015-02-19 22:51:24 +0100 | [diff] [blame] | 275 | |
| 276 | // render fast scroll |
| 277 | if (hasScroll) { |
| 278 | int fWidth = mRenderW - listW; |
| 279 | int fHeight = mRenderH - mHeaderH; |
| 280 | int centerX = listW + mRenderX + fWidth / 2; |
| 281 | |
| 282 | // first determine the total list height and where we are in the list |
| 283 | int totalHeight = GetItemCount() * actualItemHeight; // total height of the full list in pixels |
| 284 | int topPos = firstDisplayedItem * actualItemHeight - y_offset; |
| 285 | |
| 286 | // now scale it proportionally to the scrollbar height |
| 287 | int boxH = fHeight * fHeight / totalHeight; // proportional height of the displayed portion |
| 288 | boxH = std::max(boxH, mFastScrollRectH); // but keep a minimum height |
| 289 | int boxY = (fHeight - boxH) * topPos / (totalHeight - fHeight); // pixels relative to top of list |
| 290 | int boxW = mFastScrollRectW; |
| 291 | |
| 292 | int x = centerX - boxW / 2; |
| 293 | int y = mRenderY + mHeaderH + boxY; |
| 294 | |
| 295 | // line above and below box (needs to be split because box can be transparent) |
| 296 | gr_color(mFastScrollLineColor.red, mFastScrollLineColor.green, mFastScrollLineColor.blue, mFastScrollLineColor.alpha); |
| 297 | gr_fill(centerX - mFastScrollLineW / 2, mRenderY + mHeaderH, mFastScrollLineW, boxY); |
| 298 | gr_fill(centerX - mFastScrollLineW / 2, y + boxH, mFastScrollLineW, fHeight - boxY - boxH); |
| 299 | |
| 300 | // box |
| 301 | gr_color(mFastScrollRectColor.red, mFastScrollRectColor.green, mFastScrollRectColor.blue, mFastScrollRectColor.alpha); |
| 302 | gr_fill(x, y, boxW, boxH); |
| 303 | |
| 304 | mFastScrollRectCurrentY = boxY; |
| 305 | mFastScrollRectCurrentH = boxH; |
| 306 | } |
| 307 | mUpdate = 0; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 308 | return 0; |
| 309 | } |
| 310 | |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame^] | 311 | void GUIScrollList::RenderItem(size_t itemindex, int yPos, bool selected) |
| 312 | { |
| 313 | RenderStdItem(yPos, selected, NULL, "implement RenderItem!"); |
| 314 | } |
| 315 | |
| 316 | void GUIScrollList::RenderStdItem(int yPos, bool selected, ImageResource* icon, const char* text, int iconAndTextH) |
| 317 | { |
| 318 | if (hasHighlightColor && selected) { |
| 319 | // Highlight the item background of the selected item |
| 320 | gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha); |
| 321 | gr_fill(mRenderX, yPos, mRenderW, actualItemHeight); |
| 322 | } |
| 323 | |
| 324 | if (selected) { |
| 325 | // Use the highlight color for the font |
| 326 | gr_color(mFontHighlightColor.red, mFontHighlightColor.green, mFontHighlightColor.blue, mFontHighlightColor.alpha); |
| 327 | } else { |
| 328 | // Set the color for the font |
| 329 | gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, mFontColor.alpha); |
| 330 | } |
| 331 | |
| 332 | if (!iconAndTextH) |
| 333 | iconAndTextH = actualItemHeight; |
| 334 | |
| 335 | // render icon |
| 336 | if (icon && icon->GetResource()) { |
| 337 | int iconH = icon->GetHeight(); |
| 338 | int iconW = icon->GetWidth(); |
| 339 | int iconY = yPos + (iconAndTextH - iconH) / 2; |
| 340 | int iconX = mRenderX + (maxIconWidth - iconW) / 2; |
| 341 | gr_blit(icon->GetResource(), 0, 0, iconW, iconH, iconX, iconY); |
| 342 | } |
| 343 | |
| 344 | // render label text |
| 345 | int textX = mRenderX + maxIconWidth + 5; |
| 346 | int textY = yPos + (iconAndTextH - mFontHeight) / 2; |
| 347 | gr_textEx(textX, textY, text, mFont->GetResource()); |
| 348 | } |
| 349 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 350 | int GUIScrollList::Update(void) |
| 351 | { |
| 352 | if(!isConditionTrue()) |
| 353 | return 0; |
| 354 | |
| 355 | if (!mHeaderIsStatic) { |
| 356 | std::string newValue = gui_parse_text(mHeaderText); |
| 357 | if (mLastHeaderValue != newValue) { |
| 358 | mLastHeaderValue = newValue; |
| 359 | mUpdate = 1; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // Handle kinetic scrolling |
| 364 | int maxScrollDistance = actualItemHeight * SCROLLING_SPEED_LIMIT; |
that | 10ec017 | 2015-02-15 23:52:28 +0100 | [diff] [blame] | 365 | int oldScrollingSpeed = scrollingSpeed; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 366 | if (scrollingSpeed == 0) { |
| 367 | // Do nothing |
| 368 | return 0; |
| 369 | } else if (scrollingSpeed > 0) { |
| 370 | if (scrollingSpeed < maxScrollDistance) |
| 371 | y_offset += scrollingSpeed; |
| 372 | else |
| 373 | y_offset += maxScrollDistance; |
that | 10ec017 | 2015-02-15 23:52:28 +0100 | [diff] [blame] | 374 | scrollingSpeed *= SCROLLING_SPEED_DECREMENT; |
| 375 | if (scrollingSpeed == oldScrollingSpeed) |
| 376 | --scrollingSpeed; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 377 | } else if (scrollingSpeed < 0) { |
| 378 | if (abs(scrollingSpeed) < maxScrollDistance) |
| 379 | y_offset += scrollingSpeed; |
| 380 | else |
| 381 | y_offset -= maxScrollDistance; |
that | 10ec017 | 2015-02-15 23:52:28 +0100 | [diff] [blame] | 382 | scrollingSpeed *= SCROLLING_SPEED_DECREMENT; |
| 383 | if (scrollingSpeed == oldScrollingSpeed) |
| 384 | ++scrollingSpeed; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 385 | } |
| 386 | if (abs(scrollingSpeed) < SCROLLING_FLOOR) |
| 387 | scrollingSpeed = 0; |
| 388 | HandleScrolling(); |
| 389 | mUpdate = 1; |
| 390 | |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | size_t GUIScrollList::HitTestItem(int x, int y) |
| 395 | { |
| 396 | // We only care about y position |
| 397 | if (y < mRenderY || y - mRenderY <= mHeaderH || y - mRenderY > mRenderH) |
| 398 | return NO_ITEM; |
| 399 | |
| 400 | int startSelection = (y - mRenderY - mHeaderH); |
| 401 | |
| 402 | // Locate the correct item |
| 403 | size_t actualSelection = firstDisplayedItem; |
| 404 | int selectY = y_offset; |
| 405 | while (selectY + actualItemHeight < startSelection) { |
| 406 | selectY += actualItemHeight; |
| 407 | actualSelection++; |
| 408 | } |
| 409 | |
| 410 | if (actualSelection < GetItemCount()) |
| 411 | return actualSelection; |
| 412 | |
| 413 | return NO_ITEM; |
| 414 | } |
| 415 | |
| 416 | int GUIScrollList::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 417 | { |
| 418 | if(!isConditionTrue()) |
| 419 | return -1; |
| 420 | |
| 421 | switch (state) |
| 422 | { |
| 423 | case TOUCH_START: |
that | a999821 | 2015-02-19 22:51:24 +0100 | [diff] [blame] | 424 | if (hasScroll && x >= mRenderX + mRenderW - mFastScrollW) { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 425 | fastScroll = 1; // Initial touch is in the fast scroll region |
that | a999821 | 2015-02-19 22:51:24 +0100 | [diff] [blame] | 426 | int fastScrollBoxTop = mFastScrollRectCurrentY + mRenderY + mHeaderH; |
| 427 | int fastScrollBoxBottom = fastScrollBoxTop + mFastScrollRectCurrentH; |
| 428 | if (y >= fastScrollBoxTop && y < fastScrollBoxBottom) |
| 429 | // user grabbed the fastscroll bar |
| 430 | // try to keep the initially touched part of the scrollbar under the finger |
| 431 | mFastScrollRectTouchY = y - fastScrollBoxTop; |
| 432 | else |
| 433 | // user tapped outside the fastscroll bar |
| 434 | // center fastscroll rect on the initial touch position |
| 435 | mFastScrollRectTouchY = mFastScrollRectCurrentH / 2; |
| 436 | } |
| 437 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 438 | if (scrollingSpeed != 0) { |
| 439 | selectedItem = NO_ITEM; // this allows the user to tap the list to stop the scrolling without selecting the item they tap |
| 440 | scrollingSpeed = 0; // stop scrolling on a new touch |
| 441 | } else if (!fastScroll) { |
| 442 | // find out which item the user touched |
| 443 | selectedItem = HitTestItem(x, y); |
| 444 | } |
| 445 | if (selectedItem != NO_ITEM) |
| 446 | mUpdate = 1; |
| 447 | lastY = last2Y = y; |
| 448 | break; |
| 449 | |
| 450 | case TOUCH_DRAG: |
| 451 | if (fastScroll) |
| 452 | { |
that | a999821 | 2015-02-19 22:51:24 +0100 | [diff] [blame] | 453 | int relY = y - mRenderY - mHeaderH; // touch position relative to window |
| 454 | int windowH = mRenderH - mHeaderH; |
| 455 | int totalHeight = GetItemCount() * actualItemHeight; // total height of the full list in pixels |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 456 | |
that | a999821 | 2015-02-19 22:51:24 +0100 | [diff] [blame] | 457 | // calculate new top position of the fastscroll bar relative to window |
| 458 | int newY = relY - mFastScrollRectTouchY; |
| 459 | // keep it fully inside the list |
| 460 | newY = std::min(std::max(newY, 0), windowH - mFastScrollRectCurrentH); |
| 461 | |
| 462 | // now compute the new scroll position for the list |
| 463 | int newTopPos = newY * (totalHeight - windowH) / (windowH - mFastScrollRectCurrentH); // new top pixel of list |
| 464 | newTopPos = std::min(newTopPos, totalHeight - windowH); // account for rounding errors |
| 465 | firstDisplayedItem = newTopPos / actualItemHeight; |
| 466 | y_offset = - newTopPos % actualItemHeight; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 467 | |
| 468 | selectedItem = NO_ITEM; |
| 469 | mUpdate = 1; |
| 470 | scrollingSpeed = 0; // prevent kinetic scrolling when using fast scroll |
| 471 | break; |
| 472 | } |
| 473 | |
| 474 | // Provide some debounce on initial touches |
| 475 | if (selectedItem != NO_ITEM && abs(y - lastY) < touchDebounce) { |
| 476 | mUpdate = 1; |
| 477 | break; |
| 478 | } |
| 479 | |
| 480 | selectedItem = NO_ITEM; // nothing is selected because we dragged too far |
| 481 | // Handle scrolling |
| 482 | if (hasScroll) { |
| 483 | y_offset += y - lastY; // adjust the scrolling offset based on the difference between the starting touch and the current touch |
| 484 | last2Y = lastY; // keep track of previous y locations so that we can tell how fast to scroll for kinetic scrolling |
| 485 | 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 |
| 486 | |
| 487 | HandleScrolling(); |
| 488 | } else |
| 489 | y_offset = 0; |
| 490 | mUpdate = 1; |
| 491 | break; |
| 492 | |
| 493 | case TOUCH_RELEASE: |
that | a999821 | 2015-02-19 22:51:24 +0100 | [diff] [blame] | 494 | if (fastScroll) |
| 495 | mUpdate = 1; // get rid of touch effects on the fastscroll bar |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 496 | fastScroll = 0; |
| 497 | if (selectedItem != NO_ITEM) { |
| 498 | // We've selected an item! |
| 499 | NotifySelect(selectedItem); |
| 500 | mUpdate = 1; |
| 501 | |
| 502 | DataManager::Vibrate("tw_button_vibrate"); |
| 503 | selectedItem = NO_ITEM; |
| 504 | } else { |
| 505 | // Start kinetic scrolling |
| 506 | scrollingSpeed = lastY - last2Y; |
that | 10ec017 | 2015-02-15 23:52:28 +0100 | [diff] [blame] | 507 | if (abs(scrollingSpeed) < touchDebounce) |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 508 | scrollingSpeed = 0; |
| 509 | } |
| 510 | case TOUCH_REPEAT: |
| 511 | case TOUCH_HOLD: |
| 512 | break; |
| 513 | } |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | void GUIScrollList::HandleScrolling() |
| 518 | { |
| 519 | // handle dragging downward, scrolling upward |
| 520 | // the offset should always be <= 0 and > -actualItemHeight, adjust the first display row and offset as needed |
| 521 | while(firstDisplayedItem && y_offset > 0) { |
| 522 | firstDisplayedItem--; |
| 523 | y_offset -= actualItemHeight; |
| 524 | } |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 525 | if (firstDisplayedItem == 0 && y_offset > 0) { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 526 | 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 |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 527 | scrollingSpeed = 0; // stop kinetic scrolling |
| 528 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 529 | |
| 530 | // handle dragging upward, scrolling downward |
| 531 | int totalSize = GetItemCount(); |
| 532 | int lines = GetDisplayItemCount(); // number of full lines our list can display at once |
| 533 | int bottom_offset = GetDisplayRemainder() - actualItemHeight; // extra display area that can display a partial line for per pixel scrolling |
| 534 | |
| 535 | // the offset should always be <= 0 and > -actualItemHeight, adjust the first display row and offset as needed |
| 536 | while (firstDisplayedItem + lines + (bottom_offset ? 1 : 0) < totalSize && abs(y_offset) > actualItemHeight) { |
| 537 | firstDisplayedItem++; |
| 538 | y_offset += actualItemHeight; |
| 539 | } |
| 540 | // Check if we dragged too far, set the list at the bottom and adjust offset as needed |
| 541 | if (bottom_offset != 0 && firstDisplayedItem + lines + 1 >= totalSize && y_offset <= bottom_offset) { |
| 542 | firstDisplayedItem = totalSize - lines - 1; |
| 543 | y_offset = bottom_offset; |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 544 | scrollingSpeed = 0; // stop kinetic scrolling |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 545 | } else if (firstDisplayedItem + lines >= totalSize && y_offset < 0) { |
| 546 | firstDisplayedItem = totalSize - lines; |
| 547 | y_offset = 0; |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 548 | scrollingSpeed = 0; // stop kinetic scrolling |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | |
| 552 | int GUIScrollList::GetDisplayItemCount() |
| 553 | { |
| 554 | return (mRenderH - mHeaderH) / (actualItemHeight); |
| 555 | } |
| 556 | |
| 557 | int GUIScrollList::GetDisplayRemainder() |
| 558 | { |
| 559 | return (mRenderH - mHeaderH) % actualItemHeight; |
| 560 | } |
| 561 | |
| 562 | int GUIScrollList::NotifyVarChange(const std::string& varName, const std::string& value) |
| 563 | { |
| 564 | GUIObject::NotifyVarChange(varName, value); |
| 565 | |
| 566 | if(!isConditionTrue()) |
| 567 | return 0; |
| 568 | |
| 569 | if (!mHeaderIsStatic) { |
| 570 | std::string newValue = gui_parse_text(mHeaderText); |
| 571 | if (mLastHeaderValue != newValue) { |
| 572 | mLastHeaderValue = newValue; |
| 573 | firstDisplayedItem = 0; |
| 574 | y_offset = 0; |
| 575 | scrollingSpeed = 0; // stop kinetic scrolling on variable changes |
| 576 | mUpdate = 1; |
| 577 | } |
| 578 | } |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | int GUIScrollList::SetRenderPos(int x, int y, int w /* = 0 */, int h /* = 0 */) |
| 583 | { |
| 584 | mRenderX = x; |
| 585 | mRenderY = y; |
| 586 | if (w || h) |
| 587 | { |
| 588 | mRenderW = w; |
| 589 | mRenderH = h; |
| 590 | } |
| 591 | SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 592 | mUpdate = 1; |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | void GUIScrollList::SetPageFocus(int inFocus) |
| 597 | { |
| 598 | if (inFocus) { |
| 599 | NotifyVarChange("", ""); // This forces a check for the header text |
| 600 | scrollingSpeed = 0; // stop kinetic scrolling on page changes |
| 601 | mUpdate = 1; |
| 602 | } |
| 603 | } |