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_TRACK_TRACK_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TRACK_TRACK_COMPONENT_H
18 
19 #include <vector>
20 
21 #include "base/geometry/dimension.h"
22 #include "core/components/common/properties/color.h"
23 #include "core/components/common/properties/decoration.h"
24 #include "core/pipeline/base/component.h"
25 #include "core/pipeline/base/render_component.h"
26 
27 namespace OHOS::Ace {
28 
29 enum class SliderMode {
30     OUTSET, // block on track, track is thin
31     INSET,  // block inside track, track is rough
32     NONE,   // no block slider
33     CAPSULE, // capsule slider.
34 };
35 
36 inline const Dimension TRACK_HEIGHT_DP = Dimension(2.0, DimensionUnit::VP);
37 
38 class RingTrackInfo : public virtual AceType {
39     DECLARE_ACE_TYPE(RingTrackInfo, AceType);
40 
41 public:
42     RingTrackInfo() = default;
43     ~RingTrackInfo() override = default;
44 
SetStartDegree(double startDegree)45     void SetStartDegree(double startDegree)
46     {
47         startDegree_ = startDegree;
48     }
49 
SetSweepDegree(double sweepDegree)50     void SetSweepDegree(double sweepDegree)
51     {
52         sweepDegree_ = sweepDegree;
53     }
54 
SetClockwise(bool clockwise)55     void SetClockwise(bool clockwise)
56     {
57         if (clockwise) {
58             clockwise_ = 1.0;
59         } else {
60             clockwise_ = -1.0;
61         }
62     }
63 
SetDimension(const Dimension & thickness)64     void SetDimension(const Dimension& thickness)
65     {
66         trackThickness_ = thickness;
67     }
68 
SetScaleNumber(int32_t number)69     void SetScaleNumber(int32_t number)
70     {
71         totalScaleNumber_ = number;
72     }
73 
SetScaleWidth(const Dimension & width)74     void SetScaleWidth(const Dimension& width)
75     {
76         scaleWidth_ = width;
77     }
78 
SetRadius(double radius)79     void SetRadius(double radius)
80     {
81         radius_ = radius;
82     }
83 
SetX(double x)84     void SetX(double x)
85     {
86         coordinateX_ = x;
87     }
88 
SetY(double y)89     void SetY(double y)
90     {
91         coordinateY_ = y;
92     }
93 
GetRadius()94     double GetRadius() const
95     {
96         return radius_;
97     }
98 
GetX()99     double GetX() const
100     {
101         return coordinateX_;
102     }
103 
GetY()104     double GetY() const
105     {
106         return coordinateY_;
107     }
108 
GetStartDegree()109     double GetStartDegree() const
110     {
111         return startDegree_;
112     }
113 
GetSweepDegree()114     double GetSweepDegree() const
115     {
116         return sweepDegree_;
117     }
118 
GetTrackThickness()119     const Dimension& GetTrackThickness() const
120     {
121         return trackThickness_;
122     }
123 
GetClockwiseValue()124     double GetClockwiseValue() const
125     {
126         return clockwise_;
127     }
128 
GetScaleNumber()129     int32_t GetScaleNumber() const
130     {
131         return totalScaleNumber_;
132     }
133 
GetScaleWidth()134     const Dimension& GetScaleWidth() const
135     {
136         return scaleWidth_;
137     }
138 
139 private:
140     Dimension trackThickness_;
141 
142     // degree from 0 to 360
143     double startDegree_ = 0.0;
144     double sweepDegree_ = 360.0;
145     // clockwise is 1.0 and counter-clockwise is -1.0
146     double clockwise_ = 1.0;
147 
148     double coordinateX_ = -1.0;
149     double coordinateY_ = -1.0;
150     double radius_ = -1.0;
151 
152     // the number of scale in ring. default number is 120
153     int32_t totalScaleNumber_ = 120;
154     Dimension scaleWidth_;
155 };
156 
157 class TrackComponent : public RenderComponent {
158     DECLARE_ACE_TYPE(TrackComponent, RenderComponent);
159 
160 public:
161     TrackComponent() = default;
162     ~TrackComponent() override = default;
163 
CreateElement()164     RefPtr<Element> CreateElement() override
165     {
166         // never use element to create element tree.
167         return nullptr;
168     }
169 
GetSelectColor()170     const Color& GetSelectColor() const
171     {
172         return selectColor_;
173     }
174 
SetSelectColor(const Color & color)175     void SetSelectColor(const Color& color)
176     {
177         selectColor_ = color;
178     }
179 
GetSelectGradient()180     const Gradient& GetSelectGradient() const
181     {
182         return selectGradient_;
183     }
184 
SetSelectGradient(const Gradient & color)185     void SetSelectGradient(const Gradient& color)
186     {
187         selectGradient_ = color;
188     }
189 
GetCachedColor()190     const Color& GetCachedColor() const
191     {
192         return cachedColor_;
193     }
194 
SetCachedColor(const Color & color)195     void SetCachedColor(const Color& color)
196     {
197         cachedColor_ = color;
198     }
199 
GetBackgroundColor()200     const Color& GetBackgroundColor() const
201     {
202         return backgroundColor_;
203     }
204 
SetBackgroundColor(const Color & color)205     void SetBackgroundColor(const Color& color)
206     {
207         backgroundColor_ = color;
208     }
209 
GetTrackThickness()210     const Dimension& GetTrackThickness() const
211     {
212         return trackPaintData_->GetTrackThickness();
213     }
214 
GetStartDegree()215     double GetStartDegree() const
216     {
217         return trackPaintData_->GetStartDegree();
218     }
219 
GetSweepDegree()220     double GetSweepDegree() const
221     {
222         return trackPaintData_->GetSweepDegree();
223     }
224 
GetClockwiseValue()225     double GetClockwiseValue() const
226     {
227         return trackPaintData_->GetClockwiseValue();
228     }
229 
GetScaleNumber()230     int32_t GetScaleNumber() const
231     {
232         return trackPaintData_->GetScaleNumber();
233     }
234 
GetScaleWidth()235     const Dimension& GetScaleWidth() const
236     {
237         return trackPaintData_->GetScaleWidth();
238     }
239 
SetScaleNumber(int32_t number)240     void SetScaleNumber(int32_t number)
241     {
242         trackPaintData_->SetScaleNumber(number);
243     }
244 
SetScaleWidth(const Dimension & width)245     void SetScaleWidth(const Dimension& width)
246     {
247         trackPaintData_->SetScaleWidth(width);
248     }
249 
SetTrackThickness(const Dimension & thickness)250     void SetTrackThickness(const Dimension& thickness)
251     {
252         trackPaintData_->SetDimension(thickness);
253     }
254 
SetIndicatorFlag(bool flag)255     void SetIndicatorFlag(bool flag)
256     {
257         showIndicator_ = flag;
258     }
259 
SetRadius(double radius)260     void SetRadius(double radius)
261     {
262         trackPaintData_->SetRadius(radius);
263     }
264 
SetCenterX(double x)265     void SetCenterX(double x)
266     {
267         trackPaintData_->SetX(x);
268     }
269 
SetCenterY(double y)270     void SetCenterY(double y)
271     {
272         trackPaintData_->SetY(y);
273     }
274 
GetIndicatorFlag()275     bool GetIndicatorFlag() const
276     {
277         return showIndicator_;
278     }
279 
SetSectionsStyle(const std::vector<Color> & colors,const std::vector<double> & weights)280     void SetSectionsStyle(const std::vector<Color>& colors, const std::vector<double>& weights)
281     {
282         colors_ = colors;
283         weights_ = weights;
284     }
285 
GetSectionsColors()286     const std::vector<Color>& GetSectionsColors() const
287     {
288         return colors_;
289     }
290 
GetSectionsWeights()291     const std::vector<double>& GetSectionsWeights() const
292     {
293         return weights_;
294     }
295 
GetTrackInfo()296     RefPtr<RingTrackInfo> GetTrackInfo() const
297     {
298         return trackPaintData_;
299     }
300 
GetRadius()301     double GetRadius() const
302     {
303         return trackPaintData_->GetRadius();
304     }
305 
GetCenterX()306     double GetCenterX() const
307     {
308         return trackPaintData_->GetX();
309     }
310 
GetCenterY()311     double GetCenterY() const
312     {
313         return trackPaintData_->GetY();
314     }
315 
GetShowAnimation()316     bool GetShowAnimation() const
317     {
318         return showAnimation_;
319     }
320 
SetShowAnimation(bool isAnimation)321     void SetShowAnimation(bool isAnimation)
322     {
323         showAnimation_ = isAnimation;
324     }
325 
SetLabelMarkedText(std::string markedText)326     void SetLabelMarkedText(std::string markedText)
327     {
328         markedText_ = markedText;
329     }
330 
SetLabelMarkedColor(Color markedColor)331     void SetLabelMarkedColor(Color markedColor)
332     {
333         markedTextColor_ = markedColor;
334     }
335 
GetLabelMarkedText()336     std::string GetLabelMarkedText()
337     {
338         return markedText_;
339     }
340 
GetLabelMarkedColor()341     Color GetLabelMarkedColor()
342     {
343         return markedTextColor_;
344     }
345 
GetDirection()346     Axis GetDirection() const
347     {
348         return axis_;
349     }
350 
SetDirection(Axis axis)351     void SetDirection(Axis axis)
352     {
353         axis_ = axis;
354     }
355 
IsReverse()356     bool IsReverse() const
357     {
358         return isReverse_;
359     }
360 
SetReverse(bool isReverse)361     void SetReverse(bool isReverse)
362     {
363         isReverse_ = isReverse;
364     }
365 
366 private:
367     std::string markedText_;
368     Color markedTextColor_;
369     Color selectColor_;
370     Gradient selectGradient_;
371     Color cachedColor_;
372     Color backgroundColor_;
373     // the thickness of the track. when the track is circle, there is no effect.
374     RefPtr<RingTrackInfo> trackPaintData_ = AceType::MakeRefPtr<RingTrackInfo>();
375     std::vector<Color> colors_;
376     std::vector<double> weights_;
377     bool showIndicator_ = false;
378     bool showAnimation_ = true;
379     bool isReverse_ = false;
380     Axis axis_ = Axis::HORIZONTAL;
381 };
382 
383 class LinearTrack : public TrackComponent {
384     DECLARE_ACE_TYPE(LinearTrack, TrackComponent);
385 
386 public:
387     LinearTrack() = default;
388     ~LinearTrack() override = default;
389 
390     RefPtr<RenderNode> CreateRenderNode() override;
391 };
392 
393 class CircularTrack : public TrackComponent {
394     DECLARE_ACE_TYPE(CircularTrack, TrackComponent);
395 
396 public:
397     CircularTrack() = default;
398     ~CircularTrack() override = default;
399 
400     RefPtr<RenderNode> CreateRenderNode() override;
401 };
402 
403 class ScaleRingTrack : public TrackComponent {
404     DECLARE_ACE_TYPE(ScaleRingTrack, TrackComponent);
405 
406 public:
407     ScaleRingTrack() = default;
408     ~ScaleRingTrack() override = default;
409 
410     RefPtr<RenderNode> CreateRenderNode() override;
411 };
412 
413 class ArcTrack : public TrackComponent {
414     DECLARE_ACE_TYPE(ArcTrack, TrackComponent);
415 
416 public:
417     ArcTrack() = default;
418     ~ArcTrack() override = default;
419 
420     RefPtr<RenderNode> CreateRenderNode() override;
421 };
422 
423 class MoonTrack : public TrackComponent {
424     DECLARE_ACE_TYPE(MoonTrack, TrackComponent);
425 
426 public:
427     MoonTrack() = default;
428     ~MoonTrack() override = default;
429 
430     RefPtr<RenderNode> CreateRenderNode() override;
431 };
432 
433 class CapsuleTrack : public TrackComponent {
434     DECLARE_ACE_TYPE(CapsuleTrack, TrackComponent);
435 
436 public:
437     CapsuleTrack() = default;
438     ~CapsuleTrack() override = default;
439 
440     RefPtr<RenderNode> CreateRenderNode() override;
441 };
442 
443 } // namespace OHOS::Ace
444 
445 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TRACK_TRACK_COMPONENT_H
446