blob: aa9623c68c8495295c06d33f7cfcdfb2da293c95 [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"
23#include "../minuitwrp/minui.h"
24}
25
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 +010032const float SCROLLING_SPEED_LIMIT = 2.5; // maximum number of items to scroll per update
33
34GUIScrollList::GUIScrollList(xml_node<>* node) : GUIObject(node)
35{
36 xml_attribute<>* attr;
37 xml_node<>* child;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010038
39 firstDisplayedItem = mItemSpacing = mFontHeight = mSeparatorH = y_offset = scrollingSpeed = 0;
40 maxIconWidth = maxIconHeight = mHeaderIconHeight = mHeaderIconWidth = 0;
that9876ac32015-02-15 21:40:59 +010041 mHeaderSeparatorH = mHeaderH = actualItemHeight = 0;
42 mHeaderIsStatic = false;
thatf6ed8fc2015-02-14 20:23:16 +010043 mBackground = mHeaderIcon = NULL;
44 mFont = NULL;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010045 mFastScrollW = mFastScrollLineW = mFastScrollRectW = mFastScrollRectH = 0;
thata9998212015-02-19 22:51:24 +010046 mFastScrollRectCurrentY = mFastScrollRectCurrentH = mFastScrollRectTouchY = 0;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010047 lastY = last2Y = fastScroll = 0;
48 mUpdate = 0;
49 touchDebounce = 6;
50 ConvertStrToColor("black", &mBackgroundColor);
51 ConvertStrToColor("black", &mHeaderBackgroundColor);
52 ConvertStrToColor("black", &mSeparatorColor);
53 ConvertStrToColor("black", &mHeaderSeparatorColor);
54 ConvertStrToColor("white", &mFontColor);
55 ConvertStrToColor("white", &mHeaderFontColor);
56 ConvertStrToColor("white", &mFastScrollLineColor);
57 ConvertStrToColor("white", &mFastScrollRectColor);
58 hasHighlightColor = false;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010059 selectedItem = NO_ITEM;
60
61 // Load header text
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010062 child = node->first_node("text");
63 if (child) mHeaderText = child->value();
that9876ac32015-02-15 21:40:59 +010064 // Simple way to check for static state
65 mLastHeaderValue = gui_parse_text(mHeaderText);
66 mHeaderIsStatic = (mLastHeaderValue == mHeaderText);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010067
Ethan Yonker21ff02a2015-02-18 14:35:00 -060068 mHighlightColor = LoadAttrColor(FindNode(node, "highlight"), "color", &hasHighlightColor);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010069
Ethan Yonker21ff02a2015-02-18 14:35:00 -060070 child = FindNode(node, "background");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010071 if (child)
72 {
thatf6ed8fc2015-02-14 20:23:16 +010073 mBackground = LoadAttrImage(child, "resource");
that9876ac32015-02-15 21:40:59 +010074 mBackgroundColor = LoadAttrColor(child, "color");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010075 }
76
77 // Load the placement
Ethan Yonker21ff02a2015-02-18 14:35:00 -060078 LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010079 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
80
81 // Load the font, and possibly override the color
Ethan Yonker21ff02a2015-02-18 14:35:00 -060082 child = FindNode(node, "font");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010083 if (child)
84 {
thatf6ed8fc2015-02-14 20:23:16 +010085 mFont = LoadAttrFont(child, "resource");
that9876ac32015-02-15 21:40:59 +010086 mFontColor = LoadAttrColor(child, "color");
87 mFontHighlightColor = LoadAttrColor(child, "highlightcolor", mFontColor);
88 mItemSpacing = LoadAttrIntScaleY(child, "spacing");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010089 }
90
91 // Load the separator if it exists
Ethan Yonker21ff02a2015-02-18 14:35:00 -060092 child = FindNode(node, "separator");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010093 if (child)
94 {
that9876ac32015-02-15 21:40:59 +010095 mSeparatorColor = LoadAttrColor(child, "color");
96 mSeparatorH = LoadAttrIntScaleY(child, "height");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010097 }
98
that9876ac32015-02-15 21:40:59 +010099 // Fast scroll
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600100 child = FindNode(node, "fastscroll");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100101 if (child)
102 {
that9876ac32015-02-15 21:40:59 +0100103 mFastScrollLineColor = LoadAttrColor(child, "linecolor");
104 mFastScrollRectColor = LoadAttrColor(child, "rectcolor");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100105
that9876ac32015-02-15 21:40:59 +0100106 mFastScrollW = LoadAttrIntScaleX(child, "w");
107 mFastScrollLineW = LoadAttrIntScaleX(child, "linew");
108 mFastScrollRectW = LoadAttrIntScaleX(child, "rectw");
109 mFastScrollRectH = LoadAttrIntScaleY(child, "recth");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100110 }
111
112 // Retrieve the line height
thatf6ed8fc2015-02-14 20:23:16 +0100113 mFontHeight = mFont->GetHeight();
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100114 actualItemHeight = mFontHeight + mItemSpacing + mSeparatorH;
that9876ac32015-02-15 21:40:59 +0100115
116 // Load the header if it exists
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600117 child = FindNode(node, "header");
that9876ac32015-02-15 21:40:59 +0100118 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 Yonker0a3a98f2015-02-05 00:48:28 +0100141
142 if (actualItemHeight / 3 > 6)
143 touchDebounce = actualItemHeight / 3;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100144}
145
146GUIScrollList::~GUIScrollList()
147{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100148}
149
150void 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;
that9876ac32015-02-15 21:40:59 +0100158 if (mHeaderH > 0 && actualItemHeight > mHeaderH)
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100159 mHeaderH = actualItemHeight;
160 }
161}
162
163void GUIScrollList::SetVisibleListLocation(size_t list_index)
164{
165 // This will make sure that the item indicated by list_index is visible on the screen
166 size_t lines = GetDisplayItemCount(), listSize = GetItemCount();
167
168 if (list_index <= (unsigned)firstDisplayedItem) {
169 // list_index is above the currently displayed items, put the selected item at the very top
170 firstDisplayedItem = list_index;
171 y_offset = 0;
172 } else if (list_index >= firstDisplayedItem + lines) {
173 // list_index is below the currently displayed items, put the selected item at the very bottom
174 firstDisplayedItem = list_index - lines + 1;
175 if (GetDisplayRemainder() != 0) {
176 // There's a partial row displayed, set the scrolling offset so that the selected item really is at the very bottom
177 firstDisplayedItem--;
178 y_offset = GetDisplayRemainder() - actualItemHeight;
179 } else {
180 // There's no partial row so zero out the offset
181 y_offset = 0;
182 }
183 }
184 scrollingSpeed = 0; // stop kinetic scrolling on setting visible location
185 mUpdate = 1;
186}
187
188int GUIScrollList::Render(void)
189{
190 if(!isConditionTrue())
191 return 0;
192
193 // First step, fill background
that9876ac32015-02-15 21:40:59 +0100194 gr_color(mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, mBackgroundColor.alpha);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100195 gr_fill(mRenderX, mRenderY + mHeaderH, mRenderW, mRenderH - mHeaderH);
196
that9876ac32015-02-15 21:40:59 +0100197 // don't paint outside of the box
198 gr_clip(mRenderX, mRenderY, mRenderW, mRenderH);
199
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100200 // Next, render the background resource (if it exists)
201 if (mBackground && mBackground->GetResource())
202 {
that0af77952015-02-25 08:52:19 +0100203 int BackgroundW = mBackground->GetWidth();
204 int BackgroundH = mBackground->GetHeight();
205 int BackgroundX = mRenderX + ((mRenderW - BackgroundW) / 2);
206 int BackgroundY = mRenderY + ((mRenderH - BackgroundH) / 2);
207 gr_blit(mBackground->GetResource(), 0, 0, BackgroundW, BackgroundH, BackgroundX, BackgroundY);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100208 }
209
that9876ac32015-02-15 21:40:59 +0100210 // This tells us how many full lines we can actually render
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100211 size_t lines = GetDisplayItemCount();
212
213 size_t listSize = GetItemCount();
that0af77952015-02-25 08:52:19 +0100214 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 +0100215
216 if (listSize <= lines) {
217 hasScroll = false;
218 scrollingSpeed = 0;
219 lines = listSize;
220 y_offset = 0;
221 } else {
222 hasScroll = true;
223 listW -= mFastScrollW; // space for fast scroll
224 lines++;
225 if (lines < listSize)
226 lines++;
227 }
228
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100229 int yPos = mRenderY + mHeaderH + y_offset;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100230
231 // render all visible items
232 for (size_t line = 0; line < lines; line++)
233 {
234 size_t itemindex = line + firstDisplayedItem;
235 if (itemindex >= listSize)
236 break;
237
that0af77952015-02-25 08:52:19 +0100238 RenderItem(itemindex, yPos, itemindex == selectedItem);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100239
240 // Add the separator
that9876ac32015-02-15 21:40:59 +0100241 gr_color(mSeparatorColor.red, mSeparatorColor.green, mSeparatorColor.blue, mSeparatorColor.alpha);
242 gr_fill(mRenderX, yPos + actualItemHeight - mSeparatorH, listW, mSeparatorH);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100243
244 // Move the yPos
245 yPos += actualItemHeight;
246 }
247
that0af77952015-02-25 08:52:19 +0100248 // Render the Header (last so that it overwrites the top most row for per pixel scrolling)
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100249 yPos = mRenderY;
that9876ac32015-02-15 21:40:59 +0100250 if (mHeaderH > 0) {
251 // First step, fill background
252 gr_color(mHeaderBackgroundColor.red, mHeaderBackgroundColor.green, mHeaderBackgroundColor.blue, mHeaderBackgroundColor.alpha);
253 gr_fill(mRenderX, mRenderY, mRenderW, mHeaderH);
254
that0af77952015-02-25 08:52:19 +0100255 int IconOffsetX = 0;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100256
257 // render the icon if it exists
that0af77952015-02-25 08:52:19 +0100258 if (mHeaderIcon && mHeaderIcon->GetResource())
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100259 {
that0af77952015-02-25 08:52:19 +0100260 gr_blit(mHeaderIcon->GetResource(), 0, 0, mHeaderIconWidth, mHeaderIconHeight, mRenderX + ((mHeaderIconWidth - maxIconWidth) / 2), (yPos + (int)((mHeaderH - mHeaderIconHeight) / 2)));
261 IconOffsetX = maxIconWidth;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100262 }
263
264 // render the text
that9876ac32015-02-15 21:40:59 +0100265 gr_color(mHeaderFontColor.red, mHeaderFontColor.green, mHeaderFontColor.blue, mHeaderFontColor.alpha);
that0af77952015-02-25 08:52:19 +0100266 gr_textEx(mRenderX + IconOffsetX + 5, yPos + (int)((mHeaderH - mFontHeight) / 2), mLastHeaderValue.c_str(), mFont->GetResource());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100267
268 // Add the separator
that9876ac32015-02-15 21:40:59 +0100269 gr_color(mHeaderSeparatorColor.red, mHeaderSeparatorColor.green, mHeaderSeparatorColor.blue, mHeaderSeparatorColor.alpha);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100270 gr_fill(mRenderX, yPos + mHeaderH - mHeaderSeparatorH, mRenderW, mHeaderSeparatorH);
271 }
272
that9876ac32015-02-15 21:40:59 +0100273 // reset clipping
274 gr_noclip();
thata9998212015-02-19 22:51:24 +0100275
276 // render fast scroll
277 if (hasScroll) {
278 int fWidth = mRenderW - listW;
279 int fHeight = mRenderH - mHeaderH;
280 int centerX = listW + mRenderX + fWidth / 2;
281
282 // first determine the total list height and where we are in the list
283 int totalHeight = GetItemCount() * actualItemHeight; // total height of the full list in pixels
284 int topPos = firstDisplayedItem * actualItemHeight - y_offset;
285
286 // now scale it proportionally to the scrollbar height
287 int boxH = fHeight * fHeight / totalHeight; // proportional height of the displayed portion
288 boxH = std::max(boxH, mFastScrollRectH); // but keep a minimum height
289 int boxY = (fHeight - boxH) * topPos / (totalHeight - fHeight); // pixels relative to top of list
290 int boxW = mFastScrollRectW;
291
292 int x = centerX - boxW / 2;
293 int y = mRenderY + mHeaderH + boxY;
294
295 // line above and below box (needs to be split because box can be transparent)
296 gr_color(mFastScrollLineColor.red, mFastScrollLineColor.green, mFastScrollLineColor.blue, mFastScrollLineColor.alpha);
297 gr_fill(centerX - mFastScrollLineW / 2, mRenderY + mHeaderH, mFastScrollLineW, boxY);
298 gr_fill(centerX - mFastScrollLineW / 2, y + boxH, mFastScrollLineW, fHeight - boxY - boxH);
299
300 // box
301 gr_color(mFastScrollRectColor.red, mFastScrollRectColor.green, mFastScrollRectColor.blue, mFastScrollRectColor.alpha);
302 gr_fill(x, y, boxW, boxH);
303
304 mFastScrollRectCurrentY = boxY;
305 mFastScrollRectCurrentH = boxH;
306 }
307 mUpdate = 0;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100308 return 0;
309}
310
that0af77952015-02-25 08:52:19 +0100311void GUIScrollList::RenderItem(size_t itemindex, int yPos, bool selected)
312{
313 RenderStdItem(yPos, selected, NULL, "implement RenderItem!");
314}
315
316void GUIScrollList::RenderStdItem(int yPos, bool selected, ImageResource* icon, const char* text, int iconAndTextH)
317{
318 if (hasHighlightColor && selected) {
319 // Highlight the item background of the selected item
320 gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha);
321 gr_fill(mRenderX, yPos, mRenderW, actualItemHeight);
322 }
323
324 if (selected) {
325 // Use the highlight color for the font
326 gr_color(mFontHighlightColor.red, mFontHighlightColor.green, mFontHighlightColor.blue, mFontHighlightColor.alpha);
327 } else {
328 // Set the color for the font
329 gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, mFontColor.alpha);
330 }
331
332 if (!iconAndTextH)
333 iconAndTextH = actualItemHeight;
334
335 // render icon
336 if (icon && icon->GetResource()) {
337 int iconH = icon->GetHeight();
338 int iconW = icon->GetWidth();
339 int iconY = yPos + (iconAndTextH - iconH) / 2;
340 int iconX = mRenderX + (maxIconWidth - iconW) / 2;
341 gr_blit(icon->GetResource(), 0, 0, iconW, iconH, iconX, iconY);
342 }
343
344 // render label text
345 int textX = mRenderX + maxIconWidth + 5;
346 int textY = yPos + (iconAndTextH - mFontHeight) / 2;
347 gr_textEx(textX, textY, text, mFont->GetResource());
348}
349
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100350int GUIScrollList::Update(void)
351{
352 if(!isConditionTrue())
353 return 0;
354
355 if (!mHeaderIsStatic) {
356 std::string newValue = gui_parse_text(mHeaderText);
357 if (mLastHeaderValue != newValue) {
358 mLastHeaderValue = newValue;
359 mUpdate = 1;
360 }
361 }
362
363 // Handle kinetic scrolling
364 int maxScrollDistance = actualItemHeight * SCROLLING_SPEED_LIMIT;
that10ec0172015-02-15 23:52:28 +0100365 int oldScrollingSpeed = scrollingSpeed;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100366 if (scrollingSpeed == 0) {
367 // Do nothing
368 return 0;
369 } else if (scrollingSpeed > 0) {
370 if (scrollingSpeed < maxScrollDistance)
371 y_offset += scrollingSpeed;
372 else
373 y_offset += maxScrollDistance;
that10ec0172015-02-15 23:52:28 +0100374 scrollingSpeed *= SCROLLING_SPEED_DECREMENT;
375 if (scrollingSpeed == oldScrollingSpeed)
376 --scrollingSpeed;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100377 } else if (scrollingSpeed < 0) {
378 if (abs(scrollingSpeed) < maxScrollDistance)
379 y_offset += scrollingSpeed;
380 else
381 y_offset -= maxScrollDistance;
that10ec0172015-02-15 23:52:28 +0100382 scrollingSpeed *= SCROLLING_SPEED_DECREMENT;
383 if (scrollingSpeed == oldScrollingSpeed)
384 ++scrollingSpeed;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100385 }
386 if (abs(scrollingSpeed) < SCROLLING_FLOOR)
387 scrollingSpeed = 0;
388 HandleScrolling();
389 mUpdate = 1;
390
391 return 0;
392}
393
394size_t GUIScrollList::HitTestItem(int x, int y)
395{
396 // We only care about y position
397 if (y < mRenderY || y - mRenderY <= mHeaderH || y - mRenderY > mRenderH)
398 return NO_ITEM;
399
400 int startSelection = (y - mRenderY - mHeaderH);
401
402 // Locate the correct item
403 size_t actualSelection = firstDisplayedItem;
404 int selectY = y_offset;
405 while (selectY + actualItemHeight < startSelection) {
406 selectY += actualItemHeight;
407 actualSelection++;
408 }
409
410 if (actualSelection < GetItemCount())
411 return actualSelection;
412
413 return NO_ITEM;
414}
415
416int GUIScrollList::NotifyTouch(TOUCH_STATE state, int x, int y)
417{
418 if(!isConditionTrue())
419 return -1;
420
421 switch (state)
422 {
423 case TOUCH_START:
thata9998212015-02-19 22:51:24 +0100424 if (hasScroll && x >= mRenderX + mRenderW - mFastScrollW) {
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100425 fastScroll = 1; // Initial touch is in the fast scroll region
thata9998212015-02-19 22:51:24 +0100426 int fastScrollBoxTop = mFastScrollRectCurrentY + mRenderY + mHeaderH;
427 int fastScrollBoxBottom = fastScrollBoxTop + mFastScrollRectCurrentH;
428 if (y >= fastScrollBoxTop && y < fastScrollBoxBottom)
429 // user grabbed the fastscroll bar
430 // try to keep the initially touched part of the scrollbar under the finger
431 mFastScrollRectTouchY = y - fastScrollBoxTop;
432 else
433 // user tapped outside the fastscroll bar
434 // center fastscroll rect on the initial touch position
435 mFastScrollRectTouchY = mFastScrollRectCurrentH / 2;
436 }
437
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100438 if (scrollingSpeed != 0) {
439 selectedItem = NO_ITEM; // this allows the user to tap the list to stop the scrolling without selecting the item they tap
440 scrollingSpeed = 0; // stop scrolling on a new touch
441 } else if (!fastScroll) {
442 // find out which item the user touched
443 selectedItem = HitTestItem(x, y);
444 }
445 if (selectedItem != NO_ITEM)
446 mUpdate = 1;
447 lastY = last2Y = y;
448 break;
449
450 case TOUCH_DRAG:
451 if (fastScroll)
452 {
thata9998212015-02-19 22:51:24 +0100453 int relY = y - mRenderY - mHeaderH; // touch position relative to window
454 int windowH = mRenderH - mHeaderH;
455 int totalHeight = GetItemCount() * actualItemHeight; // total height of the full list in pixels
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100456
thata9998212015-02-19 22:51:24 +0100457 // calculate new top position of the fastscroll bar relative to window
458 int newY = relY - mFastScrollRectTouchY;
459 // keep it fully inside the list
460 newY = std::min(std::max(newY, 0), windowH - mFastScrollRectCurrentH);
461
462 // now compute the new scroll position for the list
463 int newTopPos = newY * (totalHeight - windowH) / (windowH - mFastScrollRectCurrentH); // new top pixel of list
464 newTopPos = std::min(newTopPos, totalHeight - windowH); // account for rounding errors
465 firstDisplayedItem = newTopPos / actualItemHeight;
466 y_offset = - newTopPos % actualItemHeight;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100467
468 selectedItem = NO_ITEM;
469 mUpdate = 1;
470 scrollingSpeed = 0; // prevent kinetic scrolling when using fast scroll
471 break;
472 }
473
474 // Provide some debounce on initial touches
475 if (selectedItem != NO_ITEM && abs(y - lastY) < touchDebounce) {
476 mUpdate = 1;
477 break;
478 }
479
480 selectedItem = NO_ITEM; // nothing is selected because we dragged too far
481 // Handle scrolling
482 if (hasScroll) {
483 y_offset += y - lastY; // adjust the scrolling offset based on the difference between the starting touch and the current touch
484 last2Y = lastY; // keep track of previous y locations so that we can tell how fast to scroll for kinetic scrolling
485 lastY = y; // update last touch to the current touch so we can tell how far and what direction we scroll for the next touch event
486
487 HandleScrolling();
488 } else
489 y_offset = 0;
490 mUpdate = 1;
491 break;
492
493 case TOUCH_RELEASE:
thata9998212015-02-19 22:51:24 +0100494 if (fastScroll)
495 mUpdate = 1; // get rid of touch effects on the fastscroll bar
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100496 fastScroll = 0;
497 if (selectedItem != NO_ITEM) {
498 // We've selected an item!
499 NotifySelect(selectedItem);
500 mUpdate = 1;
501
502 DataManager::Vibrate("tw_button_vibrate");
503 selectedItem = NO_ITEM;
504 } else {
505 // Start kinetic scrolling
506 scrollingSpeed = lastY - last2Y;
that10ec0172015-02-15 23:52:28 +0100507 if (abs(scrollingSpeed) < touchDebounce)
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100508 scrollingSpeed = 0;
509 }
510 case TOUCH_REPEAT:
511 case TOUCH_HOLD:
512 break;
513 }
514 return 0;
515}
516
517void GUIScrollList::HandleScrolling()
518{
519 // handle dragging downward, scrolling upward
520 // the offset should always be <= 0 and > -actualItemHeight, adjust the first display row and offset as needed
521 while(firstDisplayedItem && y_offset > 0) {
522 firstDisplayedItem--;
523 y_offset -= actualItemHeight;
524 }
thatde72b6d2015-02-08 08:55:00 +0100525 if (firstDisplayedItem == 0 && y_offset > 0) {
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100526 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 +0100527 scrollingSpeed = 0; // stop kinetic scrolling
528 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100529
530 // handle dragging upward, scrolling downward
531 int totalSize = GetItemCount();
532 int lines = GetDisplayItemCount(); // number of full lines our list can display at once
533 int bottom_offset = GetDisplayRemainder() - actualItemHeight; // extra display area that can display a partial line for per pixel scrolling
534
535 // the offset should always be <= 0 and > -actualItemHeight, adjust the first display row and offset as needed
536 while (firstDisplayedItem + lines + (bottom_offset ? 1 : 0) < totalSize && abs(y_offset) > actualItemHeight) {
537 firstDisplayedItem++;
538 y_offset += actualItemHeight;
539 }
540 // Check if we dragged too far, set the list at the bottom and adjust offset as needed
541 if (bottom_offset != 0 && firstDisplayedItem + lines + 1 >= totalSize && y_offset <= bottom_offset) {
542 firstDisplayedItem = totalSize - lines - 1;
543 y_offset = bottom_offset;
thatde72b6d2015-02-08 08:55:00 +0100544 scrollingSpeed = 0; // stop kinetic scrolling
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100545 } else if (firstDisplayedItem + lines >= totalSize && y_offset < 0) {
546 firstDisplayedItem = totalSize - lines;
547 y_offset = 0;
thatde72b6d2015-02-08 08:55:00 +0100548 scrollingSpeed = 0; // stop kinetic scrolling
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100549 }
550}
551
552int GUIScrollList::GetDisplayItemCount()
553{
554 return (mRenderH - mHeaderH) / (actualItemHeight);
555}
556
557int GUIScrollList::GetDisplayRemainder()
558{
559 return (mRenderH - mHeaderH) % actualItemHeight;
560}
561
562int GUIScrollList::NotifyVarChange(const std::string& varName, const std::string& value)
563{
564 GUIObject::NotifyVarChange(varName, value);
565
566 if(!isConditionTrue())
567 return 0;
568
569 if (!mHeaderIsStatic) {
570 std::string newValue = gui_parse_text(mHeaderText);
571 if (mLastHeaderValue != newValue) {
572 mLastHeaderValue = newValue;
573 firstDisplayedItem = 0;
574 y_offset = 0;
575 scrollingSpeed = 0; // stop kinetic scrolling on variable changes
576 mUpdate = 1;
577 }
578 }
579 return 0;
580}
581
582int GUIScrollList::SetRenderPos(int x, int y, int w /* = 0 */, int h /* = 0 */)
583{
584 mRenderX = x;
585 mRenderY = y;
586 if (w || h)
587 {
588 mRenderW = w;
589 mRenderH = h;
590 }
591 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
592 mUpdate = 1;
593 return 0;
594}
595
596void GUIScrollList::SetPageFocus(int inFocus)
597{
598 if (inFocus) {
599 NotifyVarChange("", ""); // This forces a check for the header text
600 scrollingSpeed = 0; // stop kinetic scrolling on page changes
601 mUpdate = 1;
602 }
603}