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;
322 * @copydoc Toolkit::Internal::Control::CreateAccessibleObject()
324 DevelControl::ControlAccessible* CreateAccessibleObject() override;
329 * @param[in] actor The actor the event is raised for
330 * @param[in] touch The touch info
331 * @return If touch is handled or not
333 bool OnTouch(Actor actor, const TouchEvent& touch);
338 * @param[in] actor The actor the event is raised for
339 * @param[in] gesture The pan event info
341 void OnPan(Actor actor, const PanGesture& gesture);
344 * Map a position onto a domain and return the result as a percentage
346 * @param[in] point The point to map onto the domain
347 * @return The result as a percentage [0..1]
349 float MapPercentage(const Vector2& point);
352 * Map a value in the range to a percentage
354 * @param[in] point The value in range [lowerBound..upperBound]
355 * @return The result as a percentage [0..1]
357 float MapValuePercentage(float value);
360 * Convert a point in local hit space into domain space
362 * @param[in] x The x position to convert
363 * @return The x position in domain space
365 float HitSpaceToDomain(float x);
368 * Map a percentage onto the slider's bounds
370 * @param[in] percent The current value of slider in percent
371 * @param[in] lowerBound The lower bound to map onto
372 * @param[in] upperBound The upper bound to map onto
373 * @return The value of percent mapped from lowerBound to upperBound
375 float MapBounds(float percent, float lowerBound, float upperBound);
378 * Get the range of the valid values the slider handle can move between
380 * @param[in] currentSize The current size of the slider
381 * @return The range as a domain pair
383 Domain CalcDomain(const Vector2& currentSize);
386 * Create the hit region
388 * @return The hit region actor
390 Actor CreateHitRegion();
393 * Create the track for the slider
395 * @return The track actor
397 Toolkit::ImageView CreateTrack();
400 * Create the progress track for the slider
402 * @return The track actor
404 Toolkit::ImageView CreateProgress();
407 * Create the handle for the slider
409 * @return The created image handle
411 Toolkit::ImageView CreateHandle();
414 * Create the popup arrow
416 * @return The created image handle
418 Toolkit::ImageView CreatePopupArrow();
423 * @return The created image handle
425 Toolkit::ImageView CreatePopup();
428 * Create the textview for the popup
430 * @return The textview created for the popup
432 Toolkit::TextLabel CreatePopupText();
435 * Create the value display for the slider
437 * @return The created root actor of the display
439 Actor CreateValueDisplay();
442 * Set the skin based on the current state
447 * Create all the children
449 void CreateChildren();
462 * Display the popup for a set duration with the given value
464 * @param[in] value The value to display in the popup
466 void DisplayPopup(float value);
469 * If there are marks present, filter the incoming percent based on snapping to any nearby marks
471 * @param[in] value The incoming value on the slider to filter
472 * @return The filtered percentage snapped to any nearby marks
474 float MarkFilter(float value);
477 * If there are marks present, snap the incoming percent to the nearest mark
479 * @param[in] value The incoming value on the slider to snap
480 * @return The filtered percentage snapped to the nearest mark
482 float SnapToMark(float value);
485 * Search for if a mark has been reached
487 * @param[in] value The value to search against
488 * @param[out] outIndex The index of the mark if found
489 * @return If a mark has been found to match percent
491 bool MarkReached(float value, int& outIndex);
494 * Handler for when the value view needs to be hidden
496 * @return If handled or not
498 bool HideValueView();
501 * Set value choosing whether to fire signals or not
503 * @paramp[in] value The value to set
504 * @param[in] raiseSignals Configure signals to be raised or not.
506 void DisplayValue(float value, bool raiseSignals);
509 * Create the image for the track
511 * @param[in] filename The track image
513 void SetTrackVisual(const std::string& filename);
516 * @brief Set the track visual from an Dali::Property::Map
518 * @param[in] map The Dali::Property::Map to use for to display
520 void SetTrackVisual(Dali::Property::Map map);
523 * @brief Return the track image.
525 * @return The track image.
527 std::string GetTrackVisual();
530 * Create the image for the progress bar
532 * @param[in] filename The progress bar image
534 void SetProgressVisual(const std::string& filename);
537 * @brief Set the progress visual from an Dali::Property::Map
539 * @param[in] map The Dali::Property::Map to use for to display
541 void SetProgressVisual(Dali::Property::Map map);
544 * @brief Return the progress bar image.
546 * @return The progress bar image if it exists.
548 std::string GetProgressVisual();
551 * @brief Create the image for the popup
553 * @param[in] filename The popup image
555 void CreatePopupImage(const std::string& filename);
558 * @brief Set the popup image
560 * @param[in] filename The popup image to set
562 void SetPopupVisual(const std::string& filename);
565 * @brief Set the popup from an Dali::Property::Map
567 * @param[in] map The Dali::Property::Map to use for to display
569 void SetPopupVisual(Dali::Property::Map map);
572 * @brief Return the popup image.
574 * @return The popup image if it exists.
576 std::string GetPopupVisual();
579 * @brief Set the popup arrow image
581 * @param[in] filename The popup arrow image to set
583 void SetPopupArrowVisual(const std::string& filename);
586 * @brief Set the popup arrow from an Dali::Property::Map
588 * @param[in] map The Dali::Property::Map to use for to display
590 void SetPopupArrowVisual(Dali::Property::Map map);
593 * @brief Return the popup arrow image.
595 * @return The popup arrow image if it exists.
597 std::string GetPopupArrowVisual();
600 * Create the image for the popup arrow
602 * @param[in] filename The popup arrow image to load and set
604 void CreatePopupArrowImage(const std::string& filename);
607 * Set the size of the progress bar region
609 * @param[in] region The size of the region to set
611 void ResizeProgressRegion(const Vector2& region);
614 * Create the image for the handle
616 * @param[in] filename The handle image
618 void SetHandleVisual(const std::string& filename);
621 * @brief Set the handle visual from an Dali::Property::Map
623 * @param[in] map The Dali::Property::Map to use for to display
625 void SetHandleVisual(Property::Map map);
628 * @brief Return the handle image.
630 * @return The handle image if it exists.
632 std::string GetHandleVisual();
635 * Reset the size of the handle
637 * @param[in] size The size of the handle to set
639 void ResizeHandleSize(const Vector2& size);
642 * Create and display the value on the handle
644 void CreateHandleValueDisplay();
647 * Remove and destroy the handle value display
649 void DestroyHandleValueDisplay();
652 * Set the size of the handle
654 * @param[in] size The handle size
656 void SetHandleSize(const Vector2& size);
659 * Get the size of the handle
661 * @return The handle size
663 const Vector2& GetHandleSize() const;
666 * Set the lower bound of the slider's value
668 * @param[in] bound The value to set for the lower bound
670 void SetLowerBound(float bound);
673 * Get the lower bound of the slider's value
675 * @return The lower bound value
677 float GetLowerBound() const;
680 * Set the upper bound of the slider's value
682 * @param[in] bound The value to set for the upper bound
684 void SetUpperBound(float bound);
687 * Get the upper bound of the slider's value
689 * @return The upper bound value
691 float GetUpperBound() const;
695 Slider(const Slider&);
698 Slider& operator=(const Slider& rhs);
701 Domain mDomain; ///< Current domain of the handle
703 Actor mHitArea; ///< The input handler
704 Actor mValueDisplay; ///< Display of the value
705 Toolkit::ImageView mTrack; ///< Track image
706 Toolkit::ImageView mHandle; ///< Slider handle
707 Toolkit::ImageView mProgress; ///< Progress bar
708 Toolkit::ImageView mPopup; ///< Popup backing
709 Toolkit::ImageView mPopupArrow; ///< Popup arrow backing
711 Toolkit::TextLabel mValueTextLabel; //< The text value in popup
712 Toolkit::TextLabel mHandleValueTextLabel; ///< The text value on handle
713 Vector2 mHandleLastTouchPoint; ///< The last touch point for the handle
714 Timer mValueTimer; ///< Timer used to hide value view
716 Toolkit::Slider::ValueChangedSignalType mValueChangedSignal; ///< Signal emitted when the value is changed
717 Toolkit::Slider::ValueChangedSignalType mSlidingFinishedSignal; ///< Signal emitted when a sliding is finished
718 Toolkit::Slider::MarkReachedSignalType mMarkReachedSignal; ///< Signal emitted when a mark is reached
720 SliderState mState; ///< The state of the slider
722 PanGestureDetector mPanDetector; ///< Hit region pan detector
724 MarkList mMarks; ///< List of discreet marks
726 std::string mPopupVisual; ///< Image for popup image
727 std::string mPopupArrowVisual; ///< Image for popup arrow image
728 std::string mTrackVisual; ///< Image for track image
729 std::string mHandleVisual; ///< Image for handle image
730 std::string mProgressVisual; ///< Image for progress bar image
732 Property::Map mPopupMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
733 Property::Map mTrackMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
734 Property::Map mHandleMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
735 Property::Map mProgressMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
736 Property::Map mPopupArrowMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
738 Vector4 mDisabledColor; ///< The color to tint the slider when disabled
740 Vector2 mHitRegion; ///< Size of hit region
741 Vector2 mTrackRegion; ///< Size of track region
742 Vector2 mHandleSize; ///< Size of the handle
744 float mLowerBound = 0.0f; ///< Lower bound on value
745 float mUpperBound = 1.0f; ///< Upper bound on value
746 float mValue = 0.0f; ///< Current value of slider
748 float mMarkTolerance = 0.05f; ///< Tolerance in percentage of slider width for which to snap to marks
750 int mValuePrecision; ///< The precision to use for outputting the value
752 bool mShowPopup : 1, ///< Show the popup or not
753 mShowValue : 1, ///< Whether to display the value number or not on the handle
754 mSnapToMarks : 1; ///< Turn on or off snapping to marks
757 class SliderAccessible : public DevelControl::ControlAccessible,
758 public virtual Dali::Accessibility::Value
761 using DevelControl::ControlAccessible::ControlAccessible;
764 * @copydoc Dali::Accessibility::Value::GetMinimum()
766 double GetMinimum() const override;
769 * @copydoc Dali::Accessibility::Value::GetCurrent()
771 double GetCurrent() const override;
774 * @copydoc Dali::Accessibility::Value::GetMaximum()
776 double GetMaximum() const override;
779 * @copydoc Dali::Accessibility::Value::SetCurrent()
781 bool SetCurrent(double) override;
784 * @copydoc Dali::Accessibility::Value::GetMinimumIncrement()
786 double GetMinimumIncrement() const override;
790 } // namespace Internal
792 // Helpers for public-api forwarding methods
794 inline Toolkit::Internal::Slider& GetImpl(Toolkit::Slider& pub)
796 DALI_ASSERT_ALWAYS(pub);
798 Dali::RefObject& handle = pub.GetImplementation();
800 return static_cast<Toolkit::Internal::Slider&>(handle);
803 inline const Toolkit::Internal::Slider& GetImpl(const Toolkit::Slider& pub)
805 DALI_ASSERT_ALWAYS(pub);
807 const Dali::RefObject& handle = pub.GetImplementation();
809 return static_cast<const Toolkit::Internal::Slider&>(handle);
812 } // namespace Toolkit
816 #endif // DALI_TOOLKIT_INTERNAL_SLIDER_H