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