f0884e193e07dcf8ebc9dc0aec40828209ff59af
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / button-impl.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_BUTTON_H__
2 #define __DALI_TOOLKIT_INTERNAL_BUTTON_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/adaptor-framework/timer.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/public-api/controls/buttons/button.h>
26 #include <dali-toolkit/public-api/controls/control-impl.h>
27 #include "button-painter-impl.h"
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 class Button;
36
37 namespace Internal
38 {
39
40 /**
41  * Button is the base class implementation for all buttons.
42  */
43 class Button : public Control
44 {
45 public:
46
47   // Properties
48   enum
49   {
50     BUTTON_PROPERTY_START_INDEX = Control::CONTROL_PROPERTY_END_INDEX + 1,
51     BUTTON_PROPERTY_END_INDEX = BUTTON_PROPERTY_START_INDEX + 1000 ///< Reserving 1000 property indices
52   };
53
54 protected:
55
56   /**
57    * Construct a new Button.
58    */
59   Button();
60
61   /**
62    * A reference counted object may only be deleted by calling Unreference()
63    */
64   virtual ~Button();
65
66 public:
67
68   /**
69    * @copydoc Dali::Toolkit::Button::SetDisabled( bool disabled )
70    */
71   void SetDisabled( bool disabled );
72
73   /**
74    * @copydoc Dali::Toolkit::Button::IsDisabled() const
75    */
76   bool IsDisabled() const;
77
78   /**
79    * @copydoc Dali::Toolkit::Button::SetAutoRepeating( bool autoRepeating )
80    */
81   void SetAutoRepeating( bool autoRepeating );
82
83   /**
84    * @copydoc Dali::Toolkit::Button::IsAutoRepeating() const
85    */
86   bool IsAutoRepeating() const;
87
88   /**
89    * @copydoc Dali::Toolkit::Button::SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay )
90    */
91   void SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay );
92
93   /**
94    * @copydoc Dali::Toolkit::Button::GetInitialAutoRepeatingDelay() const
95    */
96   float GetInitialAutoRepeatingDelay() const;
97
98   /**
99    * @copydoc Dali::Toolkit::Button::SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay )
100    */
101   void SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay );
102
103   /**
104    * @copydoc Dali::Toolkit::Button::GetNextAutoRepeatingDelay() const
105    */
106   float GetNextAutoRepeatingDelay() const;
107
108   /**
109    * @copydoc Dali::Toolkit::Button::SetTogglableButton( bool togglable )
110    */
111   void SetTogglableButton( bool togglable );
112
113   /**
114    * @copydoc Dali::Toolkit::Button::IsTogglableButton() const
115    */
116   bool IsTogglableButton() const;
117
118   /**
119    * @copydoc Dali::Toolkit::Button::SetSelected( bool selected )
120    */
121   virtual void SetSelected( bool selected );
122
123   /**
124    * @copydoc Dali::Toolkit::Button::IsSelected() const
125    */
126   bool IsSelected() const;
127
128   /**
129    * @copydoc Dali::Toolkit::Button::SetAnimationTime()
130    */
131   void SetAnimationTime( float animationTime );
132
133   /**
134    * @copydoc Dali::Toolkit::Button::GetAnimationTime()
135    */
136   float GetAnimationTime() const;
137
138   /**
139    * @copydoc Dali::Toolkit::Button::SetLabel( const std::string& label )
140    */
141   void SetLabel( const std::string& label );
142
143   /**
144    * @copydoc Dali::Toolkit::Button::SetLabel( Actor label )
145    */
146   virtual void SetLabel( Actor label );   // TODO: After refactoring painter, virtual will be removed
147
148   /**
149    * @copydoc Dali::Toolkit::Button::GetLabel()
150    */
151   Actor GetLabel() const;
152
153   /**
154    * Used by the painter only.
155    * @return A reference to the label actor.
156    */
157   Actor& GetLabel();
158
159   /**
160    * @copydoc Dali::Toolkit::PushButton::SetButtonImage( const Image image )
161    */
162   void SetButtonImage( Image image );
163
164   /**
165    * @copydoc Dali::Toolkit::PushButton::SetButtonImage( Actor image )
166    */
167   void SetButtonImage( Actor image );
168
169   /**
170    * @copydoc Dali::Toolkit::PushButton::GetButtonImage()
171    */
172   Actor GetButtonImage() const;
173
174   /**
175    * Used by the painter only.
176    * @return A reference to the button image.
177    */
178   Actor& GetButtonImage();
179
180   /**
181    * @copydoc Dali::Toolkit::PushButton::SetSelectedImage( const Image image )
182    */
183   void SetSelectedImage( Image image );
184
185   /**
186    * @copydoc Dali::Toolkit::PushButton::SetSelectedImage( Actor image )
187    */
188   void SetSelectedImage( Actor image );
189
190   /**
191    * @copydoc Dali::Toolkit::PushButton::GetSelectedImage()
192    */
193   Actor GetSelectedImage() const;
194
195   /**
196    * Used by the painter only.
197    * @return A reference to the selected image.
198    */
199   Actor& GetSelectedImage();
200
201   /**
202    * @copydoc Dali::Toolkit::PushButton::SetBackgroundImage( const Image image )
203    */
204   void SetBackgroundImage( Image image );
205
206   /**
207    * @copydoc Dali::Toolkit::PushButton::SetBackgroundImage( Actor image )
208    */
209   void SetBackgroundImage( Actor image );
210
211   /**
212    * @copydoc Dali::Toolkit::PushButton::GetBackgroundImage()
213    */
214   Actor GetBackgroundImage() const;
215
216   /**
217    * Used by the painter only.
218    * @return A reference to the background image.
219    */
220   Actor& GetBackgroundImage();
221
222   /**
223    * @copydoc Dali::Toolkit::PushButton::SetDisabledImage( Image image )
224    */
225   void SetDisabledImage( Image image );
226
227   /**
228    * @copydoc Dali::Toolkit::PushButton::SetDisabledImage( Actor image )
229    */
230   void SetDisabledImage( Actor image );
231
232   /**
233    * @copydoc Dali::Toolkit::PushButton::GetDisabledImage()
234    */
235   Actor GetDisabledImage() const;
236
237   /**
238    * Used by the painter only.
239    * @return A reference to the disabled button image.
240    */
241   Actor& GetDisabledImage();
242
243   /**
244    * @copydoc Dali::Toolkit::CheckBoxButton::SetDisabledSelectedImage( Image image )
245    */
246   void SetDisabledSelectedImage( Image image );
247
248   /**
249    * @copydoc Dali::Toolkit::CheckBoxButton::SetDisabledSelectedImage( Actor image )
250    */
251   void SetDisabledSelectedImage( Actor image );
252
253   /**
254    * @copydoc Dali::Toolkit::CheckBoxButton::GetDisabledSelectedImage()
255    */
256   Actor GetDisabledSelectedImage() const;
257
258   /**
259    * Used by the painter only.
260    * @return A reference to the disabled selected image.
261    */
262   Actor& GetDisabledSelectedImage();
263
264   /**
265    * @copydoc Dali::Toolkit::PushButton::SetDisabledBackgroundImage( Image image )
266    */
267   void SetDisabledBackgroundImage( Image image );
268
269   /**
270    * @copydoc Dali::Toolkit::PushButton::SetDisabledBackgroundImage( Actor image )
271    */
272   void SetDisabledBackgroundImage( Actor image );
273
274   /**
275    * @copydoc Dali::Toolkit::PushButton::GetDisabledBackgroundImage()
276    */
277   Actor GetDisabledBackgroundImage() const;
278
279   /**
280    * Used by the painter only.
281    * @return A reference to the disabled background image.
282    */
283   Actor& GetDisabledBackgroundImage();
284
285   /**
286    * Used by the painter only.
287    * @return A reference to the button image that is fading out.
288    */
289   Actor& GetFadeOutButtonImage();
290
291   /**
292    * Used by the painter only.
293    * @return A reference to the selected image that is fading out.
294    */
295   Actor& GetFadeOutSelectedImage();
296
297   /**
298    * Used by the painter only.
299    * @return A reference to the background image that is fading out.
300    */
301   Actor& GetFadeOutBackgroundImage();
302
303   /**
304    * Performs actions as requested using the action name.
305    * @param[in] object The object on which to perform the action.
306    * @param[in] actionName The action to perform.
307    * @param[in] attributes The attributes with which to perfrom this action.
308    * @return true if action has been accepted by this control
309    */
310   static bool DoAction( BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes );
311
312 private:
313   /**
314    * Perform the click action to click the button.
315    * @param[in] attributes The attributes to perfrom this action.
316    */
317   void DoClickAction( const PropertyValueContainer& attributes );
318
319   /**
320    * This method is called after the button initialization.
321    * Could be reimplemented in subclasses to provide specific behaviour.
322    */
323   virtual void OnButtonInitialize() { }
324
325   /**
326    * This method is called from the OnTouchEvent method when the button is down.
327    * Could be reimplemented in subclasses to provide specific behaviour.
328    */
329   virtual void OnButtonDown();
330
331   /**
332    * This method is called from the OnTouchEvent method when the button is up.
333    * Could be reimplemented in subclasses to provide specific behaviour.
334    */
335   virtual void OnButtonUp();
336
337   /**
338    * This method is called from the OnTouchEvent method when the touch point leaves the boundary of the button or
339    * more than one touch points are received.
340    * Could be reimplemented in subclasses to provide specific behaviour.
341    */
342   virtual void OnTouchPointLeave();
343
344   /**
345    * This method is called from the OnTouchEvent method when the touch point is interrupted.
346    * Could be reimplemented in subclasses to provide specific behaviour.
347    */
348   virtual void OnTouchPointInterrupted();
349
350   /**
351    * This method is called when the animation time is set.
352    * Needs to be reimplemented in subclasses to set the animation time in different buttons.
353    * @param animationTime The animation time in seconds.
354    */
355   virtual void OnAnimationTimeSet( float animationTime );
356
357   /**
358    * This method is called when the animation time is requested.
359    * Needs to be reimplemented in subclases to return the animation time.
360    * @return The animation time in seconds.
361    */
362   virtual float OnAnimationTimeRequested() const;
363
364   /**
365    * This method is called when the button is removed from the stage.
366    * Could be reimplemented in subclasses to provide specific behaviour.
367    */
368   virtual void OnButtonStageDisconnection();
369
370 public:
371
372   /**
373    * @copydoc Dali::Toolkit::PushButton::PressedSignal()
374    */
375   Toolkit::Button::ButtonSignalType& PressedSignal();
376
377   /**
378    * @copydoc Dali::Toolkit::PushButton::ReleasedSignal()
379    */
380   Toolkit::Button::ButtonSignalType& ReleasedSignal();
381
382   /**
383    * @copydoc Dali::Toolkit::Button::ClickedSignal()
384    */
385   Toolkit::Button::ButtonSignalType& ClickedSignal();
386
387   /**
388    * @copydoc Dali::Toolkit::Button::StateChangedSignal()
389    */
390   Toolkit::Button::ButtonSignalType& StateChangedSignal();
391
392   /**
393    * Connects a callback function with the object's signals.
394    * @param[in] object The object providing the signal.
395    * @param[in] tracker Used to disconnect the signal.
396    * @param[in] signalName The signal to connect to.
397    * @param[in] functor A newly allocated FunctorDelegate.
398    * @return True if the signal was connected.
399    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
400    */
401   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
402
403   // Properties
404
405   /**
406    * Called when a property of an object of this type is set.
407    * @param[in] object The object whose property is set.
408    * @param[in] index The property index.
409    * @param[in] value The new property value.
410    */
411   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
412
413   /**
414    * Called to retrieve a property of an object of this type.
415    * @param[in] object The object whose property is to be retrieved.
416    * @param[in] index The property index.
417    * @return The current value of the property.
418    */
419   static Property::Value GetProperty( BaseObject* object, Property::Index propertyIndex );
420
421 protected: // From CustomActorImpl
422
423   /**
424    * @copydoc Dali::CustomActorImpl::OnTouchEvent( const TouchEvent& event )
425    */
426   virtual bool OnTouchEvent( const TouchEvent& event );
427
428 private: // From Control
429
430   /**
431    * @copydoc Toolkit::Control::OnInitialize()
432    */
433   virtual void OnInitialize();
434
435   /**
436    * @copydoc Toolkit::Control::OnActivated()
437    */
438   virtual void OnActivated();
439
440   /**
441    * @copydoc Toolkit::Control::OnControlSizeSet( const Vector3& targetSize )
442    */
443   virtual void OnControlSizeSet( const Vector3& targetSize );
444
445   /**
446    * Callback received when the button is disconnected from the stage.
447    * It resets the button status.
448    */
449   void OnControlStageDisconnection();
450
451 private:
452
453   /**
454    * Handler for tap events.
455    * We do not actually do anything when we receive a tap as the button handles tap event through
456    * the touch event system itself as it requires more than just tap handling (e.g. leave events).
457    * This stops any of our parents receiving a tap gesture when it occurs within our area.
458    * @param[in]  actor  The tapped actor.
459    * @param[in]  tap    The tap gesture.
460    */
461   void OnTap(Actor actor, const TapGesture& tap);
462
463   /**
464    * Sets up the autorepeating timer.
465    * @param[in] delay The delay time in seconds.
466    */
467   void SetUpTimer( float delay );
468
469   /**
470    * Slot called when Dali::Timer::SignalTick signal. Resets the autorepeating timer.
471    */
472   bool AutoRepeatingSlot();
473
474 protected:
475
476   enum ButtonState
477   {
478     ButtonUp,                                  ///< The button is up.
479     ButtonDown,                                ///< The button is down.
480   };
481
482   void SetPainter(ButtonPainterPtr painter);
483
484   ButtonState GetState();
485
486   Actor mLabel;                                //TODO: After refactoring painter, this will be private
487   bool  mTogglableButton;                      //TODO: After refactoring painter, this will be private
488   bool  mSelected;                             //TODO: After refactoring painter, this will be private
489
490 private:
491
492   // Undefined
493   Button( const Button& );
494
495   // Undefined
496   Button& operator = ( const Button& );
497
498 private:
499
500   ButtonPainterPtr mPainter;                   ///< Pointer to a ButtonPainter base class.
501
502   // Signals
503   Toolkit::Button::ButtonSignalType mPressedSignal;           ///< Signal emitted when the button is pressed.
504   Toolkit::Button::ButtonSignalType mReleasedSignal;         ///< Signal emitted when the button is released.
505   Toolkit::Button::ButtonSignalType mClickedSignal;           ///< Signal emitted when the button is clicked.
506   Toolkit::Button::ButtonSignalType mStateChangedSignal; ///< Signal emitted when the button's state is changed.
507
508   Timer mAutoRepeatingTimer;                   ///< Timer used to implement the autorepeating property.
509
510 //  Actor mLabel;                                ///< Stores the button label.
511
512   Actor mButtonImage;                          ///< Stores the unselected image.
513   Actor mSelectedImage;                        ///< Stores the selected image.
514   Actor mBackgroundImage;                      ///< Stores the background image.
515   Actor mDisabledImage;                        ///< Stores the disabled image.
516   Actor mDisabledSelectedImage;                ///< Stores the disabled selected image.
517   Actor mDisabledBackgroundImage;              ///< Stores the disabled background image.
518
519   Actor mFadeOutButtonImage;                   ///< Stores a foreground image, which is in a fade out animation, to be removed when the animation finishes.
520   Actor mFadeOutSelectedImage;                 ///< Stores a foreground image, which is in a fade out animation, to be removed when the animation finishes.
521   Actor mFadeOutBackgroundImage;               ///< Stores a background image, which is in a fade out animation, to be removed when the animation finishes.
522
523   TapGestureDetector mTapDetector;
524
525   bool             mDisabled;                  ///< Stores the disabled property.
526   bool             mAutoRepeating;             ///< Stores the autorepeating property.
527 //  bool             mTogglableButton;           ///< Stores the togglable property.
528 //  bool             mSelected;                  ///< Stores the selected state.
529   float            mInitialAutoRepeatingDelay; ///< Stores the initial autorepeating delay in seconds.
530   float            mNextAutoRepeatingDelay;    ///< Stores the next autorepeating delay in seconds.
531
532   // Actions
533   bool             mClickActionPerforming;
534
535   ButtonState      mState;                     ///< Stores the button state.
536 };
537
538 } // namespace Internal
539
540
541 // Helpers for public-api forwarding methods
542
543 inline Toolkit::Internal::Button& GetImplementation( Toolkit::Button& button )
544 {
545   DALI_ASSERT_ALWAYS( button );
546
547   Dali::RefObject& handle = button.GetImplementation();
548
549   return static_cast<Toolkit::Internal::Button&>( handle );
550 }
551
552 inline const Toolkit::Internal::Button& GetImplementation( const Toolkit::Button& button )
553 {
554   DALI_ASSERT_ALWAYS( button );
555
556   const Dali::RefObject& handle = button.GetImplementation();
557
558   return static_cast<const Toolkit::Internal::Button&>( handle );
559 }
560
561 } // namespace Toolkit
562
563 } // namespace Dali
564
565 #endif // __DALI_TOOLKIT_INTERNAL_BUTTON_H__
566