ui_box_progress.cpp 11.6 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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_box_progress.h"
N
niulihua 已提交
17 18
#include "draw/draw_utils.h"
#include "engines/gfx/gfx_engine_manager.h"
N
niulihua 已提交
19
#include "gfx_utils/graphic_log.h"
M
mamingshuai 已提交
20 21 22 23 24 25 26 27

namespace OHOS {
UIBoxProgress::UIBoxProgress()
    : progressWidth_(0), progressHeight_(0), isValidWidthSet_(false), isValidHeightSet_(false)
{
    SetDirection(Direction::DIR_LEFT_TO_RIGHT);
}

28 29
void UIBoxProgress::DrawValidRect(BufferInfo& gfxDstBuffer,
                                  const Image* image,
30 31 32 33
                                  const Rect& rect,
                                  const Rect& invalidatedArea,
                                  const Style& style,
                                  uint16_t radius)
M
mamingshuai 已提交
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
{
    Rect cordsTmp;
    if ((image != nullptr) && (image->GetSrcType() != IMG_SRC_UNKNOWN)) {
        ImageHeader header = {0};
        image->GetHeader(header);

        Rect area(rect);
        switch (direction_) {
            case Direction::DIR_LEFT_TO_RIGHT:
                cordsTmp.SetPosition(area.GetLeft() - radius, area.GetTop());
                break;
            case Direction::DIR_TOP_TO_BOTTOM:
                cordsTmp.SetPosition(area.GetLeft(), area.GetTop() - radius);
                break;
            case Direction::DIR_RIGHT_TO_LEFT:
                cordsTmp.SetPosition(area.GetRight() + radius - header.width, area.GetTop());
                break;
            case Direction::DIR_BOTTOM_TO_TOP:
                cordsTmp.SetPosition(area.GetLeft(), area.GetBottom() + radius - header.height);
                break;
            default:
                GRAPHIC_LOGE("UIBoxProgress: DrawValidRect direction Err!\n");
                break;
        }
        cordsTmp.SetHeight(header.height);
        cordsTmp.SetWidth(header.width);
        if (area.Intersect(area, invalidatedArea)) {
N
niulihua 已提交
61
            image->DrawImage(gfxDstBuffer, cordsTmp, area, style, opaScale_);
M
mamingshuai 已提交
62 63
        }
    } else {
N
niulihua 已提交
64
        BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, rect, invalidatedArea, style, opaScale_);
M
mamingshuai 已提交
65 66 67
    }

    if (style.lineCap_ == CapType::CAP_ROUND) {
N
niulihua 已提交
68
        DrawRoundCap(gfxDstBuffer, image, {cordsTmp.GetX(), cordsTmp.GetY()}, rect, invalidatedArea, radius, style);
M
mamingshuai 已提交
69 70 71
    }
}

72 73
void UIBoxProgress::DrawRoundCap(BufferInfo& gfxDstBuffer,
                                 const Image* image,
74 75 76 77 78
                                 const Point& imgPos,
                                 const Rect& rect,
                                 const Rect& invalidatedArea,
                                 uint16_t radius,
                                 const Style& style)
M
mamingshuai 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
{
    Point leftTop;
    Point leftBottom;
    Point rightTop;
    Point rightBottom;

    switch (direction_) {
        case Direction::DIR_LEFT_TO_RIGHT:
        case Direction::DIR_RIGHT_TO_LEFT: {
            leftTop.x = rect.GetLeft() - 1;
            leftTop.y = rect.GetTop() + radius - 1;
            leftBottom.x = leftTop.x;
            leftBottom.y = rect.GetBottom() - radius + 1;
            rightTop.x = rect.GetRight() + 1;
            rightTop.y = leftTop.y;
            rightBottom.x = rightTop.x;
            rightBottom.y = leftBottom.y;
            break;
        }

        case Direction::DIR_TOP_TO_BOTTOM:
        case Direction::DIR_BOTTOM_TO_TOP: {
            leftTop.x = rect.GetLeft() + radius - 1;
            leftTop.y = rect.GetTop() - 1;
            rightTop.x = rect.GetRight() - radius + 1;
            rightTop.y = leftTop.y;
            leftBottom.x = leftTop.x;
            leftBottom.y = rect.GetBottom() + 1;
            rightBottom.x = rightTop.x;
            rightBottom.y = leftBottom.y;
            break;
        }
        default:
            GRAPHIC_LOGE("UIBoxProgress: DrawRoundCap direction Err!\n");
            break;
    }

    Style capStyle = style;
    capStyle.lineWidth_ = radius;
    capStyle.lineColor_ = style.bgColor_;
Y
YueBiang 已提交
119 120 121 122 123 124
    if ((image != nullptr) && (image->GetSrcType() != IMG_SRC_UNKNOWN)) {
        capStyle.lineOpa_ = style.imageOpa_;
    } else {
        capStyle.lineOpa_ = style.bgOpa_;
    }

M
mamingshuai 已提交
125 126 127 128 129
    ArcInfo arcInfo = {{0}};
    arcInfo.radius = radius;
    arcInfo.imgPos = imgPos;
    arcInfo.imgSrc = image;

Y
YueBiang 已提交
130 131 132 133
    if (rect.GetWidth() % 2 == 0) { // 2: determine the odd or even number of the width
        arcInfo.center = leftTop;
        arcInfo.startAngle = THREE_QUARTER_IN_DEGREE;
        arcInfo.endAngle = 0;
N
niulihua 已提交
134 135
        BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, capStyle, opaScale_,
                                              CapType::CAP_NONE);
Y
YueBiang 已提交
136 137 138 139

        arcInfo.center = leftBottom;
        arcInfo.startAngle = SEMICIRCLE_IN_DEGREE;
        arcInfo.endAngle = THREE_QUARTER_IN_DEGREE;
N
niulihua 已提交
140 141
        BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, capStyle, opaScale_,
                                              CapType::CAP_NONE);
Y
YueBiang 已提交
142 143 144 145

        arcInfo.center = rightTop;
        arcInfo.startAngle = 0;
        arcInfo.endAngle = QUARTER_IN_DEGREE;
N
niulihua 已提交
146 147
        BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, capStyle, opaScale_,
                                              CapType::CAP_NONE);
Y
YueBiang 已提交
148 149 150 151

        arcInfo.center = rightBottom;
        arcInfo.startAngle = QUARTER_IN_DEGREE;
        arcInfo.endAngle = SEMICIRCLE_IN_DEGREE;
N
niulihua 已提交
152 153
        BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, capStyle, opaScale_,
                                              CapType::CAP_NONE);
Y
YueBiang 已提交
154 155 156 157 158 159 160
    } else {
        switch (direction_) {
            case Direction::DIR_LEFT_TO_RIGHT:
            case Direction::DIR_RIGHT_TO_LEFT: {
                arcInfo.center = leftTop;
                arcInfo.startAngle = SEMICIRCLE_IN_DEGREE;
                arcInfo.endAngle = 0;
N
niulihua 已提交
161 162
                BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, capStyle, opaScale_,
                                                      CapType::CAP_NONE);
Y
YueBiang 已提交
163 164 165 166

                arcInfo.center = leftBottom;
                arcInfo.startAngle = 0;
                arcInfo.endAngle = SEMICIRCLE_IN_DEGREE;
N
niulihua 已提交
167 168
                BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, capStyle, opaScale_,
                                                      CapType::CAP_NONE);
Y
YueBiang 已提交
169 170 171 172 173 174 175 176
                break;
            }

            case Direction::DIR_TOP_TO_BOTTOM:
            case Direction::DIR_BOTTOM_TO_TOP: {
                arcInfo.center = leftTop;
                arcInfo.startAngle = THREE_QUARTER_IN_DEGREE;
                arcInfo.endAngle = QUARTER_IN_DEGREE;
N
niulihua 已提交
177 178
                BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, capStyle, opaScale_,
                                                      CapType::CAP_NONE);
Y
YueBiang 已提交
179 180 181 182

                arcInfo.center = leftBottom;
                arcInfo.startAngle = QUARTER_IN_DEGREE;
                arcInfo.endAngle = THREE_QUARTER_IN_DEGREE;
N
niulihua 已提交
183 184
                BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, capStyle, opaScale_,
                                                      CapType::CAP_NONE);
Y
YueBiang 已提交
185 186 187 188 189 190 191
                break;
            }
            default:
                GRAPHIC_LOGE("UIBoxProgress: DrawRoundCap direction Err!\n");
                break;
        }
    }
M
mamingshuai 已提交
192 193
}

194 195 196 197 198
void UIBoxProgress::GetBackgroundParam(Point& startPoint,
                                       int16_t& width,
                                       int16_t& height,
                                       uint16_t& radius,
                                       const Style& style)
M
mamingshuai 已提交
199 200
{
    Rect rect = GetOrigRect();
201 202 203 204
    // 2: Half of the gap
    startPoint.x = rect.GetLeft() + style_->borderWidth_ + style_->paddingLeft_ + (GetWidth() - progressWidth_) / 2;
    // 2: Half of the gap
    startPoint.y = rect.GetTop() + style_->borderWidth_ + style_->paddingTop_ + (GetHeight() - progressHeight_) / 2;
M
mamingshuai 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

    radius = 0;
    width = progressWidth_;
    height = progressHeight_;
    if (style.lineCap_ == CapType::CAP_ROUND) {
        switch (direction_) {
            case Direction::DIR_LEFT_TO_RIGHT:
            case Direction::DIR_RIGHT_TO_LEFT:
                radius = (progressHeight_ + 1) >> 1;
                width -= radius << 1;
                startPoint.x += radius;
                break;
            case Direction::DIR_TOP_TO_BOTTOM:
            case Direction::DIR_BOTTOM_TO_TOP:
                radius = (progressWidth_ + 1) >> 1;
                height -= radius << 1;
                startPoint.y += radius;
                break;
            default:
                GRAPHIC_LOGE("UIBoxProgress: GetBackgroundParam direction Err!\n");
                return;
        }
    }
}

N
niulihua 已提交
230
void UIBoxProgress::DrawBackground(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
M
mamingshuai 已提交
231 232 233 234 235 236 237 238 239
{
    Point startPoint;
    int16_t progressWidth;
    int16_t progressHeight;
    uint16_t radius;
    GetBackgroundParam(startPoint, progressWidth, progressHeight, radius, *backgroundStyle_);

    Rect coords(startPoint.x, startPoint.y, startPoint.x + progressWidth - 1, startPoint.y + progressHeight - 1);

N
niulihua 已提交
240
    DrawValidRect(gfxDstBuffer, backgroundImage_, coords, invalidatedArea, *backgroundStyle_, radius);
M
mamingshuai 已提交
241 242
}

N
niulihua 已提交
243
void UIBoxProgress::DrawForeground(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, Rect& coords)
M
mamingshuai 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
{
    Point startPoint;
    int16_t progressWidth;
    int16_t progressHeight;
    uint16_t radius;
    GetBackgroundParam(startPoint, progressWidth, progressHeight, radius, *foregroundStyle_);
    int16_t length;

    switch (direction_) {
        case Direction::DIR_LEFT_TO_RIGHT: {
            length = GetCurrentPos(progressWidth - 1);
            coords.SetRect(startPoint.x, startPoint.y, startPoint.x + length, startPoint.y + progressHeight - 1);
            break;
        }
        case Direction::DIR_RIGHT_TO_LEFT: {
            length = GetCurrentPos(progressWidth - 1);
            coords.SetRect(startPoint.x + progressWidth - 1 - length,
                startPoint.y, startPoint.x + progressWidth - 1, startPoint.y + progressHeight - 1);
            break;
        }
        case Direction::DIR_TOP_TO_BOTTOM: {
            length = GetCurrentPos(progressHeight - 1);
            coords.SetRect(startPoint.x, startPoint.y, startPoint.x + progressWidth - 1, startPoint.y + length);
            break;
        }
        case Direction::DIR_BOTTOM_TO_TOP: {
            length = GetCurrentPos(progressHeight - 1);
            coords.SetRect(startPoint.x, startPoint.y + progressHeight - 1 - length,
                startPoint.x + progressWidth - 1, startPoint.y + progressHeight - 1);
            break;
        }
        default: {
            GRAPHIC_LOGE("UIBoxProgress: DrawForeground direction Err!\n");
            return;
        }
    }

N
niulihua 已提交
281
    DrawValidRect(gfxDstBuffer, foregroundImage_, coords, invalidatedArea, *foregroundStyle_, radius);
M
mamingshuai 已提交
282 283
}

N
niulihua 已提交
284
void UIBoxProgress::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
M
mamingshuai 已提交
285
{
286
    UIView::OnDraw(gfxDstBuffer, invalidatedArea);
287
    if (enableBackground_) {
288
        DrawBackground(gfxDstBuffer, invalidatedArea);
289
    }
M
mamingshuai 已提交
290

291 292
    if ((lastValue_ - rangeMin_ != 0) || (foregroundStyle_->lineCap_ == CapType::CAP_ROUND)) {
        Rect coords;
293
        DrawForeground(gfxDstBuffer, invalidatedArea, coords);
M
mamingshuai 已提交
294 295
    }
}
N
niulihua 已提交
296
} // namespace OHOS