• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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_COMPONENTS_NG_MODIFIER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MODIFIER_H
18 
19 #include <atomic>
20 #include <cstdint>
21 #include <functional>
22 #include <optional>
23 #include <vector>
24 
25 #include "base/memory/ace_type.h"
26 #include "base/utils/utils.h"
27 #include "core/components_ng/animation/gradient_arithmetic.h"
28 #include "core/components_ng/base/linear_vector.h"
29 #include "core/components_ng/render/canvas_image.h"
30 #include "core/components_ng/render/drawing_forward.h"
31 #include "core/components_ng/render/modifier_adapter.h"
32 
33 enum class ThresholdType {
34     LAYOUT,  // 0.5f for properties like position, as the difference in properties by 0.5 appears visually unchanged
35     COARSE,  // 1.0f / 256.0f
36     MEDIUM,  // 1.0f / 1000.0f
37     FINE,    // 1.0f / 3072.0f
38     COLOR,   // 0.0f
39     DEFAULT, // 1.0f / 256.0f
40     ZERO,    // 0.0f for noanimatable property
41 };
42 
43 enum class PropertyUnit {
44     UNKNOWN,
45     PIXEL_POSITION, // animatable properties are related to position of the object, the unit is pixels
46 };
47 
48 namespace OHOS::Ace::NG {
49 class ACE_FORCE_EXPORT Modifier : public virtual AceType {
50     DECLARE_ACE_TYPE(Modifier, AceType);
51 
52 public:
53     Modifier();
~Modifier()54     ~Modifier() override
55     {
56         ModifierAdapter::RemoveModifier(id_);
57     }
58 
GetId()59     int32_t GetId() const
60     {
61         return id_;
62     }
63 
64 private:
65     int32_t id_ = 0;
66     ACE_DISALLOW_COPY_AND_MOVE(Modifier);
67 };
68 
69 class PropertyBase : public virtual AceType {
70     DECLARE_ACE_TYPE(PropertyBase, AceType);
71 
72 public:
73     PropertyBase() = default;
74     ~PropertyBase() override = default;
75 
76 private:
77     ACE_DISALLOW_COPY_AND_MOVE(PropertyBase);
78 };
79 
80 struct DrawingContext {
81     RSCanvas& canvas;
82     float width = 0;
83     float height = 0;
84 };
85 
86 template<typename T>
87 class NormalProperty : public PropertyBase {
88     DECLARE_ACE_TYPE(NormalProperty, PropertyBase);
89 
90 public:
NormalProperty(const T & value)91     explicit NormalProperty(const T& value) : value_(value) {}
92     ~NormalProperty() override = default;
93 
94     void SetUpCallbacks(std::function<T()>&& getFunc, std::function<void(const T&)>&& setFunc,
95         std::function<T()>&& getStageFunc = nullptr)
96     {
97         getFunc_ = std::move(getFunc);
98         setFunc_ =  std::move(setFunc);
99         getStageFunc_ =  std::move(getStageFunc);
100     }
101 
Get()102     T Get()
103     {
104         if (getFunc_) {
105             return getFunc_();
106         } else {
107             return value_;
108         }
109     }
110 
Set(const T & value)111     void Set(const T& value)
112     {
113         if (setFunc_) {
114             setFunc_(value);
115         } else {
116             value_ = value;
117         }
118     }
119 
GetStagingValue()120     T GetStagingValue() const
121     {
122         if (getStageFunc_) {
123             return getStageFunc_();
124         } else {
125             return value_;
126         }
127     }
128 
SetUpdateCallback(std::function<void (const T &)> && callback)129     void SetUpdateCallback(std::function<void(const T&)>&& callback)
130     {
131         updateCallback_ = std::move(callback);
132     }
133 
GetUpdateCallback()134     std::function<void(const T&)> GetUpdateCallback() const
135     {
136         return updateCallback_;
137     }
138 
139 private:
140     T value_;
141     std::function<T()> getFunc_;
142     std::function<void(const T&)> setFunc_;
143     std::function<T()> getStageFunc_;
144     std::function<void(const T&)> updateCallback_;
145     ACE_DISALLOW_COPY_AND_MOVE(NormalProperty);
146 };
147 
148 template<typename T>
149 class AnimatableProperty : public NormalProperty<T> {
150     DECLARE_ACE_TYPE(AnimatableProperty, NormalProperty<T>);
151 
152 public:
AnimatableProperty(const T & value)153     explicit AnimatableProperty(const T& value) : NormalProperty<T>(value) {}
154     ~AnimatableProperty() override = default;
155 private:
156     ACE_DISALLOW_COPY_AND_MOVE(AnimatableProperty);
157 };
158 
159 class ContentModifier : public Modifier {
160     DECLARE_ACE_TYPE(ContentModifier, Modifier);
161 
162 public:
163     ContentModifier() = default;
164     ~ContentModifier() override = default;
165     virtual void onDraw(DrawingContext& Context) = 0;
166 
AttachProperty(const RefPtr<PropertyBase> & prop)167     void AttachProperty(const RefPtr<PropertyBase>& prop)
168     {
169         attachedProperties_.push_back(prop);
170     }
171 
GetAttachedProperties()172     const std::vector<RefPtr<PropertyBase>>& GetAttachedProperties()
173     {
174         return attachedProperties_;
175     }
176 
GetBoundsRect()177     const std::optional<RectF>& GetBoundsRect()
178     {
179         return rect_;
180     }
181 
SetBoundsRect(const std::optional<RectF> & rect)182     void SetBoundsRect(const std::optional<RectF>& rect)
183     {
184         rect_ = rect;
185     }
186 
SetIsCustomFont(bool isCustomFont)187     void SetIsCustomFont(bool isCustomFont)
188     {
189         isCustomFont_ = isCustomFont;
190     }
191 
GetIsCustomFont()192     bool GetIsCustomFont()
193     {
194         return isCustomFont_;
195     }
196 
197 private:
198     std::vector<RefPtr<PropertyBase>> attachedProperties_;
199     std::optional<RectF> rect_;
200     bool isCustomFont_ = false;
201     ACE_DISALLOW_COPY_AND_MOVE(ContentModifier);
202 };
203 
204 class OverlayModifier : public Modifier {
205     DECLARE_ACE_TYPE(OverlayModifier, Modifier);
206 
207 public:
208     OverlayModifier() = default;
209     ~OverlayModifier() override = default;
210     virtual void onDraw(DrawingContext& Context) = 0;
211 
AttachProperty(const RefPtr<PropertyBase> & prop)212     void AttachProperty(const RefPtr<PropertyBase>& prop)
213     {
214         attachedProperties_.push_back(prop);
215     }
216 
GetAttachedProperties()217     const std::vector<RefPtr<PropertyBase>>& GetAttachedProperties()
218     {
219         return attachedProperties_;
220     }
221 
GetBoundsRect()222     const RectF& GetBoundsRect()
223     {
224         return rect_;
225     }
226 
SetBoundsRect(const RectF & rect)227     void SetBoundsRect(const RectF& rect)
228     {
229         rect_ = rect;
230     }
231 
232 private:
233     std::vector<RefPtr<PropertyBase>> attachedProperties_;
234     RectF rect_;
235     ACE_DISALLOW_COPY_AND_MOVE(OverlayModifier);
236 };
237 
238 #define DECLARE_PROP_TYPED_CLASS(classname, template_class, type)        \
239     class classname : public template_class<type> {                      \
240         DECLARE_ACE_TYPE(classname, template_class);                     \
241                                                                          \
242     public:                                                              \
243         explicit classname(const type& value) : template_class(value) {} \
244         ~classname() override = default;                                 \
245         ACE_DISALLOW_COPY_AND_MOVE(classname);                           \
246     };
247 
248 DECLARE_PROP_TYPED_CLASS(PropertyBool, NormalProperty, bool);
249 DECLARE_PROP_TYPED_CLASS(PropertySizeF, NormalProperty, SizeF);
250 DECLARE_PROP_TYPED_CLASS(PropertyOffsetF, NormalProperty, OffsetF);
251 DECLARE_PROP_TYPED_CLASS(PropertyInt, NormalProperty, int32_t);
252 DECLARE_PROP_TYPED_CLASS(PropertyFloat, NormalProperty, float);
253 DECLARE_PROP_TYPED_CLASS(PropertyString, NormalProperty, std::string);
254 DECLARE_PROP_TYPED_CLASS(PropertyColor, NormalProperty, Color);
255 DECLARE_PROP_TYPED_CLASS(PropertyRectF, NormalProperty, RectF);
256 DECLARE_PROP_TYPED_CLASS(AnimatablePropertyFloat, AnimatableProperty, float);
257 DECLARE_PROP_TYPED_CLASS(AnimatablePropertyUint8, AnimatableProperty, uint8_t);
258 DECLARE_PROP_TYPED_CLASS(AnimatablePropertyColor, AnimatableProperty, LinearColor);
259 DECLARE_PROP_TYPED_CLASS(AnimatablePropertyVectorFloat, AnimatableProperty, LinearVector<float>);
260 DECLARE_PROP_TYPED_CLASS(AnimatablePropertyVectorColor, AnimatableProperty, GradientArithmetic);
261 DECLARE_PROP_TYPED_CLASS(AnimatablePropertyOffsetF, AnimatableProperty, OffsetF);
262 DECLARE_PROP_TYPED_CLASS(AnimatablePropertySizeF, AnimatableProperty, SizeF);
263 DECLARE_PROP_TYPED_CLASS(AnimatableArithmeticProperty, AnimatableProperty, RefPtr<CustomAnimatableArithmetic>);
264 
265 class ModifierImpl {
266 };
267 
268 class NodeAnimatablePropertyBase : public AceType {
269     DECLARE_ACE_TYPE(NodeAnimatablePropertyBase, AceType);
270 
271 public:
272     NodeAnimatablePropertyBase() = default;
273     ~NodeAnimatablePropertyBase() override = default;
274 
GetModifyImpl()275     const std::shared_ptr<ModifierImpl>& GetModifyImpl() const
276     {
277         return modifyImpl_;
278     }
279 
SetModifyImpl(const std::shared_ptr<ModifierImpl> & impl)280     void SetModifyImpl(const std::shared_ptr<ModifierImpl>& impl)
281     {
282         modifyImpl_ = impl;
283     }
284 
GetProperty()285     const RefPtr<PropertyBase>& GetProperty() const
286     {
287         return property_;
288     }
289 
SetProperty(const RefPtr<PropertyBase> & property)290     void SetProperty(const RefPtr<PropertyBase>& property)
291     {
292         property_ = property;
293     }
294 
295 private:
296     std::shared_ptr<ModifierImpl> modifyImpl_;
297     RefPtr<PropertyBase> property_;
298 
299     ACE_DISALLOW_COPY_AND_MOVE(NodeAnimatablePropertyBase);
300 };
301 
302 using FinishCallback = std::function<void()>;
303 
304 template<typename T, typename S>
305 class NodeAnimatableProperty : public NodeAnimatablePropertyBase {
306     DECLARE_ACE_TYPE(NodeAnimatableProperty, NodeAnimatablePropertyBase);
307 
308 public:
NodeAnimatableProperty(const T & value,std::function<void (const T &)> && updateCallback)309     NodeAnimatableProperty(const T& value, std::function<void(const T&)>&& updateCallback)
310     {
311         auto property = AceType::MakeRefPtr<S>(value);
312         property->SetUpdateCallback(std::move(updateCallback));
313         SetProperty(property);
314     }
315     ~NodeAnimatableProperty() override = default;
316 
Set(const T & value)317     void Set(const T& value)
318     {
319         auto property = AceType::DynamicCast<S>(GetProperty());
320         if (property) {
321             property->Set(value);
322         }
323     }
324 
325     void SetThresholdType(ThresholdType type);
326 
327     void SetPropertyUnit(PropertyUnit unit);
328 
Get()329     T Get() const
330     {
331         auto property = AceType::DynamicCast<S>(GetProperty());
332         if (property) {
333             return property->Get();
334         }
335         return {};
336     }
337     void AnimateWithVelocity(const AnimationOption& option, T value, T velocity,
338         const FinishCallback& finishCallback);
339 private:
340     ACE_DISALLOW_COPY_AND_MOVE(NodeAnimatableProperty);
341 };
342 
343 using NodeAnimatablePropertyFloat = NodeAnimatableProperty<float, AnimatablePropertyFloat>;
344 using NodeAnimatableArithmeticProperty =
345     NodeAnimatableProperty<RefPtr<CustomAnimatableArithmetic>, AnimatableArithmeticProperty>;
346 } // namespace OHOS::Ace::NG
347 
348 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MODIFIER_H
349