1 /*
2  * Copyright (c) 2021-2022 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_DATA_PANEL_RENDER_DATA_PANEL_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DATA_PANEL_RENDER_DATA_PANEL_H
18 
19 #include <chrono>
20 
21 #include "core/animation/animator.h"
22 #include "core/animation/curve_animation.h"
23 #include "core/components/data_panel/data_panel_component.h"
24 #include "core/pipeline/base/component.h"
25 #include "core/pipeline/base/render_node.h"
26 
27 namespace OHOS::Ace {
28 
29 struct ArcData {
30     Offset center;
31     double progress = 0.0;
32     double radius = 0.0;
33     double thickness = 0.0;
34     double maxValue = 0.0;
35     Color startColor;
36     Color endColor;
37 
38     double wholeAngle = 360.0;
39     double startAngle = 0.0;
40 };
41 
42 class RenderDataPanel : public RenderNode {
43     DECLARE_ACE_TYPE(RenderDataPanel, RenderNode);
44 
45 public:
46     ~RenderDataPanel() override = default;
47     void Update(const RefPtr<Component>& component) override;
48     static RefPtr<RenderNode> Create();
49     virtual void PlayAnimation() = 0;
50     virtual void StopAnimation() = 0;
GetCloseEffect()51     bool GetCloseEffect() const
52     {
53         return !useEffect_;
54     }
55 
56 protected:
57     RenderDataPanel();
58 
59     const Size Measure();
60     void PerformLayout() override;
61     virtual void PrepareAnimation() = 0;
62     void OnVisibleChanged() override;
63     void OnHiddenChanged(bool hidden) override;
64     void AnimationChanged();
65 
66     MeasureType measureType_ = MeasureType::PARENT;
67     ChartType type_ = ChartType::LINE;
68     Dimension thickness_;
69     Color backgroundTrack_ = Color::FromString("#08000000");
70     bool autoScale_ = false;
71 
72     RefPtr<Animator> animator_;
73     RefPtr<Animator> progressTransitionController_;
74     double rotateAngle_ = 0.0;
75     double sweepDegree_ = 0.0;
76     double percent_ = 0.0;
77     bool useEffect_ = false;
78     bool animationInitialized_ = false;
79     bool isUserSetPlay_ = false;
80     double userAnimationDuration_ = -1.0;
81 
82     double previousPercentValue_ = 0.0;
83     double percentChange_ = 0.0;
84     std::chrono::steady_clock::time_point previousUpdateTime_ = std::chrono::steady_clock::now();
85     std::chrono::duration<double> animationDuring_;
86 
87     bool needReplayAnimation_ = false;
88 
89 private:
90     // data panel default height and width
91     Dimension height_;
92     Dimension width_;
93 };
94 
95 class RenderProgressDataPanel : public RenderDataPanel {
96     DECLARE_ACE_TYPE(RenderProgressDataPanel, RenderDataPanel);
97 
98 public:
99     static RefPtr<RenderNode> Create();
100     void Update(const RefPtr<Component>& component) override;
101 
GetStartColor()102     const Color& GetStartColor() const
103     {
104         return startColor_;
105     }
106 
GetEndColor()107     const Color& GetEndColor() const
108     {
109         return endColor_;
110     }
111 
IsRepaintBoundary()112     bool IsRepaintBoundary() const override
113     {
114         return true;
115     }
116 
PlayAnimation()117     void PlayAnimation() override
118     {
119         if (animator_) {
120             animator_->Play();
121             isUserSetPlay_ = true;
122         }
123 
124         if (!isLoading_ && progressTransitionController_) {
125             progressTransitionController_->Play();
126             isUserSetPlay_ = true;
127         }
128     }
129 
StopAnimation()130     void StopAnimation() override
131     {
132         if (animator_) {
133             animator_->Stop();
134             isUserSetPlay_ = false;
135         }
136 
137         if (!isLoading_ && progressTransitionController_) {
138             progressTransitionController_->Stop();
139             isUserSetPlay_ = false;
140         }
141     }
142 
143 protected:
144     void PrepareAnimation() override;
GetProgress()145     double GetProgress() const
146     {
147         return progress_;
148     }
149 
150 private:
151     Color startColor_;
152     Color endColor_;
153     double progress_ = 0.0;
154     bool isLoading_ = false;
155 
156     RefPtr<CurveAnimation<double>> animation_;
157     RefPtr<CurveAnimation<double>> transitionAnimation_;
158 };
159 
160 class RenderPercentageDataPanel : public RenderDataPanel {
161     DECLARE_ACE_TYPE(RenderPercentageDataPanel, RenderDataPanel);
162 
163 public:
164     static RefPtr<RenderNode> Create();
165     void Update(const RefPtr<Component>& component) override;
166 
PlayAnimation()167     void PlayAnimation() override
168     {
169         if (animator_) {
170             animator_->Play();
171             isUserSetPlay_ = true;
172         }
173     }
174 
StopAnimation()175     void StopAnimation() override
176     {
177         if (animator_) {
178             animator_->Pause();
179             isUserSetPlay_ = false;
180         }
181     }
182 
GetSegments()183     const std::vector<Segment>& GetSegments() const
184     {
185         return segments_;
186     }
187 
GetMaxValue()188     double GetMaxValue() const
189     {
190         return maxValue_;
191     }
192 
GetDataPanelType()193     ChartType GetDataPanelType() const
194     {
195         return panelType_;
196     }
197 
198 protected:
199     void PrepareAnimation() override;
200 
GetTotalValue()201     double GetTotalValue() const
202     {
203         return totalValue_;
204     }
205 
GetStartDegree()206     double GetStartDegree() const
207     {
208         return startDegree_;
209     }
210 
GetSweepDegree()211     double GetSweepDegree() const
212     {
213         return sweepDegree_;
214     }
215 
216     double animationPercent_ = 0.0;
217 
218 private:
219     ChartType panelType_ = ChartType::PROGRESS;
220     double startDegree_ = 0.0;
221     double sweepDegree_ = 360.0;
222     std::vector<Segment> segments_;
223     double totalValue_ = 0.0;
224     double maxValue_ = 0.0;
225     RefPtr<CurveAnimation<double>> animation_;
226 };
227 
228 } // namespace OHOS::Ace
229 
230 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DATA_PANEL_RENDER_DATA_PANEL_H
231