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