• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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 <vector>
23 
24 #include "base/memory/ace_type.h"
25 #include "base/utils/utils.h"
26 #include "core/components_ng/render/adapter/linear_vector.h"
27 #include "core/components_ng/render/canvas_image.h"
28 #include "core/components_ng/render/drawing.h"
29 #include "core/components_ng/render/modifier_adapter.h"
30 
31 namespace OHOS::Ace::NG {
32 
33 class Modifier : public virtual AceType {
34     DECLARE_ACE_TYPE(Modifier, AceType);
35 
36 public:
Modifier()37     Modifier()
38     {
39         static std::atomic<int32_t> genId = 0;
40         id_ = genId.fetch_add(1, std::memory_order_relaxed);
41     }
~Modifier()42     ~Modifier() override
43     {
44         ModifierAdapter::RemoveModifier(id_);
45     }
46 
GetId()47     int32_t GetId() const
48     {
49         return id_;
50     }
51 
52 private:
53     int32_t id_ = 0;
54     ACE_DISALLOW_COPY_AND_MOVE(Modifier);
55 };
56 
57 class PropertyBase : public virtual AceType {
58     DECLARE_ACE_TYPE(PropertyBase, AceType);
59 
60 public:
61     PropertyBase() = default;
62     ~PropertyBase() override = default;
63 
64 private:
65     ACE_DISALLOW_COPY_AND_MOVE(PropertyBase);
66 };
67 
68 struct DrawingContext {
69     RSCanvas& canvas;
70     float width = 0;
71     float height = 0;
72 };
73 
74 template<typename T>
75 class NormalProperty : public PropertyBase {
76     DECLARE_ACE_TYPE(NormalProperty, PropertyBase);
77 
78 public:
NormalProperty(const T & value)79     explicit NormalProperty(const T& value) : value_(value) {}
80     ~NormalProperty() override = default;
81 
SetUpCallbacks(std::function<T ()> && getFunc,std::function<void (const T &)> && setFunc)82     void SetUpCallbacks(std::function<T()>&& getFunc, std::function<void(const T&)>&& setFunc)
83     {
84         getFunc_ = getFunc;
85         setFunc_ = setFunc;
86     }
87 
Get()88     T Get()
89     {
90         if (getFunc_) {
91             return getFunc_();
92         } else {
93             return value_;
94         }
95     }
96 
Set(const T & value)97     void Set(const T& value)
98     {
99         if (setFunc_) {
100             setFunc_(value);
101         } else {
102             value_ = value;
103         }
104     }
105 
106 private:
107     T value_;
108     std::function<T()> getFunc_;
109     std::function<void(const T&)> setFunc_;
110     ACE_DISALLOW_COPY_AND_MOVE(NormalProperty);
111 };
112 
113 template<typename T>
114 class AnimatableProperty : public NormalProperty<T> {
115     DECLARE_ACE_TYPE(AnimatableProperty, NormalProperty<T>);
116 
117 public:
AnimatableProperty(const T & value)118     explicit AnimatableProperty(const T& value) : NormalProperty<T>(value) {}
119     ~AnimatableProperty() override = default;
120 private:
121     ACE_DISALLOW_COPY_AND_MOVE(AnimatableProperty);
122 };
123 
124 class ContentModifier : public Modifier {
125     DECLARE_ACE_TYPE(ContentModifier, Modifier);
126 
127 public:
128     ContentModifier() = default;
129     ~ContentModifier() override = default;
130     virtual void onDraw(DrawingContext& Context) = 0;
131 
AttachProperty(const RefPtr<PropertyBase> & prop)132     void AttachProperty(const RefPtr<PropertyBase>& prop)
133     {
134         attachedProperties_.push_back(prop);
135     }
136 
GetAttachedProperties()137     const std::vector<RefPtr<PropertyBase>>& GetAttachedProperties()
138     {
139         return attachedProperties_;
140     }
141 
142 private:
143     std::vector<RefPtr<PropertyBase>> attachedProperties_;
144     ACE_DISALLOW_COPY_AND_MOVE(ContentModifier);
145 };
146 
147 class OverlayModifier : public Modifier {
148     DECLARE_ACE_TYPE(OverlayModifier, Modifier);
149 
150 public:
151     OverlayModifier() = default;
152     ~OverlayModifier() override = default;
153     virtual void onDraw(DrawingContext& Context) = 0;
154 
AttachProperty(const RefPtr<PropertyBase> & prop)155     void AttachProperty(const RefPtr<PropertyBase>& prop)
156     {
157         attachedProperties_.push_back(prop);
158     }
159 
GetAttachedProperties()160     const std::vector<RefPtr<PropertyBase>>& GetAttachedProperties()
161     {
162         return attachedProperties_;
163     }
164 
GetBoundsRect()165     const RectF& GetBoundsRect()
166     {
167         return rect_;
168     }
169 
SetBoundsRect(const RectF & rect)170     void SetBoundsRect(const RectF& rect)
171     {
172         rect_ = rect;
173     }
174 
175 private:
176     std::vector<RefPtr<PropertyBase>> attachedProperties_;
177     RectF rect_;
178     ACE_DISALLOW_COPY_AND_MOVE(OverlayModifier);
179 };
180 
181 #define DECLARE_PROP_TYPED_CLASS(classname, template_class, type)        \
182     class classname : public template_class<type> {                      \
183         DECLARE_ACE_TYPE(classname, template_class);                     \
184                                                                          \
185     public:                                                              \
186         explicit classname(const type& value) : template_class(value) {} \
187         ~classname() override = default;                                 \
188         ACE_DISALLOW_COPY_AND_MOVE(classname);                           \
189     };
190 
191 DECLARE_PROP_TYPED_CLASS(PropertyBool, NormalProperty, bool);
192 DECLARE_PROP_TYPED_CLASS(PropertySizeF, NormalProperty, SizeF);
193 DECLARE_PROP_TYPED_CLASS(PropertyOffsetF, NormalProperty, OffsetF);
194 DECLARE_PROP_TYPED_CLASS(PropertyInt, NormalProperty, int32_t);
195 DECLARE_PROP_TYPED_CLASS(PropertyFloat, NormalProperty, float);
196 DECLARE_PROP_TYPED_CLASS(AnimatablePropertyFloat, AnimatableProperty, float);
197 DECLARE_PROP_TYPED_CLASS(AnimatablePropertyColor, AnimatableProperty, LinearColor);
198 DECLARE_PROP_TYPED_CLASS(AnimatablePropertyVectorFloat, AnimatableProperty, LinearVector<float>);
199 DECLARE_PROP_TYPED_CLASS(AnimatablePropertyOffsetF, AnimatableProperty, OffsetF);
200 
201 } // namespace OHOS::Ace::NG
202 
203 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MODIFIER_H
204