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