ui_button.cpp 10.2 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 18

#include "animator/interpolation.h"
M
mamingshuai 已提交
19 20 21
#include "common/image.h"
#include "draw/draw_image.h"
#include "draw/draw_rect.h"
Z
zhangguyuan 已提交
22 23
#include "gfx_utils/graphic_log.h"
#include "gfx_utils/style.h"
M
mamingshuai 已提交
24 25 26 27 28
#include "imgdecode/cache_manager.h"
#include "themes/theme_manager.h"

namespace OHOS {
UIButton::UIButton()
L
l00417912 已提交
29 30 31 32 33
    :
#if DEFAULT_ANIMATION
      animator_(*this),
#endif
      defaultImgSrc_(nullptr),
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 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
      triggeredImgSrc_(nullptr),
      currentImgSrc_(ButtonImageSrc::BTN_IMAGE_DEFAULT),
      imgX_(0),
      imgY_(0),
      contentWidth_(0),
      contentHeight_(0),
      state_(RELEASED),
      styleState_(RELEASED),
      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;
    }
}

void UIButton::DrawImg(const Rect& invalidatedArea, OpacityType opaScale)
{
    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)) {
        image->DrawImage(coords, trunc, *buttonStyles_[state_], opaScale);
    }
}

void UIButton::OnDraw(const Rect& invalidatedArea)
{
    OpacityType opa = GetMixOpaScale();
    DrawRect::Draw(GetOrigRect(), invalidatedArea, *buttonStyles_[state_], opa);
    DrawImg(invalidatedArea, opa);
}

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);
    }
111
    style_ = buttonStyles_[RELEASED];
M
mamingshuai 已提交
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 139 140 141 142 143 144 145 146
}

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) {
                    GRAPHIC_LOGE("new Style fail");
                    return;
                }
                *(buttonStyles_[i]) = styleSaved;
            }
            buttonStyleAllocFlag_ = true;
        }
147
        style_ = buttonStyles_[RELEASED];
M
mamingshuai 已提交
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 177 178
        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 已提交
179 180 181
#if DEFAULT_ANIMATION
    animator_.Start();
#endif
M
mamingshuai 已提交
182 183 184 185 186 187 188 189 190
    return UIView::OnPressEvent(event);
}

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

bool UIButton::OnCancelEvent(const CancelEvent& event)
{
    currentImgSrc_ = ButtonImageSrc::BTN_IMAGE_DEFAULT;
    SetState(RELEASED);
    Resize(contentWidth_, contentHeight_);
    Invalidate();
L
l00417912 已提交
203 204 205
#if DEFAULT_ANIMATION
    animator_.Start();
#endif
M
mamingshuai 已提交
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 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 281
    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;
    Invalidate();
}

bool UIButton::InitImage()
{
    if (defaultImgSrc_ == nullptr) {
        defaultImgSrc_ = new Image();
        if (defaultImgSrc_ == nullptr) {
            GRAPHIC_LOGE("new Image fail");
            return false;
        }
    }
    if (triggeredImgSrc_ == nullptr) {
        triggeredImgSrc_ = new Image();
        if (triggeredImgSrc_ == nullptr) {
            GRAPHIC_LOGE("new Image fail");
            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 已提交
282

M
mamingshuai 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295
    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 已提交
296 297 298 299 300 301 302 303 304 305

#if DEFAULT_ANIMATION
void UIButton::OnPostDraw(const Rect& invalidatedArea)
{
    if (state_ == ButtonState::PRESSED) {
        animator_.DrawMask(invalidatedArea);
    }
}

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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
}

void UIButton::ButtonAnimator::DrawMask(const Rect& invalidatedArea)
{
    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();
    DrawRect::Draw(button_.GetRect(), invalidatedArea, maskStyle, opa);
}

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 374 375
        button_.ResetTransParameter();
    }
}
#endif

} // namespace OHOS