1 /* 2 * Copyright (c) 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_NG_PAINTS_RADIO_RADIO_PAINT_METHOD_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_RADIO_RADIO_PAINT_METHOD_H 18 19 #include "base/memory/ace_type.h" 20 #include "core/components/checkable/checkable_theme.h" 21 #include "core/components_ng/pattern/radio/radio_modifier.h" 22 #include "core/components_ng/pattern/radio/radio_paint_property.h" 23 #include "core/components_ng/render/node_paint_method.h" 24 25 namespace OHOS::Ace::NG { 26 27 class RadioPaintMethod : public NodePaintMethod { DECLARE_ACE_TYPE(RadioPaintMethod,NodePaintMethod)28 DECLARE_ACE_TYPE(RadioPaintMethod, NodePaintMethod) 29 30 public: 31 explicit RadioPaintMethod(const RefPtr<RadioModifier>& radioModifier) : radioModifier_(radioModifier) {} 32 33 ~RadioPaintMethod() override = default; 34 GetContentModifier(PaintWrapper *)35 RefPtr<Modifier> GetContentModifier(PaintWrapper* /* paintWrapper */) override 36 { 37 CHECK_NULL_RETURN(radioModifier_, nullptr); 38 return radioModifier_; 39 } 40 UpdateContentModifier(PaintWrapper * paintWrapper)41 void UpdateContentModifier(PaintWrapper* paintWrapper) override 42 { 43 CHECK_NULL_VOID(radioModifier_); 44 auto paintProperty = DynamicCast<RadioPaintProperty>(paintWrapper->GetPaintProperty()); 45 bool checked = false; 46 if (paintProperty->HasRadioCheck()) { 47 checked = paintProperty->GetRadioCheckValue(); 48 } else { 49 paintProperty->UpdateRadioCheck(false); 50 } 51 52 auto pipeline = PipelineBase::GetCurrentContext(); 53 CHECK_NULL_VOID(pipeline); 54 auto radioTheme = pipeline->GetTheme<RadioTheme>(); 55 activeColor_ = paintProperty->GetRadioCheckedBackgroundColor().value_or(Color(radioTheme->GetActiveColor())); 56 inactiveColor_ = paintProperty->GetRadioUncheckedBorderColor().value_or(Color(radioTheme->GetInactiveColor())); 57 pointColor_ = paintProperty->GetRadioIndicatorColor().value_or(Color(radioTheme->GetPointColor())); 58 59 auto size = paintWrapper->GetContentSize(); 60 auto offset = paintWrapper->GetContentOffset(); 61 radioModifier_->InitializeParam(); 62 radioModifier_->SetPointColor(pointColor_); 63 radioModifier_->SetactiveColor(activeColor_); 64 radioModifier_->SetinactiveColor(inactiveColor_); 65 radioModifier_->SetSize(size); 66 radioModifier_->SetOffset(offset); 67 radioModifier_->SetIsOnAnimationFlag(isOnAnimationFlag_); 68 radioModifier_->SetEnabled(enabled_); 69 radioModifier_->SetTotalScale(totalScale_); 70 radioModifier_->SetPointScale(pointScale_); 71 radioModifier_->SetRingPointScale(ringPointScale_); 72 if (checked != radioModifier_->GetIsCheck()) { 73 radioModifier_->SetUIStatus(UIStatus::SELECTED); 74 if (!isFirstCreated_) { 75 radioModifier_->UpdateIsOnAnimatableProperty(checked); 76 } 77 } 78 radioModifier_->SetIsCheck(checked); 79 radioModifier_->SetTouchHoverAnimationType(touchHoverType_); 80 radioModifier_->UpdateAnimatableProperty(); 81 auto horizontalPadding = radioTheme->GetHotZoneHorizontalPadding().ConvertToPx(); 82 auto verticalPadding = radioTheme->GetHotZoneVerticalPadding().ConvertToPx(); 83 float boundsRectOriginX = offset.GetX() - horizontalPadding; 84 float boundsRectOriginY = offset.GetY() - verticalPadding; 85 float boundsRectWidth = size.Width() + 2 * horizontalPadding; 86 float boundsRectHeight = size.Height() + 2 * verticalPadding; 87 RectF boundsRect(boundsRectOriginX, boundsRectOriginY, boundsRectWidth, boundsRectHeight); 88 radioModifier_->SetBoundsRect(boundsRect); 89 } 90 SetHotZoneOffset(const OffsetF & hotZoneOffset)91 void SetHotZoneOffset(const OffsetF& hotZoneOffset) 92 { 93 hotZoneOffset_ = hotZoneOffset; 94 } 95 SetHotZoneSize(const SizeF & hotZoneSize)96 void SetHotZoneSize(const SizeF& hotZoneSize) 97 { 98 hotZoneSize_ = hotZoneSize; 99 } 100 SetEnabled(const bool enabled)101 void SetEnabled(const bool enabled) 102 { 103 enabled_ = enabled; 104 } 105 SetIsOnAnimationFlag(const bool isOnAnimationFlag)106 void SetIsOnAnimationFlag(const bool isOnAnimationFlag) 107 { 108 isOnAnimationFlag_ = isOnAnimationFlag; 109 } 110 SetIsFirstCreated(const bool isFirstCreated)111 void SetIsFirstCreated(const bool isFirstCreated) 112 { 113 isFirstCreated_ = isFirstCreated; 114 } 115 SetTotalScale(const float totalScale)116 void SetTotalScale(const float totalScale) 117 { 118 totalScale_ = totalScale; 119 } 120 SetPointScale(const float pointScale)121 void SetPointScale(const float pointScale) 122 { 123 pointScale_ = pointScale; 124 } 125 SetRingPointScale(const float ringPointScale)126 void SetRingPointScale(const float ringPointScale) 127 { 128 ringPointScale_ = ringPointScale; 129 } 130 SetUIStatus(const UIStatus uiStatus)131 void SetUIStatus(const UIStatus uiStatus) 132 { 133 uiStatus_ = uiStatus; 134 } 135 SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType)136 void SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType) 137 { 138 touchHoverType_ = touchHoverType; 139 } 140 141 private: 142 Color pointColor_; 143 Color activeColor_; 144 Color inactiveColor_; 145 bool enabled_ = true; 146 bool isOnAnimationFlag_ = false; 147 bool isFirstCreated_ = true; 148 float totalScale_ = 1.0f; 149 float pointScale_ = 0.5f; 150 float ringPointScale_ = 0.0f; 151 UIStatus uiStatus_ = UIStatus::UNSELECTED; 152 OffsetF hotZoneOffset_; 153 SizeF hotZoneSize_; 154 TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE; 155 156 RefPtr<RadioModifier> radioModifier_; 157 }; 158 } // namespace OHOS::Ace::NG 159 160 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_RADIO_RADIO_PAINT_METHOD_H 161