90a741abb3b4cb47d1ea19cb968adde9e4774b59
[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
20 #include "button-impl.h"
21
22 namespace
23 {
24 const char* const PROPERTY_DIMMED = "dimmed";
25 } // unnamed namespace
26
27 namespace Dali
28 {
29
30 namespace Toolkit
31 {
32
33 const Property::Index Button::PROPERTY_DIMMED( Internal::Button::BUTTON_PROPERTY_START_INDEX );
34
35 namespace Internal
36 {
37
38 namespace
39 {
40
41 BaseHandle Create()
42 {
43   // empty handle as we cannot create button (but type registered for clicked signal)
44   return BaseHandle();
45 }
46
47 TypeRegistration typeRegistration( typeid(Toolkit::Button), typeid(Toolkit::Control), Create );
48
49 SignalConnectorType signalConnector1( typeRegistration, Toolkit::Button::SIGNAL_CLICKED, &Button::DoConnectSignal );
50 SignalConnectorType signalConnector2( typeRegistration, Toolkit::Button::SIGNAL_TOGGLED, &Button::DoConnectSignal );
51
52 PropertyRegistration property1( typeRegistration, "dimmed", Toolkit::Button::PROPERTY_DIMMED, Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty );
53
54 } // unnamed namespace
55
56 Button::Button()
57 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
58   mState( ButtonUp ),
59   mDimmed( false ),
60   mPainter( NULL )
61 {
62 }
63
64 Button::~Button()
65 {
66 }
67
68 void Button::SetDimmed( bool dimmed )
69 {
70   mDimmed = dimmed;
71
72   // Notifies the painter.
73   Toolkit::Button handle( GetOwner() );
74   if( mPainter )
75   {
76     mPainter->SetDimmed( handle, mDimmed );
77   }
78 }
79
80 bool Button::IsDimmed() const
81 {
82   return mDimmed;
83 }
84
85 void Button::SetAnimationTime( float animationTime )
86 {
87   OnAnimationTimeSet( animationTime );
88 }
89
90 float Button::GetAnimationTime() const
91 {
92   return OnAnimationTimeRequested();
93 }
94
95 void Button::OnAnimationTimeSet( float animationTime )
96 {
97   // nothing to do.
98 }
99
100 float Button::OnAnimationTimeRequested() const
101 {
102   return 0.f;
103 }
104
105 Toolkit::Button::ClickedSignalV2& Button::ClickedSignal()
106 {
107   return mClickedSignalV2;
108 }
109
110 Toolkit::Button::ToggledSignalV2& Button::ToggledSignal()
111 {
112   return mToggledSignalV2;
113 }
114
115 bool Button::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
116 {
117   Dali::BaseHandle handle( object );
118
119   bool connected( true );
120   Toolkit::Button button = Toolkit::Button::DownCast(handle);
121
122   if( Dali::Toolkit::Button::SIGNAL_CLICKED == signalName )
123   {
124     button.ClickedSignal().Connect( tracker, functor );
125   }
126   else if( Dali::Toolkit::Button::SIGNAL_TOGGLED == signalName )
127   {
128     button.ToggledSignal().Connect( tracker, functor );
129   }
130   else
131   {
132     // signalName does not match any signal
133     connected = false;
134   }
135
136   return connected;
137 }
138
139 bool Button::OnTouchEvent(const TouchEvent& event)
140 {
141   // Only events are processed when the button is not dimmed and the touch event has only
142   // one touch point.
143   if( ( !mDimmed ) && ( 1 == event.GetPointCount() ) )
144   {
145     switch( event.GetPoint(0).state )
146     {
147       case TouchPoint::Down:
148       {
149         OnButtonDown(); // Notification for derived classes.
150
151         // Sets the button state to ButtonDown.
152         mState = ButtonDown;
153         break;
154       }
155       case TouchPoint::Up:
156       {
157         OnButtonUp(); // Notification for derived classes.
158
159         // Sets the button state to ButtonUp.
160         mState = ButtonUp;
161         break;
162       }
163       case TouchPoint::Interrupted:
164       {
165         OnTouchPointInterrupted(); // Notification for derived classes.
166
167         // Sets the button state to the default (ButtonUp).
168         mState = ButtonUp;
169         break;
170       }
171       case TouchPoint::Leave:
172       {
173         OnTouchPointLeave(); // Notification for derived classes.
174
175         // Sets the button state to the default (ButtonUp).
176         mState = ButtonUp;
177         break;
178       }
179       case TouchPoint::Motion:
180       case TouchPoint::Stationary: // FALLTHROUGH
181       {
182         // Nothing to do
183         break;
184       }
185       default:
186       {
187         DALI_ASSERT_ALWAYS( !"Point status unhandled." );
188         break;
189       }
190     }
191   }
192   else if( 1 < event.GetPointCount() )
193   {
194     OnTouchPointLeave(); // Notification for derived classes.
195
196     // Sets the button state to the default (ButtonUp).
197     mState = ButtonUp;
198   }
199
200   return false;
201 }
202
203 void Button::OnInitialize()
204 {
205   // Initialize the painter and notifies subclasses.
206   Toolkit::Button handle( GetOwner() );
207   if( mPainter )
208   {
209     mPainter->Initialize( handle );
210   }
211
212   Actor self = Self();
213
214   mTapDetector = TapGestureDetector::New();
215   mTapDetector.Attach( self );
216   mTapDetector.DetectedSignal().Connect(this, &Button::OnTap);
217
218   OnButtonInitialize();
219
220   self.SetKeyboardFocusable( true );
221 }
222
223 void Button::OnControlSizeSet(const Vector3& targetSize)
224 {
225   Toolkit::Button handle( GetOwner() );
226   if( mPainter )
227   {
228     mPainter->SetSize( handle, targetSize );
229   }
230 }
231
232 void Button::OnTap(Actor actor, TapGesture tap)
233 {
234   // Do nothing.
235 }
236
237 void Button::OnControlStageDisconnection()
238 {
239   OnButtonStageDisconnection(); // Notification for derived classes.
240   mState = ButtonUp;
241 }
242
243 void Button::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
244 {
245   Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
246
247   if ( button && ( index == Toolkit::Button::PROPERTY_DIMMED ) )
248   {
249     GetImplementation( button ).SetDimmed( value.Get<bool>() );
250   }
251 }
252
253 Property::Value Button::GetProperty( BaseObject* object, Property::Index propertyIndex )
254 {
255   Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
256
257   if ( button && ( propertyIndex == Toolkit::Button::PROPERTY_DIMMED ) )
258   {
259     return Property::Value( GetImplementation( button ).mDimmed );
260   }
261
262   return Property::Value();
263 }
264
265 } // namespace Internal
266
267 } // namespace Toolkit
268
269 } // namespace Dali