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
42
43
47
48
49
50
51
52
53
54
55
56
57
58
59
60
64
65
69
70
74
75
79
80
84
85
91
92
111
112
113
114
115
116
120
121
122
123
124
125
126
127
138
149
150
151
152
153
154
155
156
160
161
162
163
164
168
172
173
174
175
176
177
178
179
183
184
185
186
187
188
189
190
194
198
199
200
201
205
206
207
208
209
210
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/* ... */
#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)
{
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;
}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;
}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 (evt.getVelocity() < 0 && currentScreen < getNumberOfScreens()-1) { ... }
else if (evt.getVelocity() > 0 && currentScreen > 0)
{
currentState = ANIMATE_RIGHT;
}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();
}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();
}else { ... }
animationCounter++;
}{ ... }