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_PATTERNS_PARTICLE_MODEL_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PATTERNS_PARTICLE_MODEL_H
18 #include <functional>
19 #include <list>
20 #include <memory>
21 #include <mutex>
22 
23 #include "base/geometry/ng/vector.h"
24 #include "base/utils/macros.h"
25 #include "core/components_ng/base/frame_node.h"
26 #include "core/components_ng/property/particle_property.h"
27 namespace OHOS::Ace {
28 
29 enum class ParticleDisturbanceShapeType :uint32_t { RECT, CIRCLE, ELLIPSE };
30 
31 struct ParticleDisturbance {
32     float strength = 0.0f;
33     ParticleDisturbanceShapeType shape = ParticleDisturbanceShapeType::RECT;
34     int  size[2] = {0, 0};
35     int  position[2] = {0, 0};
36     int feather = 0;
37     float noiseScale = 1.0f;
38     float noiseFrequency = 1.0f;
39     float noiseAmplitude = 1.0f;
40 
41     bool operator!=(const ParticleDisturbance& data) const
42     {
43         return !(NearEqual(strength, data.strength) && shape == data.shape && size[0] == data.size[0] &&
44                  size[1] == data.size[1] && position[0] == data.position[0] && position[1] == data.position[1] &&
45                  feather == data.feather && NearEqual(noiseScale, data.noiseScale) &&
46                  NearEqual(noiseFrequency, data.noiseFrequency) && NearEqual(noiseAmplitude, data.noiseAmplitude));
47     }
48 };
49 
50 struct EmitterProperty {
51     uint32_t index = 0;
52     std::optional<NG::VectorF> position;
53     std::optional<NG::VectorF> size;
54     std::optional<uint32_t> emitRate;
55 };
56 
57 class ACE_EXPORT ParticleModel {
58 public:
59     static ParticleModel* GetInstance();
60     virtual ~ParticleModel() = default;
61     virtual void Create(std::list<NG::ParticleOption>& arrayValue) = 0;
62     virtual void DisturbanceField(const std::vector<ParticleDisturbance>& disturbanceArray) = 0;
63     virtual void updateEmitter(std::vector<EmitterProperty>& property) = 0;
64 
65 private:
66     static std::unique_ptr<ParticleModel> instance_;
67     static std::mutex mutex_;
68 };
69 } // namespace OHOS::Ace
70 #endif