1dd09381e313820b5ad6ed0cd24b52fe70ab39fd
[platform/core/uifw/dali-toolkit.git] / optional / dali-toolkit / internal / controls / slider / slider-impl.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_SLIDER_H__
2 #define __DALI_TOOLKIT_INTERNAL_SLIDER_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/dali.h>
23 #include <dali-toolkit/public-api/controls/control-impl.h>
24 #include <dali-toolkit/public-api/controls/slider/slider.h>
25 #include <dali-toolkit/public-api/controls/text-view/text-view.h>
26
27 namespace Dali
28 {
29
30 namespace Toolkit
31 {
32
33 class Button;
34
35 namespace Internal
36 {
37
38 class Slider;
39
40 typedef Dali::IntrusivePtr< Slider > SliderPtr;
41
42 /**
43  * @copydoc Toolkit::Slider
44  */
45 class Slider : public ControlImpl
46 {
47 public:
48
49   typedef Property::Array MarkList;
50
51   /**
52    * Create a new Slider.
53    *
54    * @return A public handle to the newly allocated Slider.
55    */
56   static Dali::Toolkit::Slider New();
57
58 public:
59
60   // Properties
61
62   /**
63    * Set marks from a list
64    *
65    * @param[in] marks The list of marks to set
66    */
67   void SetMarks( const MarkList& marks );
68
69   /**
70    * Get the list of marks
71    *
72    * @return The marks list
73    */
74   const MarkList& GetMarks() const;
75
76   /**
77    * Set if should snap to marks or not
78    *
79    * @param[in] snap Flag to snap to marks or not
80    */
81   void SetSnapToMarks( bool snap );
82
83   /**
84    * Return if snap to marks is set or not
85    *
86    * @return If snap to marks is set
87    */
88   bool GetSnapToMarks() const;
89
90   /**
91    * Set the value of the slider
92    *
93    * @param[in] value The value to set. Will be clamped to [lowerBound .. upperBound]
94    */
95   void SetValue( float value );
96
97   /**
98    * Get the value of the slider
99    *
100    * @return The current value of the slider
101    */
102   float GetValue() const;
103
104   /**
105    * Set hit region
106    *
107    * @param[in] region The hit region
108    */
109   void SetHitRegion( const Vector2& region );
110
111   /**
112    * Get hit region
113    *
114    * @return The hit region
115    */
116   const Vector2& GetHitRegion() const;
117
118   /**
119    * Set backing region
120    *
121    * @param[in] region The backing region
122    */
123   void SetBackingRegion( const Vector2& region );
124
125   /**
126    * Get backing region
127    *
128    * @return The backing region
129    */
130   const Vector2& GetBackingRegion() const;
131
132   /**
133    * @brief Set the disable color.
134    *
135    * @param[in] color The disable color.
136    */
137   void SetDisableColor( const Vector4& color );
138
139   /**
140    * @brief Get disable color
141    *
142    * @return The disable color
143    */
144   Vector4 GetDisableColor() const;
145
146   /**
147    * Get popup text color
148    *
149    * @return The popup text color
150    */
151   Vector4 GetPopupTextColor() const;
152
153   /**
154    * Set the value precision to be used for numbers in the slider
155    *
156    * @param[in] precision The number of decimal places to use for printing numbers
157    */
158   void SetValuePrecision( int precision );
159
160   /**
161    * Get value precision
162    *
163    * @return The value precision
164    */
165   int GetValuePrecision() const;
166
167   /**
168    * Show the popup
169    *
170    * @param[in] showPopup The show popup flag
171    */
172   void SetShowPopup( bool showPopup );
173
174   /**
175    * Get show value in popup
176    *
177    * @return The show value flag
178    */
179   bool GetShowPopup() const;
180
181   /**
182    * Set show value on handle
183    *
184    * @param[in] showValue The show value flag
185    */
186   void SetShowValue( bool showValue );
187
188   /**
189    * Get show value on handle
190    *
191    * @return The show value flag
192    */
193   bool GetShowValue() const;
194
195   /**
196    * Set enabled
197    *
198    * param[in] enabled Set the enabled flag
199    */
200   void SetEnabled( bool enabled );
201
202   /**
203    * Return if enabled or not
204    *
205    * @return If enabled
206    */
207   bool IsEnabled() const;
208
209   /**
210    * @brief Set the mark tolerance
211    *
212    * The tolerance is the percentage of the slider width for which snapping to
213    * marks occurs
214    *
215    * @param[in] tolerance The percentage of width for which to snap
216    */
217   void SetMarkTolerance( float tolerance );
218
219   /**
220    * Return the mark tolerance
221    *
222    * @return The tolerance set for snapping to marks
223    */
224   float GetMarkTolerance() const;
225
226 public:
227   //Signals
228
229   /**
230    * @copydoc Toolkit::Slider::ValueChangedSignal()
231    */
232   Toolkit::Slider::ValueChangedSignalType& ValueChangedSignal();
233
234   /**
235    * copydoc Toolkit::Slider::SlidingFinishedSignal()
236    */
237   Toolkit::Slider::ValueChangedSignalType& SlidingFinishedSignal();
238
239   /**
240    * @copydoc Toolkit::Slider::MarkSignal()
241    */
242   Toolkit::Slider::MarkSignalType& MarkSignal();
243
244   /**
245    * Connects a callback function with the object's signals.
246    * @param[in] object The object providing the signal.
247    * @param[in] tracker Used to disconnect the signal.
248    * @param[in] signalName The signal to connect to.
249    * @param[in] functor A newly allocated FunctorDelegate.
250    * @return True if the signal was connected.
251    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
252    */
253   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName,
254                                FunctorDelegate* functor );
255
256   // Properties
257
258   /**
259    * Called when a property of an object of this type is set.
260    * @param[in] object The object whose property is set.
261    * @param[in] index The property index.
262    * @param[in] value The new property value.
263    */
264   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
265
266   /**
267    * Called to retrieve a property of an object of this type.
268    * @param[in] object The object whose property is to be retrieved.
269    * @param[in] index The property index.
270    * @return The current value of the property.
271    */
272   static Property::Value GetProperty( BaseObject* object, Property::Index propertyIndex );
273
274 protected:
275
276   /**
277    * Construct a new Slider.
278    */
279   Slider();
280
281   /**
282    * A reference counted object may only be deleted by calling Unreference()
283    */
284   virtual ~Slider();
285
286   /**
287    * @copydoc Toolkit::ControlImpl::OnControlSizeSet( const Vector3& size )
288    */
289   virtual void OnControlSizeSet( const Vector3& size );
290
291 private:
292
293   /**
294    * Domain is a from/to pair
295    */
296   struct Domain
297   {
298     Vector2 from;
299     Vector2 to;
300
301     Domain()
302     {
303     }
304     Domain( Vector2 fromVal, Vector2 toVal )
305         : from( fromVal ), to( toVal )
306     {
307     }
308   };
309
310   /**
311    * Slider states
312    */
313   enum SliderState
314   {
315     NORMAL,
316     DISABLED,
317     PRESSED,
318     FOCUSED
319   };
320
321 private:
322
323   /**
324    * @copydoc Toolkit::Control::OnInitialize()
325    */
326   virtual void OnInitialize();
327
328   /**
329    * Hit region touch event
330    *
331    * @param[in] actor The actor the event is raised for
332    * @param[in] event The touch event info
333    * @return If the event is handled or not
334    */
335   bool OnTouchEvent( Actor actor, const TouchEvent& event );
336
337   /**
338    * Pan gesture event
339    *
340    * @param[in] actor The actor the event is raised for
341    * @param[in] gesture The pan event info
342    */
343   void OnPan( Actor actor, PanGesture gestur );
344
345   /**
346    * Map a position onto a domain and return the result as a percentage
347    *
348    * @param[in] point The point to map onto the domain
349    * @return The result as a percentage [0..1]
350    */
351   float MapPercentage( const Vector2& point );
352
353   /**
354    * Map a value in the range to a percentage
355    *
356    * @param[in] point The value in range [lowerBound..upperBound]
357    * @return The result as a percentage [0..1]
358    */
359   float MapValuePercentage( float value );
360
361   /**
362    * Convert a point in local hit space into domain space
363    *
364    * @param[in] x The x position to convert
365    * @return The x position in domain space
366    */
367   float HitSpaceToDomain( float x );
368
369   /**
370    * Map a percentage onto the slider's bounds
371    *
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
376    */
377   float MapBounds( float percent, float lowerBound, float upperBound );
378
379   /**
380    * Get the range of the valid values the slider handle can move between
381    *
382    * @param[in] currentSize The current size of the slider
383    * @return The range as a domain pair
384    */
385   Domain CalcDomain( const Vector2& currentSize );
386
387   /**
388    * Create the hit region
389    *
390    * @return The hit region actor
391    */
392   Actor CreateHitRegion();
393
394   /**
395    * Create the backing for the slider
396    *
397    * @return The backing actor
398    */
399   ImageActor CreateBacking();
400
401   /**
402    * Create the progress backing for the slider
403    *
404    * @return The backing actor
405    */
406   ImageActor CreateProgress();
407
408   /**
409    * Create the handle for the slider
410    *
411    * @return The created image handle
412    */
413   ImageActor CreateHandle();
414
415   /**
416    * Create the popup arrow
417    *
418    * @return The created image handle
419    */
420   ImageActor CreatePopupArrow();
421
422   /**
423    * Create the popup
424    *
425    * @return The created image handle
426    */
427   ImageActor CreatePopup();
428
429   /**
430    * Create the textview for the popup
431    *
432    * @return The textview created for the popup
433    */
434   Toolkit::TextView CreatePopupText();
435
436   /**
437    * Create the value display for the slider
438    *
439    * @return The created root actor of the display
440    */
441   Actor CreateValueDisplay();
442
443   /**
444    * Set the skin based on the current state
445    */
446   void UpdateSkin();
447
448   /**
449    * Create all the children
450    */
451   void CreateChildren();
452
453   /**
454    * Create value popup
455    */
456   void AddPopup();
457
458   /**
459    * Remove value popup
460    */
461   void RemovePopup();
462
463   /**
464    * Display the popup for a set duration with the given value
465    *
466    * @param[in] value The value to display in the popup
467    */
468   void DisplayPopup( float value );
469
470   /**
471    * If there are marks present, filter the incoming percent based on snapping to any nearby marks
472    *
473    * @param[in] value The incoming value on the slider to filter
474    * @return The filtered percentage snapped to any nearby marks
475    */
476   float MarkFilter( float value );
477
478   /**
479    * If there are marks present, snap the incoming percent to the nearest mark
480    *
481    * @param[in] value The incoming value on the slider to snap
482    * @return The filtered percentage snapped to the nearest mark
483    */
484   float SnapToMark( float value );
485
486   /**
487    * Search for if a mark has been reached
488    *
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
492    */
493   bool MarkReached( float value, int& outIndex );
494
495   /**
496    * Handler for when the value view needs to be hidden
497    *
498    * @return If handled or not
499    */
500   bool HideValueView();
501
502   /**
503    * Set value choosing whether to fire signals or not
504    *
505    * @paramp[in] value The value to set
506    * @param[in] raiseSignals Configure signals to be raised or not.
507    */
508   void DisplayValue( float value, bool raiseSignals );
509
510   /**
511    * Create the image for the backing
512    *
513    * @param[in] imageName The name of the image to load and set
514    */
515   void SetBackingImageName( const std::string& imageName );
516
517   /**
518    * @brief Return the backing image file name.
519    *
520    * @return The backing image file name.
521    */
522   std::string GetBackingImageName();
523
524   /**
525    * Create the image for the progress bar
526    *
527    * @param[in] imageName The name of the image to load and set
528    */
529   void SetProgressImageName( const std::string& imageName );
530
531   /**
532    * @brief Return the progress image name.
533    *
534    * @return The progress image name if it exists.
535    */
536   std::string GetProgressImageName();
537
538   /**
539    * @brief Create the image for the popup
540    *
541    * @param[in] imageName The name of the image to load and set
542    */
543   void CreatePopupImage( const std::string& imageName );
544
545   /**
546    * @brief Set the popup name
547    *
548    * @param[in] imageName The name of the image to set
549    */
550   void SetPopupImageName( const std::string& imageName );
551
552   /**
553    * @brief Return the popup image name.
554    *
555    * @return The name of the popup image if it exists.
556    */
557   std::string GetPopupImageName();
558
559   /**
560    * @brief Set the popup arrow image name
561    *
562    * @param[in] imageName The name of the image to set
563    */
564   void SetPopupArrowImageName( const std::string& imageName );
565
566   /**
567    * @brief Return the popup arrow image name.
568    *
569    * @return The name of the popup image if it exists.
570    */
571   std::string GetPopupArrowImageName();
572
573   /**
574    * Create the image for the popup arrow
575    *
576    * @param[in] imageName The name of the image to load and set
577    */
578   void CreatePopupArrowImage( const std::string& imageName );
579
580   /**
581    * Set the size of the progress bar region
582    *
583    * @param[in] region The size of the region to set
584    */
585   void ResizeProgressRegion( const Vector2& region );
586
587   /**
588    * Create the image for the handle
589    *
590    * @param[in] imageName The name of the image to load and set
591    */
592   void SetHandleImageName( const std::string& imageName );
593
594   /**
595    * @brief Return the handle image name.
596    *
597    * @return The name of the image handle if it exists.
598    */
599   std::string GetHandleImageName();
600
601   /**
602    * Set the size of the handle region
603    *
604    * @param[in] region The size of the region to set
605    */
606   void ResizeHandleRegion( const Vector2& region );
607
608   /**
609    * Create and display the value on the handle
610    */
611   void CreateHandleValueDisplay();
612
613   /**
614    * Remove and destroy the handle value display
615    */
616   void DestroyHandleValueDisplay();
617
618   /**
619    * Update the color of the popup text
620    *
621    * @param[in] color The new color
622    */
623   void SetPopupTextColor( const Vector4& color );
624
625   /**
626    * Set handle region
627    *
628    * @param[in] region The handle region
629    */
630   void SetHandleRegion( const Vector2& region );
631
632   /**
633    * Get handle region
634    *
635    * @return The handle region
636    */
637   const Vector2& GetHandleRegion() const;
638
639   /**
640    * Set the lower bound of the slider's value
641    *
642    * @param[in] bound The value to set for the lower bound
643    */
644   void SetLowerBound( float bound );
645
646   /**
647    * Get the lower bound of the slider's value
648    *
649    * @return The lower bound value
650    */
651   float GetLowerBound() const;
652
653   /**
654    * Set the upper bound of the slider's value
655    *
656    * @param[in] bound The value to set for the upper bound
657    */
658   void SetUpperBound( float bound );
659
660   /**
661    * Get the upper bound of the slider's value
662    *
663    * @return The upper bound value
664    */
665   float GetUpperBound() const;
666
667 private:
668
669   // Undefined
670   Slider( const Slider& );
671
672   // Undefined
673   Slider& operator=( const Slider& rhs );
674
675 private:
676
677   Domain mDomain;                           ///< Current domain of the handle
678
679   Actor mHitArea;                           ///< The input handler
680   ImageActor mBacking;                      ///< Backing image
681   ImageActor mHandle;                       ///< Slider handle
682   ImageActor mProgress;                     ///< Progress backing
683   Actor mValueDisplay;                      ///< Display of the value
684   ImageActor mPopup;                        ///< Popup backing
685   ImageActor mPopupArrow;                   ///< Popup arrow backing
686
687   Toolkit::TextView mValueTextView;         //< The text value in popup
688   Toolkit::TextView mHandleValueTextView;   ///< The text value on handle
689   Vector2 mHandleLastTouchPoint;            ///< The last touch point for the handle
690   Timer mValueTimer;                        ///< Timer used to hide value view
691
692   Toolkit::Slider::ValueChangedSignalType mValueChangedSignal;    ///< Signal emitted when the value is changed
693   Toolkit::Slider::ValueChangedSignalType mSlidingFinishedSignal;    ///< Signal emitted when a sliding is finished
694   Toolkit::Slider::MarkSignalType mMarkSignal;                    ///< Signal emitted when a mark is reached
695
696   SliderState mState;                 ///< The state of the slider
697
698   PanGestureDetector mPanDetector;    ///< Hit region pan detector
699
700   MarkList mMarks;                    ///< List of discreet marks
701
702   std::string mPopupImageName;      ///< Image name for popup image
703   std::string mPopupArrowImageName; ///< Image name for popup arrow
704
705   Vector4 mDisableColor;    ///< The color to tint the slider when disabled
706   Vector4 mPopupTextColor;  ///< The color of the popup text
707
708   Vector2 mHitRegion;     ///< Size of hit region
709   Vector2 mBackingRegion; ///< Size of backing region
710   Vector2 mHandleRegionSize;  ///< Size of the handle region
711
712   float mLowerBound;        ///< Lower bound on value
713   float mUpperBound;        ///< Upper bound on value
714   float mValue;             ///< Current value of slider
715
716   float mMarkTolerance;     ///< Tolerance in percentage of slider width for which to snap to marks
717
718   int mValuePrecision;      ///< The precision to use for outputting the value
719
720   bool mShowPopup   : 1,      ///< Show the popup or not
721        mShowValue   : 1,      ///< Whether to display the value number or not on the handle
722        mSnapToMarks : 1;      ///< Turn on or off snapping to marks
723 };
724
725 } // namespace Internal
726
727 // Helpers for public-api forwarding methods
728
729 inline Toolkit::Internal::Slider& GetImpl( Toolkit::Slider& pub )
730 {
731   DALI_ASSERT_ALWAYS( pub );
732
733   Dali::RefObject& handle = pub.GetImplementation();
734
735   return static_cast< Toolkit::Internal::Slider& >( handle );
736 }
737
738 inline const Toolkit::Internal::Slider& GetImpl( const Toolkit::Slider& pub )
739 {
740   DALI_ASSERT_ALWAYS( pub );
741
742   const Dali::RefObject& handle = pub.GetImplementation();
743
744   return static_cast< const Toolkit::Internal::Slider& >( handle );
745 }
746
747 } // namespace Toolkit
748
749 } // namespace Dali
750
751 #endif // __DALI_TOOLKIT_INTERNAL_SLIDER_H__