-
Notifications
You must be signed in to change notification settings - Fork 8
/
smooth_selector.h
227 lines (202 loc) · 7.5 KB
/
smooth_selector.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
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
/**
* @file smooth_selector.h
* @author Forairaaaaa
* @brief
* @version 0.1
* @date 2024-01-08
*
* @copyright Copyright (c) 2024
*
*/
#pragma once
#include "../../core/transition2d/transition2d.h"
#include "../base/select_menu_base.h"
#include <cstdint>
#include <vector>
namespace SmoothUIToolKit
{
namespace SelectMenu
{
/**
* @brief Menu with a smooth selector.
* Selector moves to match the option keyframes.
* The option matched by the selector at that time, treated as the selected one.
* Camera provides smooth offset to keep selector inside.
* Handy for list menu with a moving selector,
* or moving options with simple postition and shape transition. (Use selector as coordinate origin)
*/
class SmoothSelector : public SelectMenuBase
{
public:
/**
* @brief Option properties
*
*/
struct OptionProps_t
{
Vector4D_t keyframe;
void* userData = nullptr;
};
struct Config_t
{
// Selector move in loop
bool moveInLoop = true;
// Camera size
Vector2D_t cameraSize;
// Interval of onReadInput() callback
TimeSize_t readInputInterval = 20;
// Interval of onRender() callback
TimeSize_t renderInterval = 15;
};
protected:
struct Data_t
{
std::vector<OptionProps_t> option_list;
Transition2D selector_postion;
Transition2D selector_shape;
Transition2D camera_offset;
int selected_option_index = 0;
TimeSize_t read_input_time_count = 0;
TimeSize_t render_time_count = 0;
bool is_changed = true;
bool is_pressing = false;
bool was_released = false;
bool is_opening = false;
bool was_opened = false;
};
Data_t _data;
Config_t _config;
virtual void _update_selector_keyframe();
virtual void _update_camera_keyframe();
public:
virtual ~SmoothSelector() {}
// Configs
inline const Config_t& getConfig() { return _config; }
inline void setConfig(Config_t cfg) { _config = cfg; }
inline Config_t& setConfig() { return _config; }
// Options
inline void addOption(OptionProps_t optionProps) { _data.option_list.push_back(optionProps); }
inline const std::vector<OptionProps_t>& getOptionList() { return _data.option_list; }
inline int getSelectedOptionIndex() { return _data.selected_option_index; }
inline const OptionProps_t& getSelectedOption() { return _data.option_list[_data.selected_option_index]; }
inline const Vector4D_t& getSelectedKeyframe() { return getSelectedOption().keyframe; }
// Selector
inline Transition2D& getSelectorPostion() { return _data.selector_postion; }
inline Transition2D& getSelectorShape() { return _data.selector_shape; }
inline Vector4D_t getSelectorCurrentFrame()
{
return {getSelectorPostion().getValue().x,
getSelectorPostion().getValue().y,
getSelectorShape().getValue().width,
getSelectorShape().getValue().height};
}
// Camera
inline Transition2D& getCamera() { return _data.camera_offset; }
inline Vector2D_t getCameraOffset() { return _data.camera_offset.getValue(); }
inline void setCameraSize(int width, int height)
{
_config.cameraSize.width = width;
_config.cameraSize.height = height;
}
inline const Vector2D_t& getCameraSize() { return _config.cameraSize; }
/**
* @brief Set selector's position transition duration
*
* @param duration
*/
void setPositionDuration(TimeSize_t duration) { _data.selector_postion.setDuration(duration); }
/**
* @brief Set selector's shape transition duration
*
* @param duration
*/
void setShapeDuration(TimeSize_t duration) { _data.selector_shape.setDuration(duration); }
/**
* @brief Set selector's postion transition path
*
* @param path
*/
void setPositionTransitionPath(EasingPath_t path) { _data.selector_postion.setTransitionPath(path); }
/**
* @brief Set selector's shape transition path
*
* @param path
*/
void setShapeTransitionPath(EasingPath_t path) { _data.selector_shape.setTransitionPath(path); }
inline void setDuration(TimeSize_t duration)
{
setPositionDuration(duration);
setShapeDuration(duration);
}
inline void setTransitionPath(EasingPath_t path)
{
setPositionTransitionPath(path);
setShapeTransitionPath(path);
}
/**
* @brief Select last one
*
*/
virtual void goLast() override;
/**
* @brief Select next one
*
*/
virtual void goNext() override;
/**
* @brief Move to option smoothly
*
* @param optionIndex
*/
virtual void moveTo(int optionIndex);
/**
* @brief Jump to the option with on transition
*
* @param optionIndex
*/
virtual void jumpTo(int optionIndex);
/**
* @brief Press selector to the passing key frame
*
* @param pressedKeyframe
*/
virtual void press(const Vector4D_t& pressedKeyframe);
/**
* @brief Is selector being pressed
*
* @return true
* @return false
*/
inline bool isPressing() { return _data.is_pressing; }
/**
* @brief Release selector
*
*/
virtual void release();
/**
* @brief Open selector to the passing key frame
*
* @param pressedKeyframe
*/
virtual void open(const Vector4D_t& pressedKeyframe);
/**
* @brief Is selector opening
*
* @return true
* @return false
*/
inline const bool& isOpening() { return _data.is_opening; }
/**
* @brief Close selector
*
*/
virtual void close();
/**
* @brief Update menu
*
* @param currentTime
*/
virtual void update(const TimeSize_t& currentTime) override;
};
} // namespace SelectMenu
} // namespace SmoothUIToolKit