diff options
Diffstat (limited to 'source/base/StarAnimatedPartSet.cpp')
-rw-r--r-- | source/base/StarAnimatedPartSet.cpp | 127 |
1 files changed, 119 insertions, 8 deletions
diff --git a/source/base/StarAnimatedPartSet.cpp b/source/base/StarAnimatedPartSet.cpp index 5ffc68f..9b9f9d7 100644 --- a/source/base/StarAnimatedPartSet.cpp +++ b/source/base/StarAnimatedPartSet.cpp @@ -1,11 +1,12 @@ #include "StarAnimatedPartSet.hpp" #include "StarMathCommon.hpp" +#include "StarJsonExtra.hpp" namespace Star { AnimatedPartSet::AnimatedPartSet() {} -AnimatedPartSet::AnimatedPartSet(Json config) { +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; @@ -33,6 +34,7 @@ AnimatedPartSet::AnimatedPartSet(Json config) { newStateType.states.sortByKey(); newStateType.activeState.stateTypeName = stateTypeName; + newStateType.activeState.reverse = false; newStateType.activeStateDirty = true; if (newStateType.defaultState.empty() && !newStateType.states.empty()) @@ -58,20 +60,23 @@ AnimatedPartSet::AnimatedPartSet(Json config) { for (auto const& partStatePair : partStateTypePair.second.toObject()) { auto const& stateName = partStatePair.first; - auto const& stateConfig = partStatePair.second; + auto stateConfig = partStatePair.second; + if ((version() > 0) && stateConfig.isType(Json::Type::String)) + stateConfig = partStatePair.second.get(stateConfig.toString()); 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); + setActiveState(pair.first, pair.second.defaultState, true, false); } StringList AnimatedPartSet::stateTypes() const { @@ -110,11 +115,12 @@ StringList AnimatedPartSet::partNames() const { return m_parts.keys(); } -bool AnimatedPartSet::setActiveState(String const& stateTypeName, String const& stateName, bool alwaysStart) { +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) { + 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; @@ -174,11 +180,15 @@ 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 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); + return setActiveState(stateTypeName, stateName, alwaysStart, reverse); } void AnimatedPartSet::update(float dt) { @@ -247,15 +257,40 @@ void AnimatedPartSet::freshenActiveState(StateType& stateType) { if (stateType.activeStateDirty) { auto const& state = *stateType.activeStatePointer; auto& activeState = stateType.activeState; - activeState.frame = clamp<int>(activeState.timer / state.cycle * state.frames, 0, state.frames - 1); + + 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; } @@ -292,16 +327,56 @@ void AnimatedPartSet::freshenActivePart(Part& part) { 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; } @@ -310,4 +385,40 @@ void AnimatedPartSet::freshenActivePart(Part& part) { } } +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; +} + } |