• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 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 #include "components/ui_toggle_button.h"
17 
18 #include "common/image.h"
19 #include "engines/gfx/gfx_engine_manager.h"
20 #include "imgdecode/cache_manager.h"
21 namespace {
22 constexpr int16_t TOGGLE_BTN_WIDTH = 32;
23 constexpr int16_t TOGGLE_BTN_CORNER_RADIUS = 11;
24 constexpr int16_t TOGGLE_BTN_RADIUS_DIFF = 2;
25 constexpr uint8_t TOGGLE_BTN_UNSELECTED_OPA = 97;
26 #if DEFAULT_ANIMATION
27 constexpr int16_t DEFAULT_ANIMATOR_TIME = 150;
28 constexpr float BEZIER_CONTROL_POINT_X_1 = 0.2;
29 constexpr float BEZIER_CONTROL_POINT_X_2 = 0.2;
30 #endif
31 } // namespace
32 namespace OHOS {
UIToggleButton()33 UIToggleButton::UIToggleButton()
34     : corner_(TOGGLE_BTN_CORNER_RADIUS),
35       radius_(TOGGLE_BTN_CORNER_RADIUS - TOGGLE_BTN_RADIUS_DIFF),
36       rectWidth_(TOGGLE_BTN_WIDTH)
37 {
38     backgroundOpacity_ = TOGGLE_BTN_UNSELECTED_OPA;
39     image_[UNSELECTED].SetSrc("");
40     image_[SELECTED].SetSrc("");
41 #if DEFAULT_ANIMATION
42     checkBoxAnimator_.SetTime(DEFAULT_ANIMATOR_TIME);
43 #endif
44     Resize(width_, height_);
45 }
46 
SetState(bool state)47 void UIToggleButton::SetState(bool state)
48 {
49     if (state) {
50         UICheckBox::SetState(SELECTED);
51     } else {
52         UICheckBox::SetState(UNSELECTED);
53     }
54     Invalidate();
55 }
56 
CalculateSize()57 void UIToggleButton::CalculateSize()
58 {
59     width_ = GetWidth();
60     height_ = GetHeight();
61     int16_t minValue = (width_ > height_) ? height_ : width_;
62     corner_ = TOGGLE_BTN_CORNER_RADIUS * minValue / DEFAULT_HOT_HEIGHT;
63     int16_t radiusDiff = TOGGLE_BTN_RADIUS_DIFF * minValue / DEFAULT_HOT_WIDTH;
64     radius_ = corner_ - radiusDiff;
65     rectWidth_ = TOGGLE_BTN_WIDTH * minValue / DEFAULT_HOT_WIDTH;
66 
67     Rect contentRect = GetContentRect();
68     int16_t dx = (width_ - rectWidth_) / 2; // 2: half
69     int16_t dy = (height_ / 2) - corner_;   // 2: half
70     int16_t x = contentRect.GetX() + dx;
71     int16_t y = contentRect.GetY() + dy;
72     leftCenter_ = {static_cast<int16_t>(x + corner_), static_cast<int16_t>(y + corner_)};
73     rightCenter_ = {static_cast<int16_t>(x + rectWidth_ - corner_), static_cast<int16_t>(y + corner_)};
74 #if DEFAULT_ANIMATION
75     if (checkBoxAnimator_.GetState() != Animator::START) {
76         currentCenter_ = (state_ == SELECTED) ? rightCenter_ : leftCenter_;
77         backgroundOpacity_ = (state_ == SELECTED) ? OPA_OPAQUE : TOGGLE_BTN_UNSELECTED_OPA;
78         bgColor_ = (state_ == SELECTED) ? selectedStateColor_ : Color::White();
79     }
80 #else
81     currentCenter_ = (state_ == SELECTED) ? rightCenter_ : leftCenter_;
82     backgroundOpacity_ = (state_ == SELECTED) ? OPA_OPAQUE : TOGGLE_BTN_UNSELECTED_OPA;
83     bgColor_ = (state_ == SELECTED) ? selectedStateColor_ : Color::White();
84 #endif
85     rectMid_.SetRect(x, y, x + rectWidth_, y + (corner_ << 1) + 1);
86 }
87 
OnDraw(BufferInfo & gfxDstBuffer,const Rect & invalidatedArea)88 void UIToggleButton::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
89 {
90     if ((image_[SELECTED].GetSrcType() != IMG_SRC_UNKNOWN) && (image_[UNSELECTED].GetSrcType() != IMG_SRC_UNKNOWN)) {
91         UICheckBox::OnDraw(gfxDstBuffer, invalidatedArea);
92     } else {
93         CalculateSize();
94         BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, GetRect(), invalidatedArea, *style_, opaScale_);
95         Rect contentRect = GetContentRect();
96         Rect trunc = invalidatedArea;
97         bool isIntersect = trunc.Intersect(trunc, contentRect);
98         if (!isIntersect) {
99             return;
100         }
101         Style styleUnSelect = StyleDefault::GetBackgroundTransparentStyle();
102         styleUnSelect.bgColor_ = bgColor_;
103         styleUnSelect.bgOpa_ = backgroundOpacity_;
104         styleUnSelect.borderRadius_ = corner_;
105         BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, rectMid_, trunc, styleUnSelect, opaScale_);
106         ArcInfo arcInfoLeft = {currentCenter_, {0}, radius_, 0, CIRCLE_IN_DEGREE, nullptr};
107         styleUnSelect.lineColor_ = Color::White();
108         styleUnSelect.lineWidth_ = radius_;
109         BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfoLeft, trunc, styleUnSelect, OPA_OPAQUE,
110                                               CapType::CAP_NONE);
111     }
112 }
113 #if DEFAULT_ANIMATION
Callback(UIView * view)114 void UIToggleButton::Callback(UIView* view)
115 {
116     runTime_ = checkBoxAnimator_.GetRunTime();
117     float x = static_cast<float>(runTime_) / checkBoxAnimator_.GetTime();
118     float coefficient = Interpolation::GetBezierY(x, BEZIER_CONTROL_POINT_X_1, 0, BEZIER_CONTROL_POINT_X_2, 1);
119     if (state_ == SELECTED) {
120         currentCenter_.y = rightCenter_.y;
121         currentCenter_.x = static_cast<int16_t>((rightCenter_.x - leftCenter_.x) * coefficient) + leftCenter_.x;
122         backgroundOpacity_ =
123             static_cast<uint8_t>(TOGGLE_BTN_UNSELECTED_OPA + (OPA_OPAQUE - TOGGLE_BTN_UNSELECTED_OPA) * coefficient);
124         bgColor_ =
125             Color::GetMixColor(selectedStateColor_, Color::White(), static_cast<uint8_t>(OPA_OPAQUE * coefficient));
126     } else {
127         currentCenter_.y = leftCenter_.y;
128         currentCenter_.x = rightCenter_.x - static_cast<uint16_t>((rightCenter_.x - leftCenter_.x) * coefficient);
129         backgroundOpacity_ = static_cast<uint8_t>(OPA_OPAQUE - (OPA_OPAQUE - TOGGLE_BTN_UNSELECTED_OPA) * coefficient);
130         bgColor_ = Color::GetMixColor(selectedStateColor_, Color::White(),
131                                       static_cast<uint8_t>(OPA_OPAQUE * (1 - coefficient)));
132     }
133     Invalidate();
134 }
135 
OnStop(UIView & view)136 void UIToggleButton::OnStop(UIView& view)
137 {
138     if (state_ == SELECTED) {
139         bgColor_ = selectedStateColor_;
140     } else {
141         bgColor_ = Color::White();
142     }
143     backgroundOpacity_ = (state_ == SELECTED) ? OPA_OPAQUE : TOGGLE_BTN_UNSELECTED_OPA;
144     Invalidate();
145 }
146 #endif
147 } // namespace OHOS
148