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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
#include "StarAnimatedPartSet.hpp"
#include "StarMathCommon.hpp"
#include "StarJsonExtra.hpp"
namespace Star {
AnimatedPartSet::AnimatedPartSet() {}
AnimatedPartSet::AnimatedPartSet(Json config, uint8_t animatorVersion) {
for (auto const& stateTypePair : config.get("stateTypes", JsonObject()).iterateObject()) {
auto const& stateTypeName = stateTypePair.first;
auto const& stateTypeConfig = stateTypePair.second;
StateType newStateType;
newStateType.priority = stateTypeConfig.getFloat("priority", 0.0f);
newStateType.enabled = stateTypeConfig.getBool("enabled", true);
newStateType.defaultState = stateTypeConfig.getString("default", "");
newStateType.stateTypeProperties = stateTypeConfig.getObject("properties", {});
for (auto const& statePair : stateTypeConfig.get("states", JsonObject()).iterateObject()) {
auto const& stateName = statePair.first;
auto const& stateConfig = statePair.second;
auto newState = make_shared<State>();
newState->frames = stateConfig.getInt("frames", 1);
newState->cycle = stateConfig.getFloat("cycle", 1.0f);
newState->animationMode = stringToAnimationMode(stateConfig.getString("mode", "end"));
newState->transitionState = stateConfig.getString("transition", "");
newState->stateProperties = stateConfig.getObject("properties", {});
newState->stateFrameProperties = stateConfig.getObject("frameProperties", {});
newStateType.states[stateName] = std::move(newState);
}
newStateType.states.sortByKey();
newStateType.activeState.stateTypeName = stateTypeName;
newStateType.activeState.reverse = false;
newStateType.activeStateDirty = true;
if (newStateType.defaultState.empty() && !newStateType.states.empty())
newStateType.defaultState = newStateType.states.firstKey();
m_stateTypes[stateTypeName] = std::move(newStateType);
}
// Sort state types by decreasing priority.
m_stateTypes.sort([](pair<String, StateType> const& a, pair<String, StateType> const& b) {
return b.second.priority < a.second.priority;
});
for (auto const& partPair : config.get("parts", JsonObject()).iterateObject()) {
auto const& partName = partPair.first;
auto const& partConfig = partPair.second;
Part newPart;
newPart.partProperties = partConfig.getObject("properties", {});
for (auto const& partStateTypePair : partConfig.get("partStates", JsonObject()).iterateObject()) {
auto const& stateTypeName = partStateTypePair.first;
for (auto const& partStatePair : partStateTypePair.second.toObject()) {
auto const& stateName = partStatePair.first;
auto const& stateConfig = partStatePair.second;
PartState partState = {stateConfig.getObject("properties", {}), stateConfig.getObject("frameProperties", {})};
newPart.partStates[stateTypeName][stateName] = std::move(partState);
}
}
newPart.activePart.partName = partPair.first;
newPart.activePart.setAnimationAffineTransform(Mat3F::identity());
newPart.activePartDirty = true;
m_parts[partName] = std::move(newPart);
}
for (auto const& pair : m_stateTypes)
setActiveState(pair.first, pair.second.defaultState, true);
}
StringList AnimatedPartSet::stateTypes() const {
return m_stateTypes.keys();
}
void AnimatedPartSet::setStateTypeEnabled(String const& stateTypeName, bool enabled) {
auto& stateType = m_stateTypes.get(stateTypeName);
if (stateType.enabled != enabled) {
stateType.enabled = enabled;
for (auto& pair : m_parts)
pair.second.activePartDirty = true;
}
}
void AnimatedPartSet::setEnabledStateTypes(StringList const& stateTypeNames) {
for (auto& pair : m_stateTypes)
pair.second.enabled = false;
for (auto const& stateTypeName : stateTypeNames)
m_stateTypes.get(stateTypeName).enabled = true;
for (auto& pair : m_parts)
pair.second.activePartDirty = true;
}
bool AnimatedPartSet::stateTypeEnabled(String const& stateTypeName) const {
return m_stateTypes.get(stateTypeName).enabled;
}
StringList AnimatedPartSet::states(String const& stateTypeName) const {
return m_stateTypes.get(stateTypeName).states.keys();
}
StringList AnimatedPartSet::partNames() const {
return m_parts.keys();
}
bool AnimatedPartSet::setActiveState(String const& stateTypeName, String const& stateName, bool alwaysStart, bool reverse) {
auto& stateType = m_stateTypes.get(stateTypeName);
if (stateType.activeState.stateName != stateName || alwaysStart || stateType.activeState.reverse != reverse) {
stateType.activeState.stateName = stateName;
stateType.activeState.timer = 0.0f;
stateType.activeState.frameProgress = 0.0f;
stateType.activeStatePointer = stateType.states.get(stateName).get();
stateType.activeStateDirty = true;
for (auto& pair : m_parts)
pair.second.activePartDirty = true;
return true;
} else {
return false;
}
}
void AnimatedPartSet::restartState(String const& stateTypeName) {
auto& stateType = m_stateTypes.get(stateTypeName);
stateType.activeState.timer = 0.0f;
stateType.activeStateDirty = true;
for (auto& pair : m_parts)
pair.second.activePartDirty = true;
}
AnimatedPartSet::ActiveStateInformation const& AnimatedPartSet::activeState(String const& stateTypeName) const {
auto& stateType = const_cast<StateType&>(m_stateTypes.get(stateTypeName));
const_cast<AnimatedPartSet*>(this)->freshenActiveState(stateType);
return stateType.activeState;
}
AnimatedPartSet::ActivePartInformation const& AnimatedPartSet::activePart(String const& partName) const {
auto& part = const_cast<Part&>(m_parts.get(partName));
const_cast<AnimatedPartSet*>(this)->freshenActivePart(part);
return part.activePart;
}
StringMap<AnimatedPartSet::Part> const& AnimatedPartSet::constParts() const {
return m_parts;
}
StringMap<AnimatedPartSet::Part>& AnimatedPartSet::parts() {
return m_parts;
}
void AnimatedPartSet::forEachActiveState(function<void(String const&, ActiveStateInformation const&)> callback) const {
for (auto const& p : m_stateTypes) {
const_cast<AnimatedPartSet*>(this)->freshenActiveState(const_cast<StateType&>(p.second));
callback(p.first, p.second.activeState);
}
}
void AnimatedPartSet::forEachActivePart(function<void(String const&, ActivePartInformation const&)> callback) const {
for (auto const& p : m_parts) {
const_cast<AnimatedPartSet*>(this)->freshenActivePart(const_cast<Part&>(p.second));
callback(p.first, p.second.activePart);
}
}
size_t AnimatedPartSet::activeStateIndex(String const& stateTypeName) const {
auto const& stateType = m_stateTypes.get(stateTypeName);
return *stateType.states.indexOf(stateType.activeState.stateName);
}
bool AnimatedPartSet::activeStateReverse(String const& stateTypeName) const {
auto const& stateType = m_stateTypes.get(stateTypeName);
return stateType.activeState.reverse;
}
bool AnimatedPartSet::setActiveStateIndex(String const& stateTypeName, size_t stateIndex, bool alwaysStart, bool reverse) {
auto const& stateType = m_stateTypes.get(stateTypeName);
String const& stateName = stateType.states.keyAt(stateIndex);
return setActiveState(stateTypeName, stateName, alwaysStart, reverse);
}
void AnimatedPartSet::update(float dt) {
for (auto& pair : m_stateTypes) {
auto& stateType = pair.second;
auto const& state = *stateType.activeStatePointer;
stateType.activeState.timer += dt;
if (stateType.activeState.timer > state.cycle) {
if (state.animationMode == End) {
stateType.activeState.timer = state.cycle;
} else if (state.animationMode == Loop) {
stateType.activeState.timer = std::fmod(stateType.activeState.timer, state.cycle);
} else if (state.animationMode == Transition) {
stateType.activeState.stateName = state.transitionState;
stateType.activeState.timer = 0.0f;
stateType.activeStatePointer = stateType.states.get(state.transitionState).get();
}
}
stateType.activeStateDirty = true;
}
for (auto& pair : m_parts)
pair.second.activePartDirty = true;
}
void AnimatedPartSet::finishAnimations() {
for (auto& pair : m_stateTypes) {
auto& stateType = pair.second;
while (true) {
auto const& state = *stateType.activeStatePointer;
if (state.animationMode == End) {
stateType.activeState.timer = state.cycle;
} else if (state.animationMode == Transition) {
stateType.activeState.stateName = state.transitionState;
stateType.activeState.timer = 0.0f;
stateType.activeStatePointer = stateType.states.get(state.transitionState).get();
continue;
}
break;
}
stateType.activeStateDirty = true;
}
for (auto& pair : m_parts)
pair.second.activePartDirty = true;
}
AnimatedPartSet::AnimationMode AnimatedPartSet::stringToAnimationMode(String const& string) {
if (string.equals("end", String::CaseInsensitive)) {
return End;
} else if (string.equals("loop", String::CaseInsensitive)) {
return Loop;
} else if (string.equals("transition", String::CaseInsensitive)) {
return Transition;
} else {
throw AnimatedPartSetException(strf("No such AnimationMode '{}'", string));
}
}
void AnimatedPartSet::freshenActiveState(StateType& stateType) {
if (stateType.activeStateDirty) {
auto const& state = *stateType.activeStatePointer;
auto& activeState = stateType.activeState;
double progress = (activeState.timer / state.cycle * state.frames);
activeState.frameProgress = std::fmod(progress, 1);
activeState.frame = clamp<int>(progress, 0, state.frames - 1);
if (activeState.reverse) {
activeState.frame = (state.frames - 1) - activeState.frame;
if (state.animationMode == Loop)
if (activeState.frame <= 0 ) {
activeState.nextFrame = state.frames - 1;
} else {
activeState.nextFrame = clamp<int>(activeState.frame - 1, 0, state.frames - 1);
}
} else {
if (state.animationMode == Loop)
if (activeState.frame >= (state.frames-1) ) {
activeState.nextFrame = 0;
} else {
activeState.nextFrame = clamp<int>(activeState.frame + 1, 0, state.frames - 1);
}
}
activeState.properties = stateType.stateTypeProperties;
activeState.properties.merge(state.stateProperties, true);
activeState.nextProperties = activeState.properties;
for (auto const& pair : state.stateFrameProperties) {
if (activeState.frame < pair.second.size())
activeState.properties[pair.first] = pair.second.get(activeState.frame);
}
for (auto const& pair : state.stateFrameProperties) {
if (activeState.nextFrame < pair.second.size())
activeState.nextProperties[pair.first] = pair.second.get(activeState.nextFrame);
}
stateType.activeStateDirty = false;
}
}
void AnimatedPartSet::freshenActivePart(Part& part) {
if (part.activePartDirty) {
// First reset all the active part information assuming that no state type
// x state match exists.
auto& activePart = part.activePart;
activePart.activeState = {};
activePart.properties = part.partProperties;
// Then go through each of the state types and states and look for a part
// state match in order of priority.
for (auto& stateTypePair : m_stateTypes) {
auto const& stateTypeName = stateTypePair.first;
auto& stateType = stateTypePair.second;
// Skip disabled state types
if (!stateType.enabled)
continue;
auto partStateType = part.partStates.ptr(stateTypeName);
if (!partStateType)
continue;
auto const& stateName = stateType.activeState.stateName;
auto partState = partStateType->ptr(stateName);
if (!partState)
continue;
// If we have a partState match, then set the active state information.
freshenActiveState(stateType);
activePart.activeState = stateType.activeState;
unsigned frame = stateType.activeState.frame;
unsigned nextFrame = stateType.activeState.nextFrame;
// Then set the part state data, as well as any part state frame data if
// the current frame is within the list size.
activePart.properties.merge(partState->partStateProperties, true);
activePart.nextProperties = activePart.properties;
for (auto const& pair : partState->partStateFrameProperties) {
if (frame < pair.second.size())
activePart.properties[pair.first] = pair.second.get(frame);
}
for (auto const& pair : partState->partStateFrameProperties) {
if (nextFrame< pair.second.size())
activePart.nextProperties[pair.first] = pair.second.get(nextFrame);
}
if (version() > 0) {
auto processTransforms = [](Mat3F mat, JsonArray transforms, JsonObject properties) -> Mat3F {
for (auto const& v : transforms) {
auto action = v.getString(0);
if (action == "reset") {
mat = Mat3F::identity();
} else if (action == "translate") {
mat.translate(jsonToVec2F(v.getArray(1)));
} else if (action == "rotate") {
mat.rotate(v.getFloat(1), jsonToVec2F(v.getArray(2, properties.maybe("rotationCenter").value(JsonArray({0,0})).toArray())));
} else if (action == "scale") {
mat.scale(jsonToVec2F(v.getArray(1)), jsonToVec2F(v.getArray(2, properties.maybe("scalingCenter").value(JsonArray({0,0})).toArray())));
} else if (action == "transform") {
mat = Mat3F(v.getFloat(1), v.getFloat(2), v.getFloat(3), v.getFloat(4), v.getFloat(5), v.getFloat(6), 0, 0, 1) * mat;
}
}
return mat;
};
if (auto transforms = activePart.properties.ptr("transforms")) {
auto mat = processTransforms(activePart.animationAffineTransform(), transforms->toArray(), activePart.properties);
if (activePart.properties.maybe("interpolated").value(false)) {
if (auto nextTransforms = activePart.nextProperties.ptr("transforms")) {
auto nextMat = processTransforms(activePart.animationAffineTransform(), nextTransforms->toArray(), activePart.nextProperties);
activePart.setAnimationAffineTransform(mat, nextMat, stateType.activeState.frameProgress);
} else {
activePart.setAnimationAffineTransform(mat);
}
} else {
activePart.setAnimationAffineTransform(mat);
}
}
}
// Each part can only have one state type x state match, so we are done.
break;
}
part.activePartDirty = false;
}
}
void AnimatedPartSet::ActivePartInformation::setAnimationAffineTransform(Mat3F const& matrix) {
xTranslationAnimation = matrix[0][2];
yTranslationAnimation = matrix[1][2];
xScaleAnimation = sqrt(square(matrix[0][0]) + square(matrix[0][1]));
yScaleAnimation = sqrt(square(matrix[1][0]) + square(matrix[1][1]));
xShearAnimation = atan2(matrix[0][1], matrix[0][0]);
yShearAnimation = atan2(matrix[1][0], matrix[1][1]);
}
void AnimatedPartSet::ActivePartInformation::setAnimationAffineTransform(Mat3F const& mat1, Mat3F const& mat2, float progress) {
xTranslationAnimation = mat1[0][2];
yTranslationAnimation = mat1[1][2];
xScaleAnimation = sqrt(square(mat1[0][0]) + square(mat1[0][1]));
yScaleAnimation = sqrt(square(mat1[1][0]) + square(mat1[1][1]));
xShearAnimation = atan2(mat1[0][1], mat1[0][0]);
yShearAnimation = atan2(mat1[1][0], mat1[1][1]);
xTranslationAnimation += (mat2[0][2] - xTranslationAnimation) * progress;
yTranslationAnimation += (mat2[1][2] - yTranslationAnimation) * progress;
xScaleAnimation += (sqrt(square(mat2[0][0]) + square(mat2[0][1])) - xScaleAnimation) * progress;
yScaleAnimation += (sqrt(square(mat2[1][0]) + square(mat2[1][1])) - yScaleAnimation) * progress;
xShearAnimation += (atan2(mat2[0][1], mat2[0][0]) - xShearAnimation) * progress;
yShearAnimation += (atan2(mat2[1][0], mat2[1][1]) - yShearAnimation) * progress;
}
Mat3F AnimatedPartSet::ActivePartInformation::animationAffineTransform() const {
return Mat3F(
xScaleAnimation * cos(xShearAnimation), xScaleAnimation * sin(xShearAnimation), xTranslationAnimation,
yScaleAnimation * sin(yShearAnimation), yScaleAnimation * cos(yShearAnimation), yTranslationAnimation,
0, 0, 1
);
}
uint8_t AnimatedPartSet::version() const {
return m_animatorVersion;
}
}
|