ui_slider.cpp 12.3 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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_slider.h"
#include "common/image.h"
#include "dock/focus_manager.h"
Y
YueBiang 已提交
19
#include "dock/vibrator_manager.h"
M
mamingshuai 已提交
20
#include "draw/draw_image.h"
N
niulihua 已提交
21
#include "engines/gfx/gfx_engine_manager.h"
Z
zhangguyuan 已提交
22
#include "gfx_utils/graphic_log.h"
M
mamingshuai 已提交
23 24 25
#include "imgdecode/cache_manager.h"
#include "themes/theme_manager.h"

Y
YueBiang 已提交
26 27 28 29 30 31 32
namespace {
#ifdef _WIN32
constexpr float DEFAULT_SLIDER_ROTATE_FACTOR = -1;
#else
constexpr float DEFAULT_SLIDER_ROTATE_FACTOR = -0.1;
#endif
}
M
mamingshuai 已提交
33 34
namespace OHOS {
UISlider::UISlider()
Y
YueBiang 已提交
35
    : knobWidth_(0), knobStyleAllocFlag_(false), knobImage_(nullptr), listener_(nullptr)
M
mamingshuai 已提交
36 37 38 39
{
    touchable_ = true;
    draggable_ = true;
    dragParentInstead_ = false;
S
suyue 已提交
40 41 42
#if ENABLE_FOCUS_MANAGER
    focusable_ = true;
#endif
Y
YueBiang 已提交
43 44 45
#if ENABLE_ROTATE_INPUT
    rotateFactor_ = DEFAULT_SLIDER_ROTATE_FACTOR;
#endif
Y
YueBiang 已提交
46 47 48 49 50 51 52

    Theme* theme = ThemeManager::GetInstance().GetCurrent();
    if (theme != nullptr) {
        knobStyle_ = &(theme->GetSliderKnobStyle());
    } else {
        knobStyle_ = &(StyleDefault::GetSliderKnobStyle());
    }
M
mamingshuai 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
}

UISlider::~UISlider()
{
    if (knobImage_ != nullptr) {
        delete knobImage_;
        knobImage_ = nullptr;
    }

    if (knobStyleAllocFlag_) {
        delete knobStyle_;
        knobStyle_ = nullptr;
        knobStyleAllocFlag_ = false;
    }
}

void UISlider::SetKnobStyle(const Style& style)
{
    if (!knobStyleAllocFlag_) {
        knobStyle_ = new Style;
        if (knobStyle_ == nullptr) {
74
            GRAPHIC_LOGE("new Style fail");
M
mamingshuai 已提交
75 76 77 78 79 80 81 82 83 84 85 86
            return;
        }
        knobStyleAllocFlag_ = true;
    }
    *knobStyle_ = style;
}

void UISlider::SetKnobStyle(uint8_t key, int64_t value)
{
    if (!knobStyleAllocFlag_) {
        knobStyle_ = new Style(*knobStyle_);
        if (knobStyle_ == nullptr) {
87
            GRAPHIC_LOGE("new Style fail");
M
mamingshuai 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
            return;
        }
        knobStyleAllocFlag_ = true;
    }
    knobStyle_->SetStyle(key, value);
}

const Style& UISlider::GetKnobStyle() const
{
    return *knobStyle_;
}

int64_t UISlider::GetKnobStyle(uint8_t key) const
{
    return knobStyle_->GetStyle(key);
}

Y
YueBiang 已提交
105
void UISlider::SetKnobImage(const ImageInfo* knobImage)
Y
YueBiang 已提交
106 107 108 109 110 111 112
{
    if (!InitImage()) {
        return;
    }
    knobImage_->SetSrc(knobImage);
}

Y
YueBiang 已提交
113
void UISlider::SetKnobImage(const char* knobImage)
Y
YueBiang 已提交
114 115 116 117 118 119 120
{
    if (!InitImage()) {
        return;
    }
    knobImage_->SetSrc(knobImage);
}

N
niulihua 已提交
121
void UISlider::DrawKnob(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, const Rect& foregroundRect)
Y
YueBiang 已提交
122
{
123
    int16_t halfKnobWidth = GetKnobWidth() / 2; // 2: half
Y
YueBiang 已提交
124 125 126 127
    int16_t offset;
    Rect knobBar;
    switch (direction_) {
        case Direction::DIR_LEFT_TO_RIGHT: {
128
            offset = (knobWidth_ - progressHeight_) / 2; // 2: half
Y
YueBiang 已提交
129
            knobBar.SetRect(foregroundRect.GetRight() - halfKnobWidth, foregroundRect.GetTop() - offset,
Y
YueBiang 已提交
130
                foregroundRect.GetRight() + halfKnobWidth, foregroundRect.GetBottom() + offset);
Y
YueBiang 已提交
131 132 133
            break;
        }
        case Direction::DIR_RIGHT_TO_LEFT: {
134
            offset = (knobWidth_ - progressHeight_) / 2; // 2: half
Y
YueBiang 已提交
135
            knobBar.SetRect(foregroundRect.GetLeft() - halfKnobWidth, foregroundRect.GetTop() - offset,
Y
YueBiang 已提交
136
                foregroundRect.GetLeft() + halfKnobWidth, foregroundRect.GetBottom() + offset);
Y
YueBiang 已提交
137 138 139
            break;
        }
        case Direction::DIR_BOTTOM_TO_TOP: {
140
            offset = (knobWidth_ - progressWidth_) / 2; // 2: half
Y
YueBiang 已提交
141
            knobBar.SetRect(foregroundRect.GetLeft() - offset, foregroundRect.GetTop() - halfKnobWidth,
Y
YueBiang 已提交
142
                foregroundRect.GetRight() + offset, foregroundRect.GetTop() + halfKnobWidth);
Y
YueBiang 已提交
143 144 145
            break;
        }
        case Direction::DIR_TOP_TO_BOTTOM: {
146
            offset = (knobWidth_ - progressWidth_) / 2; // 2: half
Y
YueBiang 已提交
147
            knobBar.SetRect(foregroundRect.GetLeft() - offset, foregroundRect.GetBottom() - halfKnobWidth,
Y
YueBiang 已提交
148
                foregroundRect.GetRight() + offset, foregroundRect.GetBottom() + halfKnobWidth);
Y
YueBiang 已提交
149 150 151
            break;
        }
        default: {
152
            GRAPHIC_LOGW("UISlider::DrawKnob Direction error!\n");
Y
YueBiang 已提交
153 154
        }
    }
N
niulihua 已提交
155
    DrawValidRect(gfxDstBuffer, knobImage_, knobBar, invalidatedArea, *knobStyle_, 0);
Y
YueBiang 已提交
156 157 158 159 160 161 162 163 164 165
}

bool UISlider::InitImage()
{
    if (!UIAbstractProgress::InitImage()) {
        return false;
    }
    if (knobImage_ == nullptr) {
        knobImage_ = new Image();
        if (knobImage_ == nullptr) {
166
            GRAPHIC_LOGE("new Image fail");
Y
YueBiang 已提交
167 168 169 170 171
            return false;
        }
    }
    return true;
}
Y
YueBiang 已提交
172

Y
YueBiang 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
void UISlider::SetImage(const ImageInfo* backgroundImage, const ImageInfo* foregroundImage)
{
    if (!InitImage()) {
        return;
    }
    backgroundImage_->SetSrc(backgroundImage);
    foregroundImage_->SetSrc(foregroundImage);
}

void UISlider::SetImage(const char* backgroundImage, const char* foregroundImage)
{
    if (!InitImage()) {
        return;
    }
    backgroundImage_->SetSrc(backgroundImage);
    foregroundImage_->SetSrc(foregroundImage);
}

N
niulihua 已提交
191
void UISlider::DrawForeground(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, Rect& coords)
Y
YueBiang 已提交
192 193 194 195 196
{
    Point startPoint;
    int16_t progressWidth;
    int16_t progressHeight;
    uint16_t radius;
197
    GetBackgroundParam(startPoint, progressWidth, progressHeight, radius, *foregroundStyle_);
Y
YueBiang 已提交
198 199 200 201 202 203

    int16_t left;
    int16_t right;
    int16_t top;
    int16_t bottom;
    int16_t length;
Y
YueBiang 已提交
204
    Rect foregroundRect;
Y
YueBiang 已提交
205 206 207

    switch (direction_) {
        case Direction::DIR_LEFT_TO_RIGHT: {
Y
YueBiang 已提交
208
            length = GetCurrentPos(progressWidth_ + 1);
Y
YueBiang 已提交
209 210
            foregroundRect.SetRect(startPoint.x, startPoint.y, startPoint.x + progressWidth - 1,
                startPoint.y + progressHeight_ - 1);
Y
YueBiang 已提交
211 212 213

            left = startPoint.x - radius - 1;
            right = left + length;
Y
YueBiang 已提交
214
            coords.SetRect(left, startPoint.y, right, startPoint.y + progressHeight_ - 1);
Y
YueBiang 已提交
215 216 217
            break;
        }
        case Direction::DIR_RIGHT_TO_LEFT: {
Y
YueBiang 已提交
218
            length = GetCurrentPos(progressWidth_ + 1);
Y
YueBiang 已提交
219 220
            foregroundRect.SetRect(startPoint.x, startPoint.y, startPoint.x + progressWidth - 1,
                startPoint.y + progressHeight_ - 1);
Y
YueBiang 已提交
221 222 223

            right = startPoint.x + progressWidth + radius + 1;
            left = right - length;
Y
YueBiang 已提交
224
            coords.SetRect(left, startPoint.y, right, startPoint.y + progressHeight_ - 1);
Y
YueBiang 已提交
225 226 227
            break;
        }
        case Direction::DIR_TOP_TO_BOTTOM: {
Y
YueBiang 已提交
228
            length = GetCurrentPos(progressHeight_ + 1);
Y
YueBiang 已提交
229 230
            foregroundRect.SetRect(startPoint.x, startPoint.y, startPoint.x + progressWidth_ - 1,
                startPoint.y + progressHeight - 1);
Y
YueBiang 已提交
231

Y
YueBiang 已提交
232
            top = startPoint.y - radius - 1;
Y
YueBiang 已提交
233
            bottom = top + length;
Y
YueBiang 已提交
234
            coords.SetRect(startPoint.x, top, startPoint.x + progressWidth_ - 1, bottom);
Y
YueBiang 已提交
235 236 237
            break;
        }
        case Direction::DIR_BOTTOM_TO_TOP: {
Y
YueBiang 已提交
238
            length = GetCurrentPos(progressHeight_ + 1);
Y
YueBiang 已提交
239 240
            foregroundRect.SetRect(startPoint.x, startPoint.y, startPoint.x + progressWidth_ - 1,
                startPoint.y + progressHeight - 1);
Y
YueBiang 已提交
241

242
            bottom = startPoint.y + progressHeight + radius + 1;
Y
YueBiang 已提交
243
            top = bottom - length;
Y
YueBiang 已提交
244
            coords.SetRect(startPoint.x, top, startPoint.x + progressWidth_ - 1, bottom);
Y
YueBiang 已提交
245 246 247
            break;
        }
        default: {
248
            GRAPHIC_LOGE("UISlider: DrawForeground direction Err!\n");
Y
YueBiang 已提交
249 250 251
            return;
        }
    }
Y
YueBiang 已提交
252 253
    if (coords.Intersect(coords, invalidatedArea)) {
        DrawValidRect(gfxDstBuffer, foregroundImage_, foregroundRect, coords, *foregroundStyle_, radius);
Y
YueBiang 已提交
254 255 256
    }
}

N
niulihua 已提交
257
void UISlider::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
Y
YueBiang 已提交
258
{
N
niulihua 已提交
259
    BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, GetOrigRect(), invalidatedArea, *style_, opaScale_);
Y
YueBiang 已提交
260 261 262

    Rect trunc(invalidatedArea);
    if (trunc.Intersect(trunc, GetOrigRect())) {
N
niulihua 已提交
263
        DrawBackground(gfxDstBuffer, trunc);
Y
YueBiang 已提交
264
        Rect foregroundRect;
N
niulihua 已提交
265 266
        DrawForeground(gfxDstBuffer, trunc, foregroundRect);
        DrawKnob(gfxDstBuffer, trunc, foregroundRect);
Y
YueBiang 已提交
267 268 269
    }
}

M
mamingshuai 已提交
270 271 272 273 274 275 276 277 278 279 280 281
int32_t UISlider::CalculateCurrentValue(int16_t length, int16_t totalLength)
{
    if (totalLength != 0) {
        return static_cast<int32_t>(rangeMin_ + (static_cast<int64_t>(rangeMax_) - rangeMin_) * length / totalLength);
    }
    return 0;
}

int32_t UISlider::UpdateCurrentValue(const Point& knobPosition)
{
    Point startPoint;
    Rect rect = GetOrigRect();
282 283 284 285
    // 2: Half of the gap
    startPoint.x = rect.GetLeft() + style_->borderWidth_ + style_->paddingLeft_ + (GetWidth() - progressWidth_) / 2;
    // 2: Half of the gap
    startPoint.y = rect.GetTop() + style_->borderWidth_ + style_->paddingTop_ + (GetHeight() - progressHeight_) / 2;
M
mamingshuai 已提交
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 317 318 319 320 321 322 323 324 325

    int32_t value = curValue_;
    switch (direction_) {
        case Direction::DIR_LEFT_TO_RIGHT:
            if (knobPosition.x <= startPoint.x) {
                value = rangeMin_;
            } else if (knobPosition.x >= startPoint.x + progressWidth_) {
                value = rangeMax_;
            } else {
                value = CalculateCurrentValue(knobPosition.x - startPoint.x, progressWidth_);
            }
            break;
        case Direction::DIR_RIGHT_TO_LEFT:
            if (knobPosition.x <= startPoint.x) {
                value = rangeMax_;
            } else if (knobPosition.x >= startPoint.x + progressWidth_) {
                value = rangeMin_;
            } else {
                value = CalculateCurrentValue(startPoint.x + progressWidth_ - knobPosition.x, progressWidth_);
            }
            break;
        case Direction::DIR_BOTTOM_TO_TOP:
            if (knobPosition.y <= startPoint.y) {
                value = rangeMax_;
            } else if (knobPosition.y >= startPoint.y + progressHeight_) {
                value = rangeMin_;
            } else {
                value = CalculateCurrentValue(startPoint.y + progressHeight_ - knobPosition.y, progressHeight_);
            }
            break;
        case Direction::DIR_TOP_TO_BOTTOM:
            if (knobPosition.y <= startPoint.y) {
                value = rangeMin_;
            } else if (knobPosition.y >= startPoint.y + progressHeight_) {
                value = rangeMax_;
            } else {
                value = CalculateCurrentValue(knobPosition.y - startPoint.y, progressHeight_);
            }
            break;
        default:
326
            GRAPHIC_LOGW("UISlider::UpdateCurrentValue Direction error!\n");
M
mamingshuai 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 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
    }
    SetValue(value);
    return value;
}

bool UISlider::OnClickEvent(const ClickEvent& event)
{
    Point knobPosition = event.GetCurrentPos();
    int32_t value = UpdateCurrentValue(knobPosition);
    if (listener_ != nullptr) {
        listener_->OnChange(value);
    }
    bool ret = UIView::OnClickEvent(event);
    Invalidate();
    return ret;
}

bool UISlider::OnDragEvent(const DragEvent& event)
{
    Point knobPosition = event.GetCurrentPos();
    int32_t value = UpdateCurrentValue(knobPosition);
    if (listener_ != nullptr) {
        listener_->OnChange(value);
    }
    Invalidate();
    return UIView::OnDragEvent(event);
}

bool UISlider::OnDragEndEvent(const DragEvent& event)
{
    Point knobPosition = event.GetCurrentPos();
    int32_t value = UpdateCurrentValue(knobPosition);
    if (listener_ != nullptr) {
        listener_->OnChange(value);
        listener_->OnRelease(value);
    }
    Invalidate();
    return UIView::OnDragEndEvent(event);
}

#if ENABLE_ROTATE_INPUT
bool UISlider::OnRotateEvent(const RotateEvent& event)
{
Y
YueBiang 已提交
370 371
    int32_t lastValue = curValue_;
    int32_t tmp = static_cast<int32_t>(event.GetRotate()) * rotateFactor_;
M
mamingshuai 已提交
372
    SetValue(curValue_ + tmp);
Y
YueBiang 已提交
373 374 375 376 377 378
    if (listener_ != nullptr) {
        listener_->OnChange(curValue_);
    }
#if ENABLE_VIBRATOR
    VibratorFunc vibratorFunc = VibratorManager::GetInstance()->GetVibratorFunc();
    if (vibratorFunc != nullptr && lastValue != curValue_) {
379
        GRAPHIC_LOGI("UISlider::OnRotateEvent Call vibrator function");
Y
YueBiang 已提交
380
        vibratorFunc(VibratorType::VIBRATOR_TYPE_TWO);
M
mamingshuai 已提交
381 382 383 384 385
    }
#endif
    return UIView::OnRotateEvent(event);
}
#endif
386
} // namespace OHOS