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