blob: 7540356bfdf14f2c4bf4a3c0799660c5f944d0e0 [file] [log] [blame]
Ethan Yonker0a3a98f2015-02-05 00:48:28 +01001/*
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
21extern "C" {
22#include "../twcommon.h"
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010023}
Ethan Yonkerfbb43532015-12-28 21:54:50 +010024#include "../minuitwrp/minui.h"
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010025
26#include "rapidxml.hpp"
27#include "objects.hpp"
28#include "../data.hpp"
29
that10ec0172015-02-15 23:52:28 +010030const float SCROLLING_SPEED_DECREMENT = 0.9; // friction
31const int SCROLLING_FLOOR = 2; // minimum pixels for scrolling to stop
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010032
33GUIScrollList::GUIScrollList(xml_node<>* node) : GUIObject(node)
34{
35 xml_attribute<>* attr;
36 xml_node<>* child;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010037
38 firstDisplayedItem = mItemSpacing = mFontHeight = mSeparatorH = y_offset = scrollingSpeed = 0;
39 maxIconWidth = maxIconHeight = mHeaderIconHeight = mHeaderIconWidth = 0;
that9876ac32015-02-15 21:40:59 +010040 mHeaderSeparatorH = mHeaderH = actualItemHeight = 0;
41 mHeaderIsStatic = false;
thatf6ed8fc2015-02-14 20:23:16 +010042 mBackground = mHeaderIcon = NULL;
43 mFont = NULL;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010044 mFastScrollW = mFastScrollLineW = mFastScrollRectW = mFastScrollRectH = 0;
thata9998212015-02-19 22:51:24 +010045 mFastScrollRectCurrentY = mFastScrollRectCurrentH = mFastScrollRectTouchY = 0;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010046 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;
that8d46c092015-02-26 01:30:04 +010058 allowSelection = true;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010059 selectedItem = NO_ITEM;
60
61 // Load header text
that8d46c092015-02-26 01:30:04 +010062 // note: node can be NULL for the emergency console
63 child = node ? node->first_node("text") : NULL;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010064 if (child) mHeaderText = child->value();
that9876ac32015-02-15 21:40:59 +010065 // Simple way to check for static state
66 mLastHeaderValue = gui_parse_text(mHeaderText);
67 mHeaderIsStatic = (mLastHeaderValue == mHeaderText);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010068
Ethan Yonker21ff02a2015-02-18 14:35:00 -060069 mHighlightColor = LoadAttrColor(FindNode(node, "highlight"), "color", &hasHighlightColor);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010070
Ethan Yonker21ff02a2015-02-18 14:35:00 -060071 child = FindNode(node, "background");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010072 if (child)
73 {
thatf6ed8fc2015-02-14 20:23:16 +010074 mBackground = LoadAttrImage(child, "resource");
that9876ac32015-02-15 21:40:59 +010075 mBackgroundColor = LoadAttrColor(child, "color");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010076 }
77
78 // Load the placement
Ethan Yonker21ff02a2015-02-18 14:35:00 -060079 LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010080 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
81
82 // Load the font, and possibly override the color
Ethan Yonker21ff02a2015-02-18 14:35:00 -060083 child = FindNode(node, "font");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010084 if (child)
85 {
thatf6ed8fc2015-02-14 20:23:16 +010086 mFont = LoadAttrFont(child, "resource");
that9876ac32015-02-15 21:40:59 +010087 mFontColor = LoadAttrColor(child, "color");
88 mFontHighlightColor = LoadAttrColor(child, "highlightcolor", mFontColor);
89 mItemSpacing = LoadAttrIntScaleY(child, "spacing");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010090 }
91
92 // Load the separator if it exists
Ethan Yonker21ff02a2015-02-18 14:35:00 -060093 child = FindNode(node, "separator");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010094 if (child)
95 {
that9876ac32015-02-15 21:40:59 +010096 mSeparatorColor = LoadAttrColor(child, "color");
97 mSeparatorH = LoadAttrIntScaleY(child, "height");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010098 }
99
that9876ac32015-02-15 21:40:59 +0100100 // Fast scroll
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600101 child = FindNode(node, "fastscroll");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100102 if (child)
103 {
that9876ac32015-02-15 21:40:59 +0100104 mFastScrollLineColor = LoadAttrColor(child, "linecolor");
105 mFastScrollRectColor = LoadAttrColor(child, "rectcolor");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100106
that9876ac32015-02-15 21:40:59 +0100107 mFastScrollW = LoadAttrIntScaleX(child, "w");
108 mFastScrollLineW = LoadAttrIntScaleX(child, "linew");
109 mFastScrollRectW = LoadAttrIntScaleX(child, "rectw");
110 mFastScrollRectH = LoadAttrIntScaleY(child, "recth");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100111 }
112
113 // Retrieve the line height
thatf6ed8fc2015-02-14 20:23:16 +0100114 mFontHeight = mFont->GetHeight();
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100115 actualItemHeight = mFontHeight + mItemSpacing + mSeparatorH;
that9876ac32015-02-15 21:40:59 +0100116
117 // Load the header if it exists
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600118 child = FindNode(node, "header");
that9876ac32015-02-15 21:40:59 +0100119 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 Yonker0a3a98f2015-02-05 00:48:28 +0100142
143 if (actualItemHeight / 3 > 6)
144 touchDebounce = actualItemHeight / 3;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100145}
146
147GUIScrollList::~GUIScrollList()
148{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100149}
150
151void 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;
that9876ac32015-02-15 21:40:59 +0100159 if (mHeaderH > 0 && actualItemHeight > mHeaderH)
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100160 mHeaderH = actualItemHeight;
161 }
162}
163
164void GUIScrollList::SetVisibleListLocation(size_t list_index)
165{
166 // This will make sure that the item indicated by list_index is visible on the screen
that8d46c092015-02-26 01:30:04 +0100167 size_t lines = GetDisplayItemCount();
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100168
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 }
that8d46c092015-02-26 01:30:04 +0100184 if (firstDisplayedItem < 0)
185 firstDisplayedItem = 0;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100186 }
187 scrollingSpeed = 0; // stop kinetic scrolling on setting visible location
188 mUpdate = 1;
189}
190
191int GUIScrollList::Render(void)
192{
Matt Mowera8a89d12016-12-30 18:10:37 -0600193 if (!isConditionTrue())
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100194 return 0;
195
196 // First step, fill background
that9876ac32015-02-15 21:40:59 +0100197 gr_color(mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, mBackgroundColor.alpha);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100198 gr_fill(mRenderX, mRenderY + mHeaderH, mRenderW, mRenderH - mHeaderH);
199
that9876ac32015-02-15 21:40:59 +0100200 // don't paint outside of the box
201 gr_clip(mRenderX, mRenderY, mRenderW, mRenderH);
202
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100203 // Next, render the background resource (if it exists)
204 if (mBackground && mBackground->GetResource())
205 {
that0af77952015-02-25 08:52:19 +0100206 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 Yonker0a3a98f2015-02-05 00:48:28 +0100211 }
212
that9876ac32015-02-15 21:40:59 +0100213 // This tells us how many full lines we can actually render
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100214 size_t lines = GetDisplayItemCount();
215
216 size_t listSize = GetItemCount();
that0af77952015-02-25 08:52:19 +0100217 int listW = mRenderW; // this is only used for the separators - the list items are rendered in the full width of the list
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100218
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 Yonker0a3a98f2015-02-05 00:48:28 +0100232 int yPos = mRenderY + mHeaderH + y_offset;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100233
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
that0af77952015-02-25 08:52:19 +0100241 RenderItem(itemindex, yPos, itemindex == selectedItem);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100242
243 // Add the separator
that9876ac32015-02-15 21:40:59 +0100244 gr_color(mSeparatorColor.red, mSeparatorColor.green, mSeparatorColor.blue, mSeparatorColor.alpha);
245 gr_fill(mRenderX, yPos + actualItemHeight - mSeparatorH, listW, mSeparatorH);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100246
247 // Move the yPos
248 yPos += actualItemHeight;
249 }
250
that0af77952015-02-25 08:52:19 +0100251 // Render the Header (last so that it overwrites the top most row for per pixel scrolling)
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100252 yPos = mRenderY;
that9876ac32015-02-15 21:40:59 +0100253 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
that0af77952015-02-25 08:52:19 +0100258 int IconOffsetX = 0;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100259
260 // render the icon if it exists
that0af77952015-02-25 08:52:19 +0100261 if (mHeaderIcon && mHeaderIcon->GetResource())
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100262 {
that0af77952015-02-25 08:52:19 +0100263 gr_blit(mHeaderIcon->GetResource(), 0, 0, mHeaderIconWidth, mHeaderIconHeight, mRenderX + ((mHeaderIconWidth - maxIconWidth) / 2), (yPos + (int)((mHeaderH - mHeaderIconHeight) / 2)));
264 IconOffsetX = maxIconWidth;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100265 }
266
267 // render the text
that9876ac32015-02-15 21:40:59 +0100268 gr_color(mHeaderFontColor.red, mHeaderFontColor.green, mHeaderFontColor.blue, mHeaderFontColor.alpha);
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500269 gr_textEx_scaleW(mRenderX + IconOffsetX + 5, yPos + (int)(mHeaderH / 2), mLastHeaderValue.c_str(), mFont->GetResource(), mRenderW, TEXT_ONLY_RIGHT, 0);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100270
271 // Add the separator
that9876ac32015-02-15 21:40:59 +0100272 gr_color(mHeaderSeparatorColor.red, mHeaderSeparatorColor.green, mHeaderSeparatorColor.blue, mHeaderSeparatorColor.alpha);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100273 gr_fill(mRenderX, yPos + mHeaderH - mHeaderSeparatorH, mRenderW, mHeaderSeparatorH);
274 }
275
that9876ac32015-02-15 21:40:59 +0100276 // reset clipping
277 gr_noclip();
thata9998212015-02-19 22:51:24 +0100278
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 Yonker0a3a98f2015-02-05 00:48:28 +0100311 return 0;
312}
313
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500314void GUIScrollList::RenderItem(size_t itemindex __unused, int yPos, bool selected)
that0af77952015-02-25 08:52:19 +0100315{
316 RenderStdItem(yPos, selected, NULL, "implement RenderItem!");
317}
318
319void 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;
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500349 int textY = yPos + (iconAndTextH / 2);
350 gr_textEx_scaleW(textX, textY, text, mFont->GetResource(), mRenderW, TEXT_ONLY_RIGHT, 0);
that0af77952015-02-25 08:52:19 +0100351}
352
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100353int GUIScrollList::Update(void)
354{
Matt Mowera8a89d12016-12-30 18:10:37 -0600355 if (!isConditionTrue())
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100356 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
that8d46c092015-02-26 01:30:04 +0100367 // 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;
that10ec0172015-02-15 23:52:28 +0100371 int oldScrollingSpeed = scrollingSpeed;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100372 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;
that10ec0172015-02-15 23:52:28 +0100380 scrollingSpeed *= SCROLLING_SPEED_DECREMENT;
381 if (scrollingSpeed == oldScrollingSpeed)
382 --scrollingSpeed;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100383 } else if (scrollingSpeed < 0) {
384 if (abs(scrollingSpeed) < maxScrollDistance)
385 y_offset += scrollingSpeed;
386 else
387 y_offset -= maxScrollDistance;
that10ec0172015-02-15 23:52:28 +0100388 scrollingSpeed *= SCROLLING_SPEED_DECREMENT;
389 if (scrollingSpeed == oldScrollingSpeed)
390 ++scrollingSpeed;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100391 }
392 if (abs(scrollingSpeed) < SCROLLING_FLOOR)
393 scrollingSpeed = 0;
394 HandleScrolling();
395 mUpdate = 1;
396
397 return 0;
398}
399
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500400size_t GUIScrollList::HitTestItem(int x __unused, int y)
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100401{
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
422int GUIScrollList::NotifyTouch(TOUCH_STATE state, int x, int y)
423{
Matt Mowera8a89d12016-12-30 18:10:37 -0600424 if (!isConditionTrue())
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100425 return -1;
426
427 switch (state)
428 {
429 case TOUCH_START:
thata9998212015-02-19 22:51:24 +0100430 if (hasScroll && x >= mRenderX + mRenderW - mFastScrollW) {
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100431 fastScroll = 1; // Initial touch is in the fast scroll region
thata9998212015-02-19 22:51:24 +0100432 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 Yonker0a3a98f2015-02-05 00:48:28 +0100444 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
that8d46c092015-02-26 01:30:04 +0100447 } else if (!fastScroll && allowSelection) {
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100448 // 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 {
thata9998212015-02-19 22:51:24 +0100459 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 Yonker0a3a98f2015-02-05 00:48:28 +0100462
thata9998212015-02-19 22:51:24 +0100463 // 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 Yonker0a3a98f2015-02-05 00:48:28 +0100473
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:
thata9998212015-02-19 22:51:24 +0100500 if (fastScroll)
501 mUpdate = 1; // get rid of touch effects on the fastscroll bar
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100502 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;
that10ec0172015-02-15 23:52:28 +0100513 if (abs(scrollingSpeed) < touchDebounce)
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100514 scrollingSpeed = 0;
515 }
516 case TOUCH_REPEAT:
517 case TOUCH_HOLD:
518 break;
519 }
520 return 0;
521}
522
523void 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
Matt Mowera8a89d12016-12-30 18:10:37 -0600527 while (firstDisplayedItem && y_offset > 0) {
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100528 firstDisplayedItem--;
529 y_offset -= actualItemHeight;
530 }
thatde72b6d2015-02-08 08:55:00 +0100531 if (firstDisplayedItem == 0 && y_offset > 0) {
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100532 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
thatde72b6d2015-02-08 08:55:00 +0100533 scrollingSpeed = 0; // stop kinetic scrolling
534 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100535
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;
thatde72b6d2015-02-08 08:55:00 +0100550 scrollingSpeed = 0; // stop kinetic scrolling
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100551 } else if (firstDisplayedItem + lines >= totalSize && y_offset < 0) {
552 firstDisplayedItem = totalSize - lines;
553 y_offset = 0;
thatde72b6d2015-02-08 08:55:00 +0100554 scrollingSpeed = 0; // stop kinetic scrolling
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100555 }
556}
557
558int GUIScrollList::GetDisplayItemCount()
559{
560 return (mRenderH - mHeaderH) / (actualItemHeight);
561}
562
563int GUIScrollList::GetDisplayRemainder()
564{
565 return (mRenderH - mHeaderH) % actualItemHeight;
566}
567
568int GUIScrollList::NotifyVarChange(const std::string& varName, const std::string& value)
569{
570 GUIObject::NotifyVarChange(varName, value);
571
Matt Mowera8a89d12016-12-30 18:10:37 -0600572 if (!isConditionTrue())
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100573 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
588int 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
602void 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}
Ethan Yonker44925ad2015-07-22 12:33:59 -0500610
611bool 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)
612{
613 if (*lastCount == origText->size())
614 return false; // nothing to add
615
616 size_t prevCount = *lastCount;
617 *lastCount = origText->size();
618
619 // Due to word wrap, figure out what / how the newly added text needs to be added to the render vector that is word wrapped
620 // Note, that multiple consoles on different GUI pages may be different widths or use different fonts, so the word wrapping
621 // may different in different console windows
622 for (size_t i = prevCount; i < *lastCount; i++) {
623 string curr_line = origText->at(i);
624 string curr_color;
625 if (origColor)
626 curr_color = origColor->at(i);
Matt Mowera8a89d12016-12-30 18:10:37 -0600627 for (;;) {
Ethan Yonker44925ad2015-07-22 12:33:59 -0500628 size_t line_char_width = gr_ttf_maxExW(curr_line.c_str(), mFont->GetResource(), mRenderW);
629 if (line_char_width < curr_line.size()) {
630 //string left = curr_line.substr(0, line_char_width);
631 size_t wrap_pos = curr_line.find_last_of(" ,./:-_;", line_char_width - 1);
632 if (wrap_pos == string::npos)
633 wrap_pos = line_char_width;
634 else if (wrap_pos < line_char_width - 1)
635 wrap_pos++;
636 rText->push_back(curr_line.substr(0, wrap_pos));
637 if (origColor)
638 rColor->push_back(curr_color);
639 curr_line = curr_line.substr(wrap_pos);
that1cc7fed2016-01-15 22:13:45 -0600640 /* After word wrapping, delete any leading spaces. Note that the word wrapping is not smart enough to know not
641 * to wrap in the middle of something like ... so some of the ... could appear on the following line. */
642 curr_line.erase(0, curr_line.find_first_not_of(" "));
Ethan Yonker44925ad2015-07-22 12:33:59 -0500643 } else {
644 rText->push_back(curr_line);
645 if (origColor)
646 rColor->push_back(curr_color);
647 break;
648 }
649 }
650 }
651 return true;
652}