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_TRACK_RENDER_TRACK_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TRACK_RENDER_TRACK_H
18 
19 #include <vector>
20 
21 #include "core/animation/animator.h"
22 #include "core/animation/curve_animation.h"
23 #include "core/components/progress/progress_theme.h"
24 #include "core/components/track/track_component.h"
25 #include "core/pipeline/base/render_component.h"
26 #include "core/pipeline/base/render_node.h"
27 
28 namespace OHOS::Ace {
29 
30 struct RenderRingInfo {
31     double radius = 0.0;
32     Gradient gradient;
33     Color color;
34     Offset center;
35     double startDegree = 0.0;
36     double sweepDegree = 0.0;
37     double thickness = 0.0;
38     double clockwise = 1.0;
39 
40     double scaleStrokeWidth = 0.0;
41     int32_t totalScaleNumber = 0;
42 };
43 
44 class RenderTrack : public RenderNode {
45     DECLARE_ACE_TYPE(RenderTrack, RenderNode);
46 
47 public:
48     static RefPtr<RenderNode> Create();
49 
50     void Update(const RefPtr<Component>& component) override;
51 
52     void PerformLayout() override;
53 
54     void UpdateAnimation();
55 
56     virtual Size Measure();
57 
GetSelectColor()58     Color GetSelectColor() const
59     {
60         return selectColor_;
61     }
62 
SetSelectColor(const Color & color)63     void SetSelectColor(const Color& color)
64     {
65         selectColor_ = color;
66     }
67 
GetBackgroundColor()68     Color GetBackgroundColor() const
69     {
70         return backgroundColor_;
71     }
72 
SetBackgroundColor(const Color & color)73     void SetBackgroundColor(const Color& color)
74     {
75         backgroundColor_ = color;
76     }
77 
GetCachedColor()78     Color GetCachedColor() const
79     {
80         return cachedColor_;
81     }
82 
SetCachedColor(const Color & color)83     void SetCachedColor(const Color& color)
84     {
85         cachedColor_ = color;
86     }
87 
GetGradient()88     const Gradient& GetGradient() const
89     {
90         return selectGradient_;
91     }
92 
GetTotalRatio()93     double GetTotalRatio() const
94     {
95         return totalRatio_;
96     }
97 
SetTotalRatio(const double ratio)98     void SetTotalRatio(const double ratio)
99     {
100         if (!playAnimation_) {
101             totalRatio_ = ratio;
102         } else if (!NearEqual(ratio, prevousPercentValue_)) {
103             animationDuring_ = std::chrono::steady_clock::now() - prevoiusUpdateTime_;
104             percentChange_ = ratio - prevousPercentValue_;
105             prevousPercentValue_ = ratio;
106             prevoiusUpdateTime_ = std::chrono::steady_clock::now();
107             needUpdateAnimation_ = true;
108         }
109     }
110 
GetCachedRatio()111     double GetCachedRatio() const
112     {
113         return cachedRatio_;
114     }
115 
SetCachedRatio(const double ratio)116     void SetCachedRatio(const double ratio)
117     {
118         cachedRatio_ = ratio;
119     }
120 
GetRingTrackInfo()121     const RenderRingInfo& GetRingTrackInfo() const
122     {
123         return paintData_;
124     }
125 
GetTrackThickness()126     double GetTrackThickness() const
127     {
128         return paintData_.thickness;
129     }
130 
GetSliderMode()131     SliderMode GetSliderMode() const
132     {
133         return sliderMode_;
134     }
135 
SetSliderMode(SliderMode mode)136     void SetSliderMode(SliderMode mode)
137     {
138         sliderMode_ = mode;
139     }
140 
GetSliderSteps()141     double GetSliderSteps() const
142     {
143         return sliderSteps_;
144     }
145 
SetSliderSteps(double steps)146     void SetSliderSteps(double steps)
147     {
148         sliderSteps_ = steps;
149     }
150 
SetAnimationPlay(bool playAnimation)151     void SetAnimationPlay(bool playAnimation)
152     {
153         playAnimation_ = playAnimation;
154     }
155 
156     void Dump() override;
157 
158 protected:
159     std::string markedText_;
160     Color markedTextColor_;
161     RenderRingInfo paintData_;
162     bool showIndicator_ = false;
163     std::vector<Color> colors_;
164     std::vector<double> weights_;
165     bool leftToRight_ = true;
166     RefPtr<ProgressTheme> theme_;
167     SliderMode sliderMode_ = SliderMode::OUTSET;
168     double sliderSteps_ = 0.0;
169 
170     RefPtr<Animator> progressTransitionController_;
171     RefPtr<Animator> scanHaloController_;
172     double prevousPercentValue_ = 0.0;
173     double percentChange_ = 0.0;
174     std::chrono::steady_clock::time_point prevoiusUpdateTime_ = std::chrono::steady_clock::now();
175     std::chrono::duration<double> animationDuring_;
176     bool needUpdateAnimation_ = false;
177     bool playAnimation_ = false;
178     bool isReverse_ = false;
179     double scanHighLightValue_ = 0.0;
180 
181     Color selectColor_;
182     Gradient selectGradient_;
183     Color backgroundColor_;
184     Color cachedColor_;
185     double totalRatio_ = 0.0;
186     double cachedRatio_ = 0.0;
187     Dimension thickness_;
188     Dimension scaleStrokeWidth_;
189     Axis direction_ = Axis::HORIZONTAL;
190 };
191 
192 class RenderCircleTrack : public RenderTrack {
193     DECLARE_ACE_TYPE(RenderCircleTrack, RenderTrack);
194 
195 public:
196     static RefPtr<RenderNode> Create();
197 
198     Size Measure() override;
199 
200     void Dump() override;
201 };
202 
203 class RenderScaleRingTrack : public RenderTrack {
204     DECLARE_ACE_TYPE(RenderScaleRingTrack, RenderTrack);
205 
206 public:
207     static RefPtr<RenderNode> Create();
208 
209     Size Measure() override;
210 
211     void Dump() override;
212 };
213 
214 class RenderArcTrack : public RenderTrack {
215     DECLARE_ACE_TYPE(RenderArcTrack, RenderTrack);
216 
217 public:
218     static RefPtr<RenderNode> Create();
219 
220     Size Measure() override;
221 
222     void Dump() override;
223 };
224 
225 class RenderMoonTrack : public RenderTrack {
226     DECLARE_ACE_TYPE(RenderMoonTrack, RenderTrack);
227 
228 public:
229     static RefPtr<RenderNode> Create();
230 
231     Size Measure() override;
232 
233     void Dump() override;
234 };
235 
236 class RenderCapsuleTrack : public RenderTrack {
237     DECLARE_ACE_TYPE(RenderCapsuleTrack, RenderTrack);
238 
239 public:
240     static RefPtr<RenderNode> Create();
241 
242     Size Measure() override;
243 
244     void Dump() override;
245 };
246 
247 } // namespace OHOS::Ace
248 
249 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TRACK_RENDER_TRACK_H
250