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_COMPONENTS_NG_PROPERTIES_PARTICLE_PROPERTIES_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PROPERTIES_PARTICLE_PROPERTIES_H 18 #include <cstdint> 19 #include <list> 20 #include <optional> 21 #include <string> 22 #include <utility> 23 24 #include "base/geometry/dimension.h" 25 #include "core/components/common/layout/constants.h" 26 #include "core/components/common/properties/color.h" 27 #include "core/components_ng/property/particle_property_animation.h" 28 #include "core/common/resource/resource_object.h" 29 namespace OHOS::Ace::NG { 30 namespace { 31 constexpr int32_t DEFAULT_PARTICLE_COUNT = 5; 32 } 33 enum ACE_EXPORT UpdaterType { NONE_UPDATER = 0, RANDOM, CURVE }; 34 35 enum ACE_EXPORT ParticleType { POINT = 0, IMAGE }; 36 37 enum ACE_EXPORT ParticleEmitterShape { RECTANGLE = 0, CIRCLE, ELLIPSE, ANNULUS }; 38 39 enum ACE_EXPORT DistributionType { UNIFORM = 0, GAUSSIAN }; 40 41 struct PointParticleParameter { 42 public: SetRadiusPointParticleParameter43 void SetRadius(float radius) 44 { 45 radius_ = radius; 46 } 47 GetRadiusPointParticleParameter48 float GetRadius() const 49 { 50 return radius_; 51 } 52 53 bool operator==(const PointParticleParameter& other) const 54 { 55 return NearEqual(radius_, other.GetRadius()); 56 } 57 ToStringPointParticleParameter58 std::string ToString() const 59 { 60 std::string str; 61 str.append("radius: [").append(std::to_string(radius_)).append("]"); 62 return str; 63 } 64 65 private: 66 float radius_ = 0.0f; 67 }; 68 69 struct ImageParticleParameter { 70 public: GetImageSourceImageParticleParameter71 const std::string& GetImageSource() const 72 { 73 return imageSource_; 74 } 75 SetImageSourceImageParticleParameter76 void SetImageSource(std::string& imageSource) 77 { 78 imageSource_ = imageSource; 79 } 80 GetSizeImageParticleParameter81 const std::pair<Dimension, Dimension>& GetSize() const 82 { 83 return size_; 84 } 85 SetSizeXImageParticleParameter86 void SetSizeX(Dimension& sizeX) 87 { 88 size_.first = sizeX; 89 } 90 SetSizeYImageParticleParameter91 void SetSizeY(Dimension& sizeY) 92 { 93 size_.second = sizeY; 94 } 95 SetSizeImageParticleParameter96 void SetSize(std::pair<Dimension, Dimension>& size) 97 { 98 size_ = size; 99 } 100 GetImageFitImageParticleParameter101 std::optional<ImageFit> GetImageFit() const 102 { 103 return imageFit_; 104 } 105 SetImageFitImageParticleParameter106 void SetImageFit(ImageFit& imageFit) 107 { 108 imageFit_ = imageFit; 109 } 110 111 bool operator==(const ImageParticleParameter& other) const 112 { 113 return (imageSource_ == other.GetImageSource()) && (size_ == other.GetSize()) && 114 (imageFit_ == other.GetImageFit()); 115 } 116 ToStringImageParticleParameter117 std::string ToString() const 118 { 119 std::string str; 120 str.append("imageSource: [").append(imageSource_).append("]"); 121 str.append("size: [").append(size_.first.ToString()).append(",").append(size_.second.ToString()).append("]"); 122 str.append("imageFit: [") 123 .append(imageFit_.has_value() ? std::to_string(static_cast<int32_t>(imageFit_.value())) : "NA") 124 .append("]"); 125 return str; 126 } 127 AddResourceImageParticleParameter128 void AddResource( 129 const std::string& key, 130 const RefPtr<ResourceObject>& resObj, 131 std::function<void(const RefPtr<ResourceObject>&, ImageParticleParameter&)>&& updateFunc) 132 { 133 if (resObj == nullptr || !updateFunc) { 134 return; 135 } 136 EmitterResMap_[key] = { resObj, std::move(updateFunc) }; 137 } 138 ReloadResourcesImageParticleParameter139 void ReloadResources() 140 { 141 for (const auto& [key, resourceUpdater] : EmitterResMap_) { 142 resourceUpdater.updateFunc(resourceUpdater.obj, *this); 143 } 144 } 145 RemoveResourceImageParticleParameter146 void RemoveResource(const std::string& key) 147 { 148 auto iter = EmitterResMap_.find(key); 149 if (iter != EmitterResMap_.end()) { 150 EmitterResMap_.erase(iter); 151 } 152 } 153 154 private: 155 struct ResourceUpdater { 156 RefPtr<ResourceObject> obj; 157 std::function<void(const RefPtr<ResourceObject>&, ImageParticleParameter&)> updateFunc; 158 }; 159 std::unordered_map<std::string, ResourceUpdater> EmitterResMap_; 160 std::string imageSource_; 161 std::pair<Dimension, Dimension> size_; 162 std::optional<ImageFit> imageFit_; 163 }; 164 165 struct ParticleConfig { 166 public: SetPointParticleParameterParticleConfig167 void SetPointParticleParameter(const PointParticleParameter& pointParticleParameter) 168 { 169 pointParameter_ = pointParticleParameter; 170 } GetPointParticleParameterParticleConfig171 const PointParticleParameter& GetPointParticleParameter() const 172 { 173 return pointParameter_; 174 } SetImageParticleParameterParticleConfig175 void SetImageParticleParameter(const ImageParticleParameter& imageParticleParameter) 176 { 177 imageParameter_ = imageParticleParameter; 178 } GetImageParticleParameterParticleConfig179 const ImageParticleParameter& GetImageParticleParameter() const 180 { 181 return imageParameter_; 182 } 183 184 bool operator==(const ParticleConfig& other) const 185 { 186 return (pointParameter_ == other.GetPointParticleParameter()) && 187 (imageParameter_ == other.GetImageParticleParameter()); 188 } 189 ToStringParticleConfig190 std::string ToString() const 191 { 192 std::string str; 193 str.append("pointParameter: [").append(pointParameter_.ToString()).append("]"); 194 str.append("imageParameter: [").append(imageParameter_.ToString()).append("]"); 195 return str; 196 } 197 198 private: 199 PointParticleParameter pointParameter_; 200 ImageParticleParameter imageParameter_; 201 }; 202 203 struct Particle { 204 public: GetParticleTypeParticle205 const ParticleType& GetParticleType() const 206 { 207 return particleType_; 208 } SetParticleTypeParticle209 void SetParticleType(const ParticleType& particleType) 210 { 211 particleType_ = particleType; 212 } GetConfigParticle213 const ParticleConfig& GetConfig() const 214 { 215 return config_; 216 } SetConfigParticle217 void SetConfig(const ParticleConfig& config) 218 { 219 config_ = config; 220 } GetCountParticle221 int32_t GetCount() const 222 { 223 return count_; 224 } SetCountParticle225 void SetCount(int32_t count) 226 { 227 count_ = count; 228 } SetLifeTimeParticle229 void SetLifeTime(int64_t lifetime) 230 { 231 lifeTime_ = lifetime; 232 } 233 GetLifeTimeParticle234 const std::optional<int64_t>& GetLifeTime() const 235 { 236 return lifeTime_; 237 } 238 SetLifeTimeRangeParticle239 void SetLifeTimeRange(int64_t lifetimeRange) 240 { 241 lifeTimeRange_ = lifetimeRange; 242 } 243 GetLifeTimeRangeParticle244 const std::optional<int64_t>& GetLifeTimeRange() const 245 { 246 return lifeTimeRange_; 247 } 248 249 bool operator==(const Particle& other) const 250 { 251 return (particleType_ == other.GetParticleType()) && (config_ == other.GetConfig()) && 252 (count_ == other.GetCount()) && (lifeTime_ == other.GetLifeTime()) && 253 (lifeTimeRange_ == other.GetLifeTimeRange()); 254 } 255 ToStringParticle256 std::string ToString() const 257 { 258 std::string str; 259 str.append("particleType: [").append(std::to_string(static_cast<int32_t>(particleType_))).append("]"); 260 str.append("config: [").append(config_.ToString()).append("]"); 261 str.append("count: [").append(std::to_string(count_)).append("]"); 262 str.append("lifeTime: [").append(lifeTime_.has_value() ? std::to_string(lifeTime_.value()) : "NA").append("]"); 263 str.append("lifeTimeRange: [") 264 .append(lifeTimeRange_.has_value() ? std::to_string(lifeTimeRange_.value()) : "NA") 265 .append("]"); 266 return str; 267 } 268 269 private: 270 ParticleType particleType_; 271 ParticleConfig config_; 272 int32_t count_ = DEFAULT_PARTICLE_COUNT; 273 std::optional<int64_t> lifeTime_; 274 std::optional<int64_t> lifeTimeRange_; 275 }; 276 277 struct ParticleAnnulusRegion { ParticleAnnulusRegionParticleAnnulusRegion278 ParticleAnnulusRegion(std::pair<CalcDimension, CalcDimension> centerValue, CalcDimension innerRadiusValue, 279 CalcDimension outerRadiusValue, float startAngleValue, float endAngleValue) 280 : center_(centerValue), innerRadius_(innerRadiusValue), 281 outerRadius_(outerRadiusValue), startAngle_(startAngleValue), endAngle_(endAngleValue) {} 282 283 bool operator==(const ParticleAnnulusRegion& others) const 284 { 285 return (center_ == others.center_) && (innerRadius_ == others.innerRadius_) && 286 (outerRadius_ == others.outerRadius_) && (startAngle_ == others.startAngle_) && 287 (endAngle_ == others.endAngle_); 288 } 289 ToStringParticleAnnulusRegion290 std::string ToString() const 291 { 292 std::string str; 293 str.append("center: [") 294 .append(center_.first.ToString()) 295 .append(",") 296 .append(center_.second.ToString()) 297 .append("]"); 298 str.append("innerRadius: [") 299 .append(std::to_string(innerRadius_.ConvertToPx())) 300 .append("]"); 301 str.append("outerRadius: [") 302 .append(std::to_string(outerRadius_.ConvertToPx())) 303 .append("]"); 304 str.append("startAngle: [") 305 .append(std::to_string(startAngle_)) 306 .append("]"); 307 str.append("endAngle: [") 308 .append(std::to_string(endAngle_)) 309 .append("]"); 310 return str; 311 } 312 GetCenterParticleAnnulusRegion313 std::pair<CalcDimension, CalcDimension> GetCenter() const 314 { 315 return center_; 316 } 317 SetCenterParticleAnnulusRegion318 void SetCenter(const std::pair<CalcDimension, CalcDimension>& center) 319 { 320 center_ = center; 321 } 322 SetCenterXParticleAnnulusRegion323 void SetCenterX(CalcDimension& centerX) 324 { 325 center_.first = centerX; 326 } 327 SetCenterYParticleAnnulusRegion328 void SetCenterY(CalcDimension& centerY) 329 { 330 center_.second = centerY; 331 } 332 GetInnerRadiusParticleAnnulusRegion333 CalcDimension GetInnerRadius() const 334 { 335 return innerRadius_; 336 } 337 SetInnerRadiusParticleAnnulusRegion338 void SetInnerRadius(const CalcDimension& innerRadius) 339 { 340 innerRadius_ = innerRadius; 341 } 342 GetOuterRadiusParticleAnnulusRegion343 CalcDimension GetOuterRadius() const 344 { 345 return outerRadius_; 346 } 347 SetOuterRadiusParticleAnnulusRegion348 void SetOuterRadius(const CalcDimension& outerRadius) 349 { 350 outerRadius_ = outerRadius; 351 } 352 GetStartAngleParticleAnnulusRegion353 float GetStartAngle() const 354 { 355 return startAngle_; 356 } 357 SetStartAngleParticleAnnulusRegion358 void SetStartAngle(float startAngle) 359 { 360 startAngle_ = startAngle; 361 } 362 GetEndAngleParticleAnnulusRegion363 float GetEndAngle() const 364 { 365 return endAngle_; 366 } 367 SetEndAngleParticleAnnulusRegion368 void SetEndAngle(float endAngle) 369 { 370 endAngle_ = endAngle; 371 } 372 AddResourceParticleAnnulusRegion373 void AddResource( 374 const std::string& key, 375 const RefPtr<ResourceObject>& resObj, 376 std::function<void(const RefPtr<ResourceObject>&, ParticleAnnulusRegion&)>&& updateFunc) 377 { 378 if (resObj == nullptr || !updateFunc) { 379 return; 380 } 381 AnnulusResMap_[key] = { resObj, std::move(updateFunc) }; 382 } 383 ReloadResourcesParticleAnnulusRegion384 void ReloadResources() 385 { 386 for (const auto& [key, resourceUpdater] : AnnulusResMap_) { 387 resourceUpdater.updateFunc(resourceUpdater.obj, *this); 388 } 389 } 390 RemoveResourceParticleAnnulusRegion391 void RemoveResource(const std::string& key) 392 { 393 auto iter = AnnulusResMap_.find(key); 394 if (iter != AnnulusResMap_.end()) { 395 AnnulusResMap_.erase(iter); 396 } 397 } 398 399 private: 400 struct ResourceUpdater { 401 RefPtr<ResourceObject> obj; 402 std::function<void(const RefPtr<ResourceObject>&, ParticleAnnulusRegion&)> updateFunc; 403 }; 404 std::unordered_map<std::string, ResourceUpdater> AnnulusResMap_; 405 406 std::pair<CalcDimension, CalcDimension> center_; 407 CalcDimension innerRadius_; 408 CalcDimension outerRadius_; 409 float startAngle_; 410 float endAngle_; 411 }; 412 413 struct EmitterOption { 414 public: GetParticleEmitterOption415 const Particle& GetParticle() const 416 { 417 return particle_; 418 } SetParticleEmitterOption419 void SetParticle(Particle& particle) 420 { 421 particle_ = particle; 422 } SetEmitterRateEmitterOption423 void SetEmitterRate(int32_t emitterRate) 424 { 425 emitterRate_ = emitterRate; 426 } 427 GetEmitterRateEmitterOption428 const std::optional<int32_t>& GetEmitterRate() const 429 { 430 return emitterRate_; 431 } 432 SetPositionEmitterOption433 void SetPosition(std::pair<Dimension, Dimension>& point) 434 { 435 position_ = point; 436 } 437 SetPositionXEmitterOption438 void SetPositionX(Dimension& pointX) 439 { 440 position_->first = pointX; 441 } 442 SetPositionYEmitterOption443 void SetPositionY(Dimension& pointY) 444 { 445 position_->second = pointY; 446 } 447 GetPositionEmitterOption448 const std::optional<std::pair<Dimension, Dimension>>& GetPosition() const 449 { 450 return position_; 451 } 452 SetSizeEmitterOption453 void SetSize(std::pair<Dimension, Dimension>& size) 454 { 455 size_ = size; 456 } 457 SetSizeXEmitterOption458 void SetSizeX(Dimension& sizeX) 459 { 460 size_->first = sizeX; 461 } 462 SetSizeYEmitterOption463 void SetSizeY(Dimension& sizeY) 464 { 465 size_->second = sizeY; 466 } 467 GetSizeEmitterOption468 const std::optional<std::pair<Dimension, Dimension>>& GetSize() const 469 { 470 return size_; 471 } 472 SetShapeEmitterOption473 void SetShape(ParticleEmitterShape& shape) 474 { 475 shape_ = shape; 476 } 477 GetShapeEmitterOption478 const std::optional<ParticleEmitterShape>& GetShape() const 479 { 480 return shape_; 481 } 482 SetAnnulusRegionEmitterOption483 void SetAnnulusRegion(ParticleAnnulusRegion& annulusRegion) 484 { 485 annulusRegion_ = annulusRegion; 486 } 487 GetAnnulusRegionEmitterOption488 const std::optional<ParticleAnnulusRegion>& GetAnnulusRegion() const 489 { 490 return annulusRegion_; 491 } 492 493 bool operator==(const EmitterOption& other) const 494 { 495 return particle_ == other.GetParticle() && emitterRate_ == other.GetEmitterRate() && 496 position_ == other.GetPosition() && size_ == other.GetSize() && 497 shape_ == other.GetShape() && annulusRegion_ == other.GetAnnulusRegion(); 498 } 499 ToStringEmitterOption500 std::string ToString() const 501 { 502 std::string str; 503 str.append("particle: [").append(particle_.ToString()).append("]"); 504 str.append("emitterRate: [") 505 .append(emitterRate_.has_value() ? std::to_string(emitterRate_.value()) : "NA") 506 .append("]"); 507 if (position_.has_value()) { 508 str.append("position: [") 509 .append(position_->first.ToString()) 510 .append(",") 511 .append(position_->second.ToString()) 512 .append("]"); 513 } else { 514 str.append("position: [").append("NA").append("]"); 515 } 516 if (size_.has_value()) { 517 str.append("size: [") 518 .append(size_->first.ToString()) 519 .append(",") 520 .append(size_->second.ToString()) 521 .append("]"); 522 } else { 523 str.append("size: [").append("NA").append("]"); 524 } 525 str.append("shape: [") 526 .append(shape_.has_value() ? std::to_string(static_cast<int32_t>(shape_.value())) : "NA") 527 .append("]"); 528 str.append("annulusRegion: [") 529 .append(annulusRegion_.has_value() ? annulusRegion_->ToString() : "NA") 530 .append("]"); 531 return str; 532 } AddResourceEmitterOption533 void AddResource( 534 const std::string& key, 535 const RefPtr<ResourceObject>& resObj, 536 std::function<void(const RefPtr<ResourceObject>&, EmitterOption&)>&& updateFunc) 537 { 538 if (resObj == nullptr || !updateFunc) { 539 return; 540 } 541 EmitterResMap_[key] = { resObj, std::move(updateFunc) }; 542 } 543 ReloadResourcesEmitterOption544 void ReloadResources() 545 { 546 for (const auto& [key, resourceUpdater] : EmitterResMap_) { 547 resourceUpdater.updateFunc(resourceUpdater.obj, *this); 548 } 549 } 550 RemoveResourceEmitterOption551 void RemoveResource(const std::string& key) 552 { 553 auto iter = EmitterResMap_.find(key); 554 if (iter != EmitterResMap_.end()) { 555 EmitterResMap_.erase(iter); 556 } 557 } 558 559 private: 560 struct ResourceUpdater { 561 RefPtr<ResourceObject> obj; 562 std::function<void(const RefPtr<ResourceObject>&, EmitterOption&)> updateFunc; 563 }; 564 std::unordered_map<std::string, ResourceUpdater> EmitterResMap_; 565 566 Particle particle_; 567 std::optional<int32_t> emitterRate_; 568 std::optional<std::pair<Dimension, Dimension>> position_; 569 std::optional<std::pair<Dimension, Dimension>> size_; 570 std::optional<ParticleEmitterShape> shape_; 571 std::optional<ParticleAnnulusRegion> annulusRegion_; 572 }; 573 574 struct ParticleFloatPropertyUpdaterConfig { 575 public: SetRandomConfigParticleFloatPropertyUpdaterConfig576 void SetRandomConfig(const std::pair<float, float>& randomConfig) 577 { 578 randomConfig_ = randomConfig; 579 } 580 GetRandomConfigParticleFloatPropertyUpdaterConfig581 const std::pair<float, float>& GetRandomConfig() const 582 { 583 return randomConfig_; 584 } 585 SetAnimationsParticleFloatPropertyUpdaterConfig586 void SetAnimations(const std::list<ParticlePropertyAnimation<float>>& animations) 587 { 588 animations_ = animations; 589 } 590 GetAnimationsParticleFloatPropertyUpdaterConfig591 const std::list<ParticlePropertyAnimation<float>>& GetAnimations() const 592 { 593 return animations_; 594 } 595 SetNullStrParticleFloatPropertyUpdaterConfig596 void SetNullStr(const std::string noneValue) 597 { 598 noneValue_ = noneValue; 599 } 600 GetNullStrParticleFloatPropertyUpdaterConfig601 const std::string& GetNullStr() const 602 { 603 return noneValue_; 604 } 605 606 bool operator==(const ParticleFloatPropertyUpdaterConfig& other) const 607 { 608 return (noneValue_ == other.GetNullStr()) && NearEqual(randomConfig_.first, other.GetRandomConfig().first) && 609 NearEqual(randomConfig_.second, other.GetRandomConfig().second) && 610 (animations_ == other.GetAnimations()); 611 } 612 ToStringParticleFloatPropertyUpdaterConfig613 std::string ToString() const 614 { 615 std::string str; 616 str.append("noneValue: [").append(noneValue_).append("]"); 617 str.append("randomConfig: [") 618 .append(std::to_string(randomConfig_.first)) 619 .append(",") 620 .append(std::to_string(randomConfig_.second)) 621 .append("]"); 622 if (animations_.size() > 0) { 623 str.append("animations: ["); 624 for (auto& item : animations_) { 625 str.append("{").append(item.ToString()).append("}"); 626 } 627 str.append("]"); 628 } else { 629 str.append("animations: [").append("]"); 630 } 631 return str; 632 } 633 634 private: 635 std::string noneValue_; 636 std::pair<float, float> randomConfig_; 637 std::list<ParticlePropertyAnimation<float>> animations_; 638 }; 639 640 struct ParticleFloatPropertyUpdater { 641 public: GetUpdaterTypeParticleFloatPropertyUpdater642 const UpdaterType& GetUpdaterType() const 643 { 644 return updaterType_; 645 } 646 SetUpdaterTypeParticleFloatPropertyUpdater647 void SetUpdaterType(const UpdaterType& updateType) 648 { 649 updaterType_ = updateType; 650 } 651 GetConfigParticleFloatPropertyUpdater652 const ParticleFloatPropertyUpdaterConfig& GetConfig() const 653 { 654 return config_; 655 } 656 SetConfigParticleFloatPropertyUpdater657 void SetConfig(const ParticleFloatPropertyUpdaterConfig& config) 658 { 659 config_ = config; 660 } 661 662 bool operator==(const ParticleFloatPropertyUpdater& other) const 663 { 664 return (updaterType_ == other.GetUpdaterType()) && (config_ == other.GetConfig()); 665 } 666 ToStringParticleFloatPropertyUpdater667 std::string ToString() const 668 { 669 std::string str; 670 str.append("updaterType: [").append(std::to_string(static_cast<int32_t>(updaterType_))).append("]"); 671 str.append("config: [").append(config_.ToString()).append("]"); 672 return str; 673 } 674 675 private: 676 UpdaterType updaterType_ = UpdaterType::NONE_UPDATER; 677 ParticleFloatPropertyUpdaterConfig config_; 678 }; 679 680 struct ParticleFloatPropertyOption { 681 public: GetRangeParticleFloatPropertyOption682 const std::pair<float, float>& GetRange() const 683 { 684 return range_; 685 } 686 SetRangeParticleFloatPropertyOption687 void SetRange(const std::pair<float, float>& range) 688 { 689 range_ = range; 690 } 691 GetUpdaterParticleFloatPropertyOption692 const std::optional<ParticleFloatPropertyUpdater>& GetUpdater() const 693 { 694 return updater_; 695 } 696 SetUpdaterParticleFloatPropertyOption697 void SetUpdater(const ParticleFloatPropertyUpdater& updater) 698 { 699 updater_ = updater; 700 } 701 702 bool operator==(const ParticleFloatPropertyOption& other) const 703 { 704 return NearEqual(range_.first, other.GetRange().first) && NearEqual(range_.second, other.GetRange().second) && 705 (updater_ == other.GetUpdater()); 706 } 707 ToStringParticleFloatPropertyOption708 std::string ToString() const 709 { 710 std::string str; 711 str.append("range: [") 712 .append(std::to_string(range_.first)) 713 .append(",") 714 .append(std::to_string(range_.second)) 715 .append("]"); 716 str.append("updater: [").append(updater_.has_value() ? updater_->ToString() : "NA").append("]"); 717 return str; 718 } 719 720 private: 721 std::pair<float, float> range_; 722 std::optional<ParticleFloatPropertyUpdater> updater_; 723 }; 724 725 struct ColorParticleRandomUpdateConfig { 726 public: GetRedRandomColorParticleRandomUpdateConfig727 const std::pair<float, float>& GetRedRandom() const 728 { 729 return redRandom_; 730 } 731 SetRedRandomColorParticleRandomUpdateConfig732 void SetRedRandom(const std::pair<float, float>& redRandom) 733 { 734 redRandom_ = redRandom; 735 } 736 GetGreenRandomColorParticleRandomUpdateConfig737 const std::pair<float, float>& GetGreenRandom() const 738 { 739 return greenRandom_; 740 } 741 SetGreenRandomColorParticleRandomUpdateConfig742 void SetGreenRandom(const std::pair<float, float>& greenRandom) 743 { 744 greenRandom_ = greenRandom; 745 } 746 GetBlueRandomColorParticleRandomUpdateConfig747 const std::pair<float, float>& GetBlueRandom() const 748 { 749 return blueRandom_; 750 } 751 SetBlueRandomColorParticleRandomUpdateConfig752 void SetBlueRandom(const std::pair<float, float>& blueRandom) 753 { 754 blueRandom_ = blueRandom; 755 } 756 GetAlphaRandomColorParticleRandomUpdateConfig757 const std::pair<float, float>& GetAlphaRandom() const 758 { 759 return alphaRandom_; 760 } 761 SetAlphaRandomColorParticleRandomUpdateConfig762 void SetAlphaRandom(const std::pair<float, float>& alphaRandom) 763 { 764 alphaRandom_ = alphaRandom; 765 } 766 767 bool operator==(const ColorParticleRandomUpdateConfig& other) const 768 { 769 return NearEqual(redRandom_.first, other.GetRedRandom().first) && 770 NearEqual(redRandom_.second, other.GetRedRandom().second) && 771 NearEqual(greenRandom_.first, other.GetGreenRandom().first) && 772 NearEqual(greenRandom_.second, other.GetGreenRandom().second) && 773 NearEqual(blueRandom_.first, other.GetBlueRandom().first) && 774 NearEqual(blueRandom_.second, other.GetBlueRandom().second) && 775 NearEqual(alphaRandom_.first, other.GetAlphaRandom().first) && 776 NearEqual(alphaRandom_.second, other.GetAlphaRandom().second); 777 } 778 ToStringColorParticleRandomUpdateConfig779 std::string ToString() const 780 { 781 std::string str; 782 str.append("redRandom: [") 783 .append(std::to_string(redRandom_.first)) 784 .append(",") 785 .append(std::to_string(redRandom_.second)) 786 .append("]"); 787 str.append("greenRandom: [") 788 .append(std::to_string(greenRandom_.first)) 789 .append(",") 790 .append(std::to_string(greenRandom_.second)) 791 .append("]"); 792 str.append("blueRandom: [") 793 .append(std::to_string(blueRandom_.first)) 794 .append(",") 795 .append(std::to_string(blueRandom_.second)) 796 .append("]"); 797 str.append("alphaRandom: [") 798 .append(std::to_string(alphaRandom_.first)) 799 .append(",") 800 .append(std::to_string(alphaRandom_.second)) 801 .append("]"); 802 return str; 803 } 804 805 private: 806 std::pair<float, float> redRandom_; 807 std::pair<float, float> greenRandom_; 808 std::pair<float, float> blueRandom_; 809 std::pair<float, float> alphaRandom_; 810 }; 811 812 struct ParticleColorPropertyUpdaterConfig { 813 public: SetInValidParticleColorPropertyUpdaterConfig814 void SetInValid(int32_t inValid) 815 { 816 inValid_ = inValid; 817 } 818 GetInValidParticleColorPropertyUpdaterConfig819 int32_t GetInValid() const 820 { 821 return inValid_; 822 } 823 SetRandomConfigParticleColorPropertyUpdaterConfig824 void SetRandomConfig(const ColorParticleRandomUpdateConfig& randomConfig) 825 { 826 random_ = randomConfig; 827 } 828 GetRandomConfigParticleColorPropertyUpdaterConfig829 const ColorParticleRandomUpdateConfig& GetRandomConfig() const 830 { 831 return random_; 832 } 833 SetAnimationArrayParticleColorPropertyUpdaterConfig834 void SetAnimationArray(const std::list<ParticlePropertyAnimation<Color>>& animationArray) 835 { 836 animationArray_ = animationArray; 837 } 838 GetAnimationArrayParticleColorPropertyUpdaterConfig839 const std::list<ParticlePropertyAnimation<Color>>& GetAnimationArray() const 840 { 841 return animationArray_; 842 } 843 844 bool operator==(const ParticleColorPropertyUpdaterConfig& other) const 845 { 846 return (inValid_ == other.GetInValid()) && (random_ == other.GetRandomConfig()) && 847 (animationArray_ == other.GetAnimationArray()); 848 } 849 ToStringParticleColorPropertyUpdaterConfig850 std::string ToString() const 851 { 852 std::string str; 853 str.append("inValid: [").append(std::to_string(inValid_)).append("]"); 854 str.append("random: [").append(random_.ToString()).append("]"); 855 str.append("animationArray: ["); 856 if (animationArray_.size() > 0) { 857 for (auto& item : animationArray_) { 858 str.append("{").append(item.ToString()).append("}"); 859 } 860 } 861 str.append("]"); 862 return str; 863 } 864 865 private: 866 int32_t inValid_ = 0; 867 ColorParticleRandomUpdateConfig random_; 868 std::list<ParticlePropertyAnimation<Color>> animationArray_; 869 }; 870 871 struct ParticleColorPropertyUpdater { 872 public: GetUpdateTypeParticleColorPropertyUpdater873 const UpdaterType& GetUpdateType() const 874 { 875 return type_; 876 } 877 SetUpdateTypeParticleColorPropertyUpdater878 void SetUpdateType(UpdaterType type) 879 { 880 type_ = type; 881 } 882 GetConfigParticleColorPropertyUpdater883 const ParticleColorPropertyUpdaterConfig& GetConfig() const 884 { 885 return config_; 886 } 887 SetConfigParticleColorPropertyUpdater888 void SetConfig(const ParticleColorPropertyUpdaterConfig& config) 889 { 890 config_ = config; 891 } 892 893 bool operator==(const ParticleColorPropertyUpdater& other) const 894 { 895 return (type_ == other.GetUpdateType()) && (config_ == other.GetConfig()); 896 } 897 ToStringParticleColorPropertyUpdater898 std::string ToString() const 899 { 900 std::string str; 901 str.append("type: [").append(std::to_string(static_cast<int32_t>(type_))).append("]"); 902 str.append("config: [").append(config_.ToString()).append("]"); 903 return str; 904 } 905 906 private: 907 UpdaterType type_ = UpdaterType::NONE_UPDATER; 908 ParticleColorPropertyUpdaterConfig config_; 909 }; 910 911 struct ParticleColorPropertyOption { 912 public: GetRangeParticleColorPropertyOption913 const std::pair<Color, Color>& GetRange() const 914 { 915 return range_; 916 } SetRangeParticleColorPropertyOption917 void SetRange(std::pair<Color, Color>& range) 918 { 919 range_ = range; 920 } 921 SetRangeFirstParticleColorPropertyOption922 void SetRangeFirst(Color& rangeFirst) 923 { 924 range_.first = rangeFirst; 925 } 926 SetRangeSecondParticleColorPropertyOption927 void SetRangeSecond(Color& rangeSecond) 928 { 929 range_.second = rangeSecond; 930 } 931 GetDistributionParticleColorPropertyOption932 const std::optional<DistributionType>& GetDistribution() const 933 { 934 return distribution_; 935 } SetDistributionParticleColorPropertyOption936 void SetDistribution(DistributionType& distribution) 937 { 938 distribution_ = distribution; 939 } GetUpdaterParticleColorPropertyOption940 const std::optional<ParticleColorPropertyUpdater>& GetUpdater() const 941 { 942 return updater_; 943 } SetUpdaterParticleColorPropertyOption944 void SetUpdater(ParticleColorPropertyUpdater& updater) 945 { 946 updater_ = updater; 947 } 948 949 bool operator==(const ParticleColorPropertyOption& other) const 950 { 951 return (range_ == other.GetRange()) && (updater_ == other.GetUpdater()) && 952 (distribution_ == other.GetDistribution()); 953 } 954 ToStringParticleColorPropertyOption955 std::string ToString() const 956 { 957 std::string str; 958 str.append("range: [") 959 .append(range_.first.ColorToString()) 960 .append(",") 961 .append(range_.second.ColorToString()) 962 .append("]"); 963 str.append("config: [").append(updater_.has_value() ? updater_->ToString() : "NA").append("]"); 964 return str; 965 } AddResourceParticleColorPropertyOption966 void AddResource( 967 const std::string& key, 968 const RefPtr<ResourceObject>& resObj, 969 std::function<void(const RefPtr<ResourceObject>&, ParticleColorPropertyOption&)>&& updateFunc) 970 { 971 if (resObj == nullptr || !updateFunc) { 972 return; 973 } 974 particleColorResMap_[key] = { resObj, std::move(updateFunc) }; 975 } 976 ReloadResourcesParticleColorPropertyOption977 void ReloadResources() 978 { 979 for (const auto& [key, resourceUpdater] : particleColorResMap_) { 980 resourceUpdater.updateFunc(resourceUpdater.obj, *this); 981 } 982 } RemoveResourceParticleColorPropertyOption983 void RemoveResource(const std::string& key) 984 { 985 auto iter = particleColorResMap_.find(key); 986 if (iter != particleColorResMap_.end()) { 987 particleColorResMap_.erase(iter); 988 } 989 } 990 991 private: 992 struct ResourceUpdater { 993 RefPtr<ResourceObject> obj; 994 std::function<void(const RefPtr<ResourceObject>&, ParticleColorPropertyOption&)> updateFunc; 995 }; 996 std::unordered_map<std::string, ResourceUpdater> particleColorResMap_; 997 std::pair<Color, Color> range_; 998 std::optional<DistributionType> distribution_; 999 std::optional<ParticleColorPropertyUpdater> updater_; 1000 }; 1001 1002 struct VelocityProperty { 1003 public: GetSpeedRangeVelocityProperty1004 const std::pair<float, float>& GetSpeedRange() const 1005 { 1006 return speed_; 1007 } SetSpeedRangeVelocityProperty1008 void SetSpeedRange(std::pair<float, float>& speed) 1009 { 1010 speed_ = speed; 1011 } GetAngleRangeVelocityProperty1012 const std::pair<float, float>& GetAngleRange() const 1013 { 1014 return angle_; 1015 } SetAngleRangeVelocityProperty1016 void SetAngleRange(std::pair<float, float>& angle) 1017 { 1018 angle_ = angle; 1019 } 1020 1021 bool operator==(const VelocityProperty& other) const 1022 { 1023 return NearEqual(speed_.first, other.GetSpeedRange().first) && 1024 NearEqual(speed_.second, other.GetSpeedRange().second) && 1025 NearEqual(angle_.first, other.GetAngleRange().first) && 1026 NearEqual(angle_.second, other.GetAngleRange().second); 1027 } 1028 ToStringVelocityProperty1029 std::string ToString() const 1030 { 1031 std::string str; 1032 str.append("speed: [") 1033 .append(std::to_string(speed_.first)) 1034 .append(",") 1035 .append(std::to_string(speed_.second)) 1036 .append("]"); 1037 str.append("angle: [") 1038 .append(std::to_string(angle_.first)) 1039 .append(",") 1040 .append(std::to_string(angle_.second)) 1041 .append("]"); 1042 return str; 1043 } 1044 1045 private: 1046 std::pair<float, float> speed_; 1047 std::pair<float, float> angle_; 1048 }; 1049 1050 struct AccelerationProperty { 1051 public: GetSpeedAccelerationProperty1052 const std::optional<ParticleFloatPropertyOption>& GetSpeed() const 1053 { 1054 return speed_; 1055 } 1056 SetSpeedAccelerationProperty1057 void SetSpeed(const ParticleFloatPropertyOption& speed) 1058 { 1059 speed_ = speed; 1060 } 1061 GetAngleAccelerationProperty1062 const std::optional<ParticleFloatPropertyOption>& GetAngle() const 1063 { 1064 return angle_; 1065 } 1066 SetAngleAccelerationProperty1067 void SetAngle(ParticleFloatPropertyOption& angle) 1068 { 1069 angle_ = angle; 1070 } 1071 1072 bool operator==(const AccelerationProperty& other) const 1073 { 1074 return (speed_ == other.GetSpeed()) && (angle_ == other.GetAngle()); 1075 } 1076 ToStringAccelerationProperty1077 std::string ToString() const 1078 { 1079 std::string str; 1080 str.append("speed: [").append(speed_.has_value() ? speed_->ToString() : "NA").append("]"); 1081 str.append("angle: [").append(angle_.has_value() ? angle_->ToString() : "NA").append("]"); 1082 return str; 1083 } 1084 1085 private: 1086 std::optional<ParticleFloatPropertyOption> speed_; 1087 std::optional<ParticleFloatPropertyOption> angle_; 1088 }; 1089 1090 struct ParticleOption { 1091 public: 1092 GetEmitterOptionParticleOption1093 const EmitterOption& GetEmitterOption() const 1094 { 1095 return emitter_; 1096 } 1097 SetEmitterOptionParticleOption1098 void SetEmitterOption(EmitterOption& emitter) 1099 { 1100 emitter_ = emitter; 1101 } 1102 GetParticleColorOptionParticleOption1103 const std::optional<ParticleColorPropertyOption>& GetParticleColorOption() const 1104 { 1105 return colorOption_; 1106 } 1107 SetParticleColorOptionParticleOption1108 void SetParticleColorOption(const ParticleColorPropertyOption& colorOption) 1109 { 1110 colorOption_ = colorOption; 1111 } 1112 GetParticleOpacityOptionParticleOption1113 const std::optional<ParticleFloatPropertyOption>& GetParticleOpacityOption() const 1114 { 1115 return opacityOption_; 1116 } 1117 SetParticleOpacityOptionParticleOption1118 void SetParticleOpacityOption(ParticleFloatPropertyOption& opacityOption) 1119 { 1120 opacityOption_ = opacityOption; 1121 } 1122 GetParticleScaleOptionParticleOption1123 const std::optional<ParticleFloatPropertyOption>& GetParticleScaleOption() const 1124 { 1125 return scaleOption_; 1126 } 1127 SetParticleScaleOptionParticleOption1128 void SetParticleScaleOption(ParticleFloatPropertyOption& scaleOption) 1129 { 1130 scaleOption_ = scaleOption; 1131 } 1132 GetParticleVelocityOptionParticleOption1133 const std::optional<VelocityProperty>& GetParticleVelocityOption() const 1134 { 1135 return velocityOption_; 1136 } 1137 SetParticleVelocityOptionParticleOption1138 void SetParticleVelocityOption(VelocityProperty& velocityOption) 1139 { 1140 velocityOption_ = velocityOption; 1141 } 1142 GetParticleAccelerationOptionParticleOption1143 const std::optional<AccelerationProperty>& GetParticleAccelerationOption() const 1144 { 1145 return accelerationOption_; 1146 } 1147 SetParticleAccelerationOptionParticleOption1148 void SetParticleAccelerationOption(AccelerationProperty& accelerationOption) 1149 { 1150 accelerationOption_ = accelerationOption; 1151 } 1152 GetParticleSpinOptionParticleOption1153 const std::optional<ParticleFloatPropertyOption>& GetParticleSpinOption() const 1154 { 1155 return spinOption_; 1156 } 1157 SetParticleSpinOptionParticleOption1158 void SetParticleSpinOption(ParticleFloatPropertyOption& spinOption) 1159 { 1160 spinOption_ = spinOption; 1161 } 1162 1163 bool operator==(const ParticleOption& other) const 1164 { 1165 return (emitter_ == other.GetEmitterOption()) && (colorOption_ == other.GetParticleColorOption()) && 1166 (opacityOption_ == other.GetParticleOpacityOption()) && 1167 (scaleOption_ == other.GetParticleScaleOption()) && 1168 (velocityOption_ == other.GetParticleVelocityOption()) && 1169 (accelerationOption_ == other.GetParticleAccelerationOption()) && 1170 (spinOption_ == other.GetParticleSpinOption()); 1171 } 1172 ToStringParticleOption1173 std::string ToString() const 1174 { 1175 std::string str; 1176 str.append("emitter: [").append(emitter_.ToString()).append("]"); 1177 str.append("colorOption: [").append(colorOption_.has_value() ? colorOption_->ToString() : "NA").append("]"); 1178 str.append("opacityOption: [") 1179 .append(opacityOption_.has_value() ? opacityOption_->ToString() : "NA") 1180 .append("]"); 1181 str.append("scaleOption: [").append(scaleOption_.has_value() ? scaleOption_->ToString() : "NA").append("]"); 1182 str.append("velocityOption: [") 1183 .append(velocityOption_.has_value() ? velocityOption_->ToString() : "NA") 1184 .append("]"); 1185 str.append("accelerationOption: [") 1186 .append(accelerationOption_.has_value() ? accelerationOption_->ToString() : "NA") 1187 .append("]"); 1188 str.append("spinOption: [").append(spinOption_.has_value() ? spinOption_->ToString() : "NA").append("]"); 1189 return str; 1190 } 1191 1192 private: 1193 EmitterOption emitter_; 1194 std::optional<ParticleColorPropertyOption> colorOption_; 1195 std::optional<ParticleFloatPropertyOption> opacityOption_; 1196 std::optional<ParticleFloatPropertyOption> scaleOption_; 1197 std::optional<VelocityProperty> velocityOption_; 1198 std::optional<AccelerationProperty> accelerationOption_; 1199 std::optional<ParticleFloatPropertyOption> spinOption_; 1200 }; 1201 } // namespace OHOS::Ace::NG 1202 #endif