1 #ifndef DALI_TOOLKIT_INTERNAL_SLIDER_H
2 #define DALI_TOOLKIT_INTERNAL_SLIDER_H
5 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/devel-api/atspi-interfaces/value.h>
23 #include <dali/public-api/adaptor-framework/timer.h>
24 #include <dali/public-api/object/property-array.h>
25 #include <dali/public-api/object/property-map.h>
28 #include <dali-toolkit/devel-api/controls/control-devel.h>
29 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
30 #include <dali-toolkit/public-api/controls/control-impl.h>
31 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
32 #include <dali-toolkit/public-api/controls/slider/slider.h>
33 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
45 typedef Dali::IntrusivePtr<Slider> SliderPtr;
48 * @copydoc Toolkit::Slider
50 class Slider : public Control
53 typedef Property::Array MarkList;
56 * Create a new Slider.
58 * @return A public handle to the newly allocated Slider.
60 static Dali::Toolkit::Slider New();
66 * Set marks from a list
68 * @param[in] marks The list of marks to set
70 void SetMarks(const MarkList& marks);
73 * Get the list of marks
75 * @return The marks list
77 const MarkList& GetMarks() const;
80 * Set if should snap to marks or not
82 * @param[in] snap Flag to snap to marks or not
84 void SetSnapToMarks(bool snap);
87 * Return if snap to marks is set or not
89 * @return If snap to marks is set
91 bool GetSnapToMarks() const;
94 * Set the value of the slider
96 * @param[in] value The value to set. Will be clamped to [lowerBound .. upperBound]
98 void SetValue(float value);
101 * Get the value of the slider
103 * @return The current value of the slider
105 float GetValue() const;
110 * @param[in] region The hit region
112 void SetHitRegion(const Vector2& region);
117 * @return The hit region
119 const Vector2& GetHitRegion() const;
122 * Set the track region
124 * @param[in] region The track region
126 void SetTrackRegion(const Vector2& region);
129 * Get the track region
131 * @return The track region
133 const Vector2& GetTrackRegion() const;
136 * @brief Set the disabled color.
138 * @param[in] color The disabled color.
140 void SetDisabledColor(const Vector4& color);
143 * @brief Get disabled color
145 * @return The disabled color
147 Vector4 GetDisabledColor() const;
150 * Set the value precision to be used for numbers in the slider
152 * @param[in] precision The number of decimal places to use for printing numbers
154 void SetValuePrecision(int precision);
157 * Get value precision
159 * @return The value precision
161 int GetValuePrecision() const;
166 * @param[in] showPopup The show popup flag
168 void SetShowPopup(bool showPopup);
171 * Get show value in popup
173 * @return The show value flag
175 bool GetShowPopup() const;
178 * Set show value on handle
180 * @param[in] showValue The show value flag
182 void SetShowValue(bool showValue);
185 * Get show value on handle
187 * @return The show value flag
189 bool GetShowValue() const;
194 * param[in] enabled Set the enabled flag
196 void SetEnabled(bool enabled);
199 * Return if enabled or not
203 bool IsEnabled() const;
206 * @brief Set the mark tolerance
208 * The tolerance is the percentage of the slider width for which snapping to
211 * @param[in] tolerance The percentage of width for which to snap
213 void SetMarkTolerance(float tolerance);
216 * Return the mark tolerance
218 * @return The tolerance set for snapping to marks
220 float GetMarkTolerance() const;
226 * @copydoc Toolkit::Slider::ValueChangedSignal()
228 Toolkit::Slider::ValueChangedSignalType& ValueChangedSignal();
231 * copydoc Toolkit::Slider::SlidingFinishedSignal()
233 Toolkit::Slider::ValueChangedSignalType& SlidingFinishedSignal();
236 * @copydoc Toolkit::Slider::MarkReachedSignal()
238 Toolkit::Slider::MarkReachedSignalType& MarkReachedSignal();
241 * Connects a callback function with the object's signals.
242 * @param[in] object The object providing the signal.
243 * @param[in] tracker Used to disconnect the signal.
244 * @param[in] signalName The signal to connect to.
245 * @param[in] functor A newly allocated FunctorDelegate.
246 * @return True if the signal was connected.
247 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
249 static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
254 * Called when a property of an object of this type is set.
255 * @param[in] object The object whose property is set.
256 * @param[in] index The property index.
257 * @param[in] value The new property value.
259 static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
262 * Called to retrieve a property of an object of this type.
263 * @param[in] object The object whose property is to be retrieved.
264 * @param[in] index The property index.
265 * @return The current value of the property.
267 static Property::Value GetProperty(BaseObject* object, Property::Index propertyIndex);
271 * Construct a new Slider.
276 * A reference counted object may only be deleted by calling Unreference()
281 * @copydoc CustomActorImpl::OnRelayout
283 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
287 * Domain is a from/to pair
297 Domain(Vector2 fromVal, Vector2 toVal)
317 * @copydoc Toolkit::Control::OnInitialize()
319 void OnInitialize() override;
324 * @param[in] actor The actor the event is raised for
325 * @param[in] touch The touch info
326 * @return If touch is handled or not
328 bool OnTouch(Actor actor, const TouchEvent& touch);
333 * @param[in] actor The actor the event is raised for
334 * @param[in] gesture The pan event info
336 void OnPan(Actor actor, const PanGesture& gesture);
339 * Map a position onto a domain and return the result as a percentage
341 * @param[in] point The point to map onto the domain
342 * @return The result as a percentage [0..1]
344 float MapPercentage(const Vector2& point);
347 * Map a value in the range to a percentage
349 * @param[in] point The value in range [lowerBound..upperBound]
350 * @return The result as a percentage [0..1]
352 float MapValuePercentage(float value);
355 * Convert a point in local hit space into domain space
357 * @param[in] x The x position to convert
358 * @return The x position in domain space
360 float HitSpaceToDomain(float x);
363 * Map a percentage onto the slider's bounds
365 * @param[in] percent The current value of slider in percent
366 * @param[in] lowerBound The lower bound to map onto
367 * @param[in] upperBound The upper bound to map onto
368 * @return The value of percent mapped from lowerBound to upperBound
370 float MapBounds(float percent, float lowerBound, float upperBound);
373 * Get the range of the valid values the slider handle can move between
375 * @param[in] currentSize The current size of the slider
376 * @return The range as a domain pair
378 Domain CalcDomain(const Vector2& currentSize);
381 * Create the hit region
383 * @return The hit region actor
385 Actor CreateHitRegion();
388 * Create the track for the slider
390 * @return The track actor
392 Toolkit::ImageView CreateTrack();
395 * Create the progress track for the slider
397 * @return The track actor
399 Toolkit::ImageView CreateProgress();
402 * Create the handle for the slider
404 * @return The created image handle
406 Toolkit::ImageView CreateHandle();
409 * Create the popup arrow
411 * @return The created image handle
413 Toolkit::ImageView CreatePopupArrow();
418 * @return The created image handle
420 Toolkit::ImageView CreatePopup();
423 * Create the textview for the popup
425 * @return The textview created for the popup
427 Toolkit::TextLabel CreatePopupText();
430 * Create the value display for the slider
432 * @return The created root actor of the display
434 Actor CreateValueDisplay();
437 * Set the skin based on the current state
442 * Create all the children
444 void CreateChildren();
457 * Display the popup for a set duration with the given value
459 * @param[in] value The value to display in the popup
461 void DisplayPopup(float value);
464 * If there are marks present, filter the incoming percent based on snapping to any nearby marks
466 * @param[in] value The incoming value on the slider to filter
467 * @return The filtered percentage snapped to any nearby marks
469 float MarkFilter(float value);
472 * If there are marks present, snap the incoming percent to the nearest mark
474 * @param[in] value The incoming value on the slider to snap
475 * @return The filtered percentage snapped to the nearest mark
477 float SnapToMark(float value);
480 * Search for if a mark has been reached
482 * @param[in] value The value to search against
483 * @param[out] outIndex The index of the mark if found
484 * @return If a mark has been found to match percent
486 bool MarkReached(float value, int& outIndex);
489 * Handler for when the value view needs to be hidden
491 * @return If handled or not
493 bool HideValueView();
496 * Set value choosing whether to fire signals or not
498 * @paramp[in] value The value to set
499 * @param[in] raiseSignals Configure signals to be raised or not.
501 void DisplayValue(float value, bool raiseSignals);
504 * Create the image for the track
506 * @param[in] filename The track image
508 void SetTrackVisual(const std::string& filename);
511 * @brief Set the track visual from an Dali::Property::Map
513 * @param[in] map The Dali::Property::Map to use for to display
515 void SetTrackVisual(Dali::Property::Map map);
518 * @brief Return the track image.
520 * @return The track image.
522 std::string GetTrackVisual();
525 * Create the image for the progress bar
527 * @param[in] filename The progress bar image
529 void SetProgressVisual(const std::string& filename);
532 * @brief Set the progress visual from an Dali::Property::Map
534 * @param[in] map The Dali::Property::Map to use for to display
536 void SetProgressVisual(Dali::Property::Map map);
539 * @brief Return the progress bar image.
541 * @return The progress bar image if it exists.
543 std::string GetProgressVisual();
546 * @brief Create the image for the popup
548 * @param[in] filename The popup image
550 void CreatePopupImage(const std::string& filename);
553 * @brief Set the popup image
555 * @param[in] filename The popup image to set
557 void SetPopupVisual(const std::string& filename);
560 * @brief Set the popup from an Dali::Property::Map
562 * @param[in] map The Dali::Property::Map to use for to display
564 void SetPopupVisual(Dali::Property::Map map);
567 * @brief Return the popup image.
569 * @return The popup image if it exists.
571 std::string GetPopupVisual();
574 * @brief Set the popup arrow image
576 * @param[in] filename The popup arrow image to set
578 void SetPopupArrowVisual(const std::string& filename);
581 * @brief Set the popup arrow from an Dali::Property::Map
583 * @param[in] map The Dali::Property::Map to use for to display
585 void SetPopupArrowVisual(Dali::Property::Map map);
588 * @brief Return the popup arrow image.
590 * @return The popup arrow image if it exists.
592 std::string GetPopupArrowVisual();
595 * Create the image for the popup arrow
597 * @param[in] filename The popup arrow image to load and set
599 void CreatePopupArrowImage(const std::string& filename);
602 * Set the size of the progress bar region
604 * @param[in] region The size of the region to set
606 void ResizeProgressRegion(const Vector2& region);
609 * Create the image for the handle
611 * @param[in] filename The handle image
613 void SetHandleVisual(const std::string& filename);
616 * @brief Set the handle visual from an Dali::Property::Map
618 * @param[in] map The Dali::Property::Map to use for to display
620 void SetHandleVisual(Property::Map map);
623 * @brief Return the handle image.
625 * @return The handle image if it exists.
627 std::string GetHandleVisual();
630 * Reset the size of the handle
632 * @param[in] size The size of the handle to set
634 void ResizeHandleSize(const Vector2& size);
637 * Create and display the value on the handle
639 void CreateHandleValueDisplay();
642 * Remove and destroy the handle value display
644 void DestroyHandleValueDisplay();
647 * Set the size of the handle
649 * @param[in] size The handle size
651 void SetHandleSize(const Vector2& size);
654 * Get the size of the handle
656 * @return The handle size
658 const Vector2& GetHandleSize() const;
661 * Set the lower bound of the slider's value
663 * @param[in] bound The value to set for the lower bound
665 void SetLowerBound(float bound);
668 * Get the lower bound of the slider's value
670 * @return The lower bound value
672 float GetLowerBound() const;
675 * Set the upper bound of the slider's value
677 * @param[in] bound The value to set for the upper bound
679 void SetUpperBound(float bound);
682 * Get the upper bound of the slider's value
684 * @return The upper bound value
686 float GetUpperBound() const;
690 Slider(const Slider&);
693 Slider& operator=(const Slider& rhs);
696 Domain mDomain; ///< Current domain of the handle
698 Actor mHitArea; ///< The input handler
699 Actor mValueDisplay; ///< Display of the value
700 Toolkit::ImageView mTrack; ///< Track image
701 Toolkit::ImageView mHandle; ///< Slider handle
702 Toolkit::ImageView mProgress; ///< Progress bar
703 Toolkit::ImageView mPopup; ///< Popup backing
704 Toolkit::ImageView mPopupArrow; ///< Popup arrow backing
706 Toolkit::TextLabel mValueTextLabel; //< The text value in popup
707 Toolkit::TextLabel mHandleValueTextLabel; ///< The text value on handle
708 Vector2 mHandleLastTouchPoint; ///< The last touch point for the handle
709 Timer mValueTimer; ///< Timer used to hide value view
711 Toolkit::Slider::ValueChangedSignalType mValueChangedSignal; ///< Signal emitted when the value is changed
712 Toolkit::Slider::ValueChangedSignalType mSlidingFinishedSignal; ///< Signal emitted when a sliding is finished
713 Toolkit::Slider::MarkReachedSignalType mMarkReachedSignal; ///< Signal emitted when a mark is reached
715 SliderState mState; ///< The state of the slider
717 PanGestureDetector mPanDetector; ///< Hit region pan detector
719 MarkList mMarks; ///< List of discreet marks
721 std::string mPopupVisual; ///< Image for popup image
722 std::string mPopupArrowVisual; ///< Image for popup arrow image
723 std::string mTrackVisual; ///< Image for track image
724 std::string mHandleVisual; ///< Image for handle image
725 std::string mProgressVisual; ///< Image for progress bar image
727 Property::Map mPopupMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
728 Property::Map mTrackMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
729 Property::Map mHandleMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
730 Property::Map mProgressMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
731 Property::Map mPopupArrowMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
733 Vector4 mDisabledColor; ///< The color to tint the slider when disabled
735 Vector2 mHitRegion; ///< Size of hit region
736 Vector2 mTrackRegion; ///< Size of track region
737 Vector2 mHandleSize; ///< Size of the handle
739 float mLowerBound = 0.0f; ///< Lower bound on value
740 float mUpperBound = 1.0f; ///< Upper bound on value
741 float mValue = 0.0f; ///< Current value of slider
743 float mMarkTolerance = 0.05f; ///< Tolerance in percentage of slider width for which to snap to marks
745 int mValuePrecision; ///< The precision to use for outputting the value
747 bool mShowPopup : 1, ///< Show the popup or not
748 mShowValue : 1, ///< Whether to display the value number or not on the handle
749 mSnapToMarks : 1; ///< Turn on or off snapping to marks
752 struct AccessibleImpl : public DevelControl::ControlAccessible,
753 public virtual Dali::Accessibility::Value
755 using DevelControl::ControlAccessible::ControlAccessible;
757 double GetMinimum() const override;
758 double GetCurrent() const override;
759 double GetMaximum() const override;
760 bool SetCurrent(double) override;
761 double GetMinimumIncrement() const override;
765 } // namespace Internal
767 // Helpers for public-api forwarding methods
769 inline Toolkit::Internal::Slider& GetImpl(Toolkit::Slider& pub)
771 DALI_ASSERT_ALWAYS(pub);
773 Dali::RefObject& handle = pub.GetImplementation();
775 return static_cast<Toolkit::Internal::Slider&>(handle);
778 inline const Toolkit::Internal::Slider& GetImpl(const Toolkit::Slider& pub)
780 DALI_ASSERT_ALWAYS(pub);
782 const Dali::RefObject& handle = pub.GetImplementation();
784 return static_cast<const Toolkit::Internal::Slider&>(handle);
787 } // namespace Toolkit
791 #endif // DALI_TOOLKIT_INTERNAL_SLIDER_H