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 ),
119 RotatingSelector::~RotatingSelector()
121 mRotateAnimation.Reset();
124 void RotatingSelector::SetSelected( bool toggle )
126 if(toggle != mSelected)
130 ToggleAndAnimateSelection();
135 void RotatingSelector::SetSelectedActor( Actor& selectedActor )
137 unsigned int numChildren = mSelectedActor.GetChildCount();
138 for( unsigned int i=0; i<numChildren; ++i )
140 Actor actor = mSelectedActor.GetChildAt(i);
141 mSelectedActor.Remove(actor);
144 mSelectedActor.Add(selectedActor);
147 Actor RotatingSelector::GetSelectedActor()
149 return mSelectedActor.GetChildAt(0);
152 void RotatingSelector::SetUnSelectedActor( Actor& unSelectedActor )
154 unsigned int numChildren = mUnSelectedActor.GetChildCount();
156 for(unsigned int i=0; i<numChildren; ++i)
158 mUnSelectedActor.Remove(mUnSelectedActor.GetChildAt(0));
161 mUnSelectedActor.Add(unSelectedActor);
164 Actor RotatingSelector::GetUnSelectedActor()
166 return mUnSelectedActor.GetChildAt(0);
169 void RotatingSelector::SetSelectable( bool selectable )
171 mSelectable = selectable;
174 Toolkit::RotatingSelector::SelectedSignalV2& RotatingSelector::SelectedSignal()
176 return mCheckedSignalV2;
179 bool RotatingSelector::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
181 Dali::BaseHandle handle( object );
183 bool connected( true );
184 Toolkit::RotatingSelector selector = Toolkit::RotatingSelector::DownCast(handle);
186 if( Toolkit::RotatingSelector::SIGNAL_CHECKED == signalName )
188 selector.SelectedSignal().Connect( tracker, functor );
192 // signalName does not match any signal
199 bool RotatingSelector::OnTouchEvent(const TouchEvent& event)
201 if( 1 == event.GetPointCount() )
203 switch( event.GetPoint(0).state )
205 case TouchPoint::Down:
206 if(Self().GetCurrentOpacity() > TOUCH_OPACITY_THRESHOLD)
211 case TouchPoint::Leave:
216 if(mSelectable && mPressed)
218 ToggleAndAnimateSelection();
228 return false; // dont consume
231 void RotatingSelector::ToggleAndAnimateSelection()
235 mSelected = !mSelected;
238 //The checked image (i.e mSelectedActor should be in front)
239 mSelectedActor.SetPosition(0.0f, 0.0f, -1.0f);
240 mContainer.Add(mSelectedActor);
244 //The un checked image (i.e mUnSelectedActor should be in front)
245 mUnSelectedActor.SetPosition(0.0f, 0.0f, 1.0f);
246 mContainer.Add(mUnSelectedActor);
250 mRotateAnimation.Play();
254 void RotatingSelector::AnimationCompleted( Animation& animation )
258 //The checked image (i.e mSelectedActor should be in front)
259 mSelectedActor.SetPosition(0.0f, 0.0f, 0.0f);
260 mContainer.Remove(mUnSelectedActor);
265 //The un checked image (i.e mUnSelectedActor should be in front)
266 mContainer.Remove(mSelectedActor);
267 mUnSelectedActor.SetPosition(0.0f, 0.0f, 0.0f);
270 mIsAnimating = false;
273 Dali::Toolkit::RotatingSelector handle( GetOwner() );
274 mCheckedSignalV2.Emit( handle, mSelected );
277 } // namespace Internal
279 } // namespace Toolkit