1 #ifndef DALI_TOOLKIT_INTERNAL_SLIDER_H
2 #define DALI_TOOLKIT_INTERNAL_SLIDER_H
5 * Copyright (c) 2020 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/public-api/adaptor-framework/timer.h>
23 #include <dali/public-api/object/property-array.h>
24 #include <dali/public-api/object/property-map.h>
27 #include <dali-toolkit/public-api/controls/control-impl.h>
28 #include <dali-toolkit/public-api/controls/slider/slider.h>
29 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
30 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
31 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
46 typedef Dali::IntrusivePtr< Slider > SliderPtr;
49 * @copydoc Toolkit::Slider
51 class Slider : public Control
55 typedef Property::Array MarkList;
58 * Create a new Slider.
60 * @return A public handle to the newly allocated Slider.
62 static Dali::Toolkit::Slider New();
69 * Set marks from a list
71 * @param[in] marks The list of marks to set
73 void SetMarks( const MarkList& marks );
76 * Get the list of marks
78 * @return The marks list
80 const MarkList& GetMarks() const;
83 * Set if should snap to marks or not
85 * @param[in] snap Flag to snap to marks or not
87 void SetSnapToMarks( bool snap );
90 * Return if snap to marks is set or not
92 * @return If snap to marks is set
94 bool GetSnapToMarks() const;
97 * Set the value of the slider
99 * @param[in] value The value to set. Will be clamped to [lowerBound .. upperBound]
101 void SetValue( float value );
104 * Get the value of the slider
106 * @return The current value of the slider
108 float GetValue() const;
113 * @param[in] region The hit region
115 void SetHitRegion( const Vector2& region );
120 * @return The hit region
122 const Vector2& GetHitRegion() const;
125 * Set the track region
127 * @param[in] region The track region
129 void SetTrackRegion( const Vector2& region );
132 * Get the track region
134 * @return The track region
136 const Vector2& GetTrackRegion() const;
139 * @brief Set the disabled color.
141 * @param[in] color The disabled color.
143 void SetDisabledColor( const Vector4& color );
146 * @brief Get disabled color
148 * @return The disabled color
150 Vector4 GetDisabledColor() const;
153 * Set the value precision to be used for numbers in the slider
155 * @param[in] precision The number of decimal places to use for printing numbers
157 void SetValuePrecision( int precision );
160 * Get value precision
162 * @return The value precision
164 int GetValuePrecision() const;
169 * @param[in] showPopup The show popup flag
171 void SetShowPopup( bool showPopup );
174 * Get show value in popup
176 * @return The show value flag
178 bool GetShowPopup() const;
181 * Set show value on handle
183 * @param[in] showValue The show value flag
185 void SetShowValue( bool showValue );
188 * Get show value on handle
190 * @return The show value flag
192 bool GetShowValue() const;
197 * param[in] enabled Set the enabled flag
199 void SetEnabled( bool enabled );
202 * Return if enabled or not
206 bool IsEnabled() const;
209 * @brief Set the mark tolerance
211 * The tolerance is the percentage of the slider width for which snapping to
214 * @param[in] tolerance The percentage of width for which to snap
216 void SetMarkTolerance( float tolerance );
219 * Return the mark tolerance
221 * @return The tolerance set for snapping to marks
223 float GetMarkTolerance() const;
229 * @copydoc Toolkit::Slider::ValueChangedSignal()
231 Toolkit::Slider::ValueChangedSignalType& ValueChangedSignal();
234 * copydoc Toolkit::Slider::SlidingFinishedSignal()
236 Toolkit::Slider::ValueChangedSignalType& SlidingFinishedSignal();
239 * @copydoc Toolkit::Slider::MarkReachedSignal()
241 Toolkit::Slider::MarkReachedSignalType& MarkReachedSignal();
244 * Connects a callback function with the object's signals.
245 * @param[in] object The object providing the signal.
246 * @param[in] tracker Used to disconnect the signal.
247 * @param[in] signalName The signal to connect to.
248 * @param[in] functor A newly allocated FunctorDelegate.
249 * @return True if the signal was connected.
250 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
252 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName,
253 FunctorDelegate* functor );
258 * Called when a property of an object of this type is set.
259 * @param[in] object The object whose property is set.
260 * @param[in] index The property index.
261 * @param[in] value The new property value.
263 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
266 * Called to retrieve a property of an object of this type.
267 * @param[in] object The object whose property is to be retrieved.
268 * @param[in] index The property index.
269 * @return The current value of the property.
271 static Property::Value GetProperty( BaseObject* object, Property::Index propertyIndex );
276 * Construct a new Slider.
281 * A reference counted object may only be deleted by calling Unreference()
286 * @copydoc CustomActorImpl::OnRelayout
288 void OnRelayout( const Vector2& size, RelayoutContainer& container ) override;
294 * Domain is a from/to pair
304 Domain( Vector2 fromVal, Vector2 toVal )
305 : from( fromVal ), to( toVal )
324 * @copydoc Toolkit::Control::OnInitialize()
326 void OnInitialize() override;
331 * @param[in] actor The actor the event is raised for
332 * @param[in] touch The touch info
333 * @return If touch is handled or not
335 bool OnTouch( Actor actor, const TouchEvent& touch );
340 * @param[in] actor The actor the event is raised for
341 * @param[in] gesture The pan event info
343 void OnPan( Actor actor, const PanGesture& gesture );
346 * Map a position onto a domain and return the result as a percentage
348 * @param[in] point The point to map onto the domain
349 * @return The result as a percentage [0..1]
351 float MapPercentage( const Vector2& point );
354 * Map a value in the range to a percentage
356 * @param[in] point The value in range [lowerBound..upperBound]
357 * @return The result as a percentage [0..1]
359 float MapValuePercentage( float value );
362 * Convert a point in local hit space into domain space
364 * @param[in] x The x position to convert
365 * @return The x position in domain space
367 float HitSpaceToDomain( float x );
370 * Map a percentage onto the slider's bounds
372 * @param[in] percent The current value of slider in percent
373 * @param[in] lowerBound The lower bound to map onto
374 * @param[in] upperBound The upper bound to map onto
375 * @return The value of percent mapped from lowerBound to upperBound
377 float MapBounds( float percent, float lowerBound, float upperBound );
380 * Get the range of the valid values the slider handle can move between
382 * @param[in] currentSize The current size of the slider
383 * @return The range as a domain pair
385 Domain CalcDomain( const Vector2& currentSize );
388 * Create the hit region
390 * @return The hit region actor
392 Actor CreateHitRegion();
395 * Create the track for the slider
397 * @return The track actor
399 Toolkit::ImageView CreateTrack();
402 * Create the progress track for the slider
404 * @return The track actor
406 Toolkit::ImageView CreateProgress();
409 * Create the handle for the slider
411 * @return The created image handle
413 Toolkit::ImageView CreateHandle();
416 * Create the popup arrow
418 * @return The created image handle
420 Toolkit::ImageView CreatePopupArrow();
425 * @return The created image handle
427 Toolkit::ImageView CreatePopup();
430 * Create the textview for the popup
432 * @return The textview created for the popup
434 Toolkit::TextLabel CreatePopupText();
437 * Create the value display for the slider
439 * @return The created root actor of the display
441 Actor CreateValueDisplay();
444 * Set the skin based on the current state
449 * Create all the children
451 void CreateChildren();
464 * Display the popup for a set duration with the given value
466 * @param[in] value The value to display in the popup
468 void DisplayPopup( float value );
471 * If there are marks present, filter the incoming percent based on snapping to any nearby marks
473 * @param[in] value The incoming value on the slider to filter
474 * @return The filtered percentage snapped to any nearby marks
476 float MarkFilter( float value );
479 * If there are marks present, snap the incoming percent to the nearest mark
481 * @param[in] value The incoming value on the slider to snap
482 * @return The filtered percentage snapped to the nearest mark
484 float SnapToMark( float value );
487 * Search for if a mark has been reached
489 * @param[in] value The value to search against
490 * @param[out] outIndex The index of the mark if found
491 * @return If a mark has been found to match percent
493 bool MarkReached( float value, int& outIndex );
496 * Handler for when the value view needs to be hidden
498 * @return If handled or not
500 bool HideValueView();
503 * Set value choosing whether to fire signals or not
505 * @paramp[in] value The value to set
506 * @param[in] raiseSignals Configure signals to be raised or not.
508 void DisplayValue( float value, bool raiseSignals );
511 * Create the image for the track
513 * @param[in] filename The track image
515 void SetTrackVisual( const std::string& filename );
518 * @brief Set the track visual from an Dali::Property::Map
520 * @param[in] map The Dali::Property::Map to use for to display
522 void SetTrackVisual( Dali::Property::Map map );
525 * @brief Return the track image.
527 * @return The track image.
529 std::string GetTrackVisual();
532 * Create the image for the progress bar
534 * @param[in] filename The progress bar image
536 void SetProgressVisual( const std::string& filename );
539 * @brief Set the progress visual from an Dali::Property::Map
541 * @param[in] map The Dali::Property::Map to use for to display
543 void SetProgressVisual( Dali::Property::Map map );
546 * @brief Return the progress bar image.
548 * @return The progress bar image if it exists.
550 std::string GetProgressVisual();
553 * @brief Create the image for the popup
555 * @param[in] filename The popup image
557 void CreatePopupImage( const std::string& filename );
560 * @brief Set the popup image
562 * @param[in] filename The popup image to set
564 void SetPopupVisual( const std::string& filename );
567 * @brief Set the popup from an Dali::Property::Map
569 * @param[in] map The Dali::Property::Map to use for to display
571 void SetPopupVisual( Dali::Property::Map map );
574 * @brief Return the popup image.
576 * @return The popup image if it exists.
578 std::string GetPopupVisual();
581 * @brief Set the popup arrow image
583 * @param[in] filename The popup arrow image to set
585 void SetPopupArrowVisual( const std::string& filename );
588 * @brief Set the popup arrow from an Dali::Property::Map
590 * @param[in] map The Dali::Property::Map to use for to display
592 void SetPopupArrowVisual( Dali::Property::Map map );
595 * @brief Return the popup arrow image.
597 * @return The popup arrow image if it exists.
599 std::string GetPopupArrowVisual();
602 * Create the image for the popup arrow
604 * @param[in] filename The popup arrow image to load and set
606 void CreatePopupArrowImage( const std::string& filename );
609 * Set the size of the progress bar region
611 * @param[in] region The size of the region to set
613 void ResizeProgressRegion( const Vector2& region );
616 * Create the image for the handle
618 * @param[in] filename The handle image
620 void SetHandleVisual( const std::string& filename );
623 * @brief Set the handle visual from an Dali::Property::Map
625 * @param[in] map The Dali::Property::Map to use for to display
627 void SetHandleVisual( Property::Map map );
630 * @brief Return the handle image.
632 * @return The handle image if it exists.
634 std::string GetHandleVisual();
637 * Reset the size of the handle
639 * @param[in] size The size of the handle to set
641 void ResizeHandleSize( const Vector2& size );
644 * Create and display the value on the handle
646 void CreateHandleValueDisplay();
649 * Remove and destroy the handle value display
651 void DestroyHandleValueDisplay();
654 * Set the size of the handle
656 * @param[in] size The handle size
658 void SetHandleSize( const Vector2& size );
661 * Get the size of the handle
663 * @return The handle size
665 const Vector2& GetHandleSize() const;
668 * Set the lower bound of the slider's value
670 * @param[in] bound The value to set for the lower bound
672 void SetLowerBound( float bound );
675 * Get the lower bound of the slider's value
677 * @return The lower bound value
679 float GetLowerBound() const;
682 * Set the upper bound of the slider's value
684 * @param[in] bound The value to set for the upper bound
686 void SetUpperBound( float bound );
689 * Get the upper bound of the slider's value
691 * @return The upper bound value
693 float GetUpperBound() const;
698 Slider( const Slider& );
701 Slider& operator=( const Slider& rhs );
705 Domain mDomain; ///< Current domain of the handle
707 Actor mHitArea; ///< The input handler
708 Actor mValueDisplay; ///< Display of the value
709 Toolkit::ImageView mTrack; ///< Track image
710 Toolkit::ImageView mHandle; ///< Slider handle
711 Toolkit::ImageView mProgress; ///< Progress bar
712 Toolkit::ImageView mPopup; ///< Popup backing
713 Toolkit::ImageView mPopupArrow; ///< Popup arrow backing
715 Toolkit::TextLabel mValueTextLabel; //< The text value in popup
716 Toolkit::TextLabel mHandleValueTextLabel; ///< The text value on handle
717 Vector2 mHandleLastTouchPoint; ///< The last touch point for the handle
718 Timer mValueTimer; ///< Timer used to hide value view
720 Toolkit::Slider::ValueChangedSignalType mValueChangedSignal; ///< Signal emitted when the value is changed
721 Toolkit::Slider::ValueChangedSignalType mSlidingFinishedSignal; ///< Signal emitted when a sliding is finished
722 Toolkit::Slider::MarkReachedSignalType mMarkReachedSignal; ///< Signal emitted when a mark is reached
724 SliderState mState; ///< The state of the slider
726 PanGestureDetector mPanDetector; ///< Hit region pan detector
728 MarkList mMarks; ///< List of discreet marks
730 std::string mPopupVisual; ///< Image for popup image
731 std::string mPopupArrowVisual; ///< Image for popup arrow image
732 std::string mTrackVisual; ///< Image for track image
733 std::string mHandleVisual; ///< Image for handle image
734 std::string mProgressVisual; ///< Image for progress bar image
736 Property::Map mPopupMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
737 Property::Map mTrackMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
738 Property::Map mHandleMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
739 Property::Map mProgressMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
740 Property::Map mPopupArrowMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
742 Vector4 mDisabledColor; ///< The color to tint the slider when disabled
744 Vector2 mHitRegion; ///< Size of hit region
745 Vector2 mTrackRegion; ///< Size of track region
746 Vector2 mHandleSize; ///< Size of the handle
748 float mLowerBound = 0.0f; ///< Lower bound on value
749 float mUpperBound = 1.0f; ///< Upper bound on value
750 float mValue = 0.0f; ///< Current value of slider
752 float mMarkTolerance = 0.05f; ///< Tolerance in percentage of slider width for which to snap to marks
754 int mValuePrecision; ///< The precision to use for outputting the value
756 bool mShowPopup : 1, ///< Show the popup or not
757 mShowValue : 1, ///< Whether to display the value number or not on the handle
758 mSnapToMarks : 1; ///< Turn on or off snapping to marks
761 struct AccessibleImpl : public Control::Impl::AccessibleImpl,
762 public virtual Dali::Accessibility::Value
764 using Control::Impl::AccessibleImpl::AccessibleImpl;
765 double GetMinimum() override;
766 double GetCurrent() override;
767 double GetMaximum() override;
768 bool SetCurrent( double ) override;
769 double GetMinimumIncrement() override;
773 } // namespace Internal
775 // Helpers for public-api forwarding methods
777 inline Toolkit::Internal::Slider& GetImpl( Toolkit::Slider& pub )
779 DALI_ASSERT_ALWAYS( pub );
781 Dali::RefObject& handle = pub.GetImplementation();
783 return static_cast< Toolkit::Internal::Slider& >( handle );
786 inline const Toolkit::Internal::Slider& GetImpl( const Toolkit::Slider& pub )
788 DALI_ASSERT_ALWAYS( pub );
790 const Dali::RefObject& handle = pub.GetImplementation();
792 return static_cast< const Toolkit::Internal::Slider& >( handle );
795 } // namespace Toolkit
799 #endif // DALI_TOOLKIT_INTERNAL_SLIDER_H