You need to sign in or sign up before continuing.
ui_label.cpp 9.8 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_label.h"
#include "font/ui_font.h"
Z
zhangguyuan 已提交
18
#include "gfx_utils/graphic_log.h"
M
mamingshuai 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include "themes/theme_manager.h"

namespace OHOS {
class LabelAnimator : public Animator, public AnimatorCallback {
public:
    LabelAnimator(uint16_t textX, uint16_t labelX, int16_t startPos, UIView* view)
        : Animator(this, view, 0, true),
          startPos_(startPos),
          textX_(textX),
          labelX_(labelX),
          offsetX_(startPos),
          waitCount_(ANIM_WAIT_COUNT),
          speed_(0),
          preRunTime_(0),
          decimal_(0)
    {
    }

P
pssea 已提交
37
    virtual ~LabelAnimator() {}
M
mamingshuai 已提交
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

    int16_t GetStartPos() const
    {
        return startPos_;
    }

    void SetStartPos(int16_t pos)
    {
        startPos_ = pos;
    }

    void UpdateWidth(uint16_t textWidth, uint16_t labelWidth)
    {
        textX_ = textWidth;
        labelX_ = labelWidth;
        waitCount_ = ANIM_WAIT_COUNT;
        preRunTime_ = 0;
        decimal_ = 0;
        offsetX_ = startPos_;
        static_cast<UILabel*>(view_)->offsetX_ = offsetX_;
        view_->Invalidate();
    }

    void Callback(UIView* view) override
    {
        if (view == nullptr) {
            return;
        }

        uint32_t curTime = GetRunTime();
        if (waitCount_ > 0) {
            waitCount_--;
            preRunTime_ = curTime;
            return;
        }
73 74 75
        if (curTime == preRunTime_) {
            return;
        }
M
mamingshuai 已提交
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        uint32_t time = (curTime > preRunTime_) ? (curTime - preRunTime_) : (UINT32_MAX - preRunTime_ + curTime);
        // 1000: 1000 milliseconds is 1 second
        float floatStep = (static_cast<float>(time * speed_) / 1000) + decimal_;
        uint16_t integerStep = static_cast<uint16_t>(floatStep);
        decimal_ = floatStep - integerStep;
        preRunTime_ = curTime;

        if (integerStep != 0) {
            offsetX_ -= integerStep;
        } else {
            return;
        }
        offsetX_ = ((offsetX_ - labelX_) % (textX_ + labelX_)) + labelX_;
        static_cast<UILabel*>(view)->offsetX_ = offsetX_;
        view->Invalidate();
    }

    void SetAnimatorSpeed(uint16_t animSpeed)
    {
        speed_ = animSpeed;
        decimal_ = 0;
    }

private:
    static constexpr uint8_t ANIM_WAIT_COUNT = 50;
    int16_t startPos_;
    uint16_t textX_;
    uint16_t labelX_;
    int16_t offsetX_;
    uint16_t waitCount_;
    uint16_t speed_;
    uint32_t preRunTime_;
    float decimal_;
};

UILabel::UILabel()
    : labelText_(nullptr),
      needRefresh_(false),
      useTextColor_(false),
      hasAnimator_(false),
      lineBreakMode_(LINE_BREAK_ELLIPSIS),
      ellipsisIndex_(Text::TEXT_ELLIPSIS_END_INV),
      offsetX_(0),
      textColor_(Color::White()),
      animator_{nullptr}
{
    Theme* theme = ThemeManager::GetInstance().GetCurrent();
    Style& style = (theme != nullptr) ? (theme->GetLabelStyle()) : (StyleDefault::GetLabelStyle());
    UIView::SetStyle(style);
    animator_.speed = DEFAULT_ANIMATOR_SPEED;
}

UILabel::~UILabel()
{
    if (hasAnimator_) {
        delete animator_.animator;
W
wangtiantian 已提交
132
        animator_.animator = nullptr;
M
mamingshuai 已提交
133 134 135 136 137 138 139 140
        hasAnimator_ = false;
    }
    if (labelText_ != nullptr) {
        delete labelText_;
        labelText_ = nullptr;
    }
}

141 142 143 144 145 146 147
void UILabel::InitLabelText()
{
    if (labelText_ == nullptr) {
        labelText_ = new Text();
    }
}

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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
int16_t UILabel::GetWidth()
{
    InitLabelText();
    if (needRefresh_ && labelText_->IsExpandWidth()) {
        ReMeasure();
    }
    return UIView::GetWidth();
}

int16_t UILabel::GetHeight()
{
    InitLabelText();
    if (needRefresh_ && labelText_->IsExpandHeight()) {
        ReMeasure();
    }
    return UIView::GetHeight();
}

void UILabel::SetStyle(uint8_t key, int64_t value)
{
    UIView::SetStyle(key, value);
    RefreshLabel();
}

void UILabel::SetText(const char* text)
{
    InitLabelText();
    labelText_->SetText(text);
    if (labelText_->IsNeedRefresh()) {
        RefreshLabel();
    }
}

void UILabel::SetLineBreakMode(const uint8_t lineBreakMode)
{
    InitLabelText();
    if ((lineBreakMode >= LINE_BREAK_MAX) || (lineBreakMode_ == lineBreakMode)) {
        return;
    }
    lineBreakMode_ = lineBreakMode;
    if ((lineBreakMode_ == LINE_BREAK_ADAPT) || (lineBreakMode_ == LINE_BREAK_STRETCH) ||
        (lineBreakMode_ == LINE_BREAK_MARQUEE)) {
        labelText_->SetExpandWidth(true);
    } else {
        labelText_->SetExpandWidth(false);
    }
    if ((lineBreakMode_ == LINE_BREAK_ADAPT) || (lineBreakMode_ == LINE_BREAK_WRAP)) {
        labelText_->SetExpandHeight(true);
    } else {
        labelText_->SetExpandHeight(false);
    }
W
wangtiantian 已提交
199 200 201 202 203 204
    if (lineBreakMode_ != LINE_BREAK_MARQUEE) {
        offsetX_ = 0;
        if (hasAnimator_) {
            animator_.animator->Stop();
        }
    }
M
mamingshuai 已提交
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
    RefreshLabel();
}

void UILabel::SetAlign(UITextLanguageAlignment horizontalAlign, UITextLanguageAlignment verticalAlign)
{
    InitLabelText();
    labelText_->SetAlign(horizontalAlign, verticalAlign);
    if (labelText_->IsNeedRefresh()) {
        RefreshLabel();
    }
}

void UILabel::SetFontId(uint8_t fontId)
{
    InitLabelText();
    labelText_->SetFontId(fontId);
    if (labelText_->IsNeedRefresh()) {
        RefreshLabel();
    }
}

void UILabel::SetFont(const char* name, uint8_t size)
{
    InitLabelText();
    labelText_->SetFont(name, size);
    if (labelText_->IsNeedRefresh()) {
        RefreshLabel();
    }
}

uint16_t UILabel::GetTextWidth()
{
    InitLabelText();
    if (labelText_->IsNeedRefresh()) {
W
wanngtiantian 已提交
239
        ReMeasure();
M
mamingshuai 已提交
240 241 242 243 244 245 246 247
    }
    return labelText_->GetTextSize().x;
}

uint16_t UILabel::GetTextHeight()
{
    InitLabelText();
    if (labelText_->IsNeedRefresh()) {
W
wanngtiantian 已提交
248
        ReMeasure();
M
mamingshuai 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    }
    return labelText_->GetTextSize().y;
}

void UILabel::SetWidth(int16_t width)
{
    if (GetWidth() != width) {
        UIView::SetWidth(width);
        RefreshLabel();
    }
}

void UILabel::SetHeight(int16_t height)
{
    if (GetHeight() != height) {
        UIView::SetHeight(height);
        RefreshLabel();
    }
}

W
wangtiantian 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
void UILabel::SetX(int16_t x)
{
    if (GetX() != x) {
        UIView::SetX(x);
        RefreshLabel();
    }
}

void UILabel::SetY(int16_t y)
{
    if (GetY() != y) {
        UIView::SetY(y);
        RefreshLabel();
    }
}

M
mamingshuai 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
void UILabel::RefreshLabel()
{
    Invalidate();
    ellipsisIndex_ = Text::TEXT_ELLIPSIS_END_INV;
    if (!needRefresh_) {
        needRefresh_ = true;
    }
}

void UILabel::ReMeasure()
{
    if (!needRefresh_) {
        return;
    }
    needRefresh_ = false;
    InitLabelText();
    Style style = GetStyleConst();
    style.textColor_ = GetTextColor();
    labelText_->ReMeasureTextSize(GetContentRect(), style);
    Point textSize = labelText_->GetTextSize();
    switch (lineBreakMode_) {
        case LINE_BREAK_ADAPT:
            Resize(textSize.x, textSize.y);
            break;
        case LINE_BREAK_STRETCH:
            SetWidth(textSize.x);
            break;
        case LINE_BREAK_WRAP:
            SetHeight(textSize.y);
            break;
        case LINE_BREAK_ELLIPSIS:
            ellipsisIndex_ = labelText_->GetEllipsisIndex(GetContentRect(), style);
W
wanngtiantian 已提交
317
            labelText_->ReMeasureTextWidthInEllipsisMode(GetContentRect(), style, ellipsisIndex_);
M
mamingshuai 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
            break;
        case LINE_BREAK_MARQUEE:
            RemeasureForMarquee(textSize.x);
            break;
        default:
            break;
    }
}

void UILabel::RemeasureForMarquee(int16_t textWidth)
{
    int16_t rectWidth = GetWidth();
    if (textWidth > rectWidth) {
        offsetX_ = GetRollStartPos();
        if (labelText_->GetDirect() == TEXT_DIRECT_RTL) {
            labelText_->SetAlign(TEXT_ALIGNMENT_RIGHT);
        } else {
            labelText_->SetAlign(TEXT_ALIGNMENT_LEFT);
        }
        if (hasAnimator_) {
            static_cast<LabelAnimator*>(animator_.animator)->UpdateWidth(textWidth, rectWidth);
        } else {
            LabelAnimator* animator = new LabelAnimator(textWidth, rectWidth, offsetX_, this);
            if (animator == nullptr) {
342
                GRAPHIC_LOGE("new LabelAnimator fail");
M
mamingshuai 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
                return;
            }
            animator->SetAnimatorSpeed(animator_.speed);
            animator_.animator = animator;
            hasAnimator_ = true;
        }
        animator_.animator->Start();
    } else {
        offsetX_ = 0;
        if (hasAnimator_) {
            animator_.animator->Stop();
        }
    }
}

void UILabel::SetRollStartPos(int16_t pos)
{
    if (hasAnimator_) {
        static_cast<LabelAnimator*>(animator_.animator)->SetStartPos(pos);
    } else {
        animator_.pos = pos;
    }
}

int16_t UILabel::GetRollStartPos() const
{
    return hasAnimator_ ? static_cast<LabelAnimator*>(animator_.animator)->GetStartPos() : animator_.pos;
}

void UILabel::SetRollSpeed(uint16_t speed)
{
    if (hasAnimator_) {
        static_cast<LabelAnimator*>(animator_.animator)->SetAnimatorSpeed(speed);
    } else {
        animator_.speed = speed;
    }
}

N
niulihua 已提交
381
void UILabel::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
M
mamingshuai 已提交
382 383
{
    InitLabelText();
N
niulihua 已提交
384
    UIView::OnDraw(gfxDstBuffer, invalidatedArea);
M
mamingshuai 已提交
385 386 387
    Style style = GetStyleConst();
    style.textColor_ = GetTextColor();
    OpacityType opa = GetMixOpaScale();
N
niulihua 已提交
388 389
    labelText_->OnDraw(gfxDstBuffer, invalidatedArea, GetOrigRect(),
                       GetContentRect(), offsetX_, style, ellipsisIndex_, opa);
M
mamingshuai 已提交
390 391
}
} // namespace OHOS