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 
UpdateUIStatus(bool checked)41     void UpdateUIStatus(bool checked)
42     {
43         if (checked != radioModifier_->GetIsCheck()) {
44             if (!enabled_ && !checked && Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_TWELVE)) {
45                 radioModifier_->SetUIStatus(UIStatus::UNSELECTED);
46             } else {
47                 radioModifier_->SetUIStatus(UIStatus::SELECTED);
48             }
49             if (!isFirstCreated_) {
50                 if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_TWELVE)) {
51                     radioModifier_->UpdateIndicatorAnimation(checked);
52                 } else {
53                     radioModifier_->UpdateIsOnAnimatableProperty(checked);
54                 }
55             }
56         } else if (!checked && isFirstCreated_) {
57             radioModifier_->InitOpacityScale(checked);
58         }
59     }
60 
UpdateContentModifier(PaintWrapper * paintWrapper)61     void UpdateContentModifier(PaintWrapper* paintWrapper) override
62     {
63         CHECK_NULL_VOID(radioModifier_);
64         auto paintProperty = DynamicCast<RadioPaintProperty>(paintWrapper->GetPaintProperty());
65         bool checked = false;
66         if (paintProperty->HasRadioCheck()) {
67             checked = paintProperty->GetRadioCheckValue();
68         } else {
69             paintProperty->UpdateRadioCheck(false);
70         }
71 
72         auto pipeline = PipelineBase::GetCurrentContext();
73         CHECK_NULL_VOID(pipeline);
74         auto radioTheme = pipeline->GetTheme<RadioTheme>();
75         activeColor_ = paintProperty->GetRadioCheckedBackgroundColor().value_or(Color(radioTheme->GetActiveColor()));
76         inactiveColor_ = paintProperty->GetRadioUncheckedBorderColor().value_or(Color(radioTheme->GetInactiveColor()));
77         pointColor_ = paintProperty->GetRadioIndicatorColor().value_or(Color(radioTheme->GetPointColor()));
78 
79         auto size = paintWrapper->GetContentSize();
80         auto offset = paintWrapper->GetContentOffset();
81         radioModifier_->InitializeParam();
82         radioModifier_->SetPointColor(pointColor_);
83         radioModifier_->SetactiveColor(activeColor_);
84         radioModifier_->SetinactiveColor(inactiveColor_);
85         radioModifier_->SetSize(size);
86         radioModifier_->SetOffset(offset);
87         radioModifier_->SetIsOnAnimationFlag(isOnAnimationFlag_);
88         radioModifier_->SetEnabled(enabled_);
89         radioModifier_->SetTotalScale(totalScale_);
90         radioModifier_->SetPointScale(pointScale_);
91         radioModifier_->SetRingPointScale(ringPointScale_);
92         UpdateUIStatus(checked);
93         radioModifier_->SetShowHoverEffect(showHoverEffect_);
94         radioModifier_->SetIsCheck(checked);
95         radioModifier_->SetTouchHoverAnimationType(touchHoverType_);
96         radioModifier_->UpdateAnimatableProperty();
97         auto horizontalPadding = radioTheme->GetHotZoneHorizontalPadding().ConvertToPx();
98         auto verticalPadding = radioTheme->GetHotZoneVerticalPadding().ConvertToPx();
99         float boundsRectOriginX = offset.GetX() - horizontalPadding;
100         float boundsRectOriginY = offset.GetY() - verticalPadding;
101         float boundsRectWidth = size.Width() + 2 * horizontalPadding;
102         float boundsRectHeight = size.Height() + 2 * verticalPadding;
103         RectF boundsRect(boundsRectOriginX, boundsRectOriginY, boundsRectWidth, boundsRectHeight);
104         radioModifier_->SetBoundsRect(boundsRect);
105     }
106 
SetHotZoneOffset(const OffsetF & hotZoneOffset)107     void SetHotZoneOffset(const OffsetF& hotZoneOffset)
108     {
109         hotZoneOffset_ = hotZoneOffset;
110     }
111 
SetHotZoneSize(const SizeF & hotZoneSize)112     void SetHotZoneSize(const SizeF& hotZoneSize)
113     {
114         hotZoneSize_ = hotZoneSize;
115     }
116 
SetEnabled(const bool enabled)117     void SetEnabled(const bool enabled)
118     {
119         enabled_ = enabled;
120     }
121 
SetIsOnAnimationFlag(const bool isOnAnimationFlag)122     void SetIsOnAnimationFlag(const bool isOnAnimationFlag)
123     {
124         isOnAnimationFlag_ = isOnAnimationFlag;
125     }
126 
SetIsFirstCreated(const bool isFirstCreated)127     void SetIsFirstCreated(const bool isFirstCreated)
128     {
129         isFirstCreated_ = isFirstCreated;
130     }
131 
SetTotalScale(const float totalScale)132     void SetTotalScale(const float totalScale)
133     {
134         totalScale_ = totalScale;
135     }
136 
SetPointScale(const float pointScale)137     void SetPointScale(const float pointScale)
138     {
139         pointScale_ = pointScale;
140     }
141 
SetRingPointScale(const float ringPointScale)142     void SetRingPointScale(const float ringPointScale)
143     {
144         ringPointScale_ = ringPointScale;
145     }
146 
SetUIStatus(const UIStatus uiStatus)147     void SetUIStatus(const UIStatus uiStatus)
148     {
149         uiStatus_ = uiStatus;
150     }
151 
SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType)152     void SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType)
153     {
154         touchHoverType_ = touchHoverType;
155     }
156 
SetShowHoverEffect(bool showHoverEffect)157     void SetShowHoverEffect(bool showHoverEffect)
158     {
159         showHoverEffect_ = showHoverEffect;
160     }
161 
162 private:
163     Color pointColor_;
164     Color activeColor_;
165     Color inactiveColor_;
166     bool enabled_ = true;
167     bool isOnAnimationFlag_ = false;
168     bool isFirstCreated_ = true;
169     bool showHoverEffect_ = true;
170     float totalScale_ = 1.0f;
171     float pointScale_ = 0.5f;
172     float ringPointScale_ = 0.0f;
173     UIStatus uiStatus_ = UIStatus::UNSELECTED;
174     OffsetF hotZoneOffset_;
175     SizeF hotZoneSize_;
176     TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE;
177 
178     RefPtr<RadioModifier> radioModifier_;
179 };
180 } // namespace OHOS::Ace::NG
181 
182 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_RADIO_RADIO_PAINT_METHOD_H
183