1 /* 2 * Copyright (c) 2022-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_PAINT_METHOD_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_RATING_RATING_PAINT_METHOD_H 18 19 #include "core/components/rating/rating_theme.h" 20 #include "core/components_ng/pattern/rating/rating_modifier.h" 21 #include "core/components_ng/pattern/rating/rating_render_property.h" 22 #include "core/components_ng/render/image_painter.h" 23 #include "core/components_ng/render/node_paint_method.h" 24 25 namespace OHOS::Ace::NG { 26 class ACE_EXPORT RatingPaintMethod : public NodePaintMethod { DECLARE_ACE_TYPE(RatingPaintMethod,NodePaintMethod)27 DECLARE_ACE_TYPE(RatingPaintMethod, NodePaintMethod) 28 public: 29 RatingPaintMethod(const RefPtr<RatingModifier>& ratingModifier, int32_t starNum, 30 RatingModifier::RatingAnimationType state, bool reverse) 31 : ratingModifier_(ratingModifier), starNum_(starNum), state_(state), reverse_(reverse) 32 {} 33 ~RatingPaintMethod() override = default; 34 GetContentModifier(PaintWrapper * paintWrapper)35 RefPtr<Modifier> GetContentModifier(PaintWrapper* paintWrapper) override 36 { 37 CHECK_NULL_RETURN(ratingModifier_, nullptr); 38 return ratingModifier_; 39 } 40 UpdateContentModifier(PaintWrapper * paintWrapper)41 void UpdateContentModifier(PaintWrapper* paintWrapper) override 42 { 43 CHECK_NULL_VOID(ratingModifier_); 44 auto pipeline = PipelineBase::GetCurrentContext(); 45 CHECK_NULL_VOID(pipeline); 46 auto ratingTheme = pipeline->GetTheme<RatingTheme>(); 47 CHECK_NULL_VOID(ratingTheme); 48 auto paintProperty = DynamicCast<RatingRenderProperty>(paintWrapper->GetPaintProperty()); 49 ratingModifier_->SetContentOffset(paintWrapper->GetContentOffset()); 50 ratingModifier_->SetContentSize(paintWrapper->GetContentSize()); 51 ratingModifier_->SetStartNum(starNum_); 52 if (paintProperty) { 53 constexpr double DEFAULT_RATING_TOUCH_STAR_NUMBER = -1; 54 ratingModifier_->SetDrawScore(isfocus_ ? focusRatingScore_ : paintProperty->GetRatingScoreValue(0.f)); 55 ratingModifier_->SetStepSize(paintProperty->GetStepSize().value_or(ratingTheme->GetStepSize())); 56 ratingModifier_->SetTouchStar(paintProperty->GetTouchStar().value_or(DEFAULT_RATING_TOUCH_STAR_NUMBER)); 57 } 58 ratingModifier_->SetReverse(reverse_); 59 ratingModifier_->SetHoverState(state_); 60 } 61 UpdateFocusState(bool isfocus,double focusRatingScore)62 void UpdateFocusState(bool isfocus, double focusRatingScore) 63 { 64 isfocus_ = isfocus; 65 focusRatingScore_ = focusRatingScore; 66 } 67 68 private: 69 RefPtr<RatingModifier> ratingModifier_; 70 bool isfocus_ = false; 71 double focusRatingScore_ = .0f; 72 int32_t starNum_ = 0; 73 RatingModifier::RatingAnimationType state_; 74 bool reverse_ = false; 75 ACE_DISALLOW_COPY_AND_MOVE(RatingPaintMethod); 76 }; 77 78 } // namespace OHOS::Ace::NG 79 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_RATING_RATING_PAINT_METHOD_H 80