-
Notifications
You must be signed in to change notification settings - Fork 14
/
transition2d.h
182 lines (165 loc) · 5.22 KB
/
transition2d.h
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
/**
* @file transition2d.h
* @author Forairaaaaa
* @brief
* @version 0.1
* @date 2023-12-31
*
* @copyright Copyright (c) 2023
*
*/
#pragma once
#include "../easing_path/easing_path.h"
#include "../transition/transition.h"
#include "../types/types.h"
#include <functional>
namespace SmoothUIToolKit
{
/**
* @brief A container of 2 Transitions, provides dynamic transition methods
*
*/
class Transition2D
{
public:
struct Config_t
{
std::function<void(Transition2D*)> updateCallback = nullptr;
};
private:
struct Data_t
{
Transition x_transition;
Transition y_transition;
bool is_changed = true;
};
Data_t _data;
Config_t _config;
public:
Transition2D() = default;
Transition2D(const int& xStart, const int& yStart) { jumpTo(xStart, yStart); }
Transition2D(const Vector2D_t& pSatrt) { jumpTo(pSatrt); }
// Basic setter
inline void setDuration(TimeSize_t duration)
{
_data.x_transition.setDuration(duration);
_data.y_transition.setDuration(duration);
}
inline void setDelay(TimeSize_t delay)
{
_data.x_transition.setDelay(delay);
_data.y_transition.setDelay(delay);
}
inline void setTransitionPath(EasingPath_t transitionPath)
{
_data.x_transition.setTransitionPath(transitionPath);
_data.y_transition.setTransitionPath(transitionPath);
}
inline void setUpdateCallback(std::function<void(Transition2D*)> updateCallback)
{
_config.updateCallback = updateCallback;
}
// Basic getter
inline Transition& getXTransition() { return _data.x_transition; }
inline Transition& getYTransition() { return _data.y_transition; }
inline Vector2D_t getStartPoint()
{
return Vector2D_t(_data.x_transition.getStartValue(), _data.y_transition.getStartValue());
}
inline Vector2D_t getTargetPoint()
{
return Vector2D_t(_data.x_transition.getEndValue(), _data.y_transition.getEndValue());
}
inline std::function<void(Transition2D*)> getUpdateCallback() { return _config.updateCallback; }
// Helper setter
inline void setEachDuration(TimeSize_t x, TimeSize_t y)
{
_data.x_transition.setDuration(x);
_data.y_transition.setDuration(y);
}
inline void setEachDelay(TimeSize_t x, TimeSize_t y)
{
_data.x_transition.setDelay(x);
_data.y_transition.setDelay(y);
}
inline void setEachTransitionPath(EasingPath_t x, EasingPath_t y)
{
_data.x_transition.setTransitionPath(x);
_data.y_transition.setTransitionPath(y);
}
/**
* @brief Start moving
*
* @param currentTime
*/
void start(const TimeSize_t& currentTime);
/**
* @brief Pause moving, call start() to continue
*
* @param currentTime
*/
void pause(const TimeSize_t& currentTime);
/**
* @brief End moving to the target point
*
*/
void end();
/**
* @brief Reset moving to the start point
*
*/
void reset();
/**
* @brief Update moving
*
* @param currentTime
*/
void update(const TimeSize_t& currentTime);
/**
* @brief Jump to target point with no transition
*
* @param x
* @param y
*/
void jumpTo(const int& x, const int& y);
inline void jumpTo(const Vector2D_t& p) { jumpTo(p.x, p.y); }
/**
* @brief Move to target point smoothly
*
* @param x
* @param y
* @param currentTime
*/
void moveTo(const int& x, const int& y);
inline void moveTo(const Vector2D_t& p) { moveTo(p.x, p.y); }
/** Resize to target size with no transition
* @brief
*
* @param w
* @param h
*/
inline void resizeTo(const int& w, const int& h) { jumpTo(w, h); }
inline void resizeTo(const Vector2D_t& p) { jumpTo(p); }
/** Reshape to target size smoothly
* @brief
*
* @param w
* @param h
*/
inline void reshapeTo(const int& w, const int& h) { moveTo(w, h); }
inline void reshapeTo(const Vector2D_t& p) { moveTo(p); }
/**
* @brief Get current point
*
* @return Vector2D_t
*/
inline Vector2D_t getValue() { return Vector2D_t(_data.x_transition.getValue(), _data.y_transition.getValue()); }
/**
* @brief Is moving finish
*
* @return true
* @return false
*/
inline bool isFinish() { return _data.x_transition.isFinish() && _data.y_transition.isFinish(); }
};
} // namespace SmoothUIToolKit