1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
28
29
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
91
92
93
94
101
102
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
151
155
156
157
158
162
163
164
165
166
167
168
172
177
178
179
180
202
203
214
215
225
226
234
235
/* ... */
#include <gui/custom_controls_screen/WheelSelector.hpp>
#include <touchgfx/Color.hpp>
#include <touchgfx/EasingEquations.hpp>
WheelSelector::WheelSelector() :
currentAnimationState(NO_ANIMATION),
elementSelectedAction(0),
textMoveEndedCallback(this, &WheelSelector::textMoveEndedHandler)
{
}{ ... }
WheelSelector::~WheelSelector()
{
}{ ... }
void WheelSelector::setup(int width, int height, int textXPosition, int textYPosition, TEXTS wheelText)
{
setWidth(width);
setHeight(height);
setTouchable(true);
selectedTextYPosition = textYPosition;
textContainer.setPosition(0, 0, getWidth(), getHeight());
add(textContainer);
text.setTypedText(TypedText(wheelText));
text.setXY(textXPosition, selectedTextYPosition);
text.setMoveAnimationEndedAction(textMoveEndedCallback);
textContainer.add(text);
spaceBetweenTextRows = text.getTypedText().getFont()->getMinimumTextHeight() * 2;
textSelectedContainer.setPosition(textContainer.getX(), textContainer.getY(), textContainer.getWidth(), 0);
add(textSelectedContainer);
textSelectedBackground.setPosition(0, 0, textSelectedContainer.getWidth(), 0);
textSelectedContainer.add(textSelectedBackground);
textSelected.setTypedText(text.getTypedText());
textSelected.setXY(text.getX(), text.getY());
textSelected.setMoveAnimationEndedAction(textMoveEndedCallback);
textSelectedContainer.add(textSelected);
textSelectedContainer.setVisible(false);
topCutOff = selectedTextYPosition + (spaceBetweenTextRows / 2);
bottomCutOff = topCutOff - text.getHeight() - (spaceBetweenTextRows / 3);
reset();
}{ ... }
void WheelSelector::setTextColor(int16_t normalColor, int16_t selectedColor, uint16_t selectedBackgroundColor, int selectedYOffset, int selectedHeight)
{
text.setColor(normalColor);
if (normalColor != selectedColor)
{
selectedAreaYOffset = selectedYOffset;
textSelectedBackground.setHeight(selectedHeight);
textSelectedBackground.setColor(selectedBackgroundColor);
textSelectedContainer.setY(textContainer.getY() + selectedAreaYOffset);
textSelectedContainer.setHeight(selectedHeight);
textSelected.setColor(selectedColor);
textSelected.setY(text.getY() - selectedAreaYOffset);
textSelectedContainer.setVisible(true);
}if (normalColor != selectedColor) { ... }
else
{
textSelectedContainer.setVisible(false);
}else { ... }
text.invalidate();
}{ ... }
void WheelSelector::handleDragEvent(const DragEvent& evt)
{
if (currentAnimationState == ANIMATING_DRAG)
{
moveText(text.getY() + evt.getDeltaY());
}if (currentAnimationState == ANIMATING_DRAG) { ... }
}{ ... }
void WheelSelector::handleClickEvent(const ClickEvent& evt)
{
if (evt.getType() == ClickEvent::PRESSED)
{
if (currentAnimationState != NO_ANIMATION)
{
text.cancelMoveAnimation();
textSelected.cancelMoveAnimation();
}if (currentAnimationState != NO_ANIMATION) { ... }
currentAnimationState = ANIMATING_DRAG;
}if (evt.getType() == ClickEvent::PRESSED) { ... }
else if (evt.getType() == ClickEvent::RELEASED && currentAnimationState == ANIMATING_DRAG)
{
snap();
}else if (evt.getType() == ClickEvent::RELEASED && currentAnimationState == ANIMATING_DRAG) { ... }
}{ ... }
void WheelSelector::handleGestureEvent(const GestureEvent& evt)
{
if (evt.getType() == GestureEvent::SWIPE_VERTICAL)
{
currentAnimationState = ANIMATING_GESTURE;
int delta = evt.getVelocity() * 5;
int newYPosition = text.getY() + delta;
adjustForBoundries(newYPosition);
int distanceToMove = newYPosition - text.getY();
int distanceToMoveABS = (distanceToMove > 0) ? distanceToMove : -distanceToMove;
int duration = distanceToMoveABS / 10;
duration = (duration < 2) ? 2 : duration;
animateMoveText(newYPosition, duration, EasingEquations::cubicEaseOut);
}if (evt.getType() == GestureEvent::SWIPE_VERTICAL) { ... }
}{ ... }
void WheelSelector::snap()
{
int deltaUp = (spaceBetweenTextRows - (text.getY() - selectedTextYPosition)) % spaceBetweenTextRows;
deltaUp = (deltaUp > 0) ? deltaUp : spaceBetweenTextRows + deltaUp;
int deltaDown = spaceBetweenTextRows - deltaUp;
if (deltaUp < deltaDown)
{
animateMoveText(text.getY() + deltaUp, 10, EasingEquations::backEaseInOut);
}if (deltaUp < deltaDown) { ... }
else
{
animateMoveText(text.getY() - deltaDown, 10, EasingEquations::backEaseInOut);
}else { ... }
currentAnimationState = ANIMATING_SNAP;
}{ ... }
int WheelSelector::getSelectedIndex()
{
return (selectedTextYPosition - text.getY()) / spaceBetweenTextRows;
}{ ... }
void WheelSelector::setSelectedIndex(int index, int duration, EasingEquation equation)
{
int newYPosition = selectedTextYPosition - (spaceBetweenTextRows * index);
adjustForBoundries(newYPosition);
if (duration == 0)
{
moveText(newYPosition);
}if (duration == 0) { ... }
else
{
currentAnimationState = ANIMATING_GESTURE;
animateMoveText(newYPosition, duration, equation);
}else { ... }
}{ ... }
void WheelSelector::textMoveEndedHandler(const MoveAnimator<TextArea>& textArea)
{
if (currentAnimationState == ANIMATING_GESTURE)
{
if (!textArea.isRunning() && !textSelected.isRunning())
{
snap();
}if (!textArea.isRunning() && !textSelected.isRunning()) { ... }
}if (currentAnimationState == ANIMATING_GESTURE) { ... }
else if (currentAnimationState == ANIMATING_SNAP)
{
currentAnimationState = NO_ANIMATION;
if (elementSelectedAction && elementSelectedAction->isValid())
{
elementSelectedAction->execute(*this, getSelectedIndex());
}if (elementSelectedAction && elementSelectedAction->isValid()) { ... }
}else if (currentAnimationState == ANIMATING_SNAP) { ... }
else
{
currentAnimationState = NO_ANIMATION;
}else { ... }
}{ ... }
void WheelSelector::adjustForBoundries(int& newYValue)
{
if (newYValue > topCutOff)
{
newYValue = topCutOff;
}if (newYValue > topCutOff) { ... }
else if (newYValue < bottomCutOff)
{
newYValue = bottomCutOff;
}else if (newYValue < bottomCutOff) { ... }
}{ ... }
void WheelSelector::moveText(int newYPosition)
{
adjustForBoundries(newYPosition);
text.moveTo(text.getX(), newYPosition);
if (textSelectedContainer.isVisible())
{
textSelected.moveTo(text.getX(), newYPosition - selectedAreaYOffset);
}if (textSelectedContainer.isVisible()) { ... }
}{ ... }
void WheelSelector::animateMoveText(int newYPosition, int duration, EasingEquation equation)
{
text.startMoveAnimation(text.getX(), newYPosition, duration, EasingEquations::linearEaseNone, equation);
if (textSelectedContainer.isVisible())
{
textSelected.startMoveAnimation(text.getX(), newYPosition - selectedAreaYOffset, duration, EasingEquations::linearEaseNone, equation);
}if (textSelectedContainer.isVisible()) { ... }
}{ ... }
void WheelSelector::reset()
{
moveText(selectedTextYPosition);
}{ ... }