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