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