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