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