Split dali-toolkit into Base & Optional
[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 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::SlidingFinishedSignal()
225    */
226   Toolkit::Slider::ValueChangedSignalType& SlidingFinishedSignal();
227
228   /**
229    * @copydoc Toolkit::Slider::MarkSignal()
230    */
231   Toolkit::Slider::MarkSignalType& MarkSignal();
232
233   /**
234    * Connects a callback function with the object's signals.
235    * @param[in] object The object providing the signal.
236    * @param[in] tracker Used to disconnect the signal.
237    * @param[in] signalName The signal to connect to.
238    * @param[in] functor A newly allocated FunctorDelegate.
239    * @return True if the signal was connected.
240    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
241    */
242   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName,
243                                FunctorDelegate* functor );
244
245 protected:
246
247   /**
248    * Construct a new Slider.
249    */
250   Slider();
251
252   /**
253    * A reference counted object may only be deleted by calling Unreference()
254    */
255   virtual ~Slider();
256
257   /**
258    * @copydoc Toolkit::ControlImpl::OnControlSizeSet( const Vector3& size )
259    */
260   virtual void OnControlSizeSet( const Vector3& size );
261
262 private:
263
264   /**
265    * Domain is a from/to pair
266    */
267   struct Domain
268   {
269     Vector2 from;
270     Vector2 to;
271
272     Domain()
273     {
274     }
275     Domain( Vector2 fromVal, Vector2 toVal )
276         : from( fromVal ), to( toVal )
277     {
278     }
279   };
280
281   /**
282    * Slider states
283    */
284   enum SliderState
285   {
286     NORMAL,
287     DISABLED,
288     PRESSED,
289     FOCUSED
290   };
291
292 private:
293
294   /**
295    * @copydoc Toolkit::Control::OnInitialize()
296    */
297   virtual void OnInitialize();
298
299   /**
300    * Hit region touch event
301    *
302    * @param[in] actor The actor the event is raised for
303    * @param[in] event The touch event info
304    * @return If the event is handled or not
305    */
306   bool OnTouchEvent( Actor actor, const TouchEvent& event );
307
308   /**
309    * Pan gesture event
310    *
311    * @param[in] actor The actor the event is raised for
312    * @param[in] gesture The pan event info
313    */
314   void OnPan( Actor actor, PanGesture gestur );
315
316   /**
317    * Map a position onto a domain and return the result as a percentage
318    *
319    * @param[in] point The point to map onto the domain
320    * @return The result as a percentage [0..1]
321    */
322   float MapPercentage( const Vector2& point );
323
324   /**
325    * Map a value in the range to a percentage
326    *
327    * @param[in] point The value in range [lowerBound..upperBound]
328    * @return The result as a percentage [0..1]
329    */
330   float MapValuePercentage( float value );
331
332   /**
333    * Convert a point in local hit space into domain space
334    *
335    * @param[in] x The x position to convert
336    * @return The x position in domain space
337    */
338   float HitSpaceToDomain( float x );
339
340   /**
341    * Map a percentage onto the slider's bounds
342    *
343    * @param[in] percent The current value of slider in percent
344    * @param[in] lowerBound The lower bound to map onto
345    * @param[in] upperBound The upper bound to map onto
346    * @return The value of percent mapped from lowerBound to upperBound
347    */
348   float MapBounds( float percent, float lowerBound, float upperBound );
349
350   /**
351    * Get the range of the valid values the slider handle can move between
352    *
353    * @param[in] currentSize The current size of the slider
354    * @return The range as a domain pair
355    */
356   Domain CalcDomain( const Vector2& currentSize );
357
358   /**
359    * Create the hit region
360    *
361    * @return The hit region actor
362    */
363   Actor CreateHitRegion();
364
365   /**
366    * Create the backing for the slider
367    *
368    * @return The backing actor
369    */
370   ImageActor CreateBacking();
371
372   /**
373    * Create the progress backing for the slider
374    *
375    * @return The backing actor
376    */
377   ImageActor CreateProgress();
378
379   /**
380    * Create the handle for the slider
381    *
382    * @return The created image handle
383    */
384   ImageActor CreateHandle();
385
386   /**
387    * Create the popup arrow
388    *
389    * @return The created image handle
390    */
391   ImageActor CreatePopupArrow();
392
393   /**
394    * Create the popup
395    *
396    * @return The created image handle
397    */
398   ImageActor CreatePopup();
399
400   /**
401    * Create the textview for the popup
402    *
403    * @return The textview created for the popup
404    */
405   Toolkit::TextView CreatePopupText();
406
407   /**
408    * Create the value display for the slider
409    *
410    * @return The created root actor of the display
411    */
412   Actor CreateValueDisplay();
413
414   /**
415    * Set the skin based on the current state
416    */
417   void UpdateSkin();
418
419   /**
420    * Create all the children
421    */
422   void CreateChildren();
423
424   /**
425    * Resize the hit area
426    *
427    * @param[in] size The new size of the hit area
428    */
429   void ResizeHitRegion( const Vector2& size );
430
431   /**
432    * Create value popup
433    */
434   void AddPopup();
435
436   /**
437    * Remove value popup
438    */
439   void RemovePopup();
440
441   /**
442    * Display the popup for a set duration with the given value
443    *
444    * @param[in] value The value to display in the popup
445    */
446   void DisplayPopup( float value );
447
448   /**
449    * If there are marks present, filter the incoming percent based on snapping to any nearby marks
450    *
451    * @param[in] value The incoming value on the slider to filter
452    * @return The filtered percentage snapped to any nearby marks
453    */
454   float MarkFilter( float value );
455
456   /**
457    * If there are marks present, snap the incoming percent to the nearest mark
458    *
459    * @param[in] value The incoming value on the slider to snap
460    * @return The filtered percentage snapped to the nearest mark
461    */
462   float SnapToMark( float value );
463
464   /**
465    * Search for if a mark has been reached
466    *
467    * @param[in] value The value to search against
468    * @param[out] outIndex The index of the mark if found
469    * @return If a mark has been found to match percent
470    */
471   bool MarkReached( float value, int& outIndex );
472
473   /**
474    * Handler for when the value view needs to be hidden
475    *
476    * @return If handled or not
477    */
478   bool HideValueView();
479
480   /**
481    * Set value choosing whether to fire signals or not
482    *
483    * @paramp[in] value The value to set
484    * @param[in] raiseSignals Configure signals to be raised or not.
485    */
486   void DisplayValue( float value, bool raiseSignals );
487
488   /**
489    * Create the image for the backing
490    *
491    * @param[in] imageName The name of the image to load and set
492    */
493   void CreateBackingImage( const std::string& imageName );
494
495   /**
496    * Set the backing image to be a scale-9 image
497    *
498    * @param[in] border The scale-9 border to use
499    */
500   void SetBackingScale9( const Vector4& border );
501
502   /**
503    * Resize the backing region
504    *
505    * @param[in] region The size of the region to set
506    */
507   void ResizeBackingRegion( const Vector2& region );
508
509   /**
510    * Size the backing region
511    *
512    * @param[in] region The size of the region to set
513    */
514   void SetBackingRegionSize( const Vector2& region );
515
516   /**
517    * Create the image for the progress bar
518    *
519    * @param[in] imageName The name of the image to load and set
520    */
521   void CreateProgressImage( const std::string& imageName );
522
523   /**
524    * Create the image for the popup
525    *
526    * @param[in] imageName The name of the image to load and set
527    */
528   void CreatePopupImage( const std::string& imageName );
529
530   /**
531    * Create the image for the popup arrow
532    *
533    * @param[in] imageName The name of the image to load and set
534    */
535   void CreatePopupArrowImage( const std::string& imageName );
536
537   /**
538    * Set the progress image to be a scale-9 image
539    *
540    * @param[in] border The scale-9 border to use
541    */
542   void SetProgressScale9( const Vector4& border );
543
544   /**
545    * Set the popup image to be a scale-9 image
546    *
547    * @param[in] border The scale-9 border to use
548    */
549   void SetPopupScale9( const Vector4& border );
550
551   /**
552    * Set the size of the progress bar region
553    *
554    * @param[in] region The size of the region to set
555    */
556   void ResizeProgressRegion( const Vector2& region );
557
558   /**
559    * Create the image for the handle
560    *
561    * @param[in] imageName The name of the image to load and set
562    */
563   void CreateHandleImage( const std::string& imageName );
564
565   /**
566    * Set the size of the handle region
567    *
568    * @param[in] region The size of the region to set
569    */
570   void ResizeHandleRegion( const Vector2& region );
571
572   /**
573    * Create and display the value on the handle
574    */
575   void CreateHandleValueDisplay();
576
577   /**
578    * Remove and destroy the handle value display
579    */
580   void DestroyHandleValueDisplay();
581
582   /**
583    * Update the color of the popup text
584    *
585    * @param[in] color The new color
586    */
587   void UpdatePopupTextColor( const Vector4& color );
588
589   /**
590    * Set handle region
591    *
592    * @param[in] region The handle region
593    */
594   void UpdateHandleRegion( const Vector2& region );
595
596   /**
597    * Get handle region
598    *
599    * @return The handle region
600    */
601   Vector2 GetHandleRegion() const;
602
603   /**
604    * Set the lower bound of the slider's value
605    *
606    * @param[in] bound The value to set for the lower bound
607    */
608   void UpdateLowerBound( float bound );
609
610   /**
611    * Get the lower bound of the slider's value
612    *
613    * @return The lower bound value
614    */
615   float GetLowerBound() const;
616
617   /**
618    * Set the upper bound of the slider's value
619    *
620    * @param[in] bound The value to set for the upper bound
621    */
622   void UpdateUpperBound( float bound );
623
624   /**
625    * Get the upper bound of the slider's value
626    *
627    * @return The upper bound value
628    */
629   float GetUpperBound() const;
630
631 private:
632   // From ControlImpl
633
634   /**
635    * @copydoc Dali::CustomActorImpl::OnPropertySet()
636    */
637   virtual void OnPropertySet( Property::Index index, Property::Value propertyValue );
638
639 private:
640
641   // Undefined
642   Slider( const Slider& );
643
644   // Undefined
645   Slider& operator=( const Slider& rhs );
646
647 private:
648
649   Domain mDomain;                           ///< Current domain of the handle
650
651   Actor mHitArea;                           ///< The input handler
652   ImageActor mBacking;                      ///< Backing image
653   ImageActor mHandle;                       ///< Slider handle
654   ImageActor mProgress;                     ///< Progress backing
655   Actor mValueDisplay;                      ///< Display of the value
656   ImageActor mPopup;                        ///< Popup backing
657   ImageActor mPopupArrow;                   ///< Popup arrow backing
658
659   Toolkit::TextView mValueTextView;         //< The text value in popup
660   Toolkit::TextView mHandleValueTextView;   ///< The text value on handle
661   Vector2 mHandleLastTouchPoint;            ///< The last touch point for the handle
662   Timer mValueTimer;                        ///< Timer used to hide value view
663
664   Toolkit::Slider::ValueChangedSignalType mValueChangedSignal;    ///< Signal emitted when the value is changed
665   Toolkit::Slider::ValueChangedSignalType mSlidingFinishedSignal;    ///< Signal emitted when a sliding is finished
666   Toolkit::Slider::MarkSignalType mMarkSignal;                    ///< Signal emitted when a mark is reached
667
668   SliderState mState;                 ///< The state of the slider
669
670   PanGestureDetector mPanDetector;    ///< Hit region pan detector
671
672   MarkList mMarks;                    ///< List of discreet marks
673
674   // Properties
675   Property::Index mPropertyLowerBound;
676   Property::Index mPropertyUpperBound;
677   Property::Index mPropertyValue;
678   Property::Index mPropertyHitRegion;
679   Property::Index mPropertyBackingRegion;
680   Property::Index mPropertyHandleRegion;
681
682   Property::Index mPropertyBackingImageName;
683   Property::Index mPropertyHandleImageName;
684   Property::Index mPropertyProgressImageName;
685   Property::Index mPropertyPopupImageName;
686   Property::Index mPropertyPopupArrowImageName;
687
688   Property::Index mPropertyBackingScale9Border;
689   Property::Index mPropertyProgressScale9Border;
690   Property::Index mPropertyPopupScale9Border;
691
692   Property::Index mPropertyDisableColor;
693   Property::Index mPropertyPopupTextColor;
694
695   Property::Index mPropertyValuePrecision;
696
697   Property::Index mPropertyShowPopup;
698   Property::Index mPropertyShowValue;
699
700   Property::Index mPropertyEnabled;
701
702   Property::Index mPropertyMarks;
703   Property::Index mPropertySnapToMarks;
704   Property::Index mPropertyMarkTolerance;
705
706 };
707
708 } // namespace Internal
709
710 // Helpers for public-api forwarding methods
711
712 inline Toolkit::Internal::Slider& GetImpl( Toolkit::Slider& pub )
713 {
714   DALI_ASSERT_ALWAYS( pub );
715
716   Dali::RefObject& handle = pub.GetImplementation();
717
718   return static_cast< Toolkit::Internal::Slider& >( handle );
719 }
720
721 inline const Toolkit::Internal::Slider& GetImpl( const Toolkit::Slider& pub )
722 {
723   DALI_ASSERT_ALWAYS( pub );
724
725   const Dali::RefObject& handle = pub.GetImplementation();
726
727   return static_cast< const Toolkit::Internal::Slider& >( handle );
728 }
729
730 } // namespace Toolkit
731
732 } // namespace Dali
733
734 #endif // __DALI_TOOLKIT_INTERNAL_SLIDER_H__