ui_button.cpp 10.5 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_button.h"
L
l00417912 已提交
17
#include "animator/interpolation.h"
M
mamingshuai 已提交
18 19
#include "common/image.h"
#include "draw/draw_image.h"
N
niulihua 已提交
20
#include "engines/gfx/gfx_engine_manager.h"
Z
zhangguyuan 已提交
21 22
#include "gfx_utils/graphic_log.h"
#include "gfx_utils/style.h"
M
mamingshuai 已提交
23 24 25 26 27
#include "imgdecode/cache_manager.h"
#include "themes/theme_manager.h"

namespace OHOS {
UIButton::UIButton()
28
    : defaultImgSrc_(nullptr),
M
mamingshuai 已提交
29 30 31 32 33 34 35 36
      triggeredImgSrc_(nullptr),
      currentImgSrc_(ButtonImageSrc::BTN_IMAGE_DEFAULT),
      imgX_(0),
      imgY_(0),
      contentWidth_(0),
      contentHeight_(0),
      state_(RELEASED),
      styleState_(RELEASED),
37 38 39
#if DEFAULT_ANIMATION
      animator_(*this),
#endif
M
mamingshuai 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
      buttonStyleAllocFlag_(false)
{
    touchable_ = true;
    SetupThemeStyles();
}

UIButton::~UIButton()
{
    if (defaultImgSrc_ != nullptr) {
        delete defaultImgSrc_;
        defaultImgSrc_ = nullptr;
    }

    if (triggeredImgSrc_ != nullptr) {
        delete triggeredImgSrc_;
        triggeredImgSrc_ = nullptr;
    }

    if (buttonStyleAllocFlag_) {
        for (uint8_t i = 0; i < BTN_STATE_NUM; i++) {
            delete buttonStyles_[i];
            buttonStyles_[i] = nullptr;
        }
        buttonStyleAllocFlag_ = false;
    }
}

N
niulihua 已提交
67
void UIButton::DrawImg(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, OpacityType opaScale)
M
mamingshuai 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
{
    const Image* image = GetCurImageSrc();
    if (image == nullptr) {
        return;
    }

    ImageHeader header = {0};
    image->GetHeader(header);
    Rect coords;
    Rect viewRect = GetContentRect();
    coords.SetLeft(viewRect.GetLeft() + GetImageX());
    coords.SetTop(viewRect.GetTop() + GetImageY());
    coords.SetWidth(header.width);
    coords.SetHeight(header.height);

    Rect trunc(invalidatedArea);
    if (trunc.Intersect(trunc, viewRect)) {
N
niulihua 已提交
85
        image->DrawImage(gfxDstBuffer, coords, trunc, *buttonStyles_[state_], opaScale);
M
mamingshuai 已提交
86 87 88
    }
}

N
niulihua 已提交
89
void UIButton::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
M
mamingshuai 已提交
90 91
{
    OpacityType opa = GetMixOpaScale();
N
niulihua 已提交
92 93
    BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, GetOrigRect(), invalidatedArea, *buttonStyles_[state_], opa);
    DrawImg(gfxDstBuffer, invalidatedArea, opa);
M
mamingshuai 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
}

void UIButton::SetupThemeStyles()
{
    Theme* theme = ThemeManager::GetInstance().GetCurrent();

    if (theme == nullptr) {
        buttonStyles_[RELEASED] = &(StyleDefault::GetButtonReleasedStyle());
        buttonStyles_[PRESSED] = &(StyleDefault::GetButtonPressedStyle());
        buttonStyles_[INACTIVE] = &(StyleDefault::GetButtonInactiveStyle());
    } else {
        buttonStyles_[RELEASED] = &(theme->GetButtonStyle().released);
        buttonStyles_[PRESSED] = &(theme->GetButtonStyle().pressed);
        buttonStyles_[INACTIVE] = &(theme->GetButtonStyle().inactive);
    }
109
    style_ = buttonStyles_[RELEASED];
M
mamingshuai 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
}

int64_t UIButton::GetStyle(uint8_t key) const
{
    return GetStyleForState(key, styleState_);
}

void UIButton::SetStyle(uint8_t key, int64_t value)
{
    SetStyleForState(key, value, styleState_);
}

int64_t UIButton::GetStyleForState(uint8_t key, ButtonState state) const
{
    if (state < BTN_STATE_NUM) {
        return (buttonStyles_[state])->GetStyle(key);
    }
    return 0;
}

void UIButton::SetStyleForState(uint8_t key, int64_t value, ButtonState state)
{
    if (state < BTN_STATE_NUM) {
        if (!buttonStyleAllocFlag_) {
            for (uint8_t i = 0; i < BTN_STATE_NUM; i++) {
                Style styleSaved = *buttonStyles_[i];
                buttonStyles_[i] = new Style;
                if (buttonStyles_[i] == nullptr) {
138
                    GRAPHIC_LOGE("new Style fail");
M
mamingshuai 已提交
139 140 141 142 143 144
                    return;
                }
                *(buttonStyles_[i]) = styleSaved;
            }
            buttonStyleAllocFlag_ = true;
        }
145
        style_ = buttonStyles_[RELEASED];
M
mamingshuai 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
        int16_t width = GetWidth();
        int16_t height = GetHeight();
        buttonStyles_[state]->SetStyle(key, value);
        switch (key) {
            case STYLE_BORDER_WIDTH: {
                SetWidth(width);
                SetHeight(height);
                break;
            }
            case STYLE_PADDING_LEFT:
            case STYLE_PADDING_RIGHT: {
                SetWidth(width);
                break;
            }
            case STYLE_PADDING_TOP:
            case STYLE_PADDING_BOTTOM: {
                SetHeight(height);
                break;
            }
            default:
                break;
        }
    }
}

bool UIButton::OnPressEvent(const PressEvent& event)
{
    currentImgSrc_ = ButtonImageSrc::BTN_IMAGE_TRIGGERED;
    SetState(PRESSED);
    Resize(contentWidth_, contentHeight_);
    Invalidate();
L
l00417912 已提交
177 178 179
#if DEFAULT_ANIMATION
    animator_.Start();
#endif
M
mamingshuai 已提交
180 181 182 183 184 185 186 187 188
    return UIView::OnPressEvent(event);
}

bool UIButton::OnReleaseEvent(const ReleaseEvent& event)
{
    currentImgSrc_ = ButtonImageSrc::BTN_IMAGE_DEFAULT;
    SetState(RELEASED);
    Resize(contentWidth_, contentHeight_);
    Invalidate();
L
l00417912 已提交
189 190 191
#if DEFAULT_ANIMATION
    animator_.Start();
#endif
M
mamingshuai 已提交
192 193 194 195 196 197 198 199 200
    return UIView::OnReleaseEvent(event);
}

bool UIButton::OnCancelEvent(const CancelEvent& event)
{
    currentImgSrc_ = ButtonImageSrc::BTN_IMAGE_DEFAULT;
    SetState(RELEASED);
    Resize(contentWidth_, contentHeight_);
    Invalidate();
L
l00417912 已提交
201 202 203
#if DEFAULT_ANIMATION
    animator_.Start();
#endif
M
mamingshuai 已提交
204 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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    return UIView::OnCancelEvent(event);
}

const Image* UIButton::GetCurImageSrc() const
{
    if (currentImgSrc_ == ButtonImageSrc::BTN_IMAGE_DEFAULT) {
        return defaultImgSrc_;
    } else if (currentImgSrc_ == ButtonImageSrc::BTN_IMAGE_TRIGGERED) {
        return triggeredImgSrc_;
    } else {
        return nullptr;
    }
}

void UIButton::SetImageSrc(const char* defaultImgSrc, const char* triggeredImgSrc)
{
    if (!InitImage()) {
        return;
    }
    defaultImgSrc_->SetSrc(defaultImgSrc);
    triggeredImgSrc_->SetSrc(triggeredImgSrc);
}

void UIButton::SetImageSrc(const ImageInfo* defaultImgSrc, const ImageInfo* triggeredImgSrc)
{
    if (!InitImage()) {
        return;
    }
    defaultImgSrc_->SetSrc(defaultImgSrc);
    triggeredImgSrc_->SetSrc(triggeredImgSrc);
}

void UIButton::Disable()
{
    SetState(INACTIVE);
    touchable_ = false;
}

void UIButton::Enable()
{
    SetState(RELEASED);
    touchable_ = true;
}

void UIButton::SetState(ButtonState state)
{
    state_ = state;
邓志豪 已提交
251
    style_ = buttonStyles_[state_];
M
mamingshuai 已提交
252 253 254 255 256 257 258 259
    Invalidate();
}

bool UIButton::InitImage()
{
    if (defaultImgSrc_ == nullptr) {
        defaultImgSrc_ = new Image();
        if (defaultImgSrc_ == nullptr) {
260
            GRAPHIC_LOGE("new Image fail");
M
mamingshuai 已提交
261 262 263 264 265 266
            return false;
        }
    }
    if (triggeredImgSrc_ == nullptr) {
        triggeredImgSrc_ = new Image();
        if (triggeredImgSrc_ == nullptr) {
267
            GRAPHIC_LOGE("new Image fail");
M
mamingshuai 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280
            return false;
        }
    }
    return true;
}

bool UIButton::OnPreDraw(Rect& invalidatedArea) const
{
    Rect rect(GetRect());
    int16_t r = buttonStyles_[styleState_]->borderRadius_;
    if (r == COORD_MAX) {
        return true;
    }
L
l00417912 已提交
281

M
mamingshuai 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294
    if (r != 0) {
        r = ((r & 0x1) == 0) ? (r >> 1) : ((r + 1) >> 1);
        rect.SetLeft(rect.GetX() + r);
        rect.SetWidth(rect.GetWidth() - r);
        rect.SetTop(rect.GetY() + r);
        rect.SetHeight(rect.GetHeight() - r);
    }
    if (rect.IsContains(invalidatedArea)) {
        return true;
    }
    invalidatedArea.Intersect(invalidatedArea, rect);
    return false;
}
L
l00417912 已提交
295 296

#if DEFAULT_ANIMATION
N
niulihua 已提交
297
void UIButton::OnPostDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
L
l00417912 已提交
298 299
{
    if (state_ == ButtonState::PRESSED) {
N
niulihua 已提交
300
        animator_.DrawMask(gfxDstBuffer, invalidatedArea);
L
l00417912 已提交
301
    }
P
pssea 已提交
302
    UIView::OnPostDraw(gfxDstBuffer, invalidatedArea);
L
l00417912 已提交
303 304 305
}

namespace {
306
constexpr float FULL_SCALE = 1.0f;
L
l00417912 已提交
307 308 309 310
constexpr float SHRINK_SCALE = 0.8f;
constexpr uint32_t SHRINK_DURATION = 150;
constexpr uint32_t RECOVER_DURATION = 200;
constexpr int64_t MASK_OPA = 25;
311
constexpr float BEZIER_CONTROL = 0.2f;
L
l00417912 已提交
312 313 314 315
} // namespace

void UIButton::ButtonAnimator::Start()
{
316 317 318 319 320 321 322
    bool isReverse = (button_.state_ == UIButton::ButtonState::PRESSED);
    float targetScale = isReverse ? SHRINK_SCALE : FULL_SCALE;
    if ((animator_.GetState() == Animator::STOP) && FloatEqual(targetScale, scale_)) {
        return;
    }

    if (isReverse) {
L
l00417912 已提交
323 324 325 326 327
        animator_.SetTime(SHRINK_DURATION);
    } else {
        animator_.SetTime(RECOVER_DURATION);
    }
    animator_.Start();
328 329 330 331 332 333
    /* reverse the animator direction */
    float x = isReverseAnimation_ ? (FULL_SCALE - scale_) : (scale_ - SHRINK_SCALE);
    float y = x / (FULL_SCALE - SHRINK_SCALE);
    x = Interpolation::GetBezierY(FULL_SCALE - y, 0, BEZIER_CONTROL, FULL_SCALE, BEZIER_CONTROL);
    animator_.SetRunTime(static_cast<uint32_t>(animator_.GetTime() * x));
    isReverseAnimation_ = isReverse;
L
l00417912 已提交
334 335
}

N
niulihua 已提交
336
void UIButton::ButtonAnimator::DrawMask(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
L
l00417912 已提交
337 338 339 340 341 342
{
    Style maskStyle;
    maskStyle.SetStyle(STYLE_BACKGROUND_COLOR, Color::White().full);
    maskStyle.SetStyle(STYLE_BACKGROUND_OPA, MASK_OPA);
    maskStyle.SetStyle(STYLE_BORDER_RADIUS, button_.GetStyle(STYLE_BORDER_RADIUS));
    OpacityType opa = button_.GetMixOpaScale();
N
niulihua 已提交
343
    BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, button_.GetRect(), invalidatedArea, maskStyle, opa);
L
l00417912 已提交
344 345 346 347 348 349 350 351 352 353 354
}

static inline void ScaleButton(UIButton& button, float scale)
{
    Vector2<float> scaleValue_ = {scale, scale};
    Vector2<float> centrePoint(button.GetWidth() / 2.0f, button.GetHeight() / 2.0f);
    button.Scale(scaleValue_, centrePoint);
}

void UIButton::ButtonAnimator::Callback(UIView* view)
{
355 356 357
    float x = static_cast<float>(animator_.GetRunTime()) / animator_.GetTime();
    float offset = Interpolation::GetBezierY(x, BEZIER_CONTROL, 0, BEZIER_CONTROL, FULL_SCALE);
    float scale = (FULL_SCALE - SHRINK_SCALE) * offset;
L
l00417912 已提交
358

359 360
    scale_ = isReverseAnimation_ ? (FULL_SCALE - scale) : (scale + SHRINK_SCALE);
    ScaleButton(button_, scale_);
L
l00417912 已提交
361 362 363 364
}

void UIButton::ButtonAnimator::OnStop(UIView& view)
{
365 366
    if (isReverseAnimation_) {
        scale_ = SHRINK_SCALE;
L
l00417912 已提交
367 368
        ScaleButton(button_, SHRINK_SCALE);
    } else {
369
        scale_ = FULL_SCALE;
L
l00417912 已提交
370 371 372 373
        button_.ResetTransParameter();
    }
}
#endif
G
guyuanzhang 已提交
374
} // namespace OHOS