1a3e0fd82Sopenharmony_ci/* 2a3e0fd82Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. 3a3e0fd82Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4a3e0fd82Sopenharmony_ci * you may not use this file except in compliance with the License. 5a3e0fd82Sopenharmony_ci * You may obtain a copy of the License at 6a3e0fd82Sopenharmony_ci * 7a3e0fd82Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8a3e0fd82Sopenharmony_ci * 9a3e0fd82Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10a3e0fd82Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11a3e0fd82Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12a3e0fd82Sopenharmony_ci * See the License for the specific language governing permissions and 13a3e0fd82Sopenharmony_ci * limitations under the License. 14a3e0fd82Sopenharmony_ci */ 15a3e0fd82Sopenharmony_ci 16a3e0fd82Sopenharmony_ci/** 17a3e0fd82Sopenharmony_ci * @addtogroup UI_Components 18a3e0fd82Sopenharmony_ci * @{ 19a3e0fd82Sopenharmony_ci * 20a3e0fd82Sopenharmony_ci * @brief Defines UI components such as buttons, texts, images, lists, and progress bars. 21a3e0fd82Sopenharmony_ci * 22a3e0fd82Sopenharmony_ci * @since 1.0 23a3e0fd82Sopenharmony_ci * @version 1.0 24a3e0fd82Sopenharmony_ci */ 25a3e0fd82Sopenharmony_ci 26a3e0fd82Sopenharmony_ci/** 27a3e0fd82Sopenharmony_ci * @file ui_slider.h 28a3e0fd82Sopenharmony_ci * 29a3e0fd82Sopenharmony_ci * @brief Defines the attributes and common functions of a slider. 30a3e0fd82Sopenharmony_ci * 31a3e0fd82Sopenharmony_ci * @since 1.0 32a3e0fd82Sopenharmony_ci * @version 1.0 33a3e0fd82Sopenharmony_ci */ 34a3e0fd82Sopenharmony_ci 35a3e0fd82Sopenharmony_ci#ifndef GRAPHIC_LITE_UI_SLIDER_H 36a3e0fd82Sopenharmony_ci#define GRAPHIC_LITE_UI_SLIDER_H 37a3e0fd82Sopenharmony_ci 38a3e0fd82Sopenharmony_ci#include "common/image.h" 39a3e0fd82Sopenharmony_ci#include "components/ui_box_progress.h" 40a3e0fd82Sopenharmony_ci 41a3e0fd82Sopenharmony_cinamespace OHOS { 42a3e0fd82Sopenharmony_ci/** 43a3e0fd82Sopenharmony_ci * @brief Represents a slider. 44a3e0fd82Sopenharmony_ci * 45a3e0fd82Sopenharmony_ci * Users can drag or click the knob to adjust the progress of an event. 46a3e0fd82Sopenharmony_ci * 47a3e0fd82Sopenharmony_ci * @see UIBoxProgress 48a3e0fd82Sopenharmony_ci * @since 1.0 49a3e0fd82Sopenharmony_ci * @version 1.0 50a3e0fd82Sopenharmony_ci */ 51a3e0fd82Sopenharmony_ciclass UISlider : public UIBoxProgress { 52a3e0fd82Sopenharmony_cipublic: 53a3e0fd82Sopenharmony_ci /** 54a3e0fd82Sopenharmony_ci * @brief A constructor used to create a <b>UISlider</b> instance. 55a3e0fd82Sopenharmony_ci * 56a3e0fd82Sopenharmony_ci * @since 1.0 57a3e0fd82Sopenharmony_ci * @version 1.0 58a3e0fd82Sopenharmony_ci */ 59a3e0fd82Sopenharmony_ci UISlider(); 60a3e0fd82Sopenharmony_ci 61a3e0fd82Sopenharmony_ci /** 62a3e0fd82Sopenharmony_ci * @brief A destructor used to delete the <b>UISlider</b> instance. 63a3e0fd82Sopenharmony_ci * 64a3e0fd82Sopenharmony_ci * @since 1.0 65a3e0fd82Sopenharmony_ci * @version 1.0 66a3e0fd82Sopenharmony_ci */ 67a3e0fd82Sopenharmony_ci virtual ~UISlider(); 68a3e0fd82Sopenharmony_ci 69a3e0fd82Sopenharmony_ci /** 70a3e0fd82Sopenharmony_ci * @brief Obtains the view type. 71a3e0fd82Sopenharmony_ci * 72a3e0fd82Sopenharmony_ci * @return Returns the view type, as defined in {@link UIViewType}. 73a3e0fd82Sopenharmony_ci * @since 1.0 74a3e0fd82Sopenharmony_ci * @version 1.0 75a3e0fd82Sopenharmony_ci */ 76a3e0fd82Sopenharmony_ci UIViewType GetViewType() const override 77a3e0fd82Sopenharmony_ci { 78a3e0fd82Sopenharmony_ci return UI_SLIDER; 79a3e0fd82Sopenharmony_ci } 80a3e0fd82Sopenharmony_ci 81a3e0fd82Sopenharmony_ci /** 82a3e0fd82Sopenharmony_ci * @brief Sets the width for this knob. 83a3e0fd82Sopenharmony_ci * 84a3e0fd82Sopenharmony_ci * The width of this knob is the same as its height. \n 85a3e0fd82Sopenharmony_ci * By default, the width of this knob is the same as the height of the horizontal slider 86a3e0fd82Sopenharmony_ci * or the width of the vertical slider. \n 87a3e0fd82Sopenharmony_ci * 88a3e0fd82Sopenharmony_ci * @param width Indicates the knob width to set. 89a3e0fd82Sopenharmony_ci * @see GetKnobWidth 90a3e0fd82Sopenharmony_ci * @since 1.0 91a3e0fd82Sopenharmony_ci * @version 1.0 92a3e0fd82Sopenharmony_ci */ 93a3e0fd82Sopenharmony_ci void SetKnobWidth(int16_t width) 94a3e0fd82Sopenharmony_ci { 95a3e0fd82Sopenharmony_ci knobWidth_ = width; 96a3e0fd82Sopenharmony_ci } 97a3e0fd82Sopenharmony_ci 98a3e0fd82Sopenharmony_ci /** 99a3e0fd82Sopenharmony_ci * @brief Obtains the knob width. 100a3e0fd82Sopenharmony_ci * 101a3e0fd82Sopenharmony_ci * @return Returns the knob width. 102a3e0fd82Sopenharmony_ci * @see SetKnobWidth 103a3e0fd82Sopenharmony_ci * @since 1.0 104a3e0fd82Sopenharmony_ci * @version 1.0 105a3e0fd82Sopenharmony_ci */ 106a3e0fd82Sopenharmony_ci int16_t GetKnobWidth() 107a3e0fd82Sopenharmony_ci { 108a3e0fd82Sopenharmony_ci return knobWidth_; 109a3e0fd82Sopenharmony_ci } 110a3e0fd82Sopenharmony_ci 111a3e0fd82Sopenharmony_ci /** 112a3e0fd82Sopenharmony_ci * @brief Sets the image as pixel maps for this slider's knob. 113a3e0fd82Sopenharmony_ci * 114a3e0fd82Sopenharmony_ci * @param knobImage Indicates the knob image to set. 115a3e0fd82Sopenharmony_ci * @since 6 116a3e0fd82Sopenharmony_ci */ 117a3e0fd82Sopenharmony_ci void SetKnobImage(const ImageInfo* knobImage); 118a3e0fd82Sopenharmony_ci 119a3e0fd82Sopenharmony_ci /** 120a3e0fd82Sopenharmony_ci * @brief Sets the image for this slider's knob. 121a3e0fd82Sopenharmony_ci * 122a3e0fd82Sopenharmony_ci * @param knobImage Indicates the knob image to set. 123a3e0fd82Sopenharmony_ci * @since 6 124a3e0fd82Sopenharmony_ci */ 125a3e0fd82Sopenharmony_ci void SetKnobImage(const char* knobImage); 126a3e0fd82Sopenharmony_ci 127a3e0fd82Sopenharmony_ci /** 128a3e0fd82Sopenharmony_ci * @brief Sets the color for this slider's knob. 129a3e0fd82Sopenharmony_ci * 130a3e0fd82Sopenharmony_ci * @param knobColor Indicates the knob color to set. 131a3e0fd82Sopenharmony_ci * @since 6 132a3e0fd82Sopenharmony_ci */ 133a3e0fd82Sopenharmony_ci void SetKnobColor(const ColorType knobColor) 134a3e0fd82Sopenharmony_ci { 135a3e0fd82Sopenharmony_ci SetKnobStyle(STYLE_BACKGROUND_COLOR, knobColor.full); 136a3e0fd82Sopenharmony_ci } 137a3e0fd82Sopenharmony_ci 138a3e0fd82Sopenharmony_ci /** 139a3e0fd82Sopenharmony_ci * @brief Sets the corner radius for this slider's knob. 140a3e0fd82Sopenharmony_ci * 141a3e0fd82Sopenharmony_ci * @param knobRadius Indicates the knob corner radius to set. 142a3e0fd82Sopenharmony_ci * @since 6 143a3e0fd82Sopenharmony_ci */ 144a3e0fd82Sopenharmony_ci void SetKnobRadius(int16_t knobRadius) 145a3e0fd82Sopenharmony_ci { 146a3e0fd82Sopenharmony_ci SetKnobStyle(STYLE_BORDER_RADIUS, knobRadius); 147a3e0fd82Sopenharmony_ci } 148a3e0fd82Sopenharmony_ci 149a3e0fd82Sopenharmony_ci /** 150a3e0fd82Sopenharmony_ci * @brief Sets the knob style. 151a3e0fd82Sopenharmony_ci * 152a3e0fd82Sopenharmony_ci * @param style Indicates the knob style to set. For details, see {@link Style}. 153a3e0fd82Sopenharmony_ci * @see GetKnobStyle 154a3e0fd82Sopenharmony_ci * @since 1.0 155a3e0fd82Sopenharmony_ci * @version 1.0 156a3e0fd82Sopenharmony_ci */ 157a3e0fd82Sopenharmony_ci void SetKnobStyle(const Style& style); 158a3e0fd82Sopenharmony_ci 159a3e0fd82Sopenharmony_ci /** 160a3e0fd82Sopenharmony_ci * @brief Sets a knob style. 161a3e0fd82Sopenharmony_ci * 162a3e0fd82Sopenharmony_ci * @param key Indicates the key of the style to set. 163a3e0fd82Sopenharmony_ci * @param value Indicates the value matching the key. 164a3e0fd82Sopenharmony_ci * @since 1.0 165a3e0fd82Sopenharmony_ci * @version 1.0 166a3e0fd82Sopenharmony_ci */ 167a3e0fd82Sopenharmony_ci void SetKnobStyle(uint8_t key, int64_t value); 168a3e0fd82Sopenharmony_ci 169a3e0fd82Sopenharmony_ci /** 170a3e0fd82Sopenharmony_ci * @brief Obtains the knob style. 171a3e0fd82Sopenharmony_ci * 172a3e0fd82Sopenharmony_ci * @return Returns the knob style. 173a3e0fd82Sopenharmony_ci * @since 1.0 174a3e0fd82Sopenharmony_ci * @version 1.0 175a3e0fd82Sopenharmony_ci */ 176a3e0fd82Sopenharmony_ci const Style& GetKnobStyle() const; 177a3e0fd82Sopenharmony_ci 178a3e0fd82Sopenharmony_ci /** 179a3e0fd82Sopenharmony_ci * @brief Obtains the value of a knob style. 180a3e0fd82Sopenharmony_ci * 181a3e0fd82Sopenharmony_ci * @param key Indicates the key of the style. 182a3e0fd82Sopenharmony_ci * @return Returns the value of the style. 183a3e0fd82Sopenharmony_ci * @since 1.0 184a3e0fd82Sopenharmony_ci * @version 1.0 185a3e0fd82Sopenharmony_ci */ 186a3e0fd82Sopenharmony_ci int64_t GetKnobStyle(uint8_t key) const; 187a3e0fd82Sopenharmony_ci 188a3e0fd82Sopenharmony_ci /** 189a3e0fd82Sopenharmony_ci * @brief Sets the images as pixel maps for this slider, including the background, foreground images. 190a3e0fd82Sopenharmony_ci * 191a3e0fd82Sopenharmony_ci * @param backgroundImage Indicates the background image to set. 192a3e0fd82Sopenharmony_ci * @param foregroundImage Indicates the foreground image to set. 193a3e0fd82Sopenharmony_ci * @since 1.0 194a3e0fd82Sopenharmony_ci * @version 1.0 195a3e0fd82Sopenharmony_ci */ 196a3e0fd82Sopenharmony_ci void SetImage(const ImageInfo* backgroundImage, const ImageInfo* foregroundImage); 197a3e0fd82Sopenharmony_ci 198a3e0fd82Sopenharmony_ci /** 199a3e0fd82Sopenharmony_ci * @brief Sets the images for this slider, including the background, foreground images. 200a3e0fd82Sopenharmony_ci * 201a3e0fd82Sopenharmony_ci * @param backgroundImage Indicates the background image to set. 202a3e0fd82Sopenharmony_ci * @param foregroundImage Indicates the foreground image to set. 203a3e0fd82Sopenharmony_ci * @since 1.0 204a3e0fd82Sopenharmony_ci * @version 1.0 205a3e0fd82Sopenharmony_ci */ 206a3e0fd82Sopenharmony_ci void SetImage(const char* backgroundImage, const char* foregroundImage); 207a3e0fd82Sopenharmony_ci 208a3e0fd82Sopenharmony_ci /** 209a3e0fd82Sopenharmony_ci * @brief Sets the colors for this slider, including the background, foreground colors. 210a3e0fd82Sopenharmony_ci * 211a3e0fd82Sopenharmony_ci * @param backgroundColor Indicates the background color to set. 212a3e0fd82Sopenharmony_ci * @param foregroundColor Indicates the foreground color to set. 213a3e0fd82Sopenharmony_ci * @since 1.0 214a3e0fd82Sopenharmony_ci * @version 1.0 215a3e0fd82Sopenharmony_ci */ 216a3e0fd82Sopenharmony_ci void SetSliderColor(const ColorType backgroundColor, const ColorType foregroundColor) 217a3e0fd82Sopenharmony_ci { 218a3e0fd82Sopenharmony_ci SetBackgroundStyle(STYLE_BACKGROUND_COLOR, backgroundColor.full); 219a3e0fd82Sopenharmony_ci SetForegroundStyle(STYLE_BACKGROUND_COLOR, foregroundColor.full); 220a3e0fd82Sopenharmony_ci } 221a3e0fd82Sopenharmony_ci 222a3e0fd82Sopenharmony_ci /** 223a3e0fd82Sopenharmony_ci * @brief Sets the corner radiuses for this slider, including the background, foreground corner radiuses. 224a3e0fd82Sopenharmony_ci * 225a3e0fd82Sopenharmony_ci * @param backgroundRadius Indicates the background corner radius to set. 226a3e0fd82Sopenharmony_ci * @param foregroundRadius Indicates the foreground corner radius to set. 227a3e0fd82Sopenharmony_ci * @since 1.0 228a3e0fd82Sopenharmony_ci * @version 1.0 229a3e0fd82Sopenharmony_ci */ 230a3e0fd82Sopenharmony_ci void SetSliderRadius(int16_t backgroundRadius, int16_t foregroundRadius) 231a3e0fd82Sopenharmony_ci { 232a3e0fd82Sopenharmony_ci SetBackgroundStyle(STYLE_BORDER_RADIUS, backgroundRadius); 233a3e0fd82Sopenharmony_ci SetForegroundStyle(STYLE_BORDER_RADIUS, foregroundRadius); 234a3e0fd82Sopenharmony_ci } 235a3e0fd82Sopenharmony_ci 236a3e0fd82Sopenharmony_ci#if ENABLE_ROTATE_INPUT 237a3e0fd82Sopenharmony_ci /** 238a3e0fd82Sopenharmony_ci * @brief Obtains the rotation factor. 239a3e0fd82Sopenharmony_ci * 240a3e0fd82Sopenharmony_ci * @return Returns the rotation factor. 241a3e0fd82Sopenharmony_ci * @since 5.0 242a3e0fd82Sopenharmony_ci * @version 3.0 243a3e0fd82Sopenharmony_ci */ 244a3e0fd82Sopenharmony_ci float GetRotateFactor() 245a3e0fd82Sopenharmony_ci { 246a3e0fd82Sopenharmony_ci return rotateFactor_; 247a3e0fd82Sopenharmony_ci } 248a3e0fd82Sopenharmony_ci 249a3e0fd82Sopenharmony_ci /** 250a3e0fd82Sopenharmony_ci * @brief Sets the rotation factor. 251a3e0fd82Sopenharmony_ci * 252a3e0fd82Sopenharmony_ci * @param factor Indicates the rotation factor to set. 253a3e0fd82Sopenharmony_ci * @since 5.0 254a3e0fd82Sopenharmony_ci * @version 3.0 255a3e0fd82Sopenharmony_ci */ 256a3e0fd82Sopenharmony_ci void SetRotateFactor(float factor) 257a3e0fd82Sopenharmony_ci { 258a3e0fd82Sopenharmony_ci if (MATH_ABS(factor) > MAX_ROTATE_FACTOR) { 259a3e0fd82Sopenharmony_ci rotateFactor_ = (factor > 0) ? MAX_ROTATE_FACTOR : -MAX_ROTATE_FACTOR; 260a3e0fd82Sopenharmony_ci return; 261a3e0fd82Sopenharmony_ci } 262a3e0fd82Sopenharmony_ci rotateFactor_ = factor; 263a3e0fd82Sopenharmony_ci } 264a3e0fd82Sopenharmony_ci 265a3e0fd82Sopenharmony_ci bool OnRotateEvent(const RotateEvent& event) override; 266a3e0fd82Sopenharmony_ci 267a3e0fd82Sopenharmony_ci bool OnRotateEndEvent(const RotateEvent& event) override; 268a3e0fd82Sopenharmony_ci#endif 269a3e0fd82Sopenharmony_ci bool OnClickEvent(const ClickEvent& event) override; 270a3e0fd82Sopenharmony_ci 271a3e0fd82Sopenharmony_ci bool OnDragEvent(const DragEvent& event) override; 272a3e0fd82Sopenharmony_ci 273a3e0fd82Sopenharmony_ci bool OnDragEndEvent(const DragEvent& event) override; 274a3e0fd82Sopenharmony_ci 275a3e0fd82Sopenharmony_ci bool OnPreDraw(Rect& invalidatedArea) const override 276a3e0fd82Sopenharmony_ci { 277a3e0fd82Sopenharmony_ci return false; 278a3e0fd82Sopenharmony_ci } 279a3e0fd82Sopenharmony_ci 280a3e0fd82Sopenharmony_ci void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override; 281a3e0fd82Sopenharmony_ci 282a3e0fd82Sopenharmony_ci /** 283a3e0fd82Sopenharmony_ci * @brief Represents the listener for a slider change. 284a3e0fd82Sopenharmony_ci * 285a3e0fd82Sopenharmony_ci * This is an inner class of <b>UISlider</b> used to listen for slider events and invoke the callback function. 286a3e0fd82Sopenharmony_ci * 287a3e0fd82Sopenharmony_ci * @see UISlider 288a3e0fd82Sopenharmony_ci * @since 1.0 289a3e0fd82Sopenharmony_ci * @version 1.0 290a3e0fd82Sopenharmony_ci */ 291a3e0fd82Sopenharmony_ci class UISliderEventListener : public HeapBase { 292a3e0fd82Sopenharmony_ci public: 293a3e0fd82Sopenharmony_ci /** 294a3e0fd82Sopenharmony_ci * @brief A destructor used to delete the <b> UISliderEventListener </b> instance. 295a3e0fd82Sopenharmony_ci * 296a3e0fd82Sopenharmony_ci * @since 1.0 297a3e0fd82Sopenharmony_ci * @version 1.0 298a3e0fd82Sopenharmony_ci */ 299a3e0fd82Sopenharmony_ci virtual ~UISliderEventListener() {} 300a3e0fd82Sopenharmony_ci /** 301a3e0fd82Sopenharmony_ci * @brief Called when the slider is dragged or clicked. This is a virtual function, which needs your 302a3e0fd82Sopenharmony_ci * implementation. 303a3e0fd82Sopenharmony_ci * 304a3e0fd82Sopenharmony_ci * @param value Indicates the current value of the slider. 305a3e0fd82Sopenharmony_ci * @since 1.0 306a3e0fd82Sopenharmony_ci * @version 1.0 307a3e0fd82Sopenharmony_ci */ 308a3e0fd82Sopenharmony_ci virtual void OnChange(int32_t value) {} 309a3e0fd82Sopenharmony_ci /** 310a3e0fd82Sopenharmony_ci * @brief Called when the slider is released. This is a virtual function, which needs your implementation. 311a3e0fd82Sopenharmony_ci * 312a3e0fd82Sopenharmony_ci * @param value Indicates the current value of the slider. 313a3e0fd82Sopenharmony_ci * @since 1.0 314a3e0fd82Sopenharmony_ci * @version 1.0 315a3e0fd82Sopenharmony_ci */ 316a3e0fd82Sopenharmony_ci virtual void OnRelease(int32_t value) {} 317a3e0fd82Sopenharmony_ci }; 318a3e0fd82Sopenharmony_ci 319a3e0fd82Sopenharmony_ci /** 320a3e0fd82Sopenharmony_ci * @brief Sets the listener for a slider change. 321a3e0fd82Sopenharmony_ci * 322a3e0fd82Sopenharmony_ci * When a user drags or clicks the slider, listening is triggered and the <b>OnChange</b> callback is invoked. 323a3e0fd82Sopenharmony_ci * When a user releases the slider, the <b>OnRelease</b> callback is invoked. 324a3e0fd82Sopenharmony_ci * 325a3e0fd82Sopenharmony_ci * @param listener Indicates the listener to set. For details, see {@link UISliderEventListener}. 326a3e0fd82Sopenharmony_ci * @since 1.0 327a3e0fd82Sopenharmony_ci * @version 1.0 328a3e0fd82Sopenharmony_ci */ 329a3e0fd82Sopenharmony_ci void SetSliderEventListener(UISliderEventListener* listener) 330a3e0fd82Sopenharmony_ci { 331a3e0fd82Sopenharmony_ci listener_ = listener; 332a3e0fd82Sopenharmony_ci } 333a3e0fd82Sopenharmony_ci 334a3e0fd82Sopenharmony_ciprotected: 335a3e0fd82Sopenharmony_ci bool InitImage() override; 336a3e0fd82Sopenharmony_ci 337a3e0fd82Sopenharmony_ciprivate: 338a3e0fd82Sopenharmony_ci static constexpr uint8_t MAX_ROTATE_FACTOR = 128; 339a3e0fd82Sopenharmony_ci 340a3e0fd82Sopenharmony_ci int16_t knobWidth_; 341a3e0fd82Sopenharmony_ci bool knobStyleAllocFlag_; 342a3e0fd82Sopenharmony_ci Style* knobStyle_; 343a3e0fd82Sopenharmony_ci Image* knobImage_; 344a3e0fd82Sopenharmony_ci 345a3e0fd82Sopenharmony_ci void DrawKnob(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, const Rect& foregroundRect); 346a3e0fd82Sopenharmony_ci void DrawForeground(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, Rect& coords); 347a3e0fd82Sopenharmony_ci int32_t CalculateCurrentValue(int16_t length, int16_t totalLength); 348a3e0fd82Sopenharmony_ci int32_t UpdateCurrentValue(const Point& knobPosition); 349a3e0fd82Sopenharmony_ci#if ENABLE_ROTATE_INPUT 350a3e0fd82Sopenharmony_ci float rotateFactor_; 351a3e0fd82Sopenharmony_ci float cachedRotation_; 352a3e0fd82Sopenharmony_ci#endif 353a3e0fd82Sopenharmony_ci UISliderEventListener* listener_; 354a3e0fd82Sopenharmony_ci}; // class UISlider 355a3e0fd82Sopenharmony_ci} // namespace OHOS 356a3e0fd82Sopenharmony_ci#endif // GRAPHIC_LITE_UI_SLIDER_H 357