ui_arc_scroll_bar.cpp 4.2 KB
Newer Older
Y
YueBiang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (c) 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_arc_scroll_bar.h"

#include "draw/draw_arc.h"
#include "engines/gfx/gfx_engine_manager.h"
20
#include "gfx_utils/graphic_types.h"
Y
YueBiang 已提交
21

Y
YueBiang 已提交
22
namespace {
23 24 25 26
constexpr uint16_t RIGHT_SIDE_START_ANGLE_IN_DEGREE = 60;
constexpr uint16_t RIGHT_SIDE_END_ANGLE_IN_DEGREE = 120;
constexpr uint16_t LEFT_SIDE_START_ANGLE_IN_DEGREE = 240;
constexpr uint16_t LEFT_SIDE_END_ANGLE_IN_DEGREE = 300;
Y
YueBiang 已提交
27 28 29
constexpr uint16_t SCROLL_BAR_MIN_ARC = 10;
} // namespace

Y
YueBiang 已提交
30
namespace OHOS {
Y
YueBiang 已提交
31 32
UIArcScrollBar::UIArcScrollBar()
    : radius_(0),
Y
YueBiang 已提交
33
      width_(0),
34 35 36
      startAngle_(RIGHT_SIDE_START_ANGLE_IN_DEGREE),
      endAngle_(RIGHT_SIDE_END_ANGLE_IN_DEGREE),
      center_({0, 0}),
37 38 39
      side_(SCROLL_BAR_RIGHT_SIDE)
{
}
Y
YueBiang 已提交
40

Y
YueBiang 已提交
41 42 43 44 45 46 47 48 49 50
void UIArcScrollBar::SetPosition(int16_t x, int16_t y, int16_t width, int16_t radius)
{
    if ((width > 0) && (radius > 0)) {
        center_.x = x;
        center_.y = y;
        width_ = width;
        radius_ = radius;
    }
}

51 52 53
void UIArcScrollBar::SetScrollBarSide(uint8_t side)
{
    if (side == SCROLL_BAR_RIGHT_SIDE) {
54
        startAngle_ = RIGHT_SIDE_START_ANGLE_IN_DEGREE;
55 56
        endAngle_ = RIGHT_SIDE_END_ANGLE_IN_DEGREE;
    } else {
57
        startAngle_ = LEFT_SIDE_START_ANGLE_IN_DEGREE;
58 59 60 61 62
        endAngle_ = LEFT_SIDE_END_ANGLE_IN_DEGREE;
    }
    side_ = side;
}

Y
YueBiang 已提交
63 64 65 66 67 68 69 70 71 72
void UIArcScrollBar::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, uint8_t backgroundOpa)
{
    // 8: Shift right 8 bits
    backgroundOpa = (backgroundOpa == OPA_OPAQUE) ? opacity_ : (static_cast<uint16_t>(backgroundOpa) * opacity_) >> 8;
    DrawBackground(gfxDstBuffer, invalidatedArea, backgroundOpa);
    DrawForeground(gfxDstBuffer, invalidatedArea, backgroundOpa);
}

void UIArcScrollBar::DrawForeground(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, uint8_t backgroundOpa)
{
73
    uint16_t foregoundAngleRange = static_cast<uint16_t>(foregroundProportion_ * (endAngle_ - startAngle_));
Y
YueBiang 已提交
74 75 76
    if (foregoundAngleRange < SCROLL_BAR_MIN_ARC) {
        foregoundAngleRange = SCROLL_BAR_MIN_ARC;
    }
77 78 79 80 81 82 83
    int16_t startAngle;
    int16_t endAngle;
    int16_t minAngle;
    int16_t maxAngle;
    if (side_ == SCROLL_BAR_RIGHT_SIDE) {
        minAngle = startAngle_;
        maxAngle = endAngle_ - foregoundAngleRange;
84
        startAngle = minAngle + static_cast<int16_t>(scrollProgress_ * (maxAngle - minAngle));
85 86 87 88
        endAngle = startAngle + foregoundAngleRange;
    } else {
        maxAngle = endAngle_;
        minAngle = startAngle_ + foregoundAngleRange;
89
        endAngle = maxAngle - static_cast<int16_t>(scrollProgress_ * (maxAngle - minAngle));
90 91 92
        startAngle = endAngle - foregoundAngleRange;
    }
    if ((startAngle > endAngle_) || (endAngle < startAngle_)) {
Y
YueBiang 已提交
93 94
        return;
    }
N
niulihua 已提交
95
    ArcInfo arcInfo = {{0, 0}, {0, 0}, 0, 0, 0, nullptr};
Y
YueBiang 已提交
96
    arcInfo.radius = (radius_ > 0) ? (radius_ - 1) : 0;
Y
YueBiang 已提交
97
    arcInfo.center = center_;
98 99
    arcInfo.startAngle = MATH_MAX(startAngle, startAngle_);
    arcInfo.endAngle = MATH_MIN(endAngle, endAngle_);
Y
YueBiang 已提交
100 101 102 103 104 105
    BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, *foregroundStyle_, backgroundOpa,
                                          foregroundStyle_->lineCap_);
}

void UIArcScrollBar::DrawBackground(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, uint8_t backgroundOpa)
{
N
niulihua 已提交
106
    ArcInfo arcInfo = {{0, 0}, {0, 0}, 0, 0, 0, nullptr};
Y
YueBiang 已提交
107 108
    arcInfo.radius = radius_;
    arcInfo.center = center_;
109 110
    arcInfo.startAngle = startAngle_;
    arcInfo.endAngle = endAngle_;
Y
YueBiang 已提交
111 112 113 114
    BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, *backgroundStyle_, backgroundOpa,
                                          backgroundStyle_->lineCap_);
}
} // namespace OHOS