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>
34 const float TOUCH_OPACITY_THRESHOLD = 0.1f;
50 // empty handle, as RotatingSelector takes children during construction
51 return Toolkit::RotatingSelector();
53 TypeRegistration mType( typeid(Toolkit::RotatingSelector), typeid(Toolkit::Control), Create );
55 const Quaternion ROTATION_ANGLE(0.0f, Vector3(1.0f, 0.0f, 0.0f));
58 Dali::Toolkit::RotatingSelector RotatingSelector::New(Actor& unSelectedActor, Actor& selectedActor)
60 // Create the implementation, temporarily owned on stack
61 IntrusivePtr< RotatingSelector > customCheckActor = new RotatingSelector;
63 // Pass ownership to CustomActor
64 Dali::Toolkit::RotatingSelector handle( *customCheckActor );
66 // Second-phase init of the implementation
67 // This can only be done after the CustomActor connection has been made...
68 customCheckActor->Initialize();
70 customCheckActor->SetSelectedActor(selectedActor);
71 customCheckActor->SetUnSelectedActor(unSelectedActor);
76 void RotatingSelector::OnInitialize()
78 mContainer = Actor::New();
79 mContainer.SetName("Selector Container");
80 mUnSelectedActor = Actor::New();
81 mSelectedActor = Actor::New();
83 mRotateAnimation = Animation::New(0.5f);
84 mRotateAnimation.FinishedSignal().Connect(this, &RotatingSelector::AnimationCompleted);
86 mUnSelectedActor.SetParentOrigin(ParentOrigin::CENTER);
87 mUnSelectedActor.SetAnchorPoint(AnchorPoint::CENTER);
89 mSelectedActor.SetParentOrigin(ParentOrigin::CENTER);
90 mSelectedActor.SetAnchorPoint(AnchorPoint::CENTER);
92 mContainer.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION_PLUS_LOCAL_POSITION );
94 Constraint constraint = Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() );
95 mSelectedActor.ApplyConstraint(constraint);
96 mUnSelectedActor.ApplyConstraint(constraint);
97 mContainer.ApplyConstraint(constraint);
99 mContainer.Add(mUnSelectedActor);
100 mSelectedActor.SetRotation(Radian(Math::PI), Vector3::XAXIS );
102 mUnSelectedActor.SetName("RotatingSelector : UnSelectedActor");
103 mSelectedActor.SetName("RotatingSelector : SelectedActor");
105 Self().Add(mContainer);
106 Self().SetLeaveRequired(true);
108 mRotateAnimation.RotateBy(mContainer, Radian(Math::PI), Vector3(1.0f, 0.0f, 0.0f));
111 RotatingSelector::RotatingSelector()
112 : Control( REQUIRES_TOUCH_EVENTS ),
120 RotatingSelector::~RotatingSelector()
122 mRotateAnimation.Reset();
125 void RotatingSelector::SetSelected( bool toggle )
127 if(toggle != mSelected)
131 ToggleAndAnimateSelection();
136 void RotatingSelector::SetSelectedActor( Actor& selectedActor )
138 unsigned int numChildren = mSelectedActor.GetChildCount();
139 for( unsigned int i=0; i<numChildren; ++i )
141 Actor actor = mSelectedActor.GetChildAt(i);
142 mSelectedActor.Remove(actor);
145 mSelectedActor.Add(selectedActor);
148 Actor RotatingSelector::GetSelectedActor()
150 return mSelectedActor.GetChildAt(0);
153 void RotatingSelector::SetUnSelectedActor( Actor& unSelectedActor )
155 unsigned int numChildren = mUnSelectedActor.GetChildCount();
157 for(unsigned int i=0; i<numChildren; ++i)
159 mUnSelectedActor.Remove(mUnSelectedActor.GetChildAt(0));
162 mUnSelectedActor.Add(unSelectedActor);
165 Actor RotatingSelector::GetUnSelectedActor()
167 return mUnSelectedActor.GetChildAt(0);
170 void RotatingSelector::SetSelectable( bool selectable )
172 mSelectable = selectable;
175 Toolkit::RotatingSelector::SelectedSignalV2& RotatingSelector::SelectedSignal()
177 return mCheckedSignalV2;
180 bool RotatingSelector::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
182 Dali::BaseHandle handle( object );
184 bool connected( true );
185 Toolkit::RotatingSelector selector = Toolkit::RotatingSelector::DownCast(handle);
187 if( Toolkit::RotatingSelector::SIGNAL_CHECKED == signalName )
189 selector.SelectedSignal().Connect( tracker, functor );
193 // signalName does not match any signal
200 bool RotatingSelector::OnTouchEvent(const TouchEvent& event)
202 if( 1 == event.GetPointCount() )
204 switch( event.GetPoint(0).state )
206 case TouchPoint::Down:
207 if(Self().GetCurrentOpacity() > TOUCH_OPACITY_THRESHOLD)
212 case TouchPoint::Leave:
217 if(mSelectable && mPressed)
219 ToggleAndAnimateSelection();
229 return false; // dont consume
232 void RotatingSelector::ToggleAndAnimateSelection()
236 mSelected = !mSelected;
239 //The checked image (i.e mSelectedActor should be in front)
240 mSelectedActor.SetPosition(0.0f, 0.0f, -1.0f);
241 mContainer.Add(mSelectedActor);
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);
251 mRotateAnimation.Play();
255 void RotatingSelector::AnimationCompleted( Animation& animation )
259 //The checked image (i.e mSelectedActor should be in front)
260 mSelectedActor.SetPosition(0.0f, 0.0f, 0.0f);
261 mContainer.Remove(mUnSelectedActor);
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);
271 mIsAnimating = false;
274 Dali::Toolkit::RotatingSelector handle( GetOwner() );
275 mCheckedSignalV2.Emit( handle, mSelected );
278 } // namespace Internal
280 } // namespace Toolkit