fix Klocwork warning of unintialized data members, unneeded assignment & unreachable...
[platform/core/uifw/dali-toolkit.git] / optional / dali-toolkit / internal / controls / selectors / rotating-selector-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 <dali-toolkit/internal/controls/selectors/rotating-selector-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <algorithm>
23 #include <dali/public-api/animation/constraints.h>
24 #include <dali/public-api/events/touch-event.h>
25 #include <dali/public-api/object/type-registry.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/public-api/controls/selectors/rotating-selector.h>
29
30 using namespace std;
31
32 namespace
33 {
34 const float TOUCH_OPACITY_THRESHOLD = 0.1f;
35 } // namespace
36
37 namespace Dali
38 {
39
40 namespace Toolkit
41 {
42
43 namespace Internal
44 {
45 namespace
46 {
47 //Type registration
48 BaseHandle Create()
49 {
50   // empty handle, as RotatingSelector takes children during construction
51   return Toolkit::RotatingSelector();
52 }
53 TypeRegistration mType( typeid(Toolkit::RotatingSelector), typeid(Toolkit::Control), Create );
54
55 const Quaternion ROTATION_ANGLE(0.0f, Vector3(1.0f, 0.0f, 0.0f));
56 }
57
58 Dali::Toolkit::RotatingSelector RotatingSelector::New(Actor& unSelectedActor, Actor& selectedActor)
59 {
60   // Create the implementation, temporarily owned on stack
61   IntrusivePtr< RotatingSelector > customCheckActor = new RotatingSelector;
62
63   // Pass ownership to CustomActor
64   Dali::Toolkit::RotatingSelector handle( *customCheckActor );
65
66   // Second-phase init of the implementation
67   // This can only be done after the CustomActor connection has been made...
68   customCheckActor->Initialize();
69
70   customCheckActor->SetSelectedActor(selectedActor);
71   customCheckActor->SetUnSelectedActor(unSelectedActor);
72
73   return handle;
74 }
75
76 void RotatingSelector::OnInitialize()
77 {
78   mContainer = Actor::New();
79   mContainer.SetName("Selector Container");
80   mUnSelectedActor = Actor::New();
81   mSelectedActor = Actor::New();
82
83   mRotateAnimation = Animation::New(0.5f);
84   mRotateAnimation.FinishedSignal().Connect(this, &RotatingSelector::AnimationCompleted);
85
86   mUnSelectedActor.SetParentOrigin(ParentOrigin::CENTER);
87   mUnSelectedActor.SetAnchorPoint(AnchorPoint::CENTER);
88
89   mSelectedActor.SetParentOrigin(ParentOrigin::CENTER);
90   mSelectedActor.SetAnchorPoint(AnchorPoint::CENTER);
91
92   mContainer.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION_PLUS_LOCAL_POSITION );
93
94   Constraint constraint = Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() );
95   mSelectedActor.ApplyConstraint(constraint);
96   mUnSelectedActor.ApplyConstraint(constraint);
97   mContainer.ApplyConstraint(constraint);
98
99   mContainer.Add(mUnSelectedActor);
100   mSelectedActor.SetRotation(Radian(Math::PI), Vector3::XAXIS );
101
102   mUnSelectedActor.SetName("RotatingSelector : UnSelectedActor");
103   mSelectedActor.SetName("RotatingSelector : SelectedActor");
104
105   Self().Add(mContainer);
106   Self().SetLeaveRequired(true);
107
108   mRotateAnimation.RotateBy(mContainer, Radian(Math::PI), Vector3(1.0f, 0.0f, 0.0f));
109 }
110
111 RotatingSelector::RotatingSelector()
112 : Control( REQUIRES_TOUCH_EVENTS ),
113   mSelected(false),
114   mSelectable(true),
115   mPressed(false),
116   mIsAnimating(false)
117 {
118 }
119
120 RotatingSelector::~RotatingSelector()
121 {
122   mRotateAnimation.Reset();
123 }
124
125 void RotatingSelector::SetSelected( bool toggle )
126 {
127   if(toggle != mSelected)
128   {
129     if( mSelectable )
130     {
131       ToggleAndAnimateSelection();
132     }
133   }
134 }
135
136 void RotatingSelector::SetSelectedActor( Actor& selectedActor )
137 {
138   unsigned int numChildren = mSelectedActor.GetChildCount();
139   for( unsigned int i=0; i<numChildren; ++i )
140   {
141     Actor actor = mSelectedActor.GetChildAt(i);
142     mSelectedActor.Remove(actor);
143   }
144
145   mSelectedActor.Add(selectedActor);
146 }
147
148 Actor RotatingSelector::GetSelectedActor()
149 {
150   return mSelectedActor.GetChildAt(0);
151 }
152
153 void RotatingSelector::SetUnSelectedActor( Actor& unSelectedActor )
154 {
155   unsigned int numChildren = mUnSelectedActor.GetChildCount();
156
157   for(unsigned int i=0; i<numChildren; ++i)
158   {
159     mUnSelectedActor.Remove(mUnSelectedActor.GetChildAt(0));
160   }
161
162   mUnSelectedActor.Add(unSelectedActor);
163 }
164
165 Actor RotatingSelector::GetUnSelectedActor()
166 {
167   return mUnSelectedActor.GetChildAt(0);
168 }
169
170 void RotatingSelector::SetSelectable( bool selectable )
171 {
172   mSelectable = selectable;
173 }
174
175 Toolkit::RotatingSelector::SelectedSignalV2& RotatingSelector::SelectedSignal()
176 {
177   return mCheckedSignalV2;
178 }
179
180 bool RotatingSelector::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
181 {
182   Dali::BaseHandle handle( object );
183
184   bool connected( true );
185   Toolkit::RotatingSelector selector = Toolkit::RotatingSelector::DownCast(handle);
186
187   if( Toolkit::RotatingSelector::SIGNAL_CHECKED == signalName )
188   {
189     selector.SelectedSignal().Connect( tracker, functor );
190   }
191   else
192   {
193     // signalName does not match any signal
194     connected = false;
195   }
196
197   return connected;
198 }
199
200 bool RotatingSelector::OnTouchEvent(const TouchEvent& event)
201 {
202   if( 1 == event.GetPointCount() )
203   {
204     switch( event.GetPoint(0).state )
205     {
206       case TouchPoint::Down:
207         if(Self().GetCurrentOpacity() > TOUCH_OPACITY_THRESHOLD)
208         {
209           mPressed = true;
210         }
211         break;
212       case TouchPoint::Leave:
213         mPressed = false;
214         break;
215       case TouchPoint::Up:
216       {
217         if(mSelectable && mPressed)
218         {
219           ToggleAndAnimateSelection();
220         }
221         mPressed = false;
222         break;
223       }
224       default:
225         break;
226     }
227   }
228
229  return false; // dont consume
230 }
231
232 void RotatingSelector::ToggleAndAnimateSelection()
233 {
234   if(!mIsAnimating)
235   {
236     mSelected = !mSelected;
237     if(mSelected)
238     {
239       //The checked image (i.e mSelectedActor should be in front)
240       mSelectedActor.SetPosition(0.0f, 0.0f, -1.0f);
241       mContainer.Add(mSelectedActor);
242     }
243     else
244     {
245       //The un checked image (i.e mUnSelectedActor should be in front)
246       mUnSelectedActor.SetPosition(0.0f, 0.0f, 1.0f);
247       mContainer.Add(mUnSelectedActor);
248     }
249
250     mIsAnimating = true;
251     mRotateAnimation.Play();
252   }
253 }
254
255 void RotatingSelector::AnimationCompleted( Animation& animation )
256 {
257   if(mSelected)
258   {
259     //The checked image (i.e mSelectedActor should be in front)
260     mSelectedActor.SetPosition(0.0f, 0.0f, 0.0f);
261     mContainer.Remove(mUnSelectedActor);
262
263   }
264   else
265   {
266     //The un checked image (i.e mUnSelectedActor should be in front)
267     mContainer.Remove(mSelectedActor);
268     mUnSelectedActor.SetPosition(0.0f, 0.0f, 0.0f);
269   }
270
271   mIsAnimating = false;
272
273   //Emit signal.
274   Dali::Toolkit::RotatingSelector handle( GetOwner() );
275   mCheckedSignalV2.Emit( handle, mSelected );
276 }
277
278 } // namespace Internal
279
280 } // namespace Toolkit
281
282 } // namespace Dali