• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_NG_PATTERN_RATING_RATING_MODIFIER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_RATING_RATING_MODIFIER_H
18 
19 #include "base/memory/referenced.h"
20 #include "base/utils/utils.h"
21 #include "core/components/common/properties/animation_option.h"
22 #include "core/components/rating/rating_theme.h"
23 #include "core/components_ng/base/modifier.h"
24 #include "core/components_ng/render/animation_utils.h"
25 #include "core/image/image_source_info.h"
26 #include "core/pipeline/pipeline_base.h"
27 
28 namespace OHOS::Ace::NG {
29 class RatingModifier : public ContentModifier {
30     DECLARE_ACE_TYPE(RatingModifier, ContentModifier);
31 
32 public:
33     RatingModifier();
34     ~RatingModifier() override = default;
35 
36     enum class RatingAnimationType {
37         NONE = 0,
38         HOVER,
39         HOVERTOPRESS,
40         PRESSTOHOVER,
41         PRESS,
42     };
43 
44     void PaintBoard(DrawingContext& context);
45     void PaintStar(DrawingContext& context);
46     void PaintReverseStar(DrawingContext& context);
47 
48     void onDraw(DrawingContext& context) override;
49 
UpdateCanvasImage(const RefPtr<CanvasImage> & foregroundImageCanvas,const RefPtr<CanvasImage> & secondaryImageCanvas,const RefPtr<CanvasImage> & backgroundImageCanvas,const ImagePaintConfig & foregroundConfig,const ImagePaintConfig & secondaryConfig,const ImagePaintConfig & backgroundConfig)50     void UpdateCanvasImage(const RefPtr<CanvasImage>& foregroundImageCanvas,
51         const RefPtr<CanvasImage>& secondaryImageCanvas, const RefPtr<CanvasImage>& backgroundImageCanvas,
52         const ImagePaintConfig& foregroundConfig, const ImagePaintConfig& secondaryConfig,
53         const ImagePaintConfig& backgroundConfig)
54     {
55         SetNeedDraw(true);
56         foregroundImageCanvas_ = foregroundImageCanvas;
57         secondaryImageCanvas_ = secondaryImageCanvas;
58         backgroundImageCanvas_ = backgroundImageCanvas;
59         foregroundImageCanvas_->SetPaintConfig(foregroundConfig);
60         secondaryImageCanvas_->SetPaintConfig(secondaryConfig);
61         backgroundImageCanvas_->SetPaintConfig(backgroundConfig);
62     }
63 
UpdateImageSourceInfo(const ImageSourceInfo & foreground,const ImageSourceInfo & secondary,const ImageSourceInfo & background)64     void UpdateImageSourceInfo(
65         const ImageSourceInfo& foreground, const ImageSourceInfo& secondary, const ImageSourceInfo& background)
66     {
67         foreground_ = foreground;
68         secondary_ = secondary;
69         background_ = background;
70     }
71 
JudgeImageSourceInfo(const ImageSourceInfo & foreground,const ImageSourceInfo & secondary,const ImageSourceInfo & background,const ImagePaintConfig & foregroundConfig)72     bool JudgeImageSourceInfo(const ImageSourceInfo& foreground, const ImageSourceInfo& secondary,
73         const ImageSourceInfo& background, const ImagePaintConfig& foregroundConfig)
74     {
75         if (foreground_ != foreground) {
76             return true;
77         }
78         if (secondary_ != secondary) {
79             return true;
80         }
81         if (background_ != background) {
82             return true;
83         }
84         CHECK_NULL_RETURN(foregroundImageCanvas_, true);
85         if (foregroundImageCanvas_->GetPaintConfig().dstRect_ != foregroundConfig.dstRect_) {
86             return true;
87         }
88         if (foregroundImageCanvas_->GetPaintConfig().scaleX_ != foregroundConfig.scaleX_ ||
89             foregroundImageCanvas_->GetPaintConfig().scaleY_ != foregroundConfig.scaleY_) {
90             return true;
91         }
92         // No need to update three CanvasImages
93         return false;
94     }
95 
SetNeedDraw(bool flag)96     void SetNeedDraw(bool flag)
97     {
98         if (needDraw_) {
99             needDraw_->Set(flag);
100         }
101     }
102 
SetBoardColor(LinearColor color,int32_t duratuion,const RefPtr<CubicCurve> & curve)103     void SetBoardColor(LinearColor color, int32_t duratuion, const RefPtr<CubicCurve>& curve)
104     {
105         if (boardColor_) {
106             AnimationOption option = AnimationOption();
107             option.SetDuration(duratuion);
108             option.SetCurve(curve);
109             AnimationUtils::Animate(option, [&]() { boardColor_->Set(color); });
110         }
111     }
112 
SetContentOffset(OffsetF contentOffset)113     void SetContentOffset(OffsetF contentOffset)
114     {
115         if (contentOffset_) {
116             contentOffset_->Set(contentOffset);
117         }
118     }
119 
SetContentSize(SizeF contentSize)120     void SetContentSize(SizeF contentSize)
121     {
122         if (contentSize_) {
123             contentSize_->Set(contentSize);
124         }
125     }
126 
SetDrawScore(double drawScore)127     void SetDrawScore(double drawScore)
128     {
129         if (drawScore_) {
130             drawScore_->Set(static_cast<float>(drawScore));
131         }
132     }
133 
SetStepSize(double stepSize)134     void SetStepSize(double stepSize)
135     {
136         if (stepSize_) {
137             stepSize_->Set(static_cast<float>(stepSize));
138         }
139     }
140 
SetTouchStar(int32_t touchStar)141     void SetTouchStar(int32_t touchStar)
142     {
143         if (touchStar < 0 || touchStar >= starNum_->Get() || touchStar_->Get() != touchStar) {
144             SetHoverState(RatingAnimationType::NONE);
145         }
146         if (touchStar_) {
147             touchStar_->Set(touchStar);
148         }
149     }
150 
SetStartNum(int32_t starNum)151     void SetStartNum(int32_t starNum)
152     {
153         if (starNum_) {
154             starNum_->Set(starNum);
155         }
156     }
157 
SetHoverState(const RatingAnimationType & state)158     void SetHoverState(const RatingAnimationType& state)
159     {
160         if (state_ == state) {
161             return;
162         }
163         state_ = state;
164         auto pipeline = PipelineBase::GetCurrentContext();
165         CHECK_NULL_VOID(pipeline);
166         auto ratingTheme = pipeline->GetTheme<RatingTheme>();
167         CHECK_NULL_VOID(ratingTheme);
168         auto hoverDuration = static_cast<int32_t>(ratingTheme->GetHoverAnimationDuration());
169         auto pressDuration = static_cast<int32_t>(ratingTheme->GetPressAnimationDuration());
170         switch (state) {
171             case RatingAnimationType::HOVER:
172                 SetBoardColor(LinearColor(ratingTheme->GetHoverColor()), hoverDuration, Curves::FRICTION);
173                 break;
174             case RatingAnimationType::HOVERTOPRESS:
175                 SetBoardColor(LinearColor(ratingTheme->GetPressColor()), pressDuration, Curves::SHARP);
176                 break;
177             case RatingAnimationType::PRESSTOHOVER:
178                 SetBoardColor(LinearColor(ratingTheme->GetHoverColor()), pressDuration, Curves::SHARP);
179                 break;
180             case RatingAnimationType::PRESS:
181                 SetBoardColor(LinearColor(ratingTheme->GetPressColor()), hoverDuration, Curves::SHARP);
182                 break;
183             case RatingAnimationType::NONE:
184                 SetBoardColor(LinearColor(Color::TRANSPARENT), hoverDuration, Curves::FRICTION);
185                 break;
186             default:
187                 break;
188         }
189     }
190 
SetReverse(bool reverse)191     void SetReverse(bool reverse)
192     {
193         if (reverse_) {
194             reverse_->Set(reverse);
195         }
196     }
197 
198 private:
199     // others
200     RatingAnimationType state_ = RatingAnimationType::NONE;
201     RefPtr<CanvasImage> foregroundImageCanvas_;
202     RefPtr<CanvasImage> secondaryImageCanvas_;
203     RefPtr<CanvasImage> backgroundImageCanvas_;
204     ImageSourceInfo foreground_;
205     ImageSourceInfo secondary_;
206     ImageSourceInfo background_;
207     // non-animatable property
208     RefPtr<PropertyBool> needDraw_;
209     RefPtr<PropertyInt> starNum_;
210     RefPtr<PropertyInt> touchStar_;
211     RefPtr<PropertyFloat> drawScore_;
212     RefPtr<PropertyFloat> stepSize_;
213     RefPtr<PropertyOffsetF> contentOffset_;
214     RefPtr<PropertySizeF> contentSize_;
215     // animatable property
216     RefPtr<AnimatablePropertyColor> boardColor_;
217     RefPtr<PropertyBool> reverse_;
218     ACE_DISALLOW_COPY_AND_MOVE(RatingModifier);
219 };
220 } // namespace OHOS::Ace::NG
221 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_RATING_RATING_MODIFIER_H
222