diff options
author | emmaker <emmaker@myyahoo.com> | 2025-06-02 18:47:58 -0400 |
---|---|---|
committer | emmaker <emmaker@myyahoo.com> | 2025-06-02 18:47:58 -0400 |
commit | 844f76fd55eaa932b6654218d46d0dffdc6db413 (patch) | |
tree | a7895951f4687093952f7a30f0bbdc92818947a9 /source/base | |
parent | adf61fd0df78cc7a2f1d40cacfe6cfd745d37b40 (diff) | |
parent | e8fcbabb7a71134fcd58cc2251d7eb3aa443039f (diff) |
Merge remote-tracking branch 'origin/main'
Diffstat (limited to 'source/base')
-rw-r--r-- | source/base/StarAnimatedPartSet.cpp | 127 | ||||
-rw-r--r-- | source/base/StarAnimatedPartSet.hpp | 28 |
2 files changed, 11 insertions, 144 deletions
diff --git a/source/base/StarAnimatedPartSet.cpp b/source/base/StarAnimatedPartSet.cpp index 9b9f9d7..5ffc68f 100644 --- a/source/base/StarAnimatedPartSet.cpp +++ b/source/base/StarAnimatedPartSet.cpp @@ -1,12 +1,11 @@ #include "StarAnimatedPartSet.hpp" #include "StarMathCommon.hpp" -#include "StarJsonExtra.hpp" namespace Star { AnimatedPartSet::AnimatedPartSet() {} -AnimatedPartSet::AnimatedPartSet(Json config, uint8_t animatorVersion) { +AnimatedPartSet::AnimatedPartSet(Json config) { for (auto const& stateTypePair : config.get("stateTypes", JsonObject()).iterateObject()) { auto const& stateTypeName = stateTypePair.first; auto const& stateTypeConfig = stateTypePair.second; @@ -34,7 +33,6 @@ AnimatedPartSet::AnimatedPartSet(Json config, uint8_t animatorVersion) { newStateType.states.sortByKey(); newStateType.activeState.stateTypeName = stateTypeName; - newStateType.activeState.reverse = false; newStateType.activeStateDirty = true; if (newStateType.defaultState.empty() && !newStateType.states.empty()) @@ -60,23 +58,20 @@ AnimatedPartSet::AnimatedPartSet(Json config, uint8_t animatorVersion) { for (auto const& partStatePair : partStateTypePair.second.toObject()) { auto const& stateName = partStatePair.first; - auto stateConfig = partStatePair.second; - if ((version() > 0) && stateConfig.isType(Json::Type::String)) - stateConfig = partStatePair.second.get(stateConfig.toString()); + 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, false); + setActiveState(pair.first, pair.second.defaultState, true); } StringList AnimatedPartSet::stateTypes() const { @@ -115,12 +110,11 @@ StringList AnimatedPartSet::partNames() const { return m_parts.keys(); } -bool AnimatedPartSet::setActiveState(String const& stateTypeName, String const& stateName, bool alwaysStart, bool reverse) { +bool AnimatedPartSet::setActiveState(String const& stateTypeName, String const& stateName, bool alwaysStart) { auto& stateType = m_stateTypes.get(stateTypeName); - if (stateType.activeState.stateName != stateName || alwaysStart || stateType.activeState.reverse != reverse) { + if (stateType.activeState.stateName != stateName || alwaysStart) { stateType.activeState.stateName = stateName; stateType.activeState.timer = 0.0f; - stateType.activeState.frameProgress = 0.0f; stateType.activeStatePointer = stateType.states.get(stateName).get(); stateType.activeStateDirty = true; @@ -180,15 +174,11 @@ 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) { +bool AnimatedPartSet::setActiveStateIndex(String const& stateTypeName, size_t stateIndex, bool alwaysStart) { auto const& stateType = m_stateTypes.get(stateTypeName); String const& stateName = stateType.states.keyAt(stateIndex); - return setActiveState(stateTypeName, stateName, alwaysStart, reverse); + return setActiveState(stateTypeName, stateName, alwaysStart); } void AnimatedPartSet::update(float dt) { @@ -257,40 +247,15 @@ 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.frame = clamp<int>(activeState.timer / state.cycle * state.frames, 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; } @@ -327,56 +292,16 @@ 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; } @@ -385,40 +310,4 @@ 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; -} - } diff --git a/source/base/StarAnimatedPartSet.hpp b/source/base/StarAnimatedPartSet.hpp index 43755be..a608924 100644 --- a/source/base/StarAnimatedPartSet.hpp +++ b/source/base/StarAnimatedPartSet.hpp @@ -2,7 +2,6 @@ #include "StarOrderedMap.hpp" #include "StarJson.hpp" -#include "StarMatrix3.hpp" namespace Star { @@ -47,11 +46,7 @@ public: String stateName; float timer; unsigned frame; - float frameProgress; JsonObject properties; - bool reverse; - unsigned nextFrame; - JsonObject nextProperties; }; struct ActivePartInformation { @@ -59,18 +54,6 @@ public: // If a state match is found, this will be set. Maybe<ActiveStateInformation> activeState; JsonObject properties; - JsonObject nextProperties; - - Mat3F animationAffineTransform() const; - void setAnimationAffineTransform(Mat3F const& matrix); - void setAnimationAffineTransform(Mat3F const& mat1, Mat3F const& mat2, float progress); - - float xTranslationAnimation; - float yTranslationAnimation; - float xScaleAnimation; - float yScaleAnimation; - float xShearAnimation; - float yShearAnimation; }; enum AnimationMode { @@ -114,7 +97,7 @@ public: }; AnimatedPartSet(); - AnimatedPartSet(Json config, uint8_t animatiorVersion); + AnimatedPartSet(Json config); // Returns the available state types. StringList stateTypes() const; @@ -135,7 +118,7 @@ public: // beginning. If alwaysStart is true, then starts the state animation off at // the beginning even if no state change has occurred. Returns true if a // state animation reset was done. - bool setActiveState(String const& stateTypeName, String const& stateName, bool alwaysStart = false, bool reverse = false); + bool setActiveState(String const& stateTypeName, String const& stateName, bool alwaysStart = false); // Restart this given state type's timer off at the beginning. void restartState(String const& stateTypeName); @@ -158,8 +141,7 @@ public: // state type is ordered, it is possible to simply serialize and deserialize // the state index for that state type. size_t activeStateIndex(String const& stateTypeName) const; - bool activeStateReverse(String const& stateTypeName) const; - bool setActiveStateIndex(String const& stateTypeName, size_t stateIndex, bool alwaysStart = false, bool reverse = false); + bool setActiveStateIndex(String const& stateTypeName, size_t stateIndex, bool alwaysStart = false); // Animate each state type forward 'dt' time, and either change state frames // or transition to new states, depending on the config. @@ -168,8 +150,6 @@ public: // Pushes all the animations into their final state void finishAnimations(); - uint8_t version() const; - private: static AnimationMode stringToAnimationMode(String const& string); @@ -178,8 +158,6 @@ private: OrderedHashMap<String, StateType> m_stateTypes; StringMap<Part> m_parts; - - uint8_t m_animatorVersion; }; } |