1 /*
2 * Copyright (c) 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_arc_scroll_bar.h"
17
18 #include "draw/draw_arc.h"
19 #include "engines/gfx/gfx_engine_manager.h"
20 #include "gfx_utils/graphic_types.h"
21
22 namespace {
23 constexpr uint16_t RIGHT_SIDE_START_ANGLE_IN_DEGREE = 60;
24 constexpr uint16_t RIGHT_SIDE_END_ANGLE_IN_DEGREE = 120;
25 constexpr uint16_t LEFT_SIDE_START_ANGLE_IN_DEGREE = 240;
26 constexpr uint16_t LEFT_SIDE_END_ANGLE_IN_DEGREE = 300;
27 constexpr uint16_t SCROLL_BAR_MIN_ARC = 10;
28 } // namespace
29
30 namespace OHOS {
UIArcScrollBar()31 UIArcScrollBar::UIArcScrollBar()
32 : radius_(0),
33 width_(0),
34 startAngle_(RIGHT_SIDE_START_ANGLE_IN_DEGREE),
35 endAngle_(RIGHT_SIDE_END_ANGLE_IN_DEGREE),
36 center_({0, 0}),
37 side_(SCROLL_BAR_RIGHT_SIDE)
38 {
39 }
40
SetPosition(int16_t x,int16_t y,int16_t width,int16_t radius)41 void UIArcScrollBar::SetPosition(int16_t x, int16_t y, int16_t width, int16_t radius)
42 {
43 if ((width > 0) && (radius > 0)) {
44 center_.x = x;
45 center_.y = y;
46 width_ = width;
47 radius_ = radius;
48 }
49 }
50
SetScrollBarSide(uint8_t side)51 void UIArcScrollBar::SetScrollBarSide(uint8_t side)
52 {
53 if (side == SCROLL_BAR_RIGHT_SIDE) {
54 startAngle_ = RIGHT_SIDE_START_ANGLE_IN_DEGREE;
55 endAngle_ = RIGHT_SIDE_END_ANGLE_IN_DEGREE;
56 } else {
57 startAngle_ = LEFT_SIDE_START_ANGLE_IN_DEGREE;
58 endAngle_ = LEFT_SIDE_END_ANGLE_IN_DEGREE;
59 }
60 side_ = side;
61 }
62
OnDraw(BufferInfo & gfxDstBuffer,const Rect & invalidatedArea,uint8_t backgroundOpa)63 void UIArcScrollBar::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, uint8_t backgroundOpa)
64 {
65 // 8: Shift right 8 bits
66 backgroundOpa = (backgroundOpa == OPA_OPAQUE) ? opacity_ : (static_cast<uint16_t>(backgroundOpa) * opacity_) >> 8;
67 DrawBackground(gfxDstBuffer, invalidatedArea, backgroundOpa);
68 DrawForeground(gfxDstBuffer, invalidatedArea, backgroundOpa);
69 }
70
DrawForeground(BufferInfo & gfxDstBuffer,const Rect & invalidatedArea,uint8_t backgroundOpa)71 void UIArcScrollBar::DrawForeground(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, uint8_t backgroundOpa)
72 {
73 uint16_t foregoundAngleRange = static_cast<uint16_t>(foregroundProportion_ * (endAngle_ - startAngle_));
74 if (foregoundAngleRange < SCROLL_BAR_MIN_ARC) {
75 foregoundAngleRange = SCROLL_BAR_MIN_ARC;
76 }
77 int16_t startAngle;
78 int16_t endAngle;
79 int16_t minAngle;
80 int16_t maxAngle;
81 if (side_ == SCROLL_BAR_RIGHT_SIDE) {
82 minAngle = startAngle_;
83 maxAngle = endAngle_ - foregoundAngleRange;
84 startAngle = minAngle + static_cast<int16_t>(scrollProgress_ * (maxAngle - minAngle));
85 endAngle = startAngle + foregoundAngleRange;
86 } else {
87 maxAngle = endAngle_;
88 minAngle = startAngle_ + foregoundAngleRange;
89 endAngle = maxAngle - static_cast<int16_t>(scrollProgress_ * (maxAngle - minAngle));
90 startAngle = endAngle - foregoundAngleRange;
91 }
92 if ((startAngle > endAngle_) || (endAngle < startAngle_)) {
93 return;
94 }
95 ArcInfo arcInfo = {{0, 0}, {0, 0}, 0, 0, 0, nullptr};
96 arcInfo.radius = (radius_ > 0) ? (radius_ - 1) : 0;
97 arcInfo.center = center_;
98 arcInfo.startAngle = MATH_MAX(startAngle, startAngle_);
99 arcInfo.endAngle = MATH_MIN(endAngle, endAngle_);
100 BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, *foregroundStyle_, backgroundOpa,
101 foregroundStyle_->lineCap_);
102 }
103
DrawBackground(BufferInfo & gfxDstBuffer,const Rect & invalidatedArea,uint8_t backgroundOpa)104 void UIArcScrollBar::DrawBackground(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, uint8_t backgroundOpa)
105 {
106 ArcInfo arcInfo = {{0, 0}, {0, 0}, 0, 0, 0, nullptr};
107 arcInfo.radius = radius_;
108 arcInfo.center = center_;
109 arcInfo.startAngle = startAngle_;
110 arcInfo.endAngle = endAngle_;
111 BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, *backgroundStyle_, backgroundOpa,
112 backgroundStyle_->lineCap_);
113 }
114 } // namespace OHOS
115