1 /*
2 * Copyright (c) 2021 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_DECLARATION_IMAGE_ANIMATOR_IMAGE_ANIMATOR_DECLARATION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_IMAGE_ANIMATOR_IMAGE_ANIMATOR_DECLARATION_H
18 
19 #include <string>
20 #include "core/components/common/properties/border.h"
21 #include "core/components/declaration/common/declaration.h"
22 #include "frameworks/bridge/common/dom/dom_type.h"
23 #include "frameworks/core/animation/animator.h"
24 
25 namespace OHOS::Ace {
26 
27 struct ImageProperties {
28     std::string src;
29     RefPtr<PixelMap> pixelMap;
30     std::string bundleName;
31     std::string moduleName;
32     CalcDimension width;
33     CalcDimension height;
34     CalcDimension top;
35     CalcDimension left;
36     int32_t duration = 0;
37 };
38 
39 struct ImageAnimatorAttribute : Attribute {
40     FillMode fillMode = FillMode::FORWARDS;
41     Animator::Status status = Animator::Status::IDLE;
42     int32_t iteration = -1;
43     int32_t duration = 0;
44     int32_t preDecode = 0;
45     bool isReverse = false;
46     bool isFixedSize = true;
47     std::vector<ImageProperties> images;
48 };
49 
50 class ImageAnimatorDeclaration : public Declaration {
51     DECLARE_ACE_TYPE(ImageAnimatorDeclaration, Declaration);
52 
53 public:
54     ImageAnimatorDeclaration() = default;
55     ~ImageAnimatorDeclaration() override = default;
56 
SetFillMode(FillMode fillMode)57     void SetFillMode(FillMode fillMode)
58     {
59         auto& attribute = MaybeResetAttribute<ImageAnimatorAttribute>(AttributeTag::SPECIALIZED_ATTR);
60         attribute.fillMode = fillMode;
61     }
62 
SetStatus(Animator::Status status)63     void SetStatus(Animator::Status status)
64     {
65         auto& attribute = MaybeResetAttribute<ImageAnimatorAttribute>(AttributeTag::SPECIALIZED_ATTR);
66         attribute.status = status;
67     }
68 
SetIteration(int32_t iteration)69     void SetIteration(int32_t iteration)
70     {
71         auto& attribute = MaybeResetAttribute<ImageAnimatorAttribute>(AttributeTag::SPECIALIZED_ATTR);
72         attribute.iteration = iteration;
73     }
74 
SetDuration(int32_t duration)75     void SetDuration(int32_t duration)
76     {
77         auto& attribute = MaybeResetAttribute<ImageAnimatorAttribute>(AttributeTag::SPECIALIZED_ATTR);
78         attribute.duration = duration;
79     }
80 
SetIsReverse(bool isReverse)81     void SetIsReverse(bool isReverse)
82     {
83         auto& attribute = MaybeResetAttribute<ImageAnimatorAttribute>(AttributeTag::SPECIALIZED_ATTR);
84         attribute.isReverse = isReverse;
85     }
86 
SetIsFixedSize(bool isFixedSize)87     void SetIsFixedSize(bool isFixedSize)
88     {
89         auto& attribute = MaybeResetAttribute<ImageAnimatorAttribute>(AttributeTag::SPECIALIZED_ATTR);
90         attribute.isFixedSize = isFixedSize;
91     }
92 
SetBorder(const Border & border)93     void SetBorder(const Border& border)
94     {
95         border_ = border;
96     }
97 
SetImageProperties(const std::vector<ImageProperties> & images)98     void SetImageProperties(const std::vector<ImageProperties>& images)
99     {
100         auto& attribute = MaybeResetAttribute<ImageAnimatorAttribute>(AttributeTag::SPECIALIZED_ATTR);
101         attribute.images = images;
102     }
103 
SetPreDecode(int32_t preDecode)104     void SetPreDecode(int32_t preDecode)
105     {
106         auto& attribute = MaybeResetAttribute<ImageAnimatorAttribute>(AttributeTag::SPECIALIZED_ATTR);
107         attribute.preDecode = preDecode;
108     }
109 
GetFillMode()110     FillMode GetFillMode() const
111     {
112         auto& attribute = static_cast<ImageAnimatorAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
113         return attribute.fillMode;
114     }
115 
GetStatus()116     Animator::Status GetStatus() const
117     {
118         auto& attribute = static_cast<ImageAnimatorAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
119         return attribute.status;
120     }
121 
GetIteration()122     int32_t GetIteration() const
123     {
124         auto& attribute = static_cast<ImageAnimatorAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
125         return attribute.iteration;
126     }
127 
GetDuration()128     int32_t GetDuration() const
129     {
130         auto& attribute = static_cast<ImageAnimatorAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
131         return attribute.duration;
132     }
133 
GetPreDecode()134     int32_t GetPreDecode() const
135     {
136         auto& attribute = static_cast<ImageAnimatorAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
137         return attribute.preDecode;
138     }
139 
GetIsReverse()140     bool GetIsReverse() const
141     {
142         auto& attribute = static_cast<ImageAnimatorAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
143         return attribute.isReverse;
144     }
145 
GetIsFixedSize()146     bool GetIsFixedSize() const
147     {
148         auto& attribute = static_cast<ImageAnimatorAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
149         return attribute.isFixedSize;
150     }
151 
GetBorder()152     const Border& GetBorder() const
153     {
154         return border_;
155     }
156 
GetImageProperties()157     const std::vector<ImageProperties>& GetImageProperties() const
158     {
159         auto& attribute = static_cast<ImageAnimatorAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
160         return attribute.images;
161     }
162 
163 protected:
164     void InitSpecialized() override;
165 
166 private:
167     Border border_;
168 };
169 
170 } // namespace OHOS::Ace
171 
172 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_IMAGE_ANIMATOR_IMAGE_ANIMATOR_DECLARATION_H
173