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/devel-api/controls/control-devel.h>
28 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
29 #include <dali-toolkit/public-api/controls/control-impl.h>
30 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
31 #include <dali-toolkit/public-api/controls/slider/slider.h>
32 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
44 typedef Dali::IntrusivePtr<Slider> SliderPtr;
47 * @copydoc Toolkit::Slider
49 class Slider : public Control
52 typedef Property::Array MarkList;
55 * Create a new Slider.
57 * @return A public handle to the newly allocated Slider.
59 static Dali::Toolkit::Slider New();
65 * Set marks from a list
67 * @param[in] marks The list of marks to set
69 void SetMarks(const MarkList& marks);
72 * Get the list of marks
74 * @return The marks list
76 const MarkList& GetMarks() const;
79 * Set if should snap to marks or not
81 * @param[in] snap Flag to snap to marks or not
83 void SetSnapToMarks(bool snap);
86 * Return if snap to marks is set or not
88 * @return If snap to marks is set
90 bool GetSnapToMarks() const;
93 * Set the value of the slider
95 * @param[in] value The value to set. Will be clamped to [lowerBound .. upperBound]
97 void SetValue(float value);
100 * Get the value of the slider
102 * @return The current value of the slider
104 float GetValue() const;
109 * @param[in] region The hit region
111 void SetHitRegion(const Vector2& region);
116 * @return The hit region
118 const Vector2& GetHitRegion() const;
121 * Set the track region
123 * @param[in] region The track region
125 void SetTrackRegion(const Vector2& region);
128 * Get the track region
130 * @return The track region
132 const Vector2& GetTrackRegion() const;
135 * @brief Set the disabled color.
137 * @param[in] color The disabled color.
139 void SetDisabledColor(const Vector4& color);
142 * @brief Get disabled color
144 * @return The disabled color
146 Vector4 GetDisabledColor() const;
149 * Set the value precision to be used for numbers in the slider
151 * @param[in] precision The number of decimal places to use for printing numbers
153 void SetValuePrecision(int precision);
156 * Get value precision
158 * @return The value precision
160 int GetValuePrecision() const;
165 * @param[in] showPopup The show popup flag
167 void SetShowPopup(bool showPopup);
170 * Get show value in popup
172 * @return The show value flag
174 bool GetShowPopup() const;
177 * Set show value on handle
179 * @param[in] showValue The show value flag
181 void SetShowValue(bool showValue);
184 * Get show value on handle
186 * @return The show value flag
188 bool GetShowValue() const;
193 * param[in] enabled Set the enabled flag
195 void SetEnabled(bool enabled);
198 * Return if enabled or not
202 bool IsEnabled() const;
205 * @brief Set the mark tolerance
207 * The tolerance is the percentage of the slider width for which snapping to
210 * @param[in] tolerance The percentage of width for which to snap
212 void SetMarkTolerance(float tolerance);
215 * Return the mark tolerance
217 * @return The tolerance set for snapping to marks
219 float GetMarkTolerance() const;
225 * @copydoc Toolkit::Slider::ValueChangedSignal()
227 Toolkit::Slider::ValueChangedSignalType& ValueChangedSignal();
230 * copydoc Toolkit::Slider::SlidingFinishedSignal()
232 Toolkit::Slider::ValueChangedSignalType& SlidingFinishedSignal();
235 * @copydoc Toolkit::Slider::MarkReachedSignal()
237 Toolkit::Slider::MarkReachedSignalType& MarkReachedSignal();
240 * Connects a callback function with the object's signals.
241 * @param[in] object The object providing the signal.
242 * @param[in] tracker Used to disconnect the signal.
243 * @param[in] signalName The signal to connect to.
244 * @param[in] functor A newly allocated FunctorDelegate.
245 * @return True if the signal was connected.
246 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
248 static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
253 * Called when a property of an object of this type is set.
254 * @param[in] object The object whose property is set.
255 * @param[in] index The property index.
256 * @param[in] value The new property value.
258 static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
261 * Called to retrieve a property of an object of this type.
262 * @param[in] object The object whose property is to be retrieved.
263 * @param[in] index The property index.
264 * @return The current value of the property.
266 static Property::Value GetProperty(BaseObject* object, Property::Index propertyIndex);
270 * Construct a new Slider.
275 * A reference counted object may only be deleted by calling Unreference()
280 * @copydoc CustomActorImpl::OnRelayout
282 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
286 * Domain is a from/to pair
296 Domain(Vector2 fromVal, Vector2 toVal)
316 * @copydoc Toolkit::Control::OnInitialize()
318 void OnInitialize() override;
323 * @param[in] actor The actor the event is raised for
324 * @param[in] touch The touch info
325 * @return If touch is handled or not
327 bool OnTouch(Actor actor, const TouchEvent& touch);
332 * @param[in] actor The actor the event is raised for
333 * @param[in] gesture The pan event info
335 void OnPan(Actor actor, const PanGesture& gesture);
338 * Map a position onto a domain and return the result as a percentage
340 * @param[in] point The point to map onto the domain
341 * @return The result as a percentage [0..1]
343 float MapPercentage(const Vector2& point);
346 * Map a value in the range to a percentage
348 * @param[in] point The value in range [lowerBound..upperBound]
349 * @return The result as a percentage [0..1]
351 float MapValuePercentage(float value);
354 * Convert a point in local hit space into domain space
356 * @param[in] x The x position to convert
357 * @return The x position in domain space
359 float HitSpaceToDomain(float x);
362 * Map a percentage onto the slider's bounds
364 * @param[in] percent The current value of slider in percent
365 * @param[in] lowerBound The lower bound to map onto
366 * @param[in] upperBound The upper bound to map onto
367 * @return The value of percent mapped from lowerBound to upperBound
369 float MapBounds(float percent, float lowerBound, float upperBound);
372 * Get the range of the valid values the slider handle can move between
374 * @param[in] currentSize The current size of the slider
375 * @return The range as a domain pair
377 Domain CalcDomain(const Vector2& currentSize);
380 * Create the hit region
382 * @return The hit region actor
384 Actor CreateHitRegion();
387 * Create the track for the slider
389 * @return The track actor
391 Toolkit::ImageView CreateTrack();
394 * Create the progress track for the slider
396 * @return The track actor
398 Toolkit::ImageView CreateProgress();
401 * Create the handle for the slider
403 * @return The created image handle
405 Toolkit::ImageView CreateHandle();
408 * Create the popup arrow
410 * @return The created image handle
412 Toolkit::ImageView CreatePopupArrow();
417 * @return The created image handle
419 Toolkit::ImageView CreatePopup();
422 * Create the textview for the popup
424 * @return The textview created for the popup
426 Toolkit::TextLabel CreatePopupText();
429 * Create the value display for the slider
431 * @return The created root actor of the display
433 Actor CreateValueDisplay();
436 * Set the skin based on the current state
441 * Create all the children
443 void CreateChildren();
456 * Display the popup for a set duration with the given value
458 * @param[in] value The value to display in the popup
460 void DisplayPopup(float value);
463 * If there are marks present, filter the incoming percent based on snapping to any nearby marks
465 * @param[in] value The incoming value on the slider to filter
466 * @return The filtered percentage snapped to any nearby marks
468 float MarkFilter(float value);
471 * If there are marks present, snap the incoming percent to the nearest mark
473 * @param[in] value The incoming value on the slider to snap
474 * @return The filtered percentage snapped to the nearest mark
476 float SnapToMark(float value);
479 * Search for if a mark has been reached
481 * @param[in] value The value to search against
482 * @param[out] outIndex The index of the mark if found
483 * @return If a mark has been found to match percent
485 bool MarkReached(float value, int& outIndex);
488 * Handler for when the value view needs to be hidden
490 * @return If handled or not
492 bool HideValueView();
495 * Set value choosing whether to fire signals or not
497 * @paramp[in] value The value to set
498 * @param[in] raiseSignals Configure signals to be raised or not.
500 void DisplayValue(float value, bool raiseSignals);
503 * Create the image for the track
505 * @param[in] filename The track image
507 void SetTrackVisual(const std::string& filename);
510 * @brief Set the track visual from an Dali::Property::Map
512 * @param[in] map The Dali::Property::Map to use for to display
514 void SetTrackVisual(Dali::Property::Map map);
517 * @brief Return the track image.
519 * @return The track image.
521 std::string GetTrackVisual();
524 * Create the image for the progress bar
526 * @param[in] filename The progress bar image
528 void SetProgressVisual(const std::string& filename);
531 * @brief Set the progress visual from an Dali::Property::Map
533 * @param[in] map The Dali::Property::Map to use for to display
535 void SetProgressVisual(Dali::Property::Map map);
538 * @brief Return the progress bar image.
540 * @return The progress bar image if it exists.
542 std::string GetProgressVisual();
545 * @brief Create the image for the popup
547 * @param[in] filename The popup image
549 void CreatePopupImage(const std::string& filename);
552 * @brief Set the popup image
554 * @param[in] filename The popup image to set
556 void SetPopupVisual(const std::string& filename);
559 * @brief Set the popup from an Dali::Property::Map
561 * @param[in] map The Dali::Property::Map to use for to display
563 void SetPopupVisual(Dali::Property::Map map);
566 * @brief Return the popup image.
568 * @return The popup image if it exists.
570 std::string GetPopupVisual();
573 * @brief Set the popup arrow image
575 * @param[in] filename The popup arrow image to set
577 void SetPopupArrowVisual(const std::string& filename);
580 * @brief Set the popup arrow from an Dali::Property::Map
582 * @param[in] map The Dali::Property::Map to use for to display
584 void SetPopupArrowVisual(Dali::Property::Map map);
587 * @brief Return the popup arrow image.
589 * @return The popup arrow image if it exists.
591 std::string GetPopupArrowVisual();
594 * Create the image for the popup arrow
596 * @param[in] filename The popup arrow image to load and set
598 void CreatePopupArrowImage(const std::string& filename);
601 * Set the size of the progress bar region
603 * @param[in] region The size of the region to set
605 void ResizeProgressRegion(const Vector2& region);
608 * Create the image for the handle
610 * @param[in] filename The handle image
612 void SetHandleVisual(const std::string& filename);
615 * @brief Set the handle visual from an Dali::Property::Map
617 * @param[in] map The Dali::Property::Map to use for to display
619 void SetHandleVisual(Property::Map map);
622 * @brief Return the handle image.
624 * @return The handle image if it exists.
626 std::string GetHandleVisual();
629 * Reset the size of the handle
631 * @param[in] size The size of the handle to set
633 void ResizeHandleSize(const Vector2& size);
636 * Create and display the value on the handle
638 void CreateHandleValueDisplay();
641 * Remove and destroy the handle value display
643 void DestroyHandleValueDisplay();
646 * Set the size of the handle
648 * @param[in] size The handle size
650 void SetHandleSize(const Vector2& size);
653 * Get the size of the handle
655 * @return The handle size
657 const Vector2& GetHandleSize() const;
660 * Set the lower bound of the slider's value
662 * @param[in] bound The value to set for the lower bound
664 void SetLowerBound(float bound);
667 * Get the lower bound of the slider's value
669 * @return The lower bound value
671 float GetLowerBound() const;
674 * Set the upper bound of the slider's value
676 * @param[in] bound The value to set for the upper bound
678 void SetUpperBound(float bound);
681 * Get the upper bound of the slider's value
683 * @return The upper bound value
685 float GetUpperBound() const;
689 Slider(const Slider&);
692 Slider& operator=(const Slider& rhs);
695 Domain mDomain; ///< Current domain of the handle
697 Actor mHitArea; ///< The input handler
698 Actor mValueDisplay; ///< Display of the value
699 Toolkit::ImageView mTrack; ///< Track image
700 Toolkit::ImageView mHandle; ///< Slider handle
701 Toolkit::ImageView mProgress; ///< Progress bar
702 Toolkit::ImageView mPopup; ///< Popup backing
703 Toolkit::ImageView mPopupArrow; ///< Popup arrow backing
705 Toolkit::TextLabel mValueTextLabel; //< The text value in popup
706 Toolkit::TextLabel mHandleValueTextLabel; ///< The text value on handle
707 Vector2 mHandleLastTouchPoint; ///< The last touch point for the handle
708 Timer mValueTimer; ///< Timer used to hide value view
710 Toolkit::Slider::ValueChangedSignalType mValueChangedSignal; ///< Signal emitted when the value is changed
711 Toolkit::Slider::ValueChangedSignalType mSlidingFinishedSignal; ///< Signal emitted when a sliding is finished
712 Toolkit::Slider::MarkReachedSignalType mMarkReachedSignal; ///< Signal emitted when a mark is reached
714 SliderState mState; ///< The state of the slider
716 PanGestureDetector mPanDetector; ///< Hit region pan detector
718 MarkList mMarks; ///< List of discreet marks
720 std::string mPopupVisual; ///< Image for popup image
721 std::string mPopupArrowVisual; ///< Image for popup arrow image
722 std::string mTrackVisual; ///< Image for track image
723 std::string mHandleVisual; ///< Image for handle image
724 std::string mProgressVisual; ///< Image for progress bar image
726 Property::Map mPopupMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
727 Property::Map mTrackMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
728 Property::Map mHandleMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
729 Property::Map mProgressMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
730 Property::Map mPopupArrowMap; ///< the Property::Map if the image came from a Property::Map, empty otherwise
732 Vector4 mDisabledColor; ///< The color to tint the slider when disabled
734 Vector2 mHitRegion; ///< Size of hit region
735 Vector2 mTrackRegion; ///< Size of track region
736 Vector2 mHandleSize; ///< Size of the handle
738 float mLowerBound = 0.0f; ///< Lower bound on value
739 float mUpperBound = 1.0f; ///< Upper bound on value
740 float mValue = 0.0f; ///< Current value of slider
742 float mMarkTolerance = 0.05f; ///< Tolerance in percentage of slider width for which to snap to marks
744 int mValuePrecision; ///< The precision to use for outputting the value
746 bool mShowPopup : 1, ///< Show the popup or not
747 mShowValue : 1, ///< Whether to display the value number or not on the handle
748 mSnapToMarks : 1; ///< Turn on or off snapping to marks
751 struct AccessibleImpl : public DevelControl::AccessibleImpl,
752 public virtual Dali::Accessibility::Value
754 using DevelControl::AccessibleImpl::AccessibleImpl;
755 double GetMinimum() override;
756 double GetCurrent() override;
757 double GetMaximum() override;
758 bool SetCurrent(double) override;
759 double GetMinimumIncrement() override;
763 } // namespace Internal
765 // Helpers for public-api forwarding methods
767 inline Toolkit::Internal::Slider& GetImpl(Toolkit::Slider& pub)
769 DALI_ASSERT_ALWAYS(pub);
771 Dali::RefObject& handle = pub.GetImplementation();
773 return static_cast<Toolkit::Internal::Slider&>(handle);
776 inline const Toolkit::Internal::Slider& GetImpl(const Toolkit::Slider& pub)
778 DALI_ASSERT_ALWAYS(pub);
780 const Dali::RefObject& handle = pub.GetImplementation();
782 return static_cast<const Toolkit::Internal::Slider&>(handle);
785 } // namespace Toolkit
789 #endif // DALI_TOOLKIT_INTERNAL_SLIDER_H