1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_ACE_INTERFACES_INNER_API_ACE_KIT_INCLUDE_ANIMATION_ANIMATION_OPTION_H 17 #define FOUNDATION_ACE_INTERFACES_INNER_API_ACE_KIT_INCLUDE_ANIMATION_ANIMATION_OPTION_H 18 19 #include <functional> 20 #include <string> 21 #include <unordered_map> 22 23 #include "ui/animation/animation_constants.h" 24 #include "ui/animation/curve.h" 25 #include "ui/animation/frame_rate_range.h" 26 27 namespace OHOS::Ace { 28 29 inline const std::unordered_map<AnimationInterface, std::string> animationTypeToStrMap = { 30 { AnimationInterface::ANIMATION, "animation" }, 31 { AnimationInterface::ANIMATE_TO, "animateTo" }, 32 { AnimationInterface::ANIMATE_TO_IMMEDIATELY, "animateToImmediately" }, 33 { AnimationInterface::KEYFRAME_ANIMATE_TO, "keyframeAnimateTo" }, 34 { AnimationInterface::TRANSITION, "transition" }, 35 { AnimationInterface::SHARED_TRANSITION, "sharedTransition" }, 36 { AnimationInterface::PAGE_TRANSITION, "pageTransition" }, 37 { AnimationInterface::UNKNOWN, "unknown" }, 38 }; 39 40 class AnimationOption final { 41 public: 42 AnimationOption() = default; AnimationOption(const RefPtr<Curve> & curve,int32_t duration)43 AnimationOption(const RefPtr<Curve>& curve, int32_t duration) : duration_(duration), curve_(curve) {} 44 ~AnimationOption() = default; 45 SetDuration(int32_t duration)46 void SetDuration(int32_t duration) 47 { 48 duration_ = duration; 49 } 50 GetDuration()51 int32_t GetDuration() const 52 { 53 return duration_; 54 } 55 SetDelay(int32_t delay)56 void SetDelay(int32_t delay) 57 { 58 delay_ = delay; 59 } 60 GetDelay()61 int32_t GetDelay() const 62 { 63 return delay_; 64 } 65 SetCurve(const RefPtr<Curve> & curve)66 void SetCurve(const RefPtr<Curve>& curve) 67 { 68 if (!curve) { 69 return; 70 } 71 curve_ = curve; 72 } 73 SetIteration(int32_t iteration)74 void SetIteration(int32_t iteration) 75 { 76 if (iteration < 0 && iteration != ANIMATION_REPEAT_INFINITE) { 77 return; 78 } 79 iteration_ = iteration; 80 } 81 GetIteration()82 int32_t GetIteration() const 83 { 84 return iteration_; 85 } 86 SetTempo(float tempo)87 void SetTempo(float tempo) 88 { 89 if (tempo < 0.0f) { 90 return; 91 } 92 tempo_ = tempo; 93 } 94 GetTempo()95 float GetTempo() const 96 { 97 return tempo_; 98 } 99 SetAnimationDirection(AnimationDirection direction)100 void SetAnimationDirection(AnimationDirection direction) 101 { 102 direction_ = direction; 103 } 104 GetAnimationDirection()105 AnimationDirection GetAnimationDirection() const 106 { 107 return direction_; 108 } 109 GetCurve()110 const RefPtr<Curve>& GetCurve() const 111 { 112 return curve_; 113 } 114 SetFillMode(const FillMode & fillMode)115 void SetFillMode(const FillMode& fillMode) 116 { 117 fillMode_ = fillMode; 118 } 119 GetFillMode()120 FillMode GetFillMode() const 121 { 122 return fillMode_; 123 } 124 SetOnFinishEvent(const std::function<void ()> & onFinishEvent)125 void SetOnFinishEvent(const std::function<void()>& onFinishEvent) 126 { 127 onFinishEvent_ = onFinishEvent; 128 } 129 GetOnFinishEvent()130 const std::function<void()>& GetOnFinishEvent() const 131 { 132 return onFinishEvent_; 133 } 134 IsValid()135 bool IsValid() const 136 { 137 return (GetDuration() > 0 || GetAllowRunningAsynchronously()); 138 } 139 SetAllowRunningAsynchronously(bool runAsync)140 void SetAllowRunningAsynchronously(bool runAsync) 141 { 142 allowRunningAsynchronously_ = runAsync; 143 } 144 GetAllowRunningAsynchronously()145 bool GetAllowRunningAsynchronously() const 146 { 147 return allowRunningAsynchronously_; 148 } 149 SetFinishCallbackType(FinishCallbackType finishCallbackType)150 void SetFinishCallbackType(FinishCallbackType finishCallbackType) 151 { 152 finishCallbackType_ = finishCallbackType; 153 } 154 GetFinishCallbackType()155 FinishCallbackType GetFinishCallbackType() const 156 { 157 return finishCallbackType_; 158 } 159 SetFrameRateRange(const RefPtr<FrameRateRange> & rateRange)160 void SetFrameRateRange(const RefPtr<FrameRateRange>& rateRange) 161 { 162 rateRange_ = rateRange; 163 } 164 GetFrameRateRange()165 const RefPtr<FrameRateRange>& GetFrameRateRange() const 166 { 167 return rateRange_; 168 } 169 SetAnimationInterface(AnimationInterface animationInterface)170 void SetAnimationInterface(AnimationInterface animationInterface) 171 { 172 animationInterface_ = animationInterface; 173 } 174 GetAnimationInterface()175 AnimationInterface GetAnimationInterface() const 176 { 177 return animationInterface_; 178 } 179 GetAnimationInterfaceString()180 const std::string GetAnimationInterfaceString() const 181 { 182 auto it = animationTypeToStrMap.find(animationInterface_); 183 return it != animationTypeToStrMap.end() ? it->second : "unknown"; 184 } 185 ToString()186 std::string ToString() const 187 { 188 std::string result = std::string("duration:").append(std::to_string(duration_)) 189 .append(", curve:").append(curve_ ? curve_->ToString() : ""); 190 if (iteration_ != 1) { 191 result.append(", iteration:").append(std::to_string(iteration_)); 192 } 193 if (delay_) { 194 result.append(", delay:").append(std::to_string(delay_)); 195 } 196 if (!NearEqual(tempo_, 1.0f)) { 197 result.append(", tempo:").append(std::to_string(tempo_)); 198 } 199 if (direction_ != AnimationDirection::NORMAL) { 200 result.append(", playMode:").append(std::to_string(static_cast<int32_t>(direction_))); 201 } 202 return result; 203 } 204 205 private: 206 int32_t duration_ = 0; 207 int32_t delay_ = 0; 208 int32_t iteration_ = 1; 209 float tempo_ = 1.0f; 210 FillMode fillMode_ = FillMode::FORWARDS; 211 AnimationInterface animationInterface_ = AnimationInterface::UNKNOWN; 212 bool allowRunningAsynchronously_ = false; 213 RefPtr<Curve> curve_; 214 std::function<void()> onFinishEvent_; 215 AnimationDirection direction_ = AnimationDirection::NORMAL; 216 FinishCallbackType finishCallbackType_ = FinishCallbackType::REMOVED; 217 RefPtr<FrameRateRange> rateRange_; 218 }; 219 } // namespace OHOS::Ace 220 221 #endif // FOUNDATION_ACE_INTERFACES_INNER_API_ACE_KIT_INCLUDE_ANIMATION_ANIMATION_OPTION_H 222