• 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 
47     void onDraw(DrawingContext& context) override;
48 
UpdateCanvasImage(const RefPtr<CanvasImage> & foregroundImageCanvas,const RefPtr<CanvasImage> & secondaryImageCanvas,const RefPtr<CanvasImage> & backgroundImageCanvas,const ImagePaintConfig & foregroundConfig,const ImagePaintConfig & secondaryConfig,const ImagePaintConfig & backgroundConfig)49     void UpdateCanvasImage(const RefPtr<CanvasImage>& foregroundImageCanvas,
50         const RefPtr<CanvasImage>& secondaryImageCanvas, const RefPtr<CanvasImage>& backgroundImageCanvas,
51         const ImagePaintConfig& foregroundConfig, const ImagePaintConfig& secondaryConfig,
52         const ImagePaintConfig& backgroundConfig)
53     {
54         SetNeedDraw(true);
55         foregroundImageCanvas_ = foregroundImageCanvas;
56         secondaryImageCanvas_ = secondaryImageCanvas;
57         backgroundImageCanvas_ = backgroundImageCanvas;
58         foregroundImageCanvas_->SetPaintConfig(foregroundConfig);
59         secondaryImageCanvas_->SetPaintConfig(secondaryConfig);
60         backgroundImageCanvas_->SetPaintConfig(backgroundConfig);
61     }
62 
UpdateImageSourceInfo(const ImageSourceInfo & foreground,const ImageSourceInfo & secondary,const ImageSourceInfo & background)63     void UpdateImageSourceInfo(
64         const ImageSourceInfo& foreground, const ImageSourceInfo& secondary, const ImageSourceInfo& background)
65     {
66         foreground_ = foreground;
67         secondary_ = secondary;
68         background_ = background;
69     }
70 
JudgeImageSourceInfo(const ImageSourceInfo & foreground,const ImageSourceInfo & secondary,const ImageSourceInfo & background,const ImagePaintConfig & foregroundConfig)71     bool JudgeImageSourceInfo(const ImageSourceInfo& foreground, const ImageSourceInfo& secondary,
72         const ImageSourceInfo& background, const ImagePaintConfig& foregroundConfig)
73     {
74         if (foreground_ != foreground) {
75             return true;
76         }
77         if (secondary_ != secondary) {
78             return true;
79         }
80         if (background_ != background) {
81             return true;
82         }
83         CHECK_NULL_RETURN(foregroundImageCanvas_, true);
84         if (foregroundImageCanvas_->GetPaintConfig().dstRect_ != foregroundConfig.dstRect_) {
85             return true;
86         }
87         if (foregroundImageCanvas_->GetPaintConfig().scaleX_ != foregroundConfig.scaleX_ ||
88             foregroundImageCanvas_->GetPaintConfig().scaleY_ != foregroundConfig.scaleY_) {
89             return true;
90         }
91         // No need to update three CanvasImages
92         return false;
93     }
94 
SetNeedDraw(bool flag)95     void SetNeedDraw(bool flag)
96     {
97         if (needDraw_) {
98             needDraw_->Set(flag);
99         }
100     }
101 
SetBoardColor(LinearColor color,int32_t duratuion,const RefPtr<CubicCurve> & curve)102     void SetBoardColor(LinearColor color, int32_t duratuion, const RefPtr<CubicCurve>& curve)
103     {
104         if (boardColor_) {
105             AnimationOption option = AnimationOption();
106             option.SetDuration(duratuion);
107             option.SetCurve(curve);
108             AnimationUtils::Animate(option, [&]() { boardColor_->Set(color); });
109         }
110     }
111 
SetContentOffset(OffsetF contentOffset)112     void SetContentOffset(OffsetF contentOffset)
113     {
114         if (contentOffset_) {
115             contentOffset_->Set(contentOffset);
116         }
117     }
118 
SetContentSize(SizeF contentSize)119     void SetContentSize(SizeF contentSize)
120     {
121         if (contentSize_) {
122             contentSize_->Set(contentSize);
123         }
124     }
125 
SetDrawScore(double drawScore)126     void SetDrawScore(double drawScore)
127     {
128         if (drawScore_) {
129             drawScore_->Set(static_cast<float>(drawScore));
130         }
131     }
132 
SetStepSize(double stepSize)133     void SetStepSize(double stepSize)
134     {
135         if (stepSize_) {
136             stepSize_->Set(static_cast<float>(stepSize));
137         }
138     }
139 
SetTouchStar(int32_t touchStar)140     void SetTouchStar(int32_t touchStar)
141     {
142         if (touchStar < 0 || touchStar >= starNum_->Get() || touchStar_->Get() != touchStar) {
143             SetHoverState(RatingAnimationType::NONE);
144         }
145         if (touchStar_) {
146             touchStar_->Set(touchStar);
147         }
148     }
149 
SetStartNum(int32_t starNum)150     void SetStartNum(int32_t starNum)
151     {
152         if (starNum_) {
153             starNum_->Set(starNum);
154         }
155     }
156 
SetHoverState(const RatingAnimationType & state)157     void SetHoverState(const RatingAnimationType& state)
158     {
159         if (state_ == state) {
160             return;
161         }
162         state_ = state;
163         auto pipeline = PipelineBase::GetCurrentContext();
164         CHECK_NULL_VOID(pipeline);
165         auto ratingTheme = pipeline->GetTheme<RatingTheme>();
166         CHECK_NULL_VOID(ratingTheme);
167         auto hoverDuration = static_cast<int32_t>(ratingTheme->GetHoverAnimationDuration());
168         auto pressDuration = static_cast<int32_t>(ratingTheme->GetPressAnimationDuration());
169         switch (state) {
170             case RatingAnimationType::HOVER:
171                 SetBoardColor(LinearColor(ratingTheme->GetHoverColor()), hoverDuration, Curves::FRICTION);
172                 break;
173             case RatingAnimationType::HOVERTOPRESS:
174                 SetBoardColor(LinearColor(ratingTheme->GetPressColor()), pressDuration, Curves::SHARP);
175                 break;
176             case RatingAnimationType::PRESSTOHOVER:
177                 SetBoardColor(LinearColor(ratingTheme->GetHoverColor()), pressDuration, Curves::SHARP);
178                 break;
179             case RatingAnimationType::PRESS:
180                 SetBoardColor(LinearColor(ratingTheme->GetPressColor()), hoverDuration, Curves::SHARP);
181                 break;
182             case RatingAnimationType::NONE:
183                 SetBoardColor(LinearColor(Color::TRANSPARENT), hoverDuration, Curves::FRICTION);
184                 break;
185             default:
186                 break;
187         }
188     }
189 
190 private:
191     // others
192     RatingAnimationType state_ = RatingAnimationType::NONE;
193     RefPtr<CanvasImage> foregroundImageCanvas_;
194     RefPtr<CanvasImage> secondaryImageCanvas_;
195     RefPtr<CanvasImage> backgroundImageCanvas_;
196     ImageSourceInfo foreground_;
197     ImageSourceInfo secondary_;
198     ImageSourceInfo background_;
199     // non-animatable property
200     RefPtr<PropertyBool> needDraw_;
201     RefPtr<PropertyInt> starNum_;
202     RefPtr<PropertyInt> touchStar_;
203     RefPtr<PropertyFloat> drawScore_;
204     RefPtr<PropertyFloat> stepSize_;
205     RefPtr<PropertyOffsetF> contentOffset_;
206     RefPtr<PropertySizeF> contentSize_;
207     // animatable property
208     RefPtr<AnimatablePropertyColor> boardColor_;
209     ACE_DISALLOW_COPY_AND_MOVE(RatingModifier);
210 };
211 } // namespace OHOS::Ace::NG
212 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_RATING_RATING_MODIFIER_H
213