1 /* 2 * Copyright (c) 2023 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_FRAMEWORKS_CORE_ANIMATION_CHAIN_ANIMATION_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_CHAIN_ANIMATION_H 18 19 #include <functional> 20 #include <map> 21 #include <utility> 22 23 #include "base/memory/ace_type.h" 24 #include "base/memory/referenced.h" 25 #include "core/animation/scheduler.h" 26 #include "core/animation/spring_motion.h" 27 28 namespace OHOS::Ace { 29 enum class ChainEdgeEffect { 30 DEFAULT, 31 STRETCH, 32 }; 33 34 class ChainAnimationNode : public AceType { 35 DECLARE_ACE_TYPE(ChainAnimationNode, AceType); 36 37 public: 38 ChainAnimationNode( 39 int32_t index, float space, float maxSpace, float minSpace, RefPtr<SpringProperty> springProperty); 40 void SetDelta(float delta, float duration); 41 float GetDelta() const; 42 bool TickAnimation(float duration); SetIndex(int32_t index)43 void SetIndex(int32_t index) 44 { 45 index_ = index; 46 } SetSpace(float space,float maxSpace,float minSpace)47 void SetSpace(float space, float maxSpace, float minSpace) 48 { 49 space_ = space; 50 maxSpace_ = maxSpace; 51 minSpace_ = minSpace; 52 } 53 54 private: 55 RefPtr<SpringMotion> spring_; 56 RefPtr<SpringProperty> springProperty_; 57 int32_t index_; 58 float space_; 59 float maxSpace_; 60 float minSpace_; 61 float curPosition_ = 0.0f; 62 float curVelocity_ = 0.0f; 63 }; 64 65 class ChainAnimation : public AceType { 66 DECLARE_ACE_TYPE(ChainAnimation, AceType); 67 68 public: 69 ChainAnimation(float space, float maxSpace, float minSpace, RefPtr<SpringProperty> springProperty); 70 void SetDelta(float delta, bool isOverDrag); 71 float GetValue(int32_t index); 72 float SetControlIndex(int32_t index); GetControlIndex()73 int32_t GetControlIndex() 74 { 75 return controlIndex_; 76 } SetMaxIndex(int32_t index)77 void SetMaxIndex(int32_t index) 78 { 79 maxIndex_ = index; 80 } SetAnimationCallback(std::function<void ()> callback)81 void SetAnimationCallback(std::function<void()> callback) 82 { 83 animationCallback_ = std::move(callback); 84 } SetConductivity(float value)85 void SetConductivity(float value) 86 { 87 conductivity_ = value; 88 } SetIntensity(float value)89 void SetIntensity(float value) 90 { 91 intensity_ = value; 92 } SetEdgeEffectIntensity(float value)93 void SetEdgeEffectIntensity(float value) 94 { 95 edgeEffectIntensity_ = value; 96 } SetEdgeEffect(ChainEdgeEffect edgeEffect)97 void SetEdgeEffect(ChainEdgeEffect edgeEffect) 98 { 99 edgeEffect_ = edgeEffect; 100 } 101 void SetSpace(float space, float maxSpace, float minSpace); 102 void SetOverDrag(bool isOverDrag); 103 104 static constexpr float DEFAULT_CONDUCTIVITY = 0.7f; 105 static constexpr float DEFAULT_INTENSITY = 0.3f; 106 static constexpr float DEFAULT_EDGE_EFFECT_INTENSITY = 0.3f; 107 108 private: 109 void TickAnimation(); 110 111 std::function<void()> animationCallback_; 112 std::map<int32_t, RefPtr<ChainAnimationNode>> nodes_; 113 RefPtr<SpringProperty> springProperty_; 114 RefPtr<Scheduler> scheduler_; 115 uint64_t timestamp_ = 0; 116 float space_; 117 float maxSpace_; 118 float minSpace_; 119 int32_t controlIndex_ = 0; 120 int32_t maxIndex_ = 0; 121 float conductivity_ = DEFAULT_CONDUCTIVITY; 122 float intensity_ = DEFAULT_INTENSITY; 123 float edgeEffectIntensity_ = DEFAULT_EDGE_EFFECT_INTENSITY; 124 bool isOverDrag_ = false; 125 ChainEdgeEffect edgeEffect_ = ChainEdgeEffect::DEFAULT; 126 }; 127 } // namespace OHOS::Ace 128 #endif