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