You need to sign in or sign up before continuing.
ui_toggle_button.cpp 4.9 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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_toggle_button.h"
#include "common/image.h"
N
niulihua 已提交
18
#include "engines/gfx/gfx_engine_manager.h"
N
niulihua 已提交
19
#include "imgdecode/cache_manager.h"
M
mamingshuai 已提交
20 21

namespace OHOS {
22
UIToggleButton::UIToggleButton() : corner_(DEFAULT_CORNER_RADIUS), radius_(DEFAULT_CORNER_RADIUS - DEAFULT_RADIUS_DIFF),
M
mamingshuai 已提交
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
                                   rectWidth_(DEFAULT_WIDTH)
{
    image_[UNSELECTED].SetSrc("");
    image_[SELECTED].SetSrc("");
    Resize(width_, height_);
}

void UIToggleButton::SetState(bool state)
{
    if (state) {
        UICheckBox::SetState(SELECTED);
    } else {
        UICheckBox::SetState(UNSELECTED);
    }
    Invalidate();
}

void UIToggleButton::CalculateSize()
{
    int16_t width = GetWidth();
    int16_t height = GetHeight();
    if ((width_ == width) && (height_ == height)) {
        return;
    }
    width_ = width;
    height_ = height;
    int16_t minValue = (width_ > height_) ? height_ : width_;
    corner_ = DEFAULT_CORNER_RADIUS * minValue / DEFAULT_HOT_HEIGHT;
    int16_t radiusDiff = DEAFULT_RADIUS_DIFF * minValue / DEFAULT_HOT_WIDTH;
    radius_ = corner_ - radiusDiff;
    rectWidth_ = DEFAULT_WIDTH * minValue / DEFAULT_HOT_WIDTH;
}

N
niulihua 已提交
56
void UIToggleButton::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
M
mamingshuai 已提交
57 58
{
    if ((image_[SELECTED].GetSrcType() != IMG_SRC_UNKNOWN) && (image_[UNSELECTED].GetSrcType() != IMG_SRC_UNKNOWN)) {
N
niulihua 已提交
59
        UICheckBox::OnDraw(gfxDstBuffer, invalidatedArea);
M
mamingshuai 已提交
60 61
    } else {
        CalculateSize();
N
niulihua 已提交
62
        BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, GetRect(), invalidatedArea, *style_, opaScale_);
M
mamingshuai 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
        Rect contentRect = GetContentRect();
        int16_t dx = (width_ - rectWidth_) >> 1;
        int16_t dy = (height_ >> 1) - corner_;
        int16_t x = contentRect.GetX() + dx;
        int16_t y = contentRect.GetY() + dy;
        Rect rectMid;
        rectMid.SetRect(x, y, x + rectWidth_, y + (corner_ << 1) + 1);
        Rect trunc = invalidatedArea;
        bool isIntersect = trunc.Intersect(trunc, contentRect);
        switch (state_) {
            case SELECTED: {
                Style styleSelect = StyleDefault::GetBackgroundTransparentStyle();
                styleSelect.borderRadius_ = corner_;
                styleSelect.bgColor_ = Color::GetColorFromRGB(DEFAULT_BG_RED, DEFAULT_BG_GREEN, DEFAULT_BG_BLUE);
                styleSelect.bgOpa_ = OPA_OPAQUE;
                if (isIntersect) {
N
niulihua 已提交
79
                    BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, rectMid, trunc, styleSelect, opaScale_);
M
mamingshuai 已提交
80 81 82 83 84 85 86 87 88
                }
                ArcInfo arcInfoRight = {
                    { static_cast<int16_t>(x + rectWidth_ - corner_), static_cast<int16_t>(y + corner_) }, { 0 },
                    radius_, 0, CIRCLE_IN_DEGREE, nullptr
                };
                styleSelect.bgColor_ = Color::White();
                styleSelect.lineWidth_ = radius_;
                styleSelect.lineColor_ = Color::White();
                if (isIntersect) {
N
niulihua 已提交
89 90
                    BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfoRight, trunc,
                                                        styleSelect, OPA_OPAQUE, CapType::CAP_NONE);
M
mamingshuai 已提交
91 92 93 94 95 96 97 98 99
                }
                break;
            }
            case UNSELECTED: {
                Style styleUnSelect = StyleDefault::GetBackgroundTransparentStyle();
                styleUnSelect.bgColor_ = Color::White();
                styleUnSelect.bgOpa_ = DEFAULT_UNSELECTED_OPA;
                styleUnSelect.borderRadius_ = corner_;
                if (isIntersect) {
N
niulihua 已提交
100
                    BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, rectMid, trunc, styleUnSelect, opaScale_);
M
mamingshuai 已提交
101 102 103 104 105 106 107 108
                }
                ArcInfo arcInfoLeft = {
                    { static_cast<int16_t>(x + corner_), static_cast<int16_t>(y + corner_) }, { 0 }, radius_, 0,
                    CIRCLE_IN_DEGREE, nullptr
                };
                styleUnSelect.lineColor_ = Color::White();
                styleUnSelect.lineWidth_ = radius_;
                if (isIntersect) {
N
niulihua 已提交
109 110
                    BaseGfxEngine::GetInstance()->DrawArc(gfxDstBuffer, arcInfoLeft, trunc,
                                                          styleUnSelect, OPA_OPAQUE, CapType::CAP_NONE);
M
mamingshuai 已提交
111 112 113 114 115 116 117 118 119
                }
                break;
            }
            default:
                break;
        }
    }
}
}   // namespace OHOS