ui_box_progress.cpp 11.9 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
{
    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:
55
                GRAPHIC_LOGE("UIBoxProgress: DrawValidRect direction Err!\n");
M
mamingshuai 已提交
56 57 58 59 60
                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
{
    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:
112
            GRAPHIC_LOGE("UIBoxProgress: DrawRoundCap direction Err!\n");
M
mamingshuai 已提交
113 114 115 116 117 118
            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 134 135 136 137 138 139
    bool isEvenLen = false;
    if (direction_ == Direction::DIR_LEFT_TO_RIGHT || direction_ == Direction::DIR_RIGHT_TO_LEFT) {
        if (rect.GetHeight() % 2 == 0) { // 2: determine the odd or even number of the height
            isEvenLen = true;
        }
    } else if (rect.GetWidth() % 2 == 0) { // 2: determine the odd or even number of the width
        isEvenLen = true;
    }

    if (isEvenLen) {
Y
YueBiang 已提交
140 141 142
        arcInfo.center = leftTop;
        arcInfo.startAngle = THREE_QUARTER_IN_DEGREE;
        arcInfo.endAngle = 0;
N
niulihua 已提交
143 144
        BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, capStyle, opaScale_,
                                              CapType::CAP_NONE);
Y
YueBiang 已提交
145 146 147 148

        arcInfo.center = leftBottom;
        arcInfo.startAngle = SEMICIRCLE_IN_DEGREE;
        arcInfo.endAngle = THREE_QUARTER_IN_DEGREE;
N
niulihua 已提交
149 150
        BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, capStyle, opaScale_,
                                              CapType::CAP_NONE);
Y
YueBiang 已提交
151 152 153 154

        arcInfo.center = rightTop;
        arcInfo.startAngle = 0;
        arcInfo.endAngle = QUARTER_IN_DEGREE;
N
niulihua 已提交
155 156
        BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, capStyle, opaScale_,
                                              CapType::CAP_NONE);
Y
YueBiang 已提交
157 158 159 160

        arcInfo.center = rightBottom;
        arcInfo.startAngle = QUARTER_IN_DEGREE;
        arcInfo.endAngle = SEMICIRCLE_IN_DEGREE;
N
niulihua 已提交
161 162
        BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, capStyle, opaScale_,
                                              CapType::CAP_NONE);
Y
YueBiang 已提交
163 164 165 166 167 168 169
    } 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 已提交
170 171
                BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, capStyle, opaScale_,
                                                      CapType::CAP_NONE);
Y
YueBiang 已提交
172

Y
YueBiang 已提交
173
                arcInfo.center = rightTop;
Y
YueBiang 已提交
174 175
                arcInfo.startAngle = 0;
                arcInfo.endAngle = SEMICIRCLE_IN_DEGREE;
N
niulihua 已提交
176 177
                BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, capStyle, opaScale_,
                                                      CapType::CAP_NONE);
Y
YueBiang 已提交
178 179 180 181 182 183 184 185
                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 已提交
186 187
                BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, capStyle, opaScale_,
                                                      CapType::CAP_NONE);
Y
YueBiang 已提交
188 189 190 191

                arcInfo.center = leftBottom;
                arcInfo.startAngle = QUARTER_IN_DEGREE;
                arcInfo.endAngle = THREE_QUARTER_IN_DEGREE;
N
niulihua 已提交
192 193
                BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfo, invalidatedArea, capStyle, opaScale_,
                                                      CapType::CAP_NONE);
Y
YueBiang 已提交
194 195 196
                break;
            }
            default:
197
                GRAPHIC_LOGE("UIBoxProgress: DrawRoundCap direction Err!\n");
Y
YueBiang 已提交
198 199 200
                break;
        }
    }
M
mamingshuai 已提交
201 202
}

203 204 205 206 207
void UIBoxProgress::GetBackgroundParam(Point& startPoint,
                                       int16_t& width,
                                       int16_t& height,
                                       uint16_t& radius,
                                       const Style& style)
M
mamingshuai 已提交
208 209
{
    Rect rect = GetOrigRect();
210 211 212 213
    // 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 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

    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:
233
                GRAPHIC_LOGE("UIBoxProgress: GetBackgroundParam direction Err!\n");
M
mamingshuai 已提交
234 235 236 237 238
                return;
        }
    }
}

N
niulihua 已提交
239
void UIBoxProgress::DrawBackground(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
M
mamingshuai 已提交
240 241 242 243 244 245 246 247 248
{
    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 已提交
249
    DrawValidRect(gfxDstBuffer, backgroundImage_, coords, invalidatedArea, *backgroundStyle_, radius);
M
mamingshuai 已提交
250 251
}

N
niulihua 已提交
252
void UIBoxProgress::DrawForeground(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, Rect& coords)
M
mamingshuai 已提交
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 281 282 283 284
{
    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: {
285
            GRAPHIC_LOGE("UIBoxProgress: DrawForeground direction Err!\n");
M
mamingshuai 已提交
286 287 288 289
            return;
        }
    }

N
niulihua 已提交
290
    DrawValidRect(gfxDstBuffer, foregroundImage_, coords, invalidatedArea, *foregroundStyle_, radius);
M
mamingshuai 已提交
291 292
}

N
niulihua 已提交
293
void UIBoxProgress::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
M
mamingshuai 已提交
294
{
295
    UIView::OnDraw(gfxDstBuffer, invalidatedArea);
296
    if (enableBackground_) {
297
        DrawBackground(gfxDstBuffer, invalidatedArea);
298
    }
M
mamingshuai 已提交
299

300 301
    if ((lastValue_ - rangeMin_ != 0) || (foregroundStyle_->lineCap_ == CapType::CAP_ROUND)) {
        Rect coords;
302
        DrawForeground(gfxDstBuffer, invalidatedArea, coords);
M
mamingshuai 已提交
303 304
    }
}
N
niulihua 已提交
305
} // namespace OHOS