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