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
45
46
50
51
52
53
54
55
56
57
58
59
60
61
62
63
67
68
72
73
77
78
82
83
87
88
94
95
114
115
116
117
118
119
123
124
125
126
127
128
129
130
146
162
163
164
165
166
167
168
169
173
174
175
176
177
181
185
186
187
188
189
190
191
192
196
197
198
199
200
201
202
203
212
221
222
223
224
228
229
230
231
232
233
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
284
285
286
287
288
289
290
291
292
293
294
295
299
300
301
302
303
304
305
306
307
314
315
316
317
318
319
320
321
322
323
324
328
329
330
331
332
333
/* ... */
#include <gui/common/SwipeContainer.hpp>
#include <touchgfx/EasingEquations.hpp>
#include "BitmapDatabase.hpp"
#include <touchgfx/Color.hpp>
using namespace touchgfx;
SwipeContainer::SwipeContainer() :
currentState(NO_ANIMATION),
numberOfScreens(0),
animationCounter(0),
swipeCutoff(80),
dragX(0),
currentScreen(0),
endElasticWidth(30),
screens(EAST),
menuChangeInitiatedAction(0),
menuChangeEndedAction(0)
{
touchgfx::Application::getInstance()->registerTimerWidget(this);
setTouchable(true);
Container::add(screens);
Container::add(dotIndicator);
}{ ... }
SwipeContainer::~SwipeContainer()
{
touchgfx::Application::getInstance()->unregisterTimerWidget(this);
}{ ... }
void SwipeContainer::add(Drawable& screen)
{
screens.add(screen);
numberOfScreens++;
dotIndicator.setNumberOfDots(numberOfScreens);
setWidth(screen.getWidth());
setHeight(screen.getHeight());
}{ ... }
void SwipeContainer::setEndSwipeElasticWidth(uint16_t width)
{
endElasticWidth = width;
}{ ... }
void SwipeContainer::setSwipeCutoff(uint16_t cutoff)
{
swipeCutoff = cutoff;
}{ ... }
void SwipeContainer::setDotIndicatorBitmaps(const touchgfx::Bitmap& normalDot, const touchgfx::Bitmap& highlightedDot)
{
dotIndicator.setBitmaps(normalDot, highlightedDot);
}{ ... }
void SwipeContainer::setDotIndicatorXY(uint16_t x, uint16_t y)
{
dotIndicator.setXY(x, y);
}{ ... }
void SwipeContainer::setDotIndicatorXYWithCenteredX(uint16_t x, uint16_t y)
{
dotIndicator.setXY(x - dotIndicator.getWidth() / 2, y);
}{ ... }
void SwipeContainer::setSelectedScreen(uint8_t screenIndex)
{
currentScreen = screenIndex;
dotIndicator.setHighlightPosition(currentScreen);
adjustScreens();
}{ ... }
void SwipeContainer::handleTickEvent()
{
if (currentState == ANIMATE_SWIPE_CANCELLED_LEFT)
{
animateSwipeCancelledLeft();
}if (currentState == ANIMATE_SWIPE_CANCELLED_LEFT) { ... }
else if (currentState == ANIMATE_SWIPE_CANCELLED_RIGHT)
{
animateSwipeCancelledRight();
}else if (currentState == ANIMATE_SWIPE_CANCELLED_RIGHT) { ... }
else if (currentState == ANIMATE_LEFT)
{
animateLeft();
}else if (currentState == ANIMATE_LEFT) { ... }
else if (currentState == ANIMATE_RIGHT)
{
animateRight();
}else if (currentState == ANIMATE_RIGHT) { ... }
}{ ... }
void SwipeContainer::handleClickEvent(const ClickEvent& evt)
{
if (currentState != NO_ANIMATION)
{
return;
}if (currentState != NO_ANIMATION) { ... }
if (evt.getType() == ClickEvent::RELEASED)
{
animateDistance = dragX;
startX = screens.getX();
if (dragX < 0)
{
if (currentScreen == getNumberOfScreens() - 1 || dragX > -swipeCutoff)
{
currentState = ANIMATE_SWIPE_CANCELLED_LEFT;
}if (currentScreen == getNumberOfScreens() - 1 || dragX > -swipeCutoff) { ... }
else
{
currentState = ANIMATE_LEFT;
if (menuChangeInitiatedAction && menuChangeInitiatedAction->isValid())
{
menuChangeInitiatedAction->execute(*this);
}if (menuChangeInitiatedAction && menuChangeInitiatedAction->isValid()) { ... }
}else { ... }
}if (dragX < 0) { ... }
else if (dragX > 0)
{
if (currentScreen == 0 || dragX < swipeCutoff)
{
currentState = ANIMATE_SWIPE_CANCELLED_RIGHT;
}if (currentScreen == 0 || dragX < swipeCutoff) { ... }
else
{
currentState = ANIMATE_RIGHT;
if (menuChangeInitiatedAction && menuChangeInitiatedAction->isValid())
{
menuChangeInitiatedAction->execute(*this);
}if (menuChangeInitiatedAction && menuChangeInitiatedAction->isValid()) { ... }
}else { ... }
}else if (dragX > 0) { ... }
}if (evt.getType() == ClickEvent::RELEASED) { ... }
}{ ... }
void SwipeContainer::handleDragEvent(const DragEvent& evt)
{
if (currentState != NO_ANIMATION)
{
return;
}if (currentState != NO_ANIMATION) { ... }
dragX += evt.getDeltaX();
if (currentScreen == 0 && dragX > endElasticWidth)
{
dragX = endElasticWidth;
}if (currentScreen == 0 && dragX > endElasticWidth) { ... }
else if (currentScreen == getNumberOfScreens() - 1 && dragX < -endElasticWidth)
{
dragX = -endElasticWidth;
}else if (currentScreen == getNumberOfScreens() - 1 && dragX < -endElasticWidth) { ... }
adjustScreens();
}{ ... }
void SwipeContainer::handleGestureEvent(const GestureEvent& evt)
{
if (currentState != NO_ANIMATION)
{
return;
}if (currentState != NO_ANIMATION) { ... }
if (evt.getType() == evt.SWIPE_HORIZONTAL)
{
animateDistance = dragX;
startX = screens.getX();
if (evt.getVelocity() < 0 && currentScreen < getNumberOfScreens() - 1)
{
currentState = ANIMATE_LEFT;
if (menuChangeInitiatedAction && menuChangeInitiatedAction->isValid())
{
menuChangeInitiatedAction->execute(*this);
}if (menuChangeInitiatedAction && menuChangeInitiatedAction->isValid()) { ... }
}if (evt.getVelocity() < 0 && currentScreen < getNumberOfScreens() - 1) { ... }
else if (evt.getVelocity() > 0 && currentScreen > 0)
{
currentState = ANIMATE_RIGHT;
if (menuChangeInitiatedAction && menuChangeInitiatedAction->isValid())
{
menuChangeInitiatedAction->execute(*this);
}if (menuChangeInitiatedAction && menuChangeInitiatedAction->isValid()) { ... }
}else if (evt.getVelocity() > 0 && currentScreen > 0) { ... }
}if (evt.getType() == evt.SWIPE_HORIZONTAL) { ... }
}{ ... }
void SwipeContainer::adjustScreens()
{
screens.moveTo(-(currentScreen * getWidth()) + dragX, 0);
}{ ... }
void SwipeContainer::animateSwipeCancelledLeft()
{
uint8_t duration = 14;
if (animationCounter <= duration)
{
int16_t delta = EasingEquations::backEaseOut(animationCounter, 0, -animateDistance, duration);
dragX = animateDistance + delta;
adjustScreens();
}if (animationCounter <= duration) { ... }
else
{
currentState = NO_ANIMATION;
animationCounter = 0;
dragX = 0;
adjustScreens();
}else { ... }
animationCounter++;
}{ ... }
void SwipeContainer::animateSwipeCancelledRight()
{
uint8_t duration = 14;
if (animationCounter <= duration)
{
int16_t delta = EasingEquations::backEaseOut(animationCounter, 0, animateDistance, duration);
dragX = animateDistance - delta;
adjustScreens();
}if (animationCounter <= duration) { ... }
else
{
currentState = NO_ANIMATION;
animationCounter = 0;
dragX = 0;
adjustScreens();
}else { ... }
animationCounter++;
}{ ... }
void SwipeContainer::animateLeft()
{
uint8_t duration = 10;
if (animationCounter <= duration)
{
int16_t delta = EasingEquations::cubicEaseOut(animationCounter, 0, getWidth() + animateDistance, duration);
dragX = animateDistance - delta;
adjustScreens();
}if (animationCounter <= duration) { ... }
else
{
currentState = NO_ANIMATION;
animationCounter = 0;
currentScreen++;
dragX = 0;
adjustScreens();
dotIndicator.goRight();
if (menuChangeEndedAction && menuChangeEndedAction->isValid())
{
menuChangeEndedAction->execute(*this);
}if (menuChangeEndedAction && menuChangeEndedAction->isValid()) { ... }
}else { ... }
animationCounter++;
}{ ... }
void SwipeContainer::animateRight()
{
uint8_t duration = 10;
if (animationCounter <= duration)
{
int16_t delta = EasingEquations::cubicEaseOut(animationCounter, 0, getWidth() - animateDistance, duration);
dragX = animateDistance + delta;
adjustScreens();
}if (animationCounter <= duration) { ... }
else
{
currentState = NO_ANIMATION;
animationCounter = 0;
currentScreen--;
dragX = 0;
adjustScreens();
dotIndicator.goLeft();
if (menuChangeEndedAction && menuChangeEndedAction->isValid())
{
menuChangeEndedAction->execute(*this);
}if (menuChangeEndedAction && menuChangeEndedAction->isValid()) { ... }
}else { ... }
animationCounter++;
}{ ... }