Chesto 0.9
A declarative and element-based library for creating GUIs on homebrew'd consoles
ListElement.cpp
1#include "ListElement.hpp"
2
4{
5 bool ret = false;
6
7 // if we're hidden, don't process input
8 if (hidden) return ret;
9
10 // perform inertia scrolling for this element
11 ret |= this->handleInertiaScroll(event);
12
13 ret |= super::process(event);
14
15 return ret;
16}
17
18bool ListElement::processUpDown(InputEvents* event)
19{
20 bool ret = false;
21 int SPEED = 1;
22
23 // handle up and down for the scroll view
24 if (event->isKeyDown())
25 {
26 // scroll the view by offsetting the elastic counter
27 event->wheelScroll += (SPEED * event->held(UP_BUTTON) - SPEED * event->held(DOWN_BUTTON));
28
29 ret |= event->held(UP_BUTTON) || event->held(DOWN_BUTTON);
30 }
31
32 return ret;
33}
34
35bool ListElement::handleInertiaScroll(InputEvents* event)
36{
37 bool ret = false;
38 ListElement* elem = this;
39
40 if (event->isTouchDown())
41 {
42 // make sure that the mouse down's X coordinate is over the app list (not sidebar)
43 if (event->xPos < elem->xAbs)
44 return false;
45
46 // saw mouse down so set it in our element object
47 elem->dragging = true;
48 elem->lastMouseY = event->yPos;
49 elem->initialTouchDown = event->yPos;
50
51 ret |= true;
52 }
53 // drag event for scrolling up or down
54 else if (event->isTouchDrag())
55 {
56 if (elem->dragging)
57 {
58 // prevent scrolling until we exceed a treshold distance in the Y direction
59 if (this->initialTouchDown >= 0 && (abs(event->yPos - this->initialTouchDown) < 10 / SCALER))
60 return false;
61
62 this->initialTouchDown = -1;
63
64 int distance = event->yPos - elem->lastMouseY;
65 elem->y += distance;
66 elem->lastMouseY = event->yPos;
67
68 // use the last distance as the rubber band value
69 elem->elasticCounter = distance;
70
71 ret |= true;
72 }
73 }
74 else if (event->isTouchUp())
75 {
76 // mouse up, no more mouse down (TODO: fire selected event here)
77 elem->dragging = false;
78
79 // if the scroll offset is less than the total number of apps
80 // (put on the mouse up to make it "snap" when going out of bounds)
81 // TODO: account for max number of apps too (prevent scrolling down forever)
82 if (elem->y > minYScroll)
83 elem->y = minYScroll;
84
85 ret |= true;
86 }
87
88 // if mouse is up, and there's some elastic counter left, burn out remaining elastic value
89 if (!elem->dragging && elem->elasticCounter != 0)
90 {
91 elem->y += elem->elasticCounter;
92
93 // reduce the elastic counter by 5% each time (slows down the scroll)
94 elem->elasticCounter *= 0.95;
95
96 // if we're less than 10 pixels on the counter, just stop
97 if (abs(elem->elasticCounter) < 10)
98 elem->elasticCounter = 0;
99
100 // TODO: same problem as above todo, also extract into method?
101 if (elem->y > minYScroll)
102 elem->y = minYScroll;
103
104 ret |= true;
105 }
106
107 if (ret) event->isScrolling = true;
108
109 if (event->wheelScroll != 0)
110 {
111 // apply wheel scroll directly to y position, and then reset
112 elem->y += event->wheelScroll * 10;
113 if (elem->y > minYScroll)
114 elem->y = minYScroll;
115 event->wheelScroll *= 0.95;
116
117 if (abs(event->wheelScroll) < 0.1)
118 event->wheelScroll = 0;
119
120 ret |= true;
121 }
122
123 return ret;
124}
virtual bool process(InputEvents *event)
process any input that is received for this element
Definition: Element.cpp:17
bool hidden
whether this element should skip rendering or not
Definition: Element.hpp:105
bool dragging
whether or not this element is currently being dragged
Definition: Element.hpp:81
int elasticCounter
how much time is left in an elastic-type flick/scroll set by the last distance traveled in a scroll,...
Definition: Element.hpp:115
int lastMouseY
the last Y, X coordinate of the mouse (from a drag probably)
Definition: Element.hpp:90
bool process(InputEvents *event)
process any input that is received for this element
Definition: ListElement.cpp:3