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