1a3e0fd82Sopenharmony_ci/*
2a3e0fd82Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3a3e0fd82Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4a3e0fd82Sopenharmony_ci * you may not use this file except in compliance with the License.
5a3e0fd82Sopenharmony_ci * You may obtain a copy of the License at
6a3e0fd82Sopenharmony_ci *
7a3e0fd82Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8a3e0fd82Sopenharmony_ci *
9a3e0fd82Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10a3e0fd82Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11a3e0fd82Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a3e0fd82Sopenharmony_ci * See the License for the specific language governing permissions and
13a3e0fd82Sopenharmony_ci * limitations under the License.
14a3e0fd82Sopenharmony_ci */
15a3e0fd82Sopenharmony_ci
16a3e0fd82Sopenharmony_ci#include "components/ui_circle_progress.h"
17a3e0fd82Sopenharmony_ci#include "draw/draw_arc.h"
18a3e0fd82Sopenharmony_ci#include "draw/draw_line.h"
19a3e0fd82Sopenharmony_ci#include "engines/gfx/gfx_engine_manager.h"
20a3e0fd82Sopenharmony_ci
21a3e0fd82Sopenharmony_cinamespace OHOS {
22a3e0fd82Sopenharmony_ciUICircleProgress::UICircleProgress()
23a3e0fd82Sopenharmony_ci    : center_({0, 0}),
24a3e0fd82Sopenharmony_ci      backgroundImagePos_({0, 0}),
25a3e0fd82Sopenharmony_ci      progressImagePos_({0, 0}),
26a3e0fd82Sopenharmony_ci      radius_(0),
27a3e0fd82Sopenharmony_ci      startAngle_(MIN_ANGLE_VALUE),
28a3e0fd82Sopenharmony_ci      endAngle_(MAX_ANGLE_VALUE)
29a3e0fd82Sopenharmony_ci{
30a3e0fd82Sopenharmony_ci}
31a3e0fd82Sopenharmony_ci
32a3e0fd82Sopenharmony_civoid UICircleProgress::SetCenterPosition(int16_t x, int16_t y)
33a3e0fd82Sopenharmony_ci{
34a3e0fd82Sopenharmony_ci    center_.x = x;
35a3e0fd82Sopenharmony_ci    center_.y = y;
36a3e0fd82Sopenharmony_ci}
37a3e0fd82Sopenharmony_ci
38a3e0fd82Sopenharmony_civoid UICircleProgress::SetStartAngle(int16_t startAngle)
39a3e0fd82Sopenharmony_ci{
40a3e0fd82Sopenharmony_ci    startAngle_ = startAngle;
41a3e0fd82Sopenharmony_ci}
42a3e0fd82Sopenharmony_ci
43a3e0fd82Sopenharmony_civoid UICircleProgress::SetEndAngle(int16_t endAngle)
44a3e0fd82Sopenharmony_ci{
45a3e0fd82Sopenharmony_ci    endAngle_ = endAngle;
46a3e0fd82Sopenharmony_ci}
47a3e0fd82Sopenharmony_ci
48a3e0fd82Sopenharmony_civoid UICircleProgress::GetStartEndAngle(int16_t& start, int16_t& end) const
49a3e0fd82Sopenharmony_ci{
50a3e0fd82Sopenharmony_ci    if (startAngle_ > endAngle_) {
51a3e0fd82Sopenharmony_ci        start = endAngle_;
52a3e0fd82Sopenharmony_ci        end = startAngle_;
53a3e0fd82Sopenharmony_ci    } else {
54a3e0fd82Sopenharmony_ci        start = startAngle_;
55a3e0fd82Sopenharmony_ci        end = endAngle_;
56a3e0fd82Sopenharmony_ci    }
57a3e0fd82Sopenharmony_ci}
58a3e0fd82Sopenharmony_ci
59a3e0fd82Sopenharmony_civoid UICircleProgress::GetAngleRange(int16_t& start, int16_t& end) const
60a3e0fd82Sopenharmony_ci{
61a3e0fd82Sopenharmony_ci    GetStartEndAngle(start, end);
62a3e0fd82Sopenharmony_ci    DrawArc::GetInstance()->GetDrawRange(start, end);
63a3e0fd82Sopenharmony_ci}
64a3e0fd82Sopenharmony_ci
65a3e0fd82Sopenharmony_civoid UICircleProgress::GetRedrawAngle(int16_t& start, int16_t& end) const
66a3e0fd82Sopenharmony_ci{
67a3e0fd82Sopenharmony_ci    GetStartEndAngle(start, end);
68a3e0fd82Sopenharmony_ci
69a3e0fd82Sopenharmony_ci    if (startAngle_ == endAngle_) {
70a3e0fd82Sopenharmony_ci        return;
71a3e0fd82Sopenharmony_ci    }
72a3e0fd82Sopenharmony_ci
73a3e0fd82Sopenharmony_ci    int16_t angleRange = end - start;
74a3e0fd82Sopenharmony_ci    angleRange = (angleRange > CIRCLE_IN_DEGREE) ? CIRCLE_IN_DEGREE : angleRange;
75a3e0fd82Sopenharmony_ci
76a3e0fd82Sopenharmony_ci    int16_t angle = GetCurrentPos(angleRange);
77a3e0fd82Sopenharmony_ci    if (startAngle_ > endAngle_) {
78a3e0fd82Sopenharmony_ci        start = end - angle;
79a3e0fd82Sopenharmony_ci    } else {
80a3e0fd82Sopenharmony_ci        end = angle + start;
81a3e0fd82Sopenharmony_ci    }
82a3e0fd82Sopenharmony_ci    DrawArc::GetInstance()->GetDrawRange(start, end);
83a3e0fd82Sopenharmony_ci}
84a3e0fd82Sopenharmony_ci
85a3e0fd82Sopenharmony_civoid UICircleProgress::DrawCommonCircle(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
86a3e0fd82Sopenharmony_ci{
87a3e0fd82Sopenharmony_ci    ArcInfo arcinfo = {{0}};
88a3e0fd82Sopenharmony_ci    arcinfo.radius = radius_;
89a3e0fd82Sopenharmony_ci    int16_t endAngle;
90a3e0fd82Sopenharmony_ci    int16_t startAngle;
91a3e0fd82Sopenharmony_ci    GetRedrawAngle(startAngle, endAngle);
92a3e0fd82Sopenharmony_ci
93a3e0fd82Sopenharmony_ci    int16_t start;
94a3e0fd82Sopenharmony_ci    int16_t end;
95a3e0fd82Sopenharmony_ci    GetAngleRange(start, end);
96a3e0fd82Sopenharmony_ci    Rect rect = GetOrigRect();
97a3e0fd82Sopenharmony_ci    arcinfo.center.x = center_.x + rect.GetLeft() + style_->paddingLeft_ + style_->borderWidth_;
98a3e0fd82Sopenharmony_ci    arcinfo.center.y = center_.y + rect.GetTop() + style_->paddingTop_ + style_->borderWidth_;
99a3e0fd82Sopenharmony_ci    BaseGfxEngine* baseGfxEngine = BaseGfxEngine::GetInstance();
100a3e0fd82Sopenharmony_ci    if (enableBackground_ && ((start != end) || (backgroundStyle_->lineCap_ == CapType::CAP_ROUND))) {
101a3e0fd82Sopenharmony_ci        arcinfo.imgPos.x = backgroundImagePos_.x + rect.GetLeft();
102a3e0fd82Sopenharmony_ci        arcinfo.imgPos.y = backgroundImagePos_.y + rect.GetTop();
103a3e0fd82Sopenharmony_ci        arcinfo.startAngle = start;
104a3e0fd82Sopenharmony_ci        arcinfo.endAngle = end;
105a3e0fd82Sopenharmony_ci        arcinfo.imgSrc = backgroundImage_;
106a3e0fd82Sopenharmony_ci        baseGfxEngine->DrawArc(gfxDstBuffer, arcinfo, invalidatedArea, *backgroundStyle_, opaScale_,
107a3e0fd82Sopenharmony_ci                               backgroundStyle_->lineCap_);
108a3e0fd82Sopenharmony_ci    }
109a3e0fd82Sopenharmony_ci
110a3e0fd82Sopenharmony_ci    if ((startAngle != endAngle) || (foregroundStyle_->lineCap_ == CapType::CAP_ROUND)) {
111a3e0fd82Sopenharmony_ci        arcinfo.imgPos.x = progressImagePos_.x + rect.GetLeft();
112a3e0fd82Sopenharmony_ci        arcinfo.imgPos.y = progressImagePos_.y + rect.GetTop();
113a3e0fd82Sopenharmony_ci        arcinfo.startAngle = startAngle;
114a3e0fd82Sopenharmony_ci        arcinfo.endAngle = endAngle;
115a3e0fd82Sopenharmony_ci        arcinfo.imgSrc = foregroundImage_;
116a3e0fd82Sopenharmony_ci        baseGfxEngine->DrawArc(gfxDstBuffer, arcinfo, invalidatedArea, *foregroundStyle_, opaScale_,
117a3e0fd82Sopenharmony_ci                               foregroundStyle_->lineCap_);
118a3e0fd82Sopenharmony_ci    }
119a3e0fd82Sopenharmony_ci}
120a3e0fd82Sopenharmony_ci
121a3e0fd82Sopenharmony_civoid UICircleProgress::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
122a3e0fd82Sopenharmony_ci{
123a3e0fd82Sopenharmony_ci    if (GetRangeSize() == 0) {
124a3e0fd82Sopenharmony_ci        return;
125a3e0fd82Sopenharmony_ci    }
126a3e0fd82Sopenharmony_ci
127a3e0fd82Sopenharmony_ci    BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, GetOrigRect(), invalidatedArea, *style_, opaScale_);
128a3e0fd82Sopenharmony_ci
129a3e0fd82Sopenharmony_ci    Rect trunc(invalidatedArea);
130a3e0fd82Sopenharmony_ci    if (trunc.Intersect(trunc, GetOrigRect())) {
131a3e0fd82Sopenharmony_ci        DrawCommonCircle(gfxDstBuffer, trunc);
132a3e0fd82Sopenharmony_ci    }
133a3e0fd82Sopenharmony_ci}
134a3e0fd82Sopenharmony_ci} // namespace OHOS
135