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