2c45d7f01fb6bdde0035f2b4c638010197c179fa
[platform/core/uifw/dali-toolkit.git] / base / 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 // INTERNAL INCLUDES
22 #include <dali/dali.h>
23 #include <dali-toolkit/public-api/controls/buttons/button.h>
24 #include <dali-toolkit/public-api/controls/control-impl.h>
25 #include "button-painter-impl.h"
26
27 namespace Dali
28 {
29
30 namespace Toolkit
31 {
32
33 class Button;
34
35 namespace Internal
36 {
37
38 /**
39  * Button is the base class implementation for all buttons.
40  */
41 class Button : public Control
42 {
43 public:
44
45   // Properties
46   enum
47   {
48     BUTTON_PROPERTY_START_INDEX = Control::CONTROL_PROPERTY_END_INDEX + 1,
49     BUTTON_PROPERTY_END_INDEX = BUTTON_PROPERTY_START_INDEX + 1000 ///< Reserving 1000 property indices
50   };
51
52 protected:
53
54   /**
55    * Construct a new Button.
56    */
57   Button();
58
59   /**
60    * A reference counted object may only be deleted by calling Unreference()
61    */
62   virtual ~Button();
63
64 public:
65
66   /**
67    * @copydoc Dali::Toolkit::Button::SetDimmed( bool dimmed )
68    */
69   void SetDimmed( bool dimmed );
70
71   /**
72    * @copydoc Dali::Toolkit::Button::IsDimmed() const
73    */
74   bool IsDimmed() const;
75
76   /**
77    * @copydoc Dali::Toolkit::Button::SetAnimationTime()
78    */
79   void SetAnimationTime( float animationTime );
80
81   /**
82    * @copydoc Dali::Toolkit::Button::GetAnimationTime()
83    */
84   float GetAnimationTime() const;
85
86 private:
87
88   /**
89    * This method is called after the button initialization.
90    * Could be reimplemented in subclasses to provide specific behaviour.
91    */
92   virtual void OnButtonInitialize() { }
93
94   /**
95    * This method is called from the OnTouchEvent method when the button is down.
96    * Could be reimplemented in subclasses to provide specific behaviour.
97    */
98   virtual void OnButtonDown() { }
99
100   /**
101    * This method is called from the OnTouchEvent method when the button is up.
102    * Could be reimplemented in subclasses to provide specific behaviour.
103    */
104   virtual void OnButtonUp() { }
105
106   /**
107    * This method is called from the OnTouchEvent method when the touch point leaves the boundary of the button or
108    * more than one touch points are received.
109    * Could be reimplemented in subclasses to provide specific behaviour.
110    */
111   virtual void OnTouchPointLeave() { }
112
113   /**
114    * This method is called from the OnTouchEvent method when the touch point is interrupted.
115    * Could be reimplemented in subclasses to provide specific behaviour.
116    */
117   virtual void OnTouchPointInterrupted() { }
118
119   /**
120    * This method is called when the animation time is set.
121    * Needs to be reimplemented in subclasses to set the animation time in different buttons.
122    * @param animationTime The animation time in seconds.
123    */
124   virtual void OnAnimationTimeSet( float animationTime );
125
126   /**
127    * This method is called when the animation time is requested.
128    * Needs to be reimplemented in subclases to return the animation time.
129    * @return The animation time in seconds.
130    */
131   virtual float OnAnimationTimeRequested() const;
132
133   /**
134    * This method is called when the button is removed from the stage.
135    * Could be reimplemented in subclasses to provide specific behaviour.
136    */
137   virtual void OnButtonStageDisconnection() { }
138
139 public:
140
141   /**
142    * @copydoc Dali::Toolkit::Button::ClickedSignal()
143    */
144   Toolkit::Button::ClickedSignalV2& ClickedSignal();
145
146   /**
147    * @copydoc Dali::Toolkit::Button::ToggledSignal()
148    */
149   Toolkit::Button::ToggledSignalV2& ToggledSignal();
150
151   /**
152    * Connects a callback function with the object's signals.
153    * @param[in] object The object providing the signal.
154    * @param[in] tracker Used to disconnect the signal.
155    * @param[in] signalName The signal to connect to.
156    * @param[in] functor A newly allocated FunctorDelegate.
157    * @return True if the signal was connected.
158    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
159    */
160   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
161
162   // Properties
163
164   /**
165    * Called when a property of an object of this type is set.
166    * @param[in] object The object whose property is set.
167    * @param[in] index The property index.
168    * @param[in] value The new property value.
169    */
170   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
171
172   /**
173    * Called to retrieve a property of an object of this type.
174    * @param[in] object The object whose property is to be retrieved.
175    * @param[in] index The property index.
176    * @return The current value of the property.
177    */
178   static Property::Value GetProperty( BaseObject* object, Property::Index propertyIndex );
179
180 protected: // From CustomActorImpl
181
182   /**
183    * @copydoc Dali::CustomActorImpl::OnTouchEvent( const TouchEvent& event )
184    */
185   virtual bool OnTouchEvent( const TouchEvent& event );
186
187 private: // From Control
188
189   /**
190    * @copydoc Toolkit::Control::OnInitialize()
191    */
192   virtual void OnInitialize();
193
194   /**
195    * @copydoc Toolkit::Control::OnControlSizeSet( const Vector3& targetSize )
196    */
197   virtual void OnControlSizeSet( const Vector3& targetSize );
198
199   /**
200    * Callback received when the button is disconnected from the stage.
201    * It resets the button status.
202    */
203   void OnControlStageDisconnection();
204
205 private:
206
207   /**
208    * Handler for tap events.
209    * We do not actually do anything when we receive a tap as the button handles tap event through
210    * the touch event system itself as it requires more than just tap handling (e.g. leave events).
211    * This stops any of our parents receiving a tap gesture when it occurs within our area.
212    * @param[in]  actor  The tapped actor.
213    * @param[in]  tap    The tap gesture.
214    */
215   void OnTap(Actor actor, TapGesture tap);
216
217 private:
218
219   // Undefined
220   Button( const Button& );
221
222   // Undefined
223   Button& operator = ( const Button& );
224
225 protected: // Signals
226
227   enum ButtonState
228   {
229     ButtonUp,                         ///< The button is up.
230     ButtonDown,                       ///< The button is down.
231   };
232
233   ButtonState      mState;                ///< Stores the button state.
234
235   bool             mDimmed;               ///< Stores the dimmed property.
236
237   ButtonPainterPtr mPainter;              ///< Pointer to a ButtonPainter base class.
238
239   Toolkit::Button::ClickedSignalV2 mClickedSignalV2; ///< Signal emitted when the button is clicked.
240   Toolkit::Button::ToggledSignalV2 mToggledSignalV2; ///< Signal emitted when the button is toggled.
241
242   TapGestureDetector mTapDetector;
243 };
244
245 } // namespace Internal
246
247
248 // Helpers for public-api forwarding methods
249
250 inline Toolkit::Internal::Button& GetImplementation( Toolkit::Button& button )
251 {
252   DALI_ASSERT_ALWAYS( button );
253
254   Dali::RefObject& handle = button.GetImplementation();
255
256   return static_cast<Toolkit::Internal::Button&>( handle );
257 }
258
259 inline const Toolkit::Internal::Button& GetImplementation( const Toolkit::Button& button )
260 {
261   DALI_ASSERT_ALWAYS( button );
262
263   const Dali::RefObject& handle = button.GetImplementation();
264
265   return static_cast<const Toolkit::Internal::Button&>( handle );
266 }
267
268 } // namespace Toolkit
269
270 } // namespace Dali
271
272 #endif // __DALI_TOOLKIT_INTERNAL_BUTTON_H__
273