1 /* 2 * Copyright (c) 2022 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_INTERFACE_INNERKITS_ANIMATOR_OPTION_H 17 #define FOUNDATION_ACE_INTERFACE_INNERKITS_ANIMATOR_OPTION_H 18 19 #include <cstdint> 20 #include <memory> 21 #include <string> 22 23 #include "napi/native_api.h" 24 #include "napi/native_node_api.h" 25 26 #include "base/memory/referenced.h" 27 #include "core/animation/animator.h" 28 29 namespace OHOS::Ace::Napi { 30 31 struct AnimatorOption { 32 int32_t duration = 0; 33 int32_t delay = 0; 34 int32_t iterations = 0; 35 double begin = 0.0; 36 double end = 0.0; 37 std::string easing = "ease"; 38 std::string fill = "none"; 39 std::string direction = "normal"; 40 ToStringAnimatorOption41 std::string ToString() const 42 { 43 return "AnimatorOption:[" + std::to_string(duration) + "," + std::to_string(delay) + "," + 44 std::to_string(iterations) + "," + std::to_string(begin) + "," + std::to_string(end) + "," + easing + 45 "," + fill + "," + direction + "]"; 46 } 47 }; 48 49 class JsSimpleAnimatorOption { 50 public: 51 JsSimpleAnimatorOption() = default; 52 ~JsSimpleAnimatorOption() = default; 53 54 napi_value OnSetDuration(napi_env env, napi_callback_info info); 55 napi_value OnSetEasing(napi_env env, napi_callback_info info); 56 napi_value OnSetDelay(napi_env env, napi_callback_info info); 57 napi_value OnSetFill(napi_env env, napi_callback_info info); 58 napi_value OnSetDirection(napi_env env, napi_callback_info info); 59 napi_value OnSetIterations(napi_env env, napi_callback_info info); 60 GetBegin()61 double GetBegin() const 62 { 63 return begin_; 64 } 65 SetBegin(const double & begin)66 void SetBegin(const double& begin) 67 { 68 begin_ = begin; 69 } 70 GetEnd()71 double GetEnd() const 72 { 73 return end_; 74 } 75 SetEnd(const double & end)76 void SetEnd(const double& end) 77 { 78 end_ = end; 79 } 80 SetDuration(const std::optional<int32_t> & duration)81 void SetDuration(const std::optional<int32_t>& duration) 82 { 83 duration_ = duration; 84 } 85 GetDuration()86 std::optional<int32_t> GetDuration() const 87 { 88 return duration_; 89 } 90 SetEasing(const std::optional<std::string> & easing)91 void SetEasing(const std::optional<std::string>& easing) 92 { 93 easing_ = easing; 94 } 95 GetEasing()96 std::optional<std::string> GetEasing() const 97 { 98 return easing_; 99 } 100 SetDelay(const std::optional<int32_t> & delay)101 void SetDelay(const std::optional<int32_t>& delay) 102 { 103 delay_ = delay; 104 } 105 GetDelay()106 std::optional<int32_t> GetDelay() const 107 { 108 return delay_; 109 } 110 SetFill(const std::optional<FillMode> & fill)111 void SetFill(const std::optional<FillMode>& fill) 112 { 113 fill_ = fill; 114 } 115 GetFill()116 std::optional<FillMode> GetFill() const 117 { 118 return fill_; 119 } 120 SetDirection(const std::optional<AnimationDirection> & direction)121 void SetDirection(const std::optional<AnimationDirection>& direction) 122 { 123 direction_ = direction; 124 } 125 GetDirection()126 std::optional<AnimationDirection> GetDirection() const 127 { 128 return direction_; 129 } 130 SetIterations(const std::optional<int32_t> & iterations)131 void SetIterations(const std::optional<int32_t>& iterations) 132 { 133 iterations_ = iterations; 134 } 135 GetIterations()136 std::optional<int32_t> GetIterations() const 137 { 138 return iterations_; 139 } 140 141 private: 142 double begin_ = 0.0; 143 double end_ = 1.0; 144 std::optional<int32_t> duration_; 145 std::optional<int32_t> delay_; 146 std::optional<int32_t> iterations_; 147 std::optional<std::string> easing_; 148 std::optional<FillMode> fill_; 149 std::optional<AnimationDirection> direction_; 150 }; 151 152 class AnimatorResult final { 153 public: 154 AnimatorResult() = default; AnimatorResult(RefPtr<Animator> & animator,std::shared_ptr<AnimatorOption> & option)155 AnimatorResult(RefPtr<Animator>& animator, std::shared_ptr<AnimatorOption>& option) 156 : animator_(animator), option_(option) 157 { 158 ApplyOption(); 159 } 160 ~AnimatorResult() = default; 161 GetAnimator()162 RefPtr<Animator> GetAnimator() const 163 { 164 return animator_; 165 } 166 GetAnimatorOption()167 std::shared_ptr<AnimatorOption> GetAnimatorOption() const 168 { 169 return option_; 170 } 171 GetOnframeRef()172 napi_ref GetOnframeRef() const 173 { 174 return onframe_; 175 } 176 SetOnframeRef(const napi_ref & onframe)177 void SetOnframeRef(const napi_ref& onframe) 178 { 179 onframe_ = onframe; 180 } 181 GetOnfinishRef()182 napi_ref GetOnfinishRef() const 183 { 184 return onfinish_; 185 } 186 SetOnfinishRef(const napi_ref & onfinish)187 void SetOnfinishRef(const napi_ref& onfinish) 188 { 189 onfinish_ = onfinish; 190 } 191 GetOncancelRef()192 napi_ref GetOncancelRef() const 193 { 194 return oncancel_; 195 } 196 SetOncancelRef(const napi_ref & oncancel)197 void SetOncancelRef(const napi_ref& oncancel) 198 { 199 oncancel_ = oncancel; 200 } 201 GetOnrepeatRef()202 napi_ref GetOnrepeatRef() const 203 { 204 return onrepeat_; 205 } 206 SetOnrepeatRef(const napi_ref & onrepeat)207 void SetOnrepeatRef(const napi_ref& onrepeat) 208 { 209 onrepeat_ = onrepeat; 210 } 211 GetMotion()212 const RefPtr<Motion>& GetMotion() const 213 { 214 return motion_; 215 } 216 SetMotion(const RefPtr<Motion> & motion)217 void SetMotion(const RefPtr<Motion>& motion) 218 { 219 motion_ = motion; 220 } 221 222 void ApplyOption(); 223 224 void Destroy(napi_env env); 225 226 private: 227 RefPtr<Animator> animator_; 228 RefPtr<Motion> motion_; 229 std::shared_ptr<AnimatorOption> option_; 230 napi_ref onframe_ = nullptr; 231 napi_ref onfinish_ = nullptr; 232 napi_ref oncancel_ = nullptr; 233 napi_ref onrepeat_ = nullptr; 234 }; 235 236 } // namespace OHOS::Ace::Napi 237 238 #endif // #define FOUNDATION_ACE_INTERFACE_INNERKITS_ANIMATOR_RESULT_H 239