ui_button.cpp 10.3 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
#if DEFAULT_ANIMATION
Y
YueBiang 已提交
38
      enableAnimation_(true),
39 40
      animator_(*this),
#endif
M
mamingshuai 已提交
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 67
      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 已提交
68
void UIButton::DrawImg(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, OpacityType opaScale)
M
mamingshuai 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
{
    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 已提交
86
        image->DrawImage(gfxDstBuffer, coords, trunc, *buttonStyles_[state_], opaScale);
M
mamingshuai 已提交
87 88 89
    }
}

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

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);
    }
110
    style_ = buttonStyles_[RELEASED];
M
mamingshuai 已提交
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 138
}

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) {
139
                    GRAPHIC_LOGE("new Style fail");
M
mamingshuai 已提交
140 141 142 143 144 145
                    return;
                }
                *(buttonStyles_[i]) = styleSaved;
            }
            buttonStyleAllocFlag_ = true;
        }
146
        style_ = buttonStyles_[RELEASED];
M
mamingshuai 已提交
147 148
        int16_t width = GetWidth();
        int16_t height = GetHeight();
149 150
        int16_t x = GetX();
        int16_t y = GetY();
M
mamingshuai 已提交
151
        buttonStyles_[state]->SetStyle(key, value);
152 153
        Rect rect(x, y, x + width - 1, y + height -  1);
        UpdateRectInfo(key, rect);
M
mamingshuai 已提交
154 155 156 157 158 159 160 161 162
    }
}

bool UIButton::OnPressEvent(const PressEvent& event)
{
    currentImgSrc_ = ButtonImageSrc::BTN_IMAGE_TRIGGERED;
    SetState(PRESSED);
    Resize(contentWidth_, contentHeight_);
    Invalidate();
L
l00417912 已提交
163
#if DEFAULT_ANIMATION
Y
YueBiang 已提交
164 165 166
    if (enableAnimation_) {
        animator_.Start();
    }
L
l00417912 已提交
167
#endif
M
mamingshuai 已提交
168 169 170 171 172 173 174 175 176
    return UIView::OnPressEvent(event);
}

bool UIButton::OnReleaseEvent(const ReleaseEvent& event)
{
    currentImgSrc_ = ButtonImageSrc::BTN_IMAGE_DEFAULT;
    SetState(RELEASED);
    Resize(contentWidth_, contentHeight_);
    Invalidate();
L
l00417912 已提交
177
#if DEFAULT_ANIMATION
Y
YueBiang 已提交
178 179 180
    if (enableAnimation_) {
        animator_.Start();
    }
L
l00417912 已提交
181
#endif
M
mamingshuai 已提交
182 183 184 185 186 187 188 189 190
    return UIView::OnReleaseEvent(event);
}

bool UIButton::OnCancelEvent(const CancelEvent& event)
{
    currentImgSrc_ = ButtonImageSrc::BTN_IMAGE_DEFAULT;
    SetState(RELEASED);
    Resize(contentWidth_, contentHeight_);
    Invalidate();
L
l00417912 已提交
191
#if DEFAULT_ANIMATION
Y
YueBiang 已提交
192 193 194
    if (enableAnimation_) {
        animator_.Start();
    }
L
l00417912 已提交
195
#endif
M
mamingshuai 已提交
196 197 198 199 200 201 202 203 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
    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;
邓志豪 已提交
243
    style_ = buttonStyles_[state_];
M
mamingshuai 已提交
244 245 246 247 248 249 250 251
    Invalidate();
}

bool UIButton::InitImage()
{
    if (defaultImgSrc_ == nullptr) {
        defaultImgSrc_ = new Image();
        if (defaultImgSrc_ == nullptr) {
252
            GRAPHIC_LOGE("new Image fail");
M
mamingshuai 已提交
253 254 255 256 257 258
            return false;
        }
    }
    if (triggeredImgSrc_ == nullptr) {
        triggeredImgSrc_ = new Image();
        if (triggeredImgSrc_ == nullptr) {
259
            GRAPHIC_LOGE("new Image fail");
M
mamingshuai 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272
            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 已提交
273

M
mamingshuai 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286
    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 已提交
287 288

#if DEFAULT_ANIMATION
N
niulihua 已提交
289
void UIButton::OnPostDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
L
l00417912 已提交
290
{
Y
YueBiang 已提交
291
    if (state_ == ButtonState::PRESSED && enableAnimation_) {
N
niulihua 已提交
292
        animator_.DrawMask(gfxDstBuffer, invalidatedArea);
L
l00417912 已提交
293
    }
P
pssea 已提交
294
    UIView::OnPostDraw(gfxDstBuffer, invalidatedArea);
L
l00417912 已提交
295 296 297
}

namespace {
298
constexpr float FULL_SCALE = 1.0f;
L
l00417912 已提交
299 300 301 302
constexpr float SHRINK_SCALE = 0.8f;
constexpr uint32_t SHRINK_DURATION = 150;
constexpr uint32_t RECOVER_DURATION = 200;
constexpr int64_t MASK_OPA = 25;
303
constexpr float BEZIER_CONTROL = 0.2f;
L
l00417912 已提交
304 305 306 307
} // namespace

void UIButton::ButtonAnimator::Start()
{
308 309 310 311 312 313 314
    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 已提交
315 316 317 318 319
        animator_.SetTime(SHRINK_DURATION);
    } else {
        animator_.SetTime(RECOVER_DURATION);
    }
    animator_.Start();
320 321 322 323 324 325
    /* 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 已提交
326 327
}

N
niulihua 已提交
328
void UIButton::ButtonAnimator::DrawMask(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
L
l00417912 已提交
329 330 331 332 333 334
{
    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 已提交
335
    BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, button_.GetRect(), invalidatedArea, maskStyle, opa);
L
l00417912 已提交
336 337 338 339 340 341 342 343 344 345 346
}

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)
{
347 348 349
    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 已提交
350

351 352
    scale_ = isReverseAnimation_ ? (FULL_SCALE - scale) : (scale + SHRINK_SCALE);
    ScaleButton(button_, scale_);
L
l00417912 已提交
353 354 355 356
}

void UIButton::ButtonAnimator::OnStop(UIView& view)
{
357 358
    if (isReverseAnimation_) {
        scale_ = SHRINK_SCALE;
L
l00417912 已提交
359 360
        ScaleButton(button_, SHRINK_SCALE);
    } else {
361
        scale_ = FULL_SCALE;
L
l00417912 已提交
362 363 364 365
        button_.ResetTransParameter();
    }
}
#endif
Y
YueBiang 已提交
366
} // namespace OHOS