• 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 namespace OHOS::Ace::NG {
34 class ACE_FORCE_EXPORT Modifier : public virtual AceType {
35     DECLARE_ACE_TYPE(Modifier, AceType);
36 
37 public:
38     Modifier();
~Modifier()39     ~Modifier() override
40     {
41         ModifierAdapter::RemoveModifier(id_);
42     }
43 
GetId()44     int32_t GetId() const
45     {
46         return id_;
47     }
48 
49 private:
50     int32_t id_ = 0;
51     ACE_DISALLOW_COPY_AND_MOVE(Modifier);
52 };
53 
54 class PropertyBase : public virtual AceType {
55     DECLARE_ACE_TYPE(PropertyBase, AceType);
56 
57 public:
58     PropertyBase() = default;
59     ~PropertyBase() override = default;
60 
61 private:
62     ACE_DISALLOW_COPY_AND_MOVE(PropertyBase);
63 };
64 
65 struct DrawingContext {
66     RSCanvas& canvas;
67     float width = 0;
68     float height = 0;
69 };
70 
71 template<typename T>
72 class NormalProperty : public PropertyBase {
73     DECLARE_ACE_TYPE(NormalProperty, PropertyBase);
74 
75 public:
NormalProperty(const T & value)76     explicit NormalProperty(const T& value) : value_(value) {}
77     ~NormalProperty() override = default;
78 
79     void SetUpCallbacks(std::function<T()>&& getFunc, std::function<void(const T&)>&& setFunc,
80         std::function<T()>&& getStageFunc = nullptr)
81     {
82         getFunc_ = std::move(getFunc);
83         setFunc_ =  std::move(setFunc);
84         getStageFunc_ =  std::move(getStageFunc);
85     }
86 
Get()87     T Get()
88     {
89         if (getFunc_) {
90             return getFunc_();
91         } else {
92             return value_;
93         }
94     }
95 
Set(const T & value)96     void Set(const T& value)
97     {
98         if (setFunc_) {
99             setFunc_(value);
100         } else {
101             value_ = value;
102         }
103     }
104 
GetStagingValue()105     T GetStagingValue() const
106     {
107         if (getStageFunc_) {
108             return getStageFunc_();
109         } else {
110             return value_;
111         }
112     }
113 
SetUpdateCallback(std::function<void (const T &)> && callback)114     void SetUpdateCallback(std::function<void(const T&)>&& callback)
115     {
116         updateCallback_ = std::move(callback);
117     }
118 
GetUpdateCallback()119     std::function<void(const T&)> GetUpdateCallback() const
120     {
121         return updateCallback_;
122     }
123 
124 private:
125     T value_;
126     std::function<T()> getFunc_;
127     std::function<void(const T&)> setFunc_;
128     std::function<T()> getStageFunc_;
129     std::function<void(const T&)> updateCallback_;
130     ACE_DISALLOW_COPY_AND_MOVE(NormalProperty);
131 };
132 
133 template<typename T>
134 class AnimatableProperty : public NormalProperty<T> {
135     DECLARE_ACE_TYPE(AnimatableProperty, NormalProperty<T>);
136 
137 public:
AnimatableProperty(const T & value)138     explicit AnimatableProperty(const T& value) : NormalProperty<T>(value) {}
139     ~AnimatableProperty() override = default;
140 private:
141     ACE_DISALLOW_COPY_AND_MOVE(AnimatableProperty);
142 };
143 
144 class ContentModifier : public Modifier {
145     DECLARE_ACE_TYPE(ContentModifier, Modifier);
146 
147 public:
148     ContentModifier() = default;
149     ~ContentModifier() override = default;
150     virtual void onDraw(DrawingContext& Context) = 0;
151 
AttachProperty(const RefPtr<PropertyBase> & prop)152     void AttachProperty(const RefPtr<PropertyBase>& prop)
153     {
154         attachedProperties_.push_back(prop);
155     }
156 
GetAttachedProperties()157     const std::vector<RefPtr<PropertyBase>>& GetAttachedProperties()
158     {
159         return attachedProperties_;
160     }
161 
GetBoundsRect()162     const std::optional<RectF>& GetBoundsRect()
163     {
164         return rect_;
165     }
166 
SetBoundsRect(const std::optional<RectF> & rect)167     void SetBoundsRect(const std::optional<RectF>& rect)
168     {
169         rect_ = rect;
170     }
171 
SetIsCustomFont(bool isCustomFont)172     void SetIsCustomFont(bool isCustomFont)
173     {
174         isCustomFont_ = isCustomFont;
175     }
176 
GetIsCustomFont()177     bool GetIsCustomFont()
178     {
179         return isCustomFont_;
180     }
181 
182 private:
183     std::vector<RefPtr<PropertyBase>> attachedProperties_;
184     std::optional<RectF> rect_;
185     bool isCustomFont_ = false;
186     ACE_DISALLOW_COPY_AND_MOVE(ContentModifier);
187 };
188 
189 class OverlayModifier : public Modifier {
190     DECLARE_ACE_TYPE(OverlayModifier, Modifier);
191 
192 public:
193     OverlayModifier() = default;
194     ~OverlayModifier() override = default;
195     virtual void onDraw(DrawingContext& Context) = 0;
196 
AttachProperty(const RefPtr<PropertyBase> & prop)197     void AttachProperty(const RefPtr<PropertyBase>& prop)
198     {
199         attachedProperties_.push_back(prop);
200     }
201 
GetAttachedProperties()202     const std::vector<RefPtr<PropertyBase>>& GetAttachedProperties()
203     {
204         return attachedProperties_;
205     }
206 
GetBoundsRect()207     const RectF& GetBoundsRect()
208     {
209         return rect_;
210     }
211 
SetBoundsRect(const RectF & rect)212     void SetBoundsRect(const RectF& rect)
213     {
214         rect_ = rect;
215     }
216 
217 private:
218     std::vector<RefPtr<PropertyBase>> attachedProperties_;
219     RectF rect_;
220     ACE_DISALLOW_COPY_AND_MOVE(OverlayModifier);
221 };
222 
223 #define DECLARE_PROP_TYPED_CLASS(classname, template_class, type)        \
224     class classname : public template_class<type> {                      \
225         DECLARE_ACE_TYPE(classname, template_class);                     \
226                                                                          \
227     public:                                                              \
228         explicit classname(const type& value) : template_class(value) {} \
229         ~classname() override = default;                                 \
230         ACE_DISALLOW_COPY_AND_MOVE(classname);                           \
231     };
232 
233 DECLARE_PROP_TYPED_CLASS(PropertyBool, NormalProperty, bool);
234 DECLARE_PROP_TYPED_CLASS(PropertySizeF, NormalProperty, SizeF);
235 DECLARE_PROP_TYPED_CLASS(PropertyOffsetF, NormalProperty, OffsetF);
236 DECLARE_PROP_TYPED_CLASS(PropertyInt, NormalProperty, int32_t);
237 DECLARE_PROP_TYPED_CLASS(PropertyFloat, NormalProperty, float);
238 DECLARE_PROP_TYPED_CLASS(PropertyString, NormalProperty, std::string);
239 DECLARE_PROP_TYPED_CLASS(PropertyColor, NormalProperty, Color);
240 DECLARE_PROP_TYPED_CLASS(PropertyRectF, NormalProperty, RectF);
241 DECLARE_PROP_TYPED_CLASS(AnimatablePropertyFloat, AnimatableProperty, float);
242 DECLARE_PROP_TYPED_CLASS(AnimatablePropertyUint8, AnimatableProperty, uint8_t);
243 DECLARE_PROP_TYPED_CLASS(AnimatablePropertyColor, AnimatableProperty, LinearColor);
244 DECLARE_PROP_TYPED_CLASS(AnimatablePropertyVectorFloat, AnimatableProperty, LinearVector<float>);
245 DECLARE_PROP_TYPED_CLASS(AnimatablePropertyVectorColor, AnimatableProperty, GradientArithmetic);
246 DECLARE_PROP_TYPED_CLASS(AnimatablePropertyOffsetF, AnimatableProperty, OffsetF);
247 DECLARE_PROP_TYPED_CLASS(AnimatablePropertySizeF, AnimatableProperty, SizeF);
248 DECLARE_PROP_TYPED_CLASS(AnimatableArithmeticProperty, AnimatableProperty, RefPtr<CustomAnimatableArithmetic>);
249 
250 class ModifierImpl {
251 };
252 
253 class NodeAnimatablePropertyBase : public AceType {
254     DECLARE_ACE_TYPE(NodeAnimatablePropertyBase, AceType);
255 
256 public:
257     NodeAnimatablePropertyBase() = default;
258     ~NodeAnimatablePropertyBase() override = default;
259 
GetModifyImpl()260     const std::shared_ptr<ModifierImpl>& GetModifyImpl() const
261     {
262         return modifyImpl_;
263     }
264 
SetModifyImpl(const std::shared_ptr<ModifierImpl> & impl)265     void SetModifyImpl(const std::shared_ptr<ModifierImpl>& impl)
266     {
267         modifyImpl_ = impl;
268     }
269 
GetProperty()270     const RefPtr<PropertyBase>& GetProperty() const
271     {
272         return property_;
273     }
274 
SetProperty(const RefPtr<PropertyBase> & property)275     void SetProperty(const RefPtr<PropertyBase>& property)
276     {
277         property_ = property;
278     }
279 
280 private:
281     std::shared_ptr<ModifierImpl> modifyImpl_;
282     RefPtr<PropertyBase> property_;
283 
284     ACE_DISALLOW_COPY_AND_MOVE(NodeAnimatablePropertyBase);
285 };
286 
287 template<typename T, typename S>
288 class NodeAnimatableProperty : public NodeAnimatablePropertyBase {
289     DECLARE_ACE_TYPE(NodeAnimatableProperty, NodeAnimatablePropertyBase);
290 
291 public:
NodeAnimatableProperty(const T & value,std::function<void (const T &)> && updateCallback)292     NodeAnimatableProperty(const T& value, std::function<void(const T&)>&& updateCallback)
293     {
294         auto property = AceType::MakeRefPtr<S>(value);
295         property->SetUpdateCallback(std::move(updateCallback));
296         SetProperty(property);
297     }
298     ~NodeAnimatableProperty() override = default;
299 
Set(const T & value)300     void Set(const T& value)
301     {
302         auto property = AceType::DynamicCast<S>(GetProperty());
303         if (property) {
304             property->Set(value);
305         }
306     }
307 
Get()308     T Get() const
309     {
310         auto property = AceType::DynamicCast<S>(GetProperty());
311         if (property) {
312             return property->Get();
313         }
314         return {};
315     }
316 private:
317     ACE_DISALLOW_COPY_AND_MOVE(NodeAnimatableProperty);
318 };
319 
320 using NodeAnimatablePropertyFloat = NodeAnimatableProperty<float, AnimatablePropertyFloat>;
321 using NodeAnimatableArithmeticProperty =
322     NodeAnimatableProperty<RefPtr<CustomAnimatableArithmetic>, AnimatableArithmeticProperty>;
323 } // namespace OHOS::Ace::NG
324 
325 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MODIFIER_H
326