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/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/internal/controls/control/control-data-impl.h>
28 #include <dali-toolkit/public-api/controls/control-impl.h>
29 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
30 #include <dali-toolkit/public-api/controls/slider/slider.h>
31 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
43 typedef Dali::IntrusivePtr<Slider> SliderPtr;
46 * @copydoc Toolkit::Slider
48 class Slider : public Control
51 typedef Property::Array MarkList;
54 * Create a new Slider.
56 * @return A public handle to the newly allocated Slider.
58 static Dali::Toolkit::Slider New();
64 * Set marks from a list
66 * @param[in] marks The list of marks to set
68 void SetMarks(const MarkList& marks);
71 * Get the list of marks
73 * @return The marks list
75 const MarkList& GetMarks() const;
78 * Set if should snap to marks or not
80 * @param[in] snap Flag to snap to marks or not
82 void SetSnapToMarks(bool snap);
85 * Return if snap to marks is set or not
87 * @return If snap to marks is set
89 bool GetSnapToMarks() const;
92 * Set the value of the slider
94 * @param[in] value The value to set. Will be clamped to [lowerBound .. upperBound]
96 void SetValue(float value);
99 * Get the value of the slider
101 * @return The current value of the slider
103 float GetValue() const;
108 * @param[in] region The hit region
110 void SetHitRegion(const Vector2& region);
115 * @return The hit region
117 const Vector2& GetHitRegion() const;
120 * Set the track region
122 * @param[in] region The track region
124 void SetTrackRegion(const Vector2& region);
127 * Get the track region
129 * @return The track region
131 const Vector2& GetTrackRegion() const;
134 * @brief Set the disabled color.
136 * @param[in] color The disabled color.
138 void SetDisabledColor(const Vector4& color);
141 * @brief Get disabled color
143 * @return The disabled color
145 Vector4 GetDisabledColor() const;
148 * Set the value precision to be used for numbers in the slider
150 * @param[in] precision The number of decimal places to use for printing numbers
152 void SetValuePrecision(int precision);
155 * Get value precision
157 * @return The value precision
159 int GetValuePrecision() const;
164 * @param[in] showPopup The show popup flag
166 void SetShowPopup(bool showPopup);
169 * Get show value in popup
171 * @return The show value flag
173 bool GetShowPopup() const;
176 * Set show value on handle
178 * @param[in] showValue The show value flag
180 void SetShowValue(bool showValue);
183 * Get show value on handle
185 * @return The show value flag
187 bool GetShowValue() const;
192 * param[in] enabled Set the enabled flag
194 void SetEnabled(bool enabled);
197 * Return if enabled or not
201 bool IsEnabled() const;
204 * @brief Set the mark tolerance
206 * The tolerance is the percentage of the slider width for which snapping to
209 * @param[in] tolerance The percentage of width for which to snap
211 void SetMarkTolerance(float tolerance);
214 * Return the mark tolerance
216 * @return The tolerance set for snapping to marks
218 float GetMarkTolerance() const;
224 * @copydoc Toolkit::Slider::ValueChangedSignal()
226 Toolkit::Slider::ValueChangedSignalType& ValueChangedSignal();
229 * copydoc Toolkit::Slider::SlidingFinishedSignal()
231 Toolkit::Slider::ValueChangedSignalType& SlidingFinishedSignal();
234 * @copydoc Toolkit::Slider::MarkReachedSignal()
236 Toolkit::Slider::MarkReachedSignalType& MarkReachedSignal();
239 * Connects a callback function with the object's signals.
240 * @param[in] object The object providing the signal.
241 * @param[in] tracker Used to disconnect the signal.
242 * @param[in] signalName The signal to connect to.
243 * @param[in] functor A newly allocated FunctorDelegate.
244 * @return True if the signal was connected.
245 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
247 static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
252 * Called when a property of an object of this type is set.
253 * @param[in] object The object whose property is set.
254 * @param[in] index The property index.
255 * @param[in] value The new property value.
257 static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
260 * Called to retrieve a property of an object of this type.
261 * @param[in] object The object whose property is to be retrieved.
262 * @param[in] index The property index.
263 * @return The current value of the property.
265 static Property::Value GetProperty(BaseObject* object, Property::Index propertyIndex);
269 * Construct a new Slider.
274 * A reference counted object may only be deleted by calling Unreference()
279 * @copydoc CustomActorImpl::OnRelayout
281 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
285 * Domain is a from/to pair
295 Domain(Vector2 fromVal, Vector2 toVal)
315 * @copydoc Toolkit::Control::OnInitialize()
317 void OnInitialize() override;
322 * @param[in] actor The actor the event is raised for
323 * @param[in] touch The touch info
324 * @return If touch is handled or not
326 bool OnTouch(Actor actor, const TouchEvent& touch);
331 * @param[in] actor The actor the event is raised for
332 * @param[in] gesture The pan event info
334 void OnPan(Actor actor, const PanGesture& gesture);
337 * Map a position onto a domain and return the result as a percentage
339 * @param[in] point The point to map onto the domain
340 * @return The result as a percentage [0..1]
342 float MapPercentage(const Vector2& point);
345 * Map a value in the range to a percentage
347 * @param[in] point The value in range [lowerBound..upperBound]
348 * @return The result as a percentage [0..1]
350 float MapValuePercentage(float value);
353 * Convert a point in local hit space into domain space
355 * @param[in] x The x position to convert
356 * @return The x position in domain space
358 float HitSpaceToDomain(float x);
361 * Map a percentage onto the slider's bounds
363 * @param[in] percent The current value of slider in percent
364 * @param[in] lowerBound The lower bound to map onto
365 * @param[in] upperBound The upper bound to map onto
366 * @return The value of percent mapped from lowerBound to upperBound
368 float MapBounds(float percent, float lowerBound, float upperBound);
371 * Get the range of the valid values the slider handle can move between
373 * @param[in] currentSize The current size of the slider
374 * @return The range as a domain pair
376 Domain CalcDomain(const Vector2& currentSize);
379 * Create the hit region
381 * @return The hit region actor
383 Actor CreateHitRegion();
386 * Create the track for the slider
388 * @return The track actor
390 Toolkit::ImageView CreateTrack();
393 * Create the progress track for the slider
395 * @return The track actor
397 Toolkit::ImageView CreateProgress();
400 * Create the handle for the slider
402 * @return The created image handle
404 Toolkit::ImageView CreateHandle();
407 * Create the popup arrow
409 * @return The created image handle
411 Toolkit::ImageView CreatePopupArrow();
416 * @return The created image handle
418 Toolkit::ImageView CreatePopup();
421 * Create the textview for the popup
423 * @return The textview created for the popup
425 Toolkit::TextLabel CreatePopupText();
428 * Create the value display for the slider
430 * @return The created root actor of the display
432 Actor CreateValueDisplay();
435 * Set the skin based on the current state
440 * Create all the children
442 void CreateChildren();
455 * Display the popup for a set duration with the given value
457 * @param[in] value The value to display in the popup
459 void DisplayPopup(float value);
462 * If there are marks present, filter the incoming percent based on snapping to any nearby marks
464 * @param[in] value The incoming value on the slider to filter
465 * @return The filtered percentage snapped to any nearby marks
467 float MarkFilter(float value);
470 * If there are marks present, snap the incoming percent to the nearest mark
472 * @param[in] value The incoming value on the slider to snap
473 * @return The filtered percentage snapped to the nearest mark
475 float SnapToMark(float value);
478 * Search for if a mark has been reached
480 * @param[in] value The value to search against
481 * @param[out] outIndex The index of the mark if found
482 * @return If a mark has been found to match percent
484 bool MarkReached(float value, int& outIndex);
487 * Handler for when the value view needs to be hidden
489 * @return If handled or not
491 bool HideValueView();
494 * Set value choosing whether to fire signals or not
496 * @paramp[in] value The value to set
497 * @param[in] raiseSignals Configure signals to be raised or not.
499 void DisplayValue(float value, bool raiseSignals);
502 * Create the image for the track
504 * @param[in] filename The track image
506 void SetTrackVisual(const std::string& filename);
509 * @brief Set the track visual from an Dali::Property::Map
511 * @param[in] map The Dali::Property::Map to use for to display
513 void SetTrackVisual(Dali::Property::Map map);
516 * @brief Return the track image.
518 * @return The track image.
520 std::string GetTrackVisual();
523 * Create the image for the progress bar
525 * @param[in] filename The progress bar image
527 void SetProgressVisual(const std::string& filename);
530 * @brief Set the progress visual from an Dali::Property::Map
532 * @param[in] map The Dali::Property::Map to use for to display
534 void SetProgressVisual(Dali::Property::Map map);
537 * @brief Return the progress bar image.
539 * @return The progress bar image if it exists.
541 std::string GetProgressVisual();
544 * @brief Create the image for the popup
546 * @param[in] filename The popup image
548 void CreatePopupImage(const std::string& filename);
551 * @brief Set the popup image
553 * @param[in] filename The popup image to set
555 void SetPopupVisual(const std::string& filename);
558 * @brief Set the popup from an Dali::Property::Map
560 * @param[in] map The Dali::Property::Map to use for to display
562 void SetPopupVisual(Dali::Property::Map map);
565 * @brief Return the popup image.
567 * @return The popup image if it exists.
569 std::string GetPopupVisual();
572 * @brief Set the popup arrow image
574 * @param[in] filename The popup arrow image to set
576 void SetPopupArrowVisual(const std::string& filename);
579 * @brief Set the popup arrow from an Dali::Property::Map
581 * @param[in] map The Dali::Property::Map to use for to display
583 void SetPopupArrowVisual(Dali::Property::Map map);
586 * @brief Return the popup arrow image.
588 * @return The popup arrow image if it exists.
590 std::string GetPopupArrowVisual();
593 * Create the image for the popup arrow
595 * @param[in] filename The popup arrow image to load and set
597 void CreatePopupArrowImage(const std::string& filename);
600 * Set the size of the progress bar region
602 * @param[in] region The size of the region to set
604 void ResizeProgressRegion(const Vector2& region);
607 * Create the image for the handle
609 * @param[in] filename The handle image
611 void SetHandleVisual(const std::string& filename);
614 * @brief Set the handle visual from an Dali::Property::Map
616 * @param[in] map The Dali::Property::Map to use for to display
618 void SetHandleVisual(Property::Map map);
621 * @brief Return the handle image.
623 * @return The handle image if it exists.
625 std::string GetHandleVisual();
628 * Reset the size of the handle
630 * @param[in] size The size of the handle to set
632 void ResizeHandleSize(const Vector2& size);
635 * Create and display the value on the handle
637 void CreateHandleValueDisplay();
640 * Remove and destroy the handle value display
642 void DestroyHandleValueDisplay();
645 * Set the size of the handle
647 * @param[in] size The handle size
649 void SetHandleSize(const Vector2& size);
652 * Get the size of the handle
654 * @return The handle size
656 const Vector2& GetHandleSize() const;
659 * Set the lower bound of the slider's value
661 * @param[in] bound The value to set for the lower bound
663 void SetLowerBound(float bound);
666 * Get the lower bound of the slider's value
668 * @return The lower bound value
670 float GetLowerBound() const;
673 * Set the upper bound of the slider's value
675 * @param[in] bound The value to set for the upper bound
677 void SetUpperBound(float bound);
680 * Get the upper bound of the slider's value
682 * @return The upper bound value
684 float GetUpperBound() const;
688 Slider(const Slider&);
691 Slider& operator=(const Slider& rhs);
694 Domain mDomain; ///< Current domain of the handle
696 Actor mHitArea; ///< The input handler
697 Actor mValueDisplay; ///< Display of the value
698 Toolkit::ImageView mTrack; ///< Track image
699 Toolkit::ImageView mHandle; ///< Slider handle
700 Toolkit::ImageView mProgress; ///< Progress bar
701 Toolkit::ImageView mPopup; ///< Popup backing
702 Toolkit::ImageView mPopupArrow; ///< Popup arrow backing
704 Toolkit::TextLabel mValueTextLabel; //< The text value in popup
705 Toolkit::TextLabel mHandleValueTextLabel; ///< The text value on handle
706 Vector2 mHandleLastTouchPoint; ///< The last touch point for the handle
707 Timer mValueTimer; ///< Timer used to hide value view
709 Toolkit::Slider::ValueChangedSignalType mValueChangedSignal; ///< Signal emitted when the value is changed
710 Toolkit::Slider::ValueChangedSignalType mSlidingFinishedSignal; ///< Signal emitted when a sliding is finished
711 Toolkit::Slider::MarkReachedSignalType mMarkReachedSignal; ///< Signal emitted when a mark is reached
713 SliderState mState; ///< The state of the slider
715 PanGestureDetector mPanDetector; ///< Hit region pan detector
717 MarkList mMarks; ///< List of discreet marks
719 std::string mPopupVisual; ///< Image for popup image
720 std::string mPopupArrowVisual; ///< Image for popup arrow image
721 std::string mTrackVisual; ///< Image for track image
722 std::string mHandleVisual; ///< Image for handle image
723 std::string mProgressVisual; ///< Image for progress bar image
725 Property::Map mPopupMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
726 Property::Map mTrackMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
727 Property::Map mHandleMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
728 Property::Map mProgressMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
729 Property::Map mPopupArrowMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
731 Vector4 mDisabledColor; ///< The color to tint the slider when disabled
733 Vector2 mHitRegion; ///< Size of hit region
734 Vector2 mTrackRegion; ///< Size of track region
735 Vector2 mHandleSize; ///< Size of the handle
737 float mLowerBound = 0.0f; ///< Lower bound on value
738 float mUpperBound = 1.0f; ///< Upper bound on value
739 float mValue = 0.0f; ///< Current value of slider
741 float mMarkTolerance = 0.05f; ///< Tolerance in percentage of slider width for which to snap to marks
743 int mValuePrecision; ///< The precision to use for outputting the value
745 bool mShowPopup : 1, ///< Show the popup or not
746 mShowValue : 1, ///< Whether to display the value number or not on the handle
747 mSnapToMarks : 1; ///< Turn on or off snapping to marks
750 struct AccessibleImpl : public Control::Impl::AccessibleImpl,
751 public virtual Dali::Accessibility::Value
753 using Control::Impl::AccessibleImpl::AccessibleImpl;
754 double GetMinimum() override;
755 double GetCurrent() override;
756 double GetMaximum() override;
757 bool SetCurrent(double) override;
758 double GetMinimumIncrement() override;
762 } // namespace Internal
764 // Helpers for public-api forwarding methods
766 inline Toolkit::Internal::Slider& GetImpl(Toolkit::Slider& pub)
768 DALI_ASSERT_ALWAYS(pub);
770 Dali::RefObject& handle = pub.GetImplementation();
772 return static_cast<Toolkit::Internal::Slider&>(handle);
775 inline const Toolkit::Internal::Slider& GetImpl(const Toolkit::Slider& pub)
777 DALI_ASSERT_ALWAYS(pub);
779 const Dali::RefObject& handle = pub.GetImplementation();
781 return static_cast<const Toolkit::Internal::Slider&>(handle);
784 } // namespace Toolkit
788 #endif // DALI_TOOLKIT_INTERNAL_SLIDER_H