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