ui_circle_progress.cpp 4.1 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "components/ui_circle_progress.h"
#include "draw/draw_arc.h"
#include "draw/draw_line.h"
N
niulihua 已提交
19
#include "engines/gfx/gfx_engine_manager.h"
M
mamingshuai 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

namespace OHOS {
UICircleProgress::UICircleProgress()
    : center_({0, 0}),
      backgroundImagePos_({0, 0}),
      progressImagePos_({0, 0}),
      radius_(0),
      startAngle_(MIN_ANGLE_VALUE),
      endAngle_(MAX_ANGLE_VALUE)
{
}

void UICircleProgress::SetCenterPosition(int16_t x, int16_t y)
{
    center_.x = x;
    center_.y = y;
}

void UICircleProgress::SetStartAngle(int16_t startAngle)
{
    startAngle_ = startAngle;
}

void UICircleProgress::SetEndAngle(int16_t endAngle)
{
    endAngle_ = endAngle;
}

void UICircleProgress::GetStartEndAngle(int16_t& start, int16_t& end) const
{
    if (startAngle_ > endAngle_) {
        start = endAngle_;
        end = startAngle_;
    } else {
        start = startAngle_;
        end = endAngle_;
    }
}

void UICircleProgress::GetAngleRange(int16_t& start, int16_t& end) const
{
    GetStartEndAngle(start, end);
    DrawArc::GetInstance()->GetDrawRange(start, end);
}

void UICircleProgress::GetRedrawAngle(int16_t& start, int16_t& end) const
{
    GetStartEndAngle(start, end);

    if (startAngle_ == endAngle_) {
        return;
    }

    int16_t angleRange = end - start;
    angleRange = (angleRange > CIRCLE_IN_DEGREE) ? CIRCLE_IN_DEGREE : angleRange;

    int16_t angle = GetCurrentPos(angleRange);
    if (startAngle_ > endAngle_) {
        start = end - angle;
    } else {
        end = angle + start;
    }
    DrawArc::GetInstance()->GetDrawRange(start, end);
}

N
niulihua 已提交
85
void UICircleProgress::DrawCommonCircle(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
M
mamingshuai 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
{
    ArcInfo arcinfo = {{0}};
    arcinfo.radius = radius_;
    int16_t endAngle;
    int16_t startAngle;
    GetRedrawAngle(startAngle, endAngle);

    int16_t start;
    int16_t end;
    GetAngleRange(start, end);
    Rect rect = GetOrigRect();
    arcinfo.center.x = center_.x + rect.GetLeft() + style_->paddingLeft_ + style_->borderWidth_;
    arcinfo.center.y = center_.y + rect.GetTop() + style_->paddingTop_ + style_->borderWidth_;

    if (enableBackground_ && ((start != end) || (backgroundStyle_->lineCap_ == CapType::CAP_ROUND))) {
        arcinfo.imgPos.x = backgroundImagePos_.x + rect.GetLeft();
        arcinfo.imgPos.y = backgroundImagePos_.y + rect.GetTop();
        arcinfo.startAngle = start;
        arcinfo.endAngle = end;
        arcinfo.imgSrc = backgroundImage_;
N
niulihua 已提交
106
        BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcinfo, invalidatedArea, *backgroundStyle_, opaScale_,
N
niulihua 已提交
107
                                              backgroundStyle_->lineCap_);
M
mamingshuai 已提交
108 109 110 111 112 113 114 115
    }

    if ((startAngle != endAngle) || (foregroundStyle_->lineCap_ == CapType::CAP_ROUND)) {
        arcinfo.imgPos.x = progressImagePos_.x + rect.GetLeft();
        arcinfo.imgPos.y = progressImagePos_.y + rect.GetTop();
        arcinfo.startAngle = startAngle;
        arcinfo.endAngle = endAngle;
        arcinfo.imgSrc = foregroundImage_;
N
niulihua 已提交
116
        BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcinfo, invalidatedArea, *foregroundStyle_, opaScale_,
N
niulihua 已提交
117
                                              foregroundStyle_->lineCap_);
M
mamingshuai 已提交
118 119 120
    }
}

N
niulihua 已提交
121
void UICircleProgress::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
M
mamingshuai 已提交
122 123 124 125 126
{
    if (GetRangeSize() == 0) {
        return;
    }

N
niulihua 已提交
127
    BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, GetOrigRect(), invalidatedArea, *style_, opaScale_);
M
mamingshuai 已提交
128 129 130

    Rect trunc(invalidatedArea);
    if (trunc.Intersect(trunc, GetOrigRect())) {
N
niulihua 已提交
131
        DrawCommonCircle(gfxDstBuffer, trunc);
M
mamingshuai 已提交
132 133
    }
}
N
niulihua 已提交
134
} // namespace OHOS