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/geometry/ng/rect_t.h" 26 #include "base/memory/ace_type.h" 27 #include "base/utils/utils.h" 28 #include "core/components/common/properties/animation_option.h" 29 #include "core/components_ng/animation/gradient_arithmetic.h" 30 #include "core/components_ng/base/linear_vector.h" 31 #include "core/components_ng/render/canvas_image.h" 32 #include "core/components_ng/render/drawing_forward.h" 33 #include "core/components_ng/render/modifier_adapter.h" 34 35 enum class ThresholdType { 36 LAYOUT, // 0.5f for properties like position, as the difference in properties by 0.5 appears visually unchanged 37 COARSE, // 1.0f / 256.0f 38 MEDIUM, // 1.0f / 1000.0f 39 FINE, // 1.0f / 3072.0f 40 COLOR, // 0.0f 41 DEFAULT, // 1.0f / 256.0f 42 ZERO, // 0.0f for noanimatable property 43 }; 44 45 enum class PropertyUnit { 46 UNKNOWN, 47 PIXEL_POSITION, // animatable properties are related to position of the object, the unit is pixels 48 }; 49 50 namespace OHOS::Ace::NG { 51 52 class ExtensionHandler; 53 54 class ACE_FORCE_EXPORT Modifier : public virtual AceType { 55 DECLARE_ACE_TYPE(Modifier, AceType); 56 57 public: 58 Modifier(); ~Modifier()59 ~Modifier() override 60 { 61 ModifierAdapter::RemoveModifier(id_); 62 } 63 GetId()64 int32_t GetId() const 65 { 66 return id_; 67 } 68 69 private: 70 int32_t id_ = 0; 71 ACE_DISALLOW_COPY_AND_MOVE(Modifier); 72 }; 73 74 class PropertyBase : public virtual AceType { 75 DECLARE_ACE_TYPE(PropertyBase, AceType); 76 77 public: 78 PropertyBase() = default; 79 ~PropertyBase() override = default; 80 81 private: 82 ACE_DISALLOW_COPY_AND_MOVE(PropertyBase); 83 }; 84 85 struct DrawingContext { 86 RSCanvas& canvas; 87 float width = 0; 88 float height = 0; 89 }; 90 91 using DrawModifierFunc = std::function<void(NG::DrawingContext& drawingContext)>; 92 93 class ACE_EXPORT DrawModifier : public virtual AceType { 94 public: 95 DECLARE_ACE_TYPE(DrawModifier, AceType); 96 97 public: 98 DrawModifierFunc drawBehindFunc; 99 DrawModifierFunc drawContentFunc; 100 DrawModifierFunc drawFrontFunc; 101 DrawModifierFunc drawForegroundFunc; 102 }; 103 104 template<typename T> 105 class NormalProperty : public PropertyBase { 106 DECLARE_ACE_TYPE(NormalProperty, PropertyBase); 107 108 public: NormalProperty(const T & value)109 explicit NormalProperty(const T& value) : value_(value) {} 110 ~NormalProperty() override = default; 111 112 void SetUpCallbacks(std::function<T()>&& getFunc, std::function<void(const T&)>&& setFunc, 113 std::function<T()>&& getStageFunc = nullptr) 114 { 115 getFunc_ = std::move(getFunc); 116 setFunc_ = std::move(setFunc); 117 getStageFunc_ = std::move(getStageFunc); 118 } 119 Get()120 T Get() 121 { 122 if (getFunc_) { 123 return getFunc_(); 124 } else { 125 return value_; 126 } 127 } 128 Set(const T & value)129 void Set(const T& value) 130 { 131 if (setFunc_) { 132 setFunc_(value); 133 } else { 134 value_ = value; 135 } 136 } 137 GetStagingValue()138 T GetStagingValue() const 139 { 140 if (getStageFunc_) { 141 return getStageFunc_(); 142 } else { 143 return value_; 144 } 145 } 146 SetUpdateCallback(std::function<void (const T &)> && callback)147 void SetUpdateCallback(std::function<void(const T&)>&& callback) 148 { 149 updateCallback_ = std::move(callback); 150 } 151 GetUpdateCallback()152 std::function<void(const T&)> GetUpdateCallback() const 153 { 154 return updateCallback_; 155 } 156 157 private: 158 T value_; 159 std::function<T()> getFunc_; 160 std::function<void(const T&)> setFunc_; 161 std::function<T()> getStageFunc_; 162 std::function<void(const T&)> updateCallback_; 163 ACE_DISALLOW_COPY_AND_MOVE(NormalProperty); 164 }; 165 166 template<typename T> 167 class AnimatableProperty : public NormalProperty<T> { 168 DECLARE_ACE_TYPE(AnimatableProperty, NormalProperty<T>); 169 170 public: AnimatableProperty(const T & value)171 explicit AnimatableProperty(const T& value) : NormalProperty<T>(value) {} 172 ~AnimatableProperty() override = default; 173 private: 174 ACE_DISALLOW_COPY_AND_MOVE(AnimatableProperty); 175 }; 176 177 #define DECLARE_PROP_TYPED_CLASS(classname, template_class, type) \ 178 class classname : public template_class<type> { \ 179 DECLARE_ACE_TYPE(classname, template_class); \ 180 \ 181 public: \ 182 explicit classname(const type& value) : template_class(value) {} \ 183 ~classname() override = default; \ 184 ACE_DISALLOW_COPY_AND_MOVE(classname); \ 185 } 186 187 DECLARE_PROP_TYPED_CLASS(PropertyBool, NormalProperty, bool); 188 DECLARE_PROP_TYPED_CLASS(PropertySizeF, NormalProperty, SizeF); 189 DECLARE_PROP_TYPED_CLASS(PropertyOffsetF, NormalProperty, OffsetF); 190 DECLARE_PROP_TYPED_CLASS(PropertyInt, NormalProperty, int32_t); 191 DECLARE_PROP_TYPED_CLASS(PropertyFloat, NormalProperty, float); 192 DECLARE_PROP_TYPED_CLASS(PropertyString, NormalProperty, std::string); 193 DECLARE_PROP_TYPED_CLASS(PropertyU16String, NormalProperty, std::u16string); 194 DECLARE_PROP_TYPED_CLASS(PropertyColor, NormalProperty, Color); 195 DECLARE_PROP_TYPED_CLASS(PropertyRectF, NormalProperty, RectF); 196 DECLARE_PROP_TYPED_CLASS(PropertyVectorFloat, NormalProperty, LinearVector<float>); 197 DECLARE_PROP_TYPED_CLASS(PropertyCanvasImageModifierWrapper, NormalProperty, CanvasImageModifierWrapper); 198 DECLARE_PROP_TYPED_CLASS(AnimatablePropertyFloat, AnimatableProperty, float); 199 DECLARE_PROP_TYPED_CLASS(AnimatablePropertyUint8, AnimatableProperty, uint8_t); 200 DECLARE_PROP_TYPED_CLASS(AnimatablePropertyColor, AnimatableProperty, LinearColor); 201 DECLARE_PROP_TYPED_CLASS(AnimatablePropertyVectorFloat, AnimatableProperty, LinearVector<float>); 202 DECLARE_PROP_TYPED_CLASS(AnimatablePropertyVectorColor, AnimatableProperty, GradientArithmetic); 203 DECLARE_PROP_TYPED_CLASS(AnimatablePropertyVectorLinearVector, AnimatableProperty, LinearVector<LinearColor>); 204 DECLARE_PROP_TYPED_CLASS(AnimatablePropertyOffsetF, AnimatableProperty, OffsetF); 205 DECLARE_PROP_TYPED_CLASS(AnimatablePropertySizeF, AnimatableProperty, SizeF); 206 DECLARE_PROP_TYPED_CLASS(AnimatableArithmeticProperty, AnimatableProperty, RefPtr<CustomAnimatableArithmetic>); 207 208 class OverlayModifier : public Modifier { 209 DECLARE_ACE_TYPE(OverlayModifier, Modifier); 210 211 public: OverlayModifier()212 OverlayModifier() 213 { 214 changeCount_ = MakeRefPtr<PropertyInt>(0); 215 AttachProperty(changeCount_); 216 }; 217 ~OverlayModifier() override = default; 218 virtual void onDraw(DrawingContext& Context) = 0; 219 void Draw(DrawingContext& Context); 220 AttachProperty(const RefPtr<PropertyBase> & prop)221 void AttachProperty(const RefPtr<PropertyBase>& prop) 222 { 223 attachedProperties_.push_back(prop); 224 } 225 GetAttachedProperties()226 const std::vector<RefPtr<PropertyBase>>& GetAttachedProperties() 227 { 228 return attachedProperties_; 229 } 230 GetBoundsRect()231 const RectF& GetBoundsRect() 232 { 233 return rect_; 234 } 235 SetBoundsRect(const RectF & rect)236 void SetBoundsRect(const RectF& rect) 237 { 238 rect_ = rect; 239 } 240 241 void SetExtensionHandler(const RefPtr<ExtensionHandler>& extensionHandler); 242 SetOverlayChange()243 void SetOverlayChange() 244 { 245 changeCount_->Set(changeCount_->Get() + 1); 246 } 247 248 private: 249 std::vector<RefPtr<PropertyBase>> attachedProperties_; 250 RectF rect_; 251 RefPtr<PropertyInt> changeCount_; 252 ACE_DISALLOW_COPY_AND_MOVE(OverlayModifier); 253 WeakPtr<ExtensionHandler> extensionHandler_; 254 }; 255 256 class ForegroundModifier : public Modifier { 257 DECLARE_ACE_TYPE(ForegroundModifier, Modifier); 258 259 public: ForegroundModifier()260 ForegroundModifier() 261 { 262 changeCount_ = MakeRefPtr<PropertyInt>(0); 263 AttachProperty(changeCount_); 264 }; 265 ~ForegroundModifier() override = default; 266 virtual void onDraw(DrawingContext& Context) = 0; 267 void Draw(DrawingContext& Context); 268 AttachProperty(const RefPtr<PropertyBase> & prop)269 void AttachProperty(const RefPtr<PropertyBase>& prop) 270 { 271 attachedProperties_.push_back(prop); 272 } 273 GetAttachedProperties()274 const std::vector<RefPtr<PropertyBase>>& GetAttachedProperties() const 275 { 276 return attachedProperties_; 277 } 278 GetBoundsRect()279 const RectF& GetBoundsRect() const 280 { 281 return rect_; 282 } 283 SetBoundsRect(const RectF & rect)284 void SetBoundsRect(const RectF& rect) 285 { 286 rect_ = rect; 287 } 288 289 void SetExtensionHandler(const RefPtr<ExtensionHandler>& extensionHandler); 290 SetForegroundChange()291 void SetForegroundChange() 292 { 293 changeCount_->Set(changeCount_->Get() + 1); 294 } 295 296 private: 297 std::vector<RefPtr<PropertyBase>> attachedProperties_; 298 RectF rect_; 299 RefPtr<PropertyInt> changeCount_; 300 ACE_DISALLOW_COPY_AND_MOVE(ForegroundModifier); 301 WeakPtr<ExtensionHandler> extensionHandler_; 302 }; 303 304 class ContentModifier : public Modifier { 305 DECLARE_ACE_TYPE(ContentModifier, Modifier); 306 307 public: ContentModifier()308 ContentModifier() 309 { 310 changeCount_ = MakeRefPtr<PropertyInt>(0); 311 AttachProperty(changeCount_); 312 }; 313 ~ContentModifier() override = default; 314 virtual void onDraw(DrawingContext& Context) = 0; 315 316 void Draw(DrawingContext& Context); 317 AttachProperty(const RefPtr<PropertyBase> & prop)318 void AttachProperty(const RefPtr<PropertyBase>& prop) 319 { 320 attachedProperties_.push_back(prop); 321 } 322 GetAttachedProperties()323 const std::vector<RefPtr<PropertyBase>>& GetAttachedProperties() 324 { 325 return attachedProperties_; 326 } 327 GetBoundsRect()328 const std::optional<RectF>& GetBoundsRect() 329 { 330 return rect_; 331 } 332 SetBoundsRect(const std::optional<RectF> & rect)333 void SetBoundsRect(const std::optional<RectF>& rect) 334 { 335 rect_ = rect; 336 } 337 SetIsCustomFont(bool isCustomFont)338 void SetIsCustomFont(bool isCustomFont) 339 { 340 isCustomFont_ = isCustomFont; 341 } 342 GetIsCustomFont()343 bool GetIsCustomFont() const 344 { 345 return isCustomFont_; 346 } 347 SetContentChange()348 void SetContentChange() 349 { 350 changeCount_->Set(changeCount_->Get() + 1); 351 } 352 353 void SetExtensionHandler(const RefPtr<ExtensionHandler>& extensionHandler); 354 355 private: 356 std::vector<RefPtr<PropertyBase>> attachedProperties_; 357 std::optional<RectF> rect_; 358 bool isCustomFont_ = false; 359 RefPtr<PropertyInt> changeCount_; // use to trigger rerendering 360 WeakPtr<ExtensionHandler> extensionHandler_; 361 362 ACE_DISALLOW_COPY_AND_MOVE(ContentModifier); 363 }; 364 365 class ModifierImpl { 366 }; 367 368 class NodeAnimatablePropertyBase : public AceType { 369 DECLARE_ACE_TYPE(NodeAnimatablePropertyBase, AceType); 370 371 public: 372 NodeAnimatablePropertyBase() = default; 373 ~NodeAnimatablePropertyBase() override = default; 374 GetModifyImpl()375 const std::shared_ptr<ModifierImpl>& GetModifyImpl() const 376 { 377 return modifyImpl_; 378 } 379 SetModifyImpl(const std::shared_ptr<ModifierImpl> & impl)380 void SetModifyImpl(const std::shared_ptr<ModifierImpl>& impl) 381 { 382 modifyImpl_ = impl; 383 } 384 GetProperty()385 const RefPtr<PropertyBase>& GetProperty() const 386 { 387 return property_; 388 } 389 SetProperty(const RefPtr<PropertyBase> & property)390 void SetProperty(const RefPtr<PropertyBase>& property) 391 { 392 property_ = property; 393 } 394 395 private: 396 std::shared_ptr<ModifierImpl> modifyImpl_; 397 RefPtr<PropertyBase> property_; 398 399 ACE_DISALLOW_COPY_AND_MOVE(NodeAnimatablePropertyBase); 400 }; 401 402 using FinishCallback = std::function<void()>; 403 404 template<typename T, typename S> 405 class NodeAnimatableProperty : public NodeAnimatablePropertyBase { 406 DECLARE_ACE_TYPE(NodeAnimatableProperty, NodeAnimatablePropertyBase); 407 408 public: NodeAnimatableProperty(const T & value,std::function<void (const T &)> && updateCallback)409 NodeAnimatableProperty(const T& value, std::function<void(const T&)>&& updateCallback) 410 { 411 auto property = AceType::MakeRefPtr<S>(value); 412 property->SetUpdateCallback(std::move(updateCallback)); 413 SetProperty(property); 414 } 415 ~NodeAnimatableProperty() override = default; 416 Set(const T & value)417 void Set(const T& value) 418 { 419 auto property = AceType::DynamicCast<S>(GetProperty()); 420 if (property) { 421 property->Set(value); 422 } 423 } 424 425 void SetThresholdType(ThresholdType type); 426 427 void SetPropertyUnit(PropertyUnit unit); 428 Get()429 T Get() const 430 { 431 auto property = AceType::DynamicCast<S>(GetProperty()); 432 if (property) { 433 return property->Get(); 434 } 435 return {}; 436 } 437 GetStagingValue()438 T GetStagingValue() const 439 { 440 auto property = AceType::DynamicCast<S>(GetProperty()); 441 if (property) { 442 return property->GetStagingValue(); 443 } 444 return {}; 445 } 446 447 void AnimateWithVelocity(const AnimationOption& option, T value, T velocity, 448 const FinishCallback& finishCallback); 449 private: 450 ACE_DISALLOW_COPY_AND_MOVE(NodeAnimatableProperty); 451 }; 452 453 // Explicit instantiation declarations 454 extern template class NodeAnimatableProperty<float, AnimatablePropertyFloat>; 455 extern template class NodeAnimatableProperty<OffsetF, AnimatablePropertyOffsetF>; 456 457 using NodeAnimatablePropertyFloat = NodeAnimatableProperty<float, AnimatablePropertyFloat>; 458 using NodeAnimatablePropertyOffsetF = NodeAnimatableProperty<OffsetF, AnimatablePropertyOffsetF>; 459 using NodeAnimatableArithmeticProperty = 460 NodeAnimatableProperty<RefPtr<CustomAnimatableArithmetic>, AnimatableArithmeticProperty>; 461 } // namespace OHOS::Ace::NG 462 463 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MODIFIER_H 464