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