Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 1 | /* |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 2 | Copyright 2012 to 2020 TeamWin |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 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" |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 23 | } |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 24 | #include "../minuitwrp/minui.h" |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 25 | #include "../minuitwrp/truetype.hpp" |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 26 | |
| 27 | #include "rapidxml.hpp" |
| 28 | #include "objects.hpp" |
| 29 | #include "../data.hpp" |
| 30 | |
that | 10ec017 | 2015-02-15 23:52:28 +0100 | [diff] [blame] | 31 | const float SCROLLING_SPEED_DECREMENT = 0.9; // friction |
| 32 | const int SCROLLING_FLOOR = 2; // minimum pixels for scrolling to stop |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 33 | |
| 34 | GUIScrollList::GUIScrollList(xml_node<>* node) : GUIObject(node) |
| 35 | { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 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 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 193 | if (!isConditionTrue()) |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 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 |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 268 | if (mFont && mFont->GetResource()) { |
| 269 | gr_color(mHeaderFontColor.red, mHeaderFontColor.green, mHeaderFontColor.blue, mHeaderFontColor.alpha); |
| 270 | gr_textEx_scaleW(mRenderX + IconOffsetX + 5, yPos + (int)(mHeaderH / 2), mLastHeaderValue.c_str(), mFont->GetResource(), mRenderW, TEXT_ONLY_RIGHT, 0); |
| 271 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 272 | |
| 273 | // Add the separator |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 274 | gr_color(mHeaderSeparatorColor.red, mHeaderSeparatorColor.green, mHeaderSeparatorColor.blue, mHeaderSeparatorColor.alpha); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 275 | gr_fill(mRenderX, yPos + mHeaderH - mHeaderSeparatorH, mRenderW, mHeaderSeparatorH); |
| 276 | } |
| 277 | |
that | 9876ac3 | 2015-02-15 21:40:59 +0100 | [diff] [blame] | 278 | // reset clipping |
| 279 | gr_noclip(); |
that | a999821 | 2015-02-19 22:51:24 +0100 | [diff] [blame] | 280 | |
| 281 | // render fast scroll |
| 282 | if (hasScroll) { |
| 283 | int fWidth = mRenderW - listW; |
| 284 | int fHeight = mRenderH - mHeaderH; |
| 285 | int centerX = listW + mRenderX + fWidth / 2; |
| 286 | |
| 287 | // first determine the total list height and where we are in the list |
| 288 | int totalHeight = GetItemCount() * actualItemHeight; // total height of the full list in pixels |
| 289 | int topPos = firstDisplayedItem * actualItemHeight - y_offset; |
| 290 | |
| 291 | // now scale it proportionally to the scrollbar height |
| 292 | int boxH = fHeight * fHeight / totalHeight; // proportional height of the displayed portion |
| 293 | boxH = std::max(boxH, mFastScrollRectH); // but keep a minimum height |
| 294 | int boxY = (fHeight - boxH) * topPos / (totalHeight - fHeight); // pixels relative to top of list |
| 295 | int boxW = mFastScrollRectW; |
| 296 | |
| 297 | int x = centerX - boxW / 2; |
| 298 | int y = mRenderY + mHeaderH + boxY; |
| 299 | |
| 300 | // line above and below box (needs to be split because box can be transparent) |
| 301 | gr_color(mFastScrollLineColor.red, mFastScrollLineColor.green, mFastScrollLineColor.blue, mFastScrollLineColor.alpha); |
| 302 | gr_fill(centerX - mFastScrollLineW / 2, mRenderY + mHeaderH, mFastScrollLineW, boxY); |
| 303 | gr_fill(centerX - mFastScrollLineW / 2, y + boxH, mFastScrollLineW, fHeight - boxY - boxH); |
| 304 | |
| 305 | // box |
| 306 | gr_color(mFastScrollRectColor.red, mFastScrollRectColor.green, mFastScrollRectColor.blue, mFastScrollRectColor.alpha); |
| 307 | gr_fill(x, y, boxW, boxH); |
| 308 | |
| 309 | mFastScrollRectCurrentY = boxY; |
| 310 | mFastScrollRectCurrentH = boxH; |
| 311 | } |
| 312 | mUpdate = 0; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 313 | return 0; |
| 314 | } |
| 315 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 316 | void GUIScrollList::RenderItem(size_t itemindex __unused, int yPos, bool selected) |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame] | 317 | { |
| 318 | RenderStdItem(yPos, selected, NULL, "implement RenderItem!"); |
| 319 | } |
| 320 | |
| 321 | void GUIScrollList::RenderStdItem(int yPos, bool selected, ImageResource* icon, const char* text, int iconAndTextH) |
| 322 | { |
| 323 | if (hasHighlightColor && selected) { |
| 324 | // Highlight the item background of the selected item |
| 325 | gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha); |
| 326 | gr_fill(mRenderX, yPos, mRenderW, actualItemHeight); |
| 327 | } |
| 328 | |
| 329 | if (selected) { |
| 330 | // Use the highlight color for the font |
| 331 | gr_color(mFontHighlightColor.red, mFontHighlightColor.green, mFontHighlightColor.blue, mFontHighlightColor.alpha); |
| 332 | } else { |
| 333 | // Set the color for the font |
| 334 | gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, mFontColor.alpha); |
| 335 | } |
| 336 | |
| 337 | if (!iconAndTextH) |
| 338 | iconAndTextH = actualItemHeight; |
| 339 | |
| 340 | // render icon |
| 341 | if (icon && icon->GetResource()) { |
| 342 | int iconH = icon->GetHeight(); |
| 343 | int iconW = icon->GetWidth(); |
| 344 | int iconY = yPos + (iconAndTextH - iconH) / 2; |
| 345 | int iconX = mRenderX + (maxIconWidth - iconW) / 2; |
| 346 | gr_blit(icon->GetResource(), 0, 0, iconW, iconH, iconX, iconY); |
| 347 | } |
| 348 | |
| 349 | // render label text |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 350 | if (mFont && mFont->GetResource()) { |
| 351 | int textX = mRenderX + maxIconWidth + 5; |
| 352 | int textY = yPos + (iconAndTextH / 2); |
| 353 | gr_textEx_scaleW(textX, textY, text, mFont->GetResource(), mRenderW, TEXT_ONLY_RIGHT, 0); |
| 354 | } |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame] | 355 | } |
| 356 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 357 | int GUIScrollList::Update(void) |
| 358 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 359 | if (!isConditionTrue()) |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 360 | return 0; |
| 361 | |
| 362 | if (!mHeaderIsStatic) { |
| 363 | std::string newValue = gui_parse_text(mHeaderText); |
| 364 | if (mLastHeaderValue != newValue) { |
| 365 | mLastHeaderValue = newValue; |
| 366 | mUpdate = 1; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // Handle kinetic scrolling |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 371 | // maximum number of items to scroll per update |
| 372 | float maxItemsScrolledPerFrame = std::max(2.5, float(GetDisplayItemCount() / 4) + 0.5); |
| 373 | |
| 374 | int maxScrollDistance = actualItemHeight * maxItemsScrolledPerFrame; |
that | 10ec017 | 2015-02-15 23:52:28 +0100 | [diff] [blame] | 375 | int oldScrollingSpeed = scrollingSpeed; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 376 | if (scrollingSpeed == 0) { |
| 377 | // Do nothing |
| 378 | return 0; |
| 379 | } else if (scrollingSpeed > 0) { |
| 380 | if (scrollingSpeed < maxScrollDistance) |
| 381 | y_offset += scrollingSpeed; |
| 382 | else |
| 383 | y_offset += maxScrollDistance; |
that | 10ec017 | 2015-02-15 23:52:28 +0100 | [diff] [blame] | 384 | scrollingSpeed *= SCROLLING_SPEED_DECREMENT; |
| 385 | if (scrollingSpeed == oldScrollingSpeed) |
| 386 | --scrollingSpeed; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 387 | } else if (scrollingSpeed < 0) { |
| 388 | if (abs(scrollingSpeed) < maxScrollDistance) |
| 389 | y_offset += scrollingSpeed; |
| 390 | else |
| 391 | y_offset -= maxScrollDistance; |
that | 10ec017 | 2015-02-15 23:52:28 +0100 | [diff] [blame] | 392 | scrollingSpeed *= SCROLLING_SPEED_DECREMENT; |
| 393 | if (scrollingSpeed == oldScrollingSpeed) |
| 394 | ++scrollingSpeed; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 395 | } |
| 396 | if (abs(scrollingSpeed) < SCROLLING_FLOOR) |
| 397 | scrollingSpeed = 0; |
| 398 | HandleScrolling(); |
| 399 | mUpdate = 1; |
| 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 404 | size_t GUIScrollList::HitTestItem(int x __unused, int y) |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 405 | { |
| 406 | // We only care about y position |
| 407 | if (y < mRenderY || y - mRenderY <= mHeaderH || y - mRenderY > mRenderH) |
| 408 | return NO_ITEM; |
| 409 | |
| 410 | int startSelection = (y - mRenderY - mHeaderH); |
| 411 | |
| 412 | // Locate the correct item |
| 413 | size_t actualSelection = firstDisplayedItem; |
| 414 | int selectY = y_offset; |
| 415 | while (selectY + actualItemHeight < startSelection) { |
| 416 | selectY += actualItemHeight; |
| 417 | actualSelection++; |
| 418 | } |
| 419 | |
| 420 | if (actualSelection < GetItemCount()) |
| 421 | return actualSelection; |
| 422 | |
| 423 | return NO_ITEM; |
| 424 | } |
| 425 | |
| 426 | int GUIScrollList::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 427 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 428 | if (!isConditionTrue()) |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 429 | return -1; |
| 430 | |
| 431 | switch (state) |
| 432 | { |
| 433 | case TOUCH_START: |
that | a999821 | 2015-02-19 22:51:24 +0100 | [diff] [blame] | 434 | if (hasScroll && x >= mRenderX + mRenderW - mFastScrollW) { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 435 | fastScroll = 1; // Initial touch is in the fast scroll region |
that | a999821 | 2015-02-19 22:51:24 +0100 | [diff] [blame] | 436 | int fastScrollBoxTop = mFastScrollRectCurrentY + mRenderY + mHeaderH; |
| 437 | int fastScrollBoxBottom = fastScrollBoxTop + mFastScrollRectCurrentH; |
| 438 | if (y >= fastScrollBoxTop && y < fastScrollBoxBottom) |
| 439 | // user grabbed the fastscroll bar |
| 440 | // try to keep the initially touched part of the scrollbar under the finger |
| 441 | mFastScrollRectTouchY = y - fastScrollBoxTop; |
| 442 | else |
| 443 | // user tapped outside the fastscroll bar |
| 444 | // center fastscroll rect on the initial touch position |
| 445 | mFastScrollRectTouchY = mFastScrollRectCurrentH / 2; |
| 446 | } |
| 447 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 448 | if (scrollingSpeed != 0) { |
| 449 | selectedItem = NO_ITEM; // this allows the user to tap the list to stop the scrolling without selecting the item they tap |
| 450 | scrollingSpeed = 0; // stop scrolling on a new touch |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 451 | } else if (!fastScroll && allowSelection) { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 452 | // find out which item the user touched |
| 453 | selectedItem = HitTestItem(x, y); |
| 454 | } |
| 455 | if (selectedItem != NO_ITEM) |
| 456 | mUpdate = 1; |
| 457 | lastY = last2Y = y; |
| 458 | break; |
| 459 | |
| 460 | case TOUCH_DRAG: |
| 461 | if (fastScroll) |
| 462 | { |
that | a999821 | 2015-02-19 22:51:24 +0100 | [diff] [blame] | 463 | int relY = y - mRenderY - mHeaderH; // touch position relative to window |
| 464 | int windowH = mRenderH - mHeaderH; |
| 465 | int totalHeight = GetItemCount() * actualItemHeight; // total height of the full list in pixels |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 466 | |
that | a999821 | 2015-02-19 22:51:24 +0100 | [diff] [blame] | 467 | // calculate new top position of the fastscroll bar relative to window |
| 468 | int newY = relY - mFastScrollRectTouchY; |
| 469 | // keep it fully inside the list |
| 470 | newY = std::min(std::max(newY, 0), windowH - mFastScrollRectCurrentH); |
| 471 | |
| 472 | // now compute the new scroll position for the list |
| 473 | int newTopPos = newY * (totalHeight - windowH) / (windowH - mFastScrollRectCurrentH); // new top pixel of list |
| 474 | newTopPos = std::min(newTopPos, totalHeight - windowH); // account for rounding errors |
| 475 | firstDisplayedItem = newTopPos / actualItemHeight; |
| 476 | y_offset = - newTopPos % actualItemHeight; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 477 | |
| 478 | selectedItem = NO_ITEM; |
| 479 | mUpdate = 1; |
| 480 | scrollingSpeed = 0; // prevent kinetic scrolling when using fast scroll |
| 481 | break; |
| 482 | } |
| 483 | |
| 484 | // Provide some debounce on initial touches |
| 485 | if (selectedItem != NO_ITEM && abs(y - lastY) < touchDebounce) { |
| 486 | mUpdate = 1; |
| 487 | break; |
| 488 | } |
| 489 | |
| 490 | selectedItem = NO_ITEM; // nothing is selected because we dragged too far |
| 491 | // Handle scrolling |
| 492 | if (hasScroll) { |
| 493 | y_offset += y - lastY; // adjust the scrolling offset based on the difference between the starting touch and the current touch |
| 494 | last2Y = lastY; // keep track of previous y locations so that we can tell how fast to scroll for kinetic scrolling |
| 495 | 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 |
| 496 | |
| 497 | HandleScrolling(); |
| 498 | } else |
| 499 | y_offset = 0; |
| 500 | mUpdate = 1; |
| 501 | break; |
| 502 | |
| 503 | case TOUCH_RELEASE: |
that | a999821 | 2015-02-19 22:51:24 +0100 | [diff] [blame] | 504 | if (fastScroll) |
| 505 | mUpdate = 1; // get rid of touch effects on the fastscroll bar |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 506 | fastScroll = 0; |
| 507 | if (selectedItem != NO_ITEM) { |
| 508 | // We've selected an item! |
| 509 | NotifySelect(selectedItem); |
| 510 | mUpdate = 1; |
| 511 | |
bigbiff bigbiff | 3ed778a | 2019-03-12 19:28:31 -0400 | [diff] [blame] | 512 | #ifndef TW_NO_HAPTICS |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 513 | DataManager::Vibrate("tw_button_vibrate"); |
bigbiff bigbiff | 3ed778a | 2019-03-12 19:28:31 -0400 | [diff] [blame] | 514 | #endif |
| 515 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 516 | selectedItem = NO_ITEM; |
| 517 | } else { |
| 518 | // Start kinetic scrolling |
| 519 | scrollingSpeed = lastY - last2Y; |
that | 10ec017 | 2015-02-15 23:52:28 +0100 | [diff] [blame] | 520 | if (abs(scrollingSpeed) < touchDebounce) |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 521 | scrollingSpeed = 0; |
| 522 | } |
| 523 | case TOUCH_REPEAT: |
| 524 | case TOUCH_HOLD: |
| 525 | break; |
| 526 | } |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | void GUIScrollList::HandleScrolling() |
| 531 | { |
| 532 | // handle dragging downward, scrolling upward |
| 533 | // the offset should always be <= 0 and > -actualItemHeight, adjust the first display row and offset as needed |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 534 | while (firstDisplayedItem && y_offset > 0) { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 535 | firstDisplayedItem--; |
| 536 | y_offset -= actualItemHeight; |
| 537 | } |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 538 | if (firstDisplayedItem == 0 && y_offset > 0) { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 539 | 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] | 540 | scrollingSpeed = 0; // stop kinetic scrolling |
| 541 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 542 | |
| 543 | // handle dragging upward, scrolling downward |
| 544 | int totalSize = GetItemCount(); |
| 545 | int lines = GetDisplayItemCount(); // number of full lines our list can display at once |
| 546 | int bottom_offset = GetDisplayRemainder() - actualItemHeight; // extra display area that can display a partial line for per pixel scrolling |
| 547 | |
| 548 | // the offset should always be <= 0 and > -actualItemHeight, adjust the first display row and offset as needed |
| 549 | while (firstDisplayedItem + lines + (bottom_offset ? 1 : 0) < totalSize && abs(y_offset) > actualItemHeight) { |
| 550 | firstDisplayedItem++; |
| 551 | y_offset += actualItemHeight; |
| 552 | } |
| 553 | // Check if we dragged too far, set the list at the bottom and adjust offset as needed |
| 554 | if (bottom_offset != 0 && firstDisplayedItem + lines + 1 >= totalSize && y_offset <= bottom_offset) { |
| 555 | firstDisplayedItem = totalSize - lines - 1; |
| 556 | y_offset = bottom_offset; |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 557 | scrollingSpeed = 0; // stop kinetic scrolling |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 558 | } else if (firstDisplayedItem + lines >= totalSize && y_offset < 0) { |
| 559 | firstDisplayedItem = totalSize - lines; |
| 560 | y_offset = 0; |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 561 | scrollingSpeed = 0; // stop kinetic scrolling |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | |
| 565 | int GUIScrollList::GetDisplayItemCount() |
| 566 | { |
| 567 | return (mRenderH - mHeaderH) / (actualItemHeight); |
| 568 | } |
| 569 | |
| 570 | int GUIScrollList::GetDisplayRemainder() |
| 571 | { |
| 572 | return (mRenderH - mHeaderH) % actualItemHeight; |
| 573 | } |
| 574 | |
| 575 | int GUIScrollList::NotifyVarChange(const std::string& varName, const std::string& value) |
| 576 | { |
| 577 | GUIObject::NotifyVarChange(varName, value); |
| 578 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 579 | if (!isConditionTrue()) |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 580 | return 0; |
| 581 | |
| 582 | if (!mHeaderIsStatic) { |
| 583 | std::string newValue = gui_parse_text(mHeaderText); |
| 584 | if (mLastHeaderValue != newValue) { |
| 585 | mLastHeaderValue = newValue; |
| 586 | firstDisplayedItem = 0; |
| 587 | y_offset = 0; |
| 588 | scrollingSpeed = 0; // stop kinetic scrolling on variable changes |
| 589 | mUpdate = 1; |
| 590 | } |
| 591 | } |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | int GUIScrollList::SetRenderPos(int x, int y, int w /* = 0 */, int h /* = 0 */) |
| 596 | { |
| 597 | mRenderX = x; |
| 598 | mRenderY = y; |
| 599 | if (w || h) |
| 600 | { |
| 601 | mRenderW = w; |
| 602 | mRenderH = h; |
| 603 | } |
| 604 | SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 605 | mUpdate = 1; |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | void GUIScrollList::SetPageFocus(int inFocus) |
| 610 | { |
| 611 | if (inFocus) { |
| 612 | NotifyVarChange("", ""); // This forces a check for the header text |
| 613 | scrollingSpeed = 0; // stop kinetic scrolling on page changes |
| 614 | mUpdate = 1; |
| 615 | } |
| 616 | } |
Ethan Yonker | 44925ad | 2015-07-22 12:33:59 -0500 | [diff] [blame] | 617 | |
| 618 | bool GUIScrollList::AddLines(std::vector<std::string>* origText, std::vector<std::string>* origColor, size_t* lastCount, std::vector<std::string>* rText, std::vector<std::string>* rColor) |
| 619 | { |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 620 | if (!mFont || !mFont->GetResource()) |
| 621 | return false; |
Ethan Yonker | 44925ad | 2015-07-22 12:33:59 -0500 | [diff] [blame] | 622 | if (*lastCount == origText->size()) |
| 623 | return false; // nothing to add |
| 624 | |
| 625 | size_t prevCount = *lastCount; |
| 626 | *lastCount = origText->size(); |
| 627 | |
| 628 | // Due to word wrap, figure out what / how the newly added text needs to be added to the render vector that is word wrapped |
| 629 | // Note, that multiple consoles on different GUI pages may be different widths or use different fonts, so the word wrapping |
| 630 | // may different in different console windows |
| 631 | for (size_t i = prevCount; i < *lastCount; i++) { |
| 632 | string curr_line = origText->at(i); |
| 633 | string curr_color; |
| 634 | if (origColor) |
| 635 | curr_color = origColor->at(i); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 636 | for (;;) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 637 | size_t line_char_width = twrpTruetype::gr_ttf_maxExW(curr_line.c_str(), mFont->GetResource(), mRenderW); |
Ethan Yonker | 44925ad | 2015-07-22 12:33:59 -0500 | [diff] [blame] | 638 | if (line_char_width < curr_line.size()) { |
| 639 | //string left = curr_line.substr(0, line_char_width); |
| 640 | size_t wrap_pos = curr_line.find_last_of(" ,./:-_;", line_char_width - 1); |
| 641 | if (wrap_pos == string::npos) |
| 642 | wrap_pos = line_char_width; |
| 643 | else if (wrap_pos < line_char_width - 1) |
| 644 | wrap_pos++; |
| 645 | rText->push_back(curr_line.substr(0, wrap_pos)); |
| 646 | if (origColor) |
| 647 | rColor->push_back(curr_color); |
| 648 | curr_line = curr_line.substr(wrap_pos); |
that | 1cc7fed | 2016-01-15 22:13:45 -0600 | [diff] [blame] | 649 | /* After word wrapping, delete any leading spaces. Note that the word wrapping is not smart enough to know not |
| 650 | * to wrap in the middle of something like ... so some of the ... could appear on the following line. */ |
| 651 | curr_line.erase(0, curr_line.find_first_not_of(" ")); |
Ethan Yonker | 44925ad | 2015-07-22 12:33:59 -0500 | [diff] [blame] | 652 | } else { |
| 653 | rText->push_back(curr_line); |
| 654 | if (origColor) |
| 655 | rColor->push_back(curr_color); |
| 656 | break; |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | return true; |
| 661 | } |