-
Notifications
You must be signed in to change notification settings - Fork 22
/
Tools.inl
377 lines (306 loc) · 11.9 KB
/
Tools.inl
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include <vector>
#include <array>
/**
* @brief Components and Data generated by these tools
*
* Tools translate application events into Sequentity events.
* For example, a mouse click on a given entity results in
* the `Activated` component being assigned. This then causes
* the relevant portion of a tool to generate a new track, new
* channel and new event.
*
* During press-and-hold, this newly created event is then mutated
* with additional data, such as where the mouse is over time.
* Incrementing the length of the event to line up with the amount
* of data generated by the input.
*
*/
// An entity has just been made active
struct Activated { int time; };
// An entity is being interacted with, e.g. dragged with mouse
struct Active { int time; };
// An entity transitioned from active to deactive
struct Deactivated { int time; };
// Halt an ongoing iteration of enities with an `Active` component
struct Abort {};
enum class ToolType : std::uint8_t {
Select,
DragSelect,
LassoSelect,
Translate,
Rotate,
Scale,
Scrub,
};
struct Tool {
ToolType type;
std::function<void()> write;
std::function<void(entt::entity entity, const Sequentity::Event& event, const int time)> read;
};
// From e.g. Wacom tablet
struct InputPressure { float strength; };
struct InputPitch { float angle; };
struct InputYaw { float angle; };
// From e.g. Mouse or WASD keys
struct InputPosition2D {
Position absolute { 0, 0 };
Position relative { 0, 0 };
};
struct InputPosition3D {
Position absolute { 0, 0 };
Position relative { 0, 0 };
};
// From e.g. WASD keys or D-PAD on XBox controller
enum class InputDirection2D : std::uint8_t { Left = 0, Up, Right, Down };
enum class InputDirection3D : std::uint8_t { Left = 0, Up, Right, Down, Forward, Backward };
/**
* @brief Application data generated by these tools
*
* In addition to each Sequentity event, there is also our internal
* application data, carried by events via a void*
*
*/
struct TranslateEventData {
Position offset;
std::vector<Position> positions;
};
struct RotateEventData {
float offset;
std::vector<float> orientations;
};
struct ScaleEventData {
std::vector<float> scales;
};
struct ScrubEventData {
std::vector<int> deltas;
};
// Possible event types
enum EventType : Sequentity::EventType {
InvalidEvent = 0, // Catch uninitialised types
SelectEvent,
LassoSelectEvent,
DragSelectEvent,
TranslateEvent,
RotateEvent,
ScaleEvent,
ScrubEvent,
};
/**
* @brief The simplest possible tool
*
*
*/
static void SelectTool() {
Registry.view<Name, Deactivated>().each([](auto entity, const auto& name, const auto&) {
// Ensure there is only ever 1 selected entity
Registry.reset<Selected>();
Registry.assign<Selected>(entity);
});
}
/**
* @brief Translate an entity
*
* __________
* | |
* | | ----------->
* | |
* |__________|
*
*
*/
static void TranslateTool() {
// Handle press input of type: 2D range, relative anything with a position
Registry.view<Name, Activated, InputPosition2D, Color, Position>().each([](
auto entity,
const auto& name,
const auto& activated,
const auto& input,
const auto& color,
const auto& position) {
// The default name for any new track is coming from the owning entity
if (!Registry.has<Sequentity::Track>(entity)) {
Registry.assign<Sequentity::Track>(entity, name.text, color);
}
auto* data = new TranslateEventData{}; {
data->offset = input.absolute - position;
data->positions.emplace_back(input.absolute);
}
auto& track = Registry.get<Sequentity::Track>(entity);
bool has_channel = Sequentity::HasChannel(track, TranslateEvent);
auto& channel = Sequentity::PushChannel(track, TranslateEvent);
if (!has_channel) {
channel.label = "Translate";
channel.color = ImColor::HSV(0.0f, 0.75f, 0.75f);
}
Sequentity::PushEvent(channel, {
activated.time + 1, /* time= */
1, /* length= */
color, /* color= */
// Store reference to our data
TranslateEvent, /* type= */
static_cast<void*>(data) /* data= */
});
Registry.reset<Selected>();
Registry.assign<Selected>(entity);
});
Registry.view<Active, InputPosition2D, Sequentity::Track>(entt::exclude<Abort>).each([](
const auto&,
const auto& input,
auto& track) {
if (!track.channels.count(TranslateEvent)) {
Warning() << "TranslateTool on" << track.label << "didn't have a TranslateEvent";
return;
}
auto& channel = track.channels[TranslateEvent];
auto& event = channel.events.back();
auto data = static_cast<TranslateEventData*>(event.data);
data->positions.emplace_back(input.absolute);
event.length += 1;
});
Registry.view<Deactivated>().each([](auto entity, const auto&) {});
}
/**
* @brief Rotate an entity
* __
* __________ \
* | | v
* | |
* | |
* |__________|
* ^
* \___
*
*/
static void RotateTool() {
Registry.view<Name, Activated, InputPosition2D, Color, Orientation>().each([](
auto entity,
const auto& name,
const auto& activated,
const auto& input,
const auto& color,
const auto& orientation) {
auto* data = new RotateEventData{}; {
data->offset = orientation;
data->orientations.push_back(orientation);
}
if (!Registry.has<Sequentity::Track>(entity)) {
Registry.assign<Sequentity::Track>(entity, name.text, color);
}
auto& track = Registry.get<Sequentity::Track>(entity);
bool has_channel = track.channels.count(RotateEvent);
auto& channel = track.channels[RotateEvent];
if (!has_channel) {
channel.label = "Rotate";
channel.color = ImColor::HSV(0.33f, 0.75f, 0.75f);
}
Sequentity::PushEvent(channel, {
activated.time + 1,
1,
color,
RotateEvent,
static_cast<void*>(data)
});
Registry.reset<Selected>();
Registry.assign<Selected>(entity);
});
Registry.view<Name, Active, InputPosition2D, Sequentity::Track>(entt::exclude<Abort>).each([](const auto& name,
const auto&,
const auto& input,
auto& track) {
if (!track.channels.count(RotateEvent)) { Warning() << "RotateTool: This should never happen"; return; }
auto& channel = track.channels[RotateEvent];
auto& event = channel.events.back();
auto data = static_cast<RotateEventData*>(event.data);
data->orientations.push_back(static_cast<float>(data->offset + input.relative.x));
event.length += 1;
});
Registry.view<Deactivated>().each([](auto entity, const auto&) {});
}
/**
* @brief Scale an entity
*
* \ /
* \ __________ /
* | |
* | |
* | |
* |__________|
* / \
* / \
*
*/
static void ScaleTool() {
Registry.view<Name, Activated, InputPosition2D, Color, Size>().each([](
auto entity,
const auto& name,
const auto& activated,
const auto& input,
const auto& color,
const auto& size) {
auto* data = new ScaleEventData{}; {
data->scales.push_back(1.0f);
}
Sequentity::Event event; {
event.time = activated.time + 1;
event.length = 1;
event.color = color;
// Store reference to our data
event.type = ScaleEvent;
event.data = static_cast<void*>(data);
}
if (!Registry.has<Sequentity::Track>(entity)) {
Registry.assign<Sequentity::Track>(entity, name.text, color);
}
auto& track = Registry.get<Sequentity::Track>(entity);
bool has_channel = track.channels.count(ScaleEvent);
auto& channel = track.channels[ScaleEvent];
if (!has_channel) {
channel.label = "Scale";
channel.color = ImColor::HSV(0.52f, 0.75f, 0.50f);
}
Sequentity::PushEvent(channel, {
activated.time + 1,
1,
color,
ScaleEvent,
static_cast<void*>(data)
});
Registry.reset<Selected>();
Registry.assign<Selected>(entity);
});
Registry.view<Name, Active, InputPosition2D, Sequentity::Track>(entt::exclude<Abort>).each([](const auto& name,
const auto&,
const auto& input,
auto& track) {
if (!track.channels.count(ScaleEvent)) { Warning() << "ScaleTool: This should never happen"; return; }
auto& channel = track.channels[ScaleEvent];
auto& event = channel.events.back();
auto data = static_cast<ScaleEventData*>(event.data);
data->scales.push_back(1.0f + input.relative.x * 0.01f);
event.length += 1;
});
Registry.view<Deactivated>().each([](auto entity, const auto&) {});
}
/**
* @brief Relatively move the timeline
*
* This tool differs from the others, in that it doesn't actually apply to the
* active entity. Instead, it applies to the global state of Sequentity. But,
* it currently can't do that, unless an entity is active. So that's a bug.
*
*/
static void ScrubTool() {
// Press
static int previous_time { 0 };
Registry.view<Activated, InputPosition2D>().each([](const auto& activated, const auto& input) {
auto& state = Registry.ctx<Sequentity::State>();
previous_time = state.current_time;
});
// Hold
Registry.view<Active, InputPosition2D>().each([](const auto&, const auto& input) {
auto& state = Registry.ctx<Sequentity::State>();
state.current_time = previous_time + input.relative.x / 10;
});
// Release
Registry.view<Deactivated>().each([](auto entity, const auto&) {});
}