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

Y
YueBiang 已提交
21 22 23 24 25 26
namespace {
constexpr uint16_t START_ANGLE_IN_DEGREE = 60;
constexpr uint16_t END_ANGLE_IN_DEGREE = 120;
constexpr uint16_t SCROLL_BAR_MIN_ARC = 10;
} // namespace

Y
YueBiang 已提交
27
namespace OHOS {
Y
YueBiang 已提交
28 29
UIArcScrollBar::UIArcScrollBar()
    : radius_(0),
Y
YueBiang 已提交
30
      width_(0),
Y
YueBiang 已提交
31 32 33 34
      startAngle_(START_ANGLE_IN_DEGREE),
      endAngle_(END_ANGLE_IN_DEGREE),
      center_({0, 0}) {}

Y
YueBiang 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
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;
    }
}

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)
{
    uint16_t foregoundAngleRange = foregroundProportion_ * (END_ANGLE_IN_DEGREE - START_ANGLE_IN_DEGREE);
Y
YueBiang 已提交
56 57 58
    if (foregoundAngleRange < SCROLL_BAR_MIN_ARC) {
        foregoundAngleRange = SCROLL_BAR_MIN_ARC;
    }
Y
YueBiang 已提交
59 60 61 62 63 64 65 66 67 68
    int16_t minAngle = START_ANGLE_IN_DEGREE;
    int16_t maxAngle = END_ANGLE_IN_DEGREE - foregoundAngleRange;
    int16_t startAngle = minAngle + scrollProgress_ * (maxAngle - minAngle);
    int16_t endAngle = startAngle + foregoundAngleRange;

    if ((startAngle > END_ANGLE_IN_DEGREE) || (endAngle < START_ANGLE_IN_DEGREE)) {
        return;
    }

    ArcInfo arcInfo = {0};
Y
YueBiang 已提交
69
    arcInfo.radius = (radius_ > 0) ? (radius_ - 1) : 0;
Y
YueBiang 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    arcInfo.center = center_;
    arcInfo.startAngle = MATH_MAX(startAngle, START_ANGLE_IN_DEGREE);
    arcInfo.endAngle = MATH_MIN(endAngle, END_ANGLE_IN_DEGREE);
    BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, *foregroundStyle_, backgroundOpa,
                                          foregroundStyle_->lineCap_);
}

void UIArcScrollBar::DrawBackground(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, uint8_t backgroundOpa)
{
    ArcInfo arcInfo = {0};
    arcInfo.radius = radius_;
    arcInfo.center = center_;
    arcInfo.startAngle = START_ANGLE_IN_DEGREE;
    arcInfo.endAngle = END_ANGLE_IN_DEGREE;
    BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, *backgroundStyle_, backgroundOpa,
                                          backgroundStyle_->lineCap_);
}
} // namespace OHOS