Include required header files directly rather than through dali.h
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / buttons / button-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "button-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/events/touch-event.h>
23 #include <dali/public-api/object/type-registry.h>
24
25 namespace
26 {
27 const char* const PROPERTY_DIMMED = "dimmed";
28 } // unnamed namespace
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 const Property::Index Button::PROPERTY_DIMMED( Internal::Button::BUTTON_PROPERTY_START_INDEX );
37
38 namespace Internal
39 {
40
41 namespace
42 {
43
44 BaseHandle Create()
45 {
46   // empty handle as we cannot create button (but type registered for clicked signal)
47   return BaseHandle();
48 }
49
50 TypeRegistration typeRegistration( typeid(Toolkit::Button), typeid(Toolkit::Control), Create );
51
52 SignalConnectorType signalConnector1( typeRegistration, Toolkit::Button::SIGNAL_CLICKED, &Button::DoConnectSignal );
53 SignalConnectorType signalConnector2( typeRegistration, Toolkit::Button::SIGNAL_TOGGLED, &Button::DoConnectSignal );
54
55 PropertyRegistration property1( typeRegistration, "dimmed", Toolkit::Button::PROPERTY_DIMMED, Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty );
56
57 } // unnamed namespace
58
59 Button::Button()
60 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
61   mState( ButtonUp ),
62   mDimmed( false ),
63   mPainter( NULL )
64 {
65 }
66
67 Button::~Button()
68 {
69 }
70
71 void Button::SetDimmed( bool dimmed )
72 {
73   mDimmed = dimmed;
74
75   // Notifies the painter.
76   Toolkit::Button handle( GetOwner() );
77   if( mPainter )
78   {
79     mPainter->SetDimmed( handle, mDimmed );
80   }
81 }
82
83 bool Button::IsDimmed() const
84 {
85   return mDimmed;
86 }
87
88 void Button::SetAnimationTime( float animationTime )
89 {
90   OnAnimationTimeSet( animationTime );
91 }
92
93 float Button::GetAnimationTime() const
94 {
95   return OnAnimationTimeRequested();
96 }
97
98 void Button::OnAnimationTimeSet( float animationTime )
99 {
100   // nothing to do.
101 }
102
103 float Button::OnAnimationTimeRequested() const
104 {
105   return 0.f;
106 }
107
108 Toolkit::Button::ClickedSignalV2& Button::ClickedSignal()
109 {
110   return mClickedSignalV2;
111 }
112
113 Toolkit::Button::ToggledSignalV2& Button::ToggledSignal()
114 {
115   return mToggledSignalV2;
116 }
117
118 bool Button::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
119 {
120   Dali::BaseHandle handle( object );
121
122   bool connected( true );
123   Toolkit::Button button = Toolkit::Button::DownCast(handle);
124
125   if( Dali::Toolkit::Button::SIGNAL_CLICKED == signalName )
126   {
127     button.ClickedSignal().Connect( tracker, functor );
128   }
129   else if( Dali::Toolkit::Button::SIGNAL_TOGGLED == signalName )
130   {
131     button.ToggledSignal().Connect( tracker, functor );
132   }
133   else
134   {
135     // signalName does not match any signal
136     connected = false;
137   }
138
139   return connected;
140 }
141
142 bool Button::OnTouchEvent(const TouchEvent& event)
143 {
144   // Only events are processed when the button is not dimmed and the touch event has only
145   // one touch point.
146   if( ( !mDimmed ) && ( 1 == event.GetPointCount() ) )
147   {
148     switch( event.GetPoint(0).state )
149     {
150       case TouchPoint::Down:
151       {
152         OnButtonDown(); // Notification for derived classes.
153
154         // Sets the button state to ButtonDown.
155         mState = ButtonDown;
156         break;
157       }
158       case TouchPoint::Up:
159       {
160         OnButtonUp(); // Notification for derived classes.
161
162         // Sets the button state to ButtonUp.
163         mState = ButtonUp;
164         break;
165       }
166       case TouchPoint::Interrupted:
167       {
168         OnTouchPointInterrupted(); // Notification for derived classes.
169
170         // Sets the button state to the default (ButtonUp).
171         mState = ButtonUp;
172         break;
173       }
174       case TouchPoint::Leave:
175       {
176         OnTouchPointLeave(); // Notification for derived classes.
177
178         // Sets the button state to the default (ButtonUp).
179         mState = ButtonUp;
180         break;
181       }
182       case TouchPoint::Motion:
183       case TouchPoint::Stationary: // FALLTHROUGH
184       {
185         // Nothing to do
186         break;
187       }
188       default:
189       {
190         DALI_ASSERT_ALWAYS( !"Point status unhandled." );
191         break;
192       }
193     }
194   }
195   else if( 1 < event.GetPointCount() )
196   {
197     OnTouchPointLeave(); // Notification for derived classes.
198
199     // Sets the button state to the default (ButtonUp).
200     mState = ButtonUp;
201   }
202
203   return false;
204 }
205
206 void Button::OnInitialize()
207 {
208   // Initialize the painter and notifies subclasses.
209   Toolkit::Button handle( GetOwner() );
210   if( mPainter )
211   {
212     mPainter->Initialize( handle );
213   }
214
215   Actor self = Self();
216
217   mTapDetector = TapGestureDetector::New();
218   mTapDetector.Attach( self );
219   mTapDetector.DetectedSignal().Connect(this, &Button::OnTap);
220
221   OnButtonInitialize();
222
223   self.SetKeyboardFocusable( true );
224 }
225
226 void Button::OnControlSizeSet(const Vector3& targetSize)
227 {
228   Toolkit::Button handle( GetOwner() );
229   if( mPainter )
230   {
231     mPainter->SetSize( handle, targetSize );
232   }
233 }
234
235 void Button::OnTap(Actor actor, TapGesture tap)
236 {
237   // Do nothing.
238 }
239
240 void Button::OnControlStageDisconnection()
241 {
242   OnButtonStageDisconnection(); // Notification for derived classes.
243   mState = ButtonUp;
244 }
245
246 void Button::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
247 {
248   Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
249
250   if ( button && ( index == Toolkit::Button::PROPERTY_DIMMED ) )
251   {
252     GetImplementation( button ).SetDimmed( value.Get<bool>() );
253   }
254 }
255
256 Property::Value Button::GetProperty( BaseObject* object, Property::Index propertyIndex )
257 {
258   Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
259
260   if ( button && ( propertyIndex == Toolkit::Button::PROPERTY_DIMMED ) )
261   {
262     return Property::Value( GetImplementation( button ).mDimmed );
263   }
264
265   return Property::Value();
266 }
267
268 } // namespace Internal
269
270 } // namespace Toolkit
271
272 } // namespace Dali