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