Select one of the symbols to view example projects that use it.
 
Outline
#include <gui/custom_controls_screen/WheelSelector.hpp>
#include <touchgfx/Color.hpp>
#include <touchgfx/EasingEquations.hpp>
WheelSelector::WheelSelector()
WheelSelector::~WheelSelector()
WheelSelector::setup(int, int, int, int, TEXTS)
WheelSelector::setTextColor(int16_t, int16_t, uint16_t, int, int)
WheelSelector::handleDragEvent(const DragEvent &)
WheelSelector::handleClickEvent(const ClickEvent &)
WheelSelector::handleGestureEvent(const GestureEvent &)
WheelSelector::snap()
WheelSelector::getSelectedIndex()
WheelSelector::setSelectedIndex(int, int, EasingEquation)
WheelSelector::textMoveEndedHandler(const MoveAnimator<TextArea> &)
WheelSelector::adjustForBoundries(int &)
WheelSelector::moveText(int)
WheelSelector::animateMoveText(int, int, EasingEquation)
WheelSelector::reset()
Files
loading...
CodeScopeSTM32 Libraries and SamplesTouchGFXGui/gui/src/custom_controls_screen/WheelSelector.cpp
 
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
26
27
28
29
30
31
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * This file is part of the TouchGFX 4.10.0 distribution. * * @attention * * Copyright (c) 2018 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* ... */ #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; // For blank lines between texts; // Prepare textSelected (if colors are different) // Position of textSelectedContainer (y coordinate) updated later in setTextColor() 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()); // Position of textSelected updated later in setTextColor() textSelected.setXY(text.getX(), text.getY()); textSelected.setMoveAnimationEndedAction(textMoveEndedCallback); textSelectedContainer.add(textSelected); // Hide selected text, shown when normal/selected colors differ, see setTextColor() 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(); // This also overlaps with textSelected which is therefore also redrawn }{ ... } 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; // Invert % value for negative values 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); }{ ... }