2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/internal/controls/selectors/rotating-selector-impl.h>
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>
28 #include <dali-toolkit/public-api/controls/selectors/rotating-selector.h>
32 const float TOUCH_OPACITY_THRESHOLD = 0.1f;
48 // empty handle, as RotatingSelector takes children during construction
49 return Toolkit::RotatingSelector();
51 TypeRegistration mType( typeid(Toolkit::RotatingSelector), typeid(Toolkit::Control), Create );
53 const Quaternion ROTATION_ANGLE(0.0f, Vector3(1.0f, 0.0f, 0.0f));
56 Dali::Toolkit::RotatingSelector RotatingSelector::New(Actor& unSelectedActor, Actor& selectedActor)
58 // Create the implementation, temporarily owned on stack
59 IntrusivePtr< RotatingSelector > customCheckActor = new RotatingSelector;
61 // Pass ownership to CustomActor
62 Dali::Toolkit::RotatingSelector handle( *customCheckActor );
64 // Second-phase init of the implementation
65 // This can only be done after the CustomActor connection has been made...
66 customCheckActor->Initialize();
68 customCheckActor->SetSelectedActor(selectedActor);
69 customCheckActor->SetUnSelectedActor(unSelectedActor);
74 void RotatingSelector::OnInitialize()
76 mContainer = Actor::New();
77 mContainer.SetName("Selector Container");
78 mUnSelectedActor = Actor::New();
79 mSelectedActor = Actor::New();
81 mRotateAnimation = Animation::New(0.5f);
82 mRotateAnimation.FinishedSignal().Connect(this, &RotatingSelector::AnimationCompleted);
84 mUnSelectedActor.SetParentOrigin(ParentOrigin::CENTER);
85 mUnSelectedActor.SetAnchorPoint(AnchorPoint::CENTER);
87 mSelectedActor.SetParentOrigin(ParentOrigin::CENTER);
88 mSelectedActor.SetAnchorPoint(AnchorPoint::CENTER);
90 mContainer.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION_PLUS_LOCAL_POSITION );
92 Constraint constraint = Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() );
93 mSelectedActor.ApplyConstraint(constraint);
94 mUnSelectedActor.ApplyConstraint(constraint);
95 mContainer.ApplyConstraint(constraint);
97 mContainer.Add(mUnSelectedActor);
98 mSelectedActor.SetRotation(Radian(Math::PI), Vector3::XAXIS );
100 mUnSelectedActor.SetName("RotatingSelector : UnSelectedActor");
101 mSelectedActor.SetName("RotatingSelector : SelectedActor");
103 Self().Add(mContainer);
104 Self().SetLeaveRequired(true);
106 mRotateAnimation.RotateBy(mContainer, Radian(Math::PI), Vector3(1.0f, 0.0f, 0.0f));
109 RotatingSelector::RotatingSelector()
110 : Control( REQUIRES_TOUCH_EVENTS ),
118 RotatingSelector::~RotatingSelector()
120 mRotateAnimation.Reset();
123 void RotatingSelector::SetSelected( bool toggle )
125 if(toggle != mSelected)
129 ToggleAndAnimateSelection();
134 void RotatingSelector::SetSelectedActor( Actor& selectedActor )
136 unsigned int numChildren = mSelectedActor.GetChildCount();
137 for( unsigned int i=0; i<numChildren; ++i )
139 Actor actor = mSelectedActor.GetChildAt(i);
140 mSelectedActor.Remove(actor);
143 mSelectedActor.Add(selectedActor);
146 Actor RotatingSelector::GetSelectedActor()
148 return mSelectedActor.GetChildAt(0);
151 void RotatingSelector::SetUnSelectedActor( Actor& unSelectedActor )
153 unsigned int numChildren = mUnSelectedActor.GetChildCount();
155 for(unsigned int i=0; i<numChildren; ++i)
157 mUnSelectedActor.Remove(mUnSelectedActor.GetChildAt(0));
160 mUnSelectedActor.Add(unSelectedActor);
163 Actor RotatingSelector::GetUnSelectedActor()
165 return mUnSelectedActor.GetChildAt(0);
168 void RotatingSelector::SetSelectable( bool selectable )
170 mSelectable = selectable;
173 Toolkit::RotatingSelector::SelectedSignalV2& RotatingSelector::SelectedSignal()
175 return mCheckedSignalV2;
178 bool RotatingSelector::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
180 Dali::BaseHandle handle( object );
182 bool connected( true );
183 Toolkit::RotatingSelector selector = Toolkit::RotatingSelector::DownCast(handle);
185 if( Toolkit::RotatingSelector::SIGNAL_CHECKED == signalName )
187 selector.SelectedSignal().Connect( tracker, functor );
191 // signalName does not match any signal
198 bool RotatingSelector::OnTouchEvent(const TouchEvent& event)
200 if( 1 == event.GetPointCount() )
202 switch( event.GetPoint(0).state )
204 case TouchPoint::Down:
205 if(Self().GetCurrentOpacity() > TOUCH_OPACITY_THRESHOLD)
210 case TouchPoint::Leave:
215 if(mSelectable && mPressed)
217 ToggleAndAnimateSelection();
227 return false; // dont consume
230 void RotatingSelector::ToggleAndAnimateSelection()
234 mSelected = !mSelected;
237 //The checked image (i.e mSelectedActor should be in front)
238 mSelectedActor.SetPosition(0.0f, 0.0f, -1.0f);
239 mContainer.Add(mSelectedActor);
243 //The un checked image (i.e mUnSelectedActor should be in front)
244 mUnSelectedActor.SetPosition(0.0f, 0.0f, 1.0f);
245 mContainer.Add(mUnSelectedActor);
249 mRotateAnimation.Play();
253 void RotatingSelector::AnimationCompleted( Animation& animation )
257 //The checked image (i.e mSelectedActor should be in front)
258 mSelectedActor.SetPosition(0.0f, 0.0f, 0.0f);
259 mContainer.Remove(mUnSelectedActor);
264 //The un checked image (i.e mUnSelectedActor should be in front)
265 mContainer.Remove(mSelectedActor);
266 mUnSelectedActor.SetPosition(0.0f, 0.0f, 0.0f);
269 mIsAnimating = false;
272 Dali::Toolkit::RotatingSelector handle( GetOwner() );
273 mCheckedSignalV2.Emit( handle, mSelected );
276 } // namespace Internal
278 } // namespace Toolkit