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/cluster/cluster-impl.h>
23 #include <dali/public-api/animation/animation.h>
24 #include <dali/public-api/object/type-registry.h>
25 #include <dali/integration-api/debug.h>
28 #include <dali-toolkit/public-api/controls/cluster/cluster-style.h>
33 namespace // unnamed namespace
36 const float CLUSTER_STYLE_CONSTRAINT_DURATION = 1.0f;
54 Toolkit::ClusterStyleStandard s = Toolkit::ClusterStyleStandard::New(Toolkit::ClusterStyleStandard::ClusterStyle1);
55 return Toolkit::Cluster::New( s );
58 TypeRegistration mType( typeid(Toolkit::Cluster), typeid(Toolkit::Control), Create );
60 TypeAction a1(mType, Toolkit::Cluster::ACTION_EXPAND , &Cluster::DoAction);
61 TypeAction a2(mType, Toolkit::Cluster::ACTION_COLLAPSE , &Cluster::DoAction);
62 TypeAction a3(mType, Toolkit::Cluster::ACTION_TRANSFORM, &Cluster::DoAction);
66 ///////////////////////////////////////////////////////////////////////////////////////////////////
68 ///////////////////////////////////////////////////////////////////////////////////////////////////
70 Dali::Toolkit::Cluster Cluster::New(Toolkit::ClusterStyle& style)
72 // Create the implementation
73 ClusterPtr cluster(new Cluster(style));
75 // Pass ownership to CustomActor via derived handle
76 Dali::Toolkit::Cluster handle(*cluster);
78 // Second-phase init of the implementation
79 // This can only be done after the CustomActor connection has been made...
80 cluster->Initialize();
85 Cluster::Cluster(Toolkit::ClusterStyle& style)
86 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
92 void Cluster::OnInitialize()
100 void Cluster::AddChild( Actor child )
102 // automatically add child with a position at end.
103 AddChild( child, mChildren.size() );
106 void Cluster::AddChild( Actor child, unsigned int positionIndex )
108 AddChildInfo( ChildInfo(child, positionIndex) );
111 void Cluster::AddChildAt( Actor child, unsigned int index )
113 // automatically add child with a position at end.
114 AddChild( child, mChildren.size() );
117 void Cluster::AddChildAt( Actor child, unsigned int positionIndex, unsigned int index )
119 AddChildInfoAt( ChildInfo(child, positionIndex), index );
122 void Cluster::AddChildInfo( ChildInfo childInfo )
124 AddChildInfoAt(childInfo, mChildren.size());
127 void Cluster::AddChildInfoAt( ChildInfo childInfo, unsigned int index )
129 // check that the child is valid
130 DALI_ASSERT_ALWAYS( childInfo.mActor );
132 ChildInfoIter offset = index < mChildren.size() ? (mChildren.begin() + index) : mChildren.end();
133 // now perform customization on this child.
136 if(childInfo.mActor.GetParent() != Self())
138 Actor& child = childInfo.mActor;
139 const float depth = std::distance(mChildren.begin(), offset);
141 Property::Index depthProperty = child.GetPropertyIndex(Toolkit::Cluster::CLUSTER_ACTOR_DEPTH);
142 if(depthProperty == Property::INVALID_INDEX)
144 child.RegisterProperty(Toolkit::Cluster::CLUSTER_ACTOR_DEPTH, depth);
148 Self().Add( childInfo.mActor );
149 mChildren.insert( offset, childInfo );
151 // Use parent position plus relative position.
152 child.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION_PLUS_LOCAL_POSITION );
154 // remove old constraints
155 child.RemoveConstraints();
157 // apply new constraints to the child
158 mClusterStyle.ApplyStyle(child, childInfo.mPositionIndex, AlphaFunctions::EaseOut, 0.0f);
163 ChildInfoContainer mNewChildren;
164 ChildInfoIter iter = mChildren.begin();
167 for( ; iter != mChildren.end(); ++iter)
171 SetDepth(childInfo, depth);
173 // insert the new childInfo before offset.
174 mNewChildren.push_back(childInfo);
176 // copy all children except the one that we wish to move.
177 if((*iter).mActor != childInfo.mActor)
179 SetDepth(*iter, depth);
181 mNewChildren.push_back(*iter);
187 SetDepth(childInfo, depth);
188 // insert the new childInfo before offset (end).
189 mNewChildren.push_back(childInfo);
192 mChildren = mNewChildren;
194 // Todo somehow adjust their perceived depth.
198 void Cluster::SetDepth( ChildInfo& childInfo, float depth )
200 Property::Index depthProperty = childInfo.mActor.GetPropertyIndex(Toolkit::Cluster::CLUSTER_ACTOR_DEPTH);
201 childInfo.mActor.SetProperty( depthProperty, depth );
204 ChildInfo Cluster::GetChildInfoAt( unsigned int index )
206 // check if we have this position in the cluster
207 if( index < mChildren.size() )
209 // return the child handle
210 return mChildren[ index ];
213 // return an empty handle
217 Actor Cluster::GetChildAt( unsigned int index )
219 // check if we have this position in the cluster
220 if( index < mChildren.size() )
222 // return the child handle
223 return mChildren[ index ].mActor;
226 // return an empty handle
230 Actor Cluster::RemoveChildAt( unsigned int index )
232 DALI_ASSERT_ALWAYS( index < mChildren.size() );
234 ChildInfoIter iter = mChildren.begin() + index;
235 Actor child = (*iter).mActor;
236 mChildren.erase( iter );
237 Self().Remove(child);
238 // note: constraints will automatically be removed in OnControlChildRemove
243 for(ChildInfoIter iter = mChildren.begin(); iter != mChildren.end(); ++iter)
245 SetDepth(*iter, depth);
252 void Cluster::ExpandChild( unsigned int index )
254 if( index < mChildren.size() )
256 ChildInfo& childInfo = mChildren[ index ];
257 DALI_ASSERT_ALWAYS(childInfo.mActor);
259 if(!childInfo.mExpanded)
261 // expand child to a random position/angle.
262 const Vector3 clusterSize = Self().GetCurrentSize();
263 const float length = clusterSize.Length() * 0.1f;
264 const float zOffset = 50.0f;
265 const float angle = (rand()%360) * Math::PI / 180.0f;
266 Vector3 position(sin(angle) * length, -cos(angle) * length, zOffset);
267 const float scale(1.2f);
268 const float rotate = ((rand()%30) - 15) * Math::PI / 180.0f;
270 position += childInfo.mActor.GetCurrentPosition();
272 TransformChild(index,
274 Vector3::ONE * scale,
275 Quaternion(rotate, Vector3::ZAXIS),
276 AlphaFunctions::EaseOut,
282 void Cluster::ExpandAllChildren()
284 for(unsigned int index = 0;index < mChildren.size(); index++)
286 ExpandChild( index );
290 void Cluster::CollapseChild( unsigned int index, bool front )
292 if( index < mChildren.size() )
295 AlphaFunctions::EaseOut,
301 void Cluster::CollapseAllChildren( bool front )
303 for(unsigned int index = 0;index < mChildren.size(); index++)
306 AlphaFunctions::EaseOut,
312 void Cluster::TransformChild( unsigned int index, const Vector3& position, const Vector3& scale, const Quaternion& rotation, AlphaFunction alpha, const TimePeriod& period )
314 if( index < mChildren.size() )
316 ChildInfo& childInfo = mChildren[ index ];
317 DALI_ASSERT_ALWAYS(childInfo.mActor);
319 if(!childInfo.mExpanded)
321 Actor child = childInfo.mActor;
322 childInfo.mExpanded = true;
325 child.RemoveConstraints();
326 Animation animation = Animation::New(period.delaySeconds + period.durationSeconds);
327 animation.AnimateTo( Property(child, Actor::POSITION), position, AlphaFunctions::EaseOut, period);
328 animation.AnimateTo( Property(child, Actor::SCALE), scale, AlphaFunctions::EaseOut, period);
329 animation.AnimateTo( Property(child, Actor::ROTATION), rotation, AlphaFunctions::EaseOut, period);
335 void Cluster::RestoreChild( unsigned int index, AlphaFunction alpha, const TimePeriod& period, bool front )
337 if( index < mChildren.size() )
339 ChildInfo& childInfo = mChildren[ index ];
340 DALI_ASSERT_ALWAYS(childInfo.mActor);
342 if(childInfo.mExpanded)
344 Actor child = childInfo.mActor;
345 childInfo.mExpanded = false;
347 mClusterStyle.ApplyStyle( child, childInfo.mPositionIndex, alpha, period );
349 const unsigned int hideIndex = front ? mChildren.size() : 0;
350 AddChildInfoAt(childInfo, hideIndex); // move child info to the back or front of the pack.
355 void Cluster::SetBackgroundImage( Actor image )
357 // Replaces the background image.
358 if(mBackgroundImage && mBackgroundImage.GetParent())
360 mBackgroundImage.GetParent().Remove(mBackgroundImage);
363 mBackgroundImage = image;
364 Self().Add(mBackgroundImage);
366 mBackgroundImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
367 mBackgroundImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
369 UpdateBackground(0.0f);
372 void Cluster::SetTitle( Actor text )
374 // Replaces the title actor.
375 if(mTitle && mTitle.GetParent())
377 mTitle.GetParent().Remove( mTitle );
381 Self().Add( mTitle );
383 mTitle.SetAnchorPoint( AnchorPoint::TOP_LEFT );
384 mTitle.SetParentOrigin( ParentOrigin::TOP_LEFT );
389 void Cluster::SetStyle(Toolkit::ClusterStyle style)
391 unsigned int previousChildrenNum = mChildren.size();
392 mClusterStyle = style;
393 unsigned int newChildrenNum = mClusterStyle.GetMaximumNumberOfChildren();
395 // New style supports less children (remove those that no longer belong)
396 if(newChildrenNum < previousChildrenNum)
398 ChildInfoIter removeStart = mChildren.begin() + newChildrenNum;
400 for(ChildInfoIter iter = removeStart; iter != mChildren.end(); ++iter)
402 Actor child = (*iter).mActor;
403 child.RemoveConstraints();
404 Self().Remove(child);
407 mChildren.erase( removeStart, mChildren.end() );
410 // Remove constraints from previous style, and apply new style's constraints.
411 for(ChildInfoIter iter = mChildren.begin(); iter != mChildren.end(); ++iter)
416 (*iter).mActor.RemoveConstraints();
417 style.ApplyStyle( (*iter).mActor,
418 (*iter).mPositionIndex,
419 AlphaFunctions::EaseOut,
420 CLUSTER_STYLE_CONSTRAINT_DURATION );
424 UpdateBackground(CLUSTER_STYLE_CONSTRAINT_DURATION);
425 UpdateTitle(CLUSTER_STYLE_CONSTRAINT_DURATION);
428 Toolkit::ClusterStyle Cluster::GetStyle() const
430 return mClusterStyle;
433 unsigned int Cluster::GetExpandedCount() const
435 return mExpandedCount;
438 unsigned int Cluster::GetTotalCount() const
440 return mChildren.size();
443 void Cluster::UpdateBackground(float duration)
445 if (mBackgroundImage)
447 mBackgroundImage.RemoveConstraints();
448 mClusterStyle.ApplyStyleToBackground(mBackgroundImage, AlphaFunctions::EaseOut, duration);
452 void Cluster::UpdateTitle(float duration)
456 mTitle.RemoveConstraints();
457 mClusterStyle.ApplyStyleToTitle(mTitle, AlphaFunctions::EaseOut, duration);
461 void Cluster::DoExpandAction(const PropertyValueContainer& attributes)
463 if(attributes.size() >= 1)
465 for(PropertyValueConstIter iter = attributes.begin(); iter != attributes.end(); ++iter)
467 const Property::Value& value = *iter;
469 DALI_ASSERT_ALWAYS(value.GetType() == Property::FLOAT);
470 unsigned int index = value.Get<float>();
471 ExpandChild( index );
480 void Cluster::DoCollapseAction(const PropertyValueContainer& attributes)
482 if(attributes.size() >= 1)
484 for(PropertyValueConstIter iter = attributes.begin(); iter != attributes.end(); ++iter)
486 const Property::Value& value = *iter;
488 DALI_ASSERT_ALWAYS(value.GetType() == Property::FLOAT);
489 unsigned int index = value.Get<float>();
490 CollapseChild( index, false );
495 CollapseAllChildren( false );
499 void Cluster::DoTransformAction(const PropertyValueContainer& attributes)
501 DALI_ASSERT_ALWAYS(attributes.size() >= 2);
503 DALI_ASSERT_ALWAYS(attributes[0].GetType() == Property::FLOAT);
504 unsigned int index = attributes[0].Get<float>();
506 Vector3 scale(Vector3::ONE);
507 Quaternion rotation(0.0f, Vector3::ZAXIS);
509 DALI_ASSERT_ALWAYS(attributes[1].GetType() == Property::VECTOR3);
510 attributes[1].Get(position);
512 if(attributes.size()>2)
514 attributes[2].Get(scale);
517 if(attributes.size()>3)
519 attributes[3].Get(rotation);
522 // wrap index around -1 => size - 1
523 index%= mChildren.size();
525 TransformChild(index, position, scale, rotation, AlphaFunctions::EaseOut, 0.5f);
528 void Cluster::OnControlChildRemove(Actor& child)
530 child.RemoveConstraints();
533 bool Cluster::DoAction(BaseObject* object, const std::string& actionName, const std::vector<Property::Value>& attributes)
537 Dali::BaseHandle handle(object);
539 Toolkit::Cluster cluster = Toolkit::Cluster::DownCast(handle);
541 DALI_ASSERT_ALWAYS(cluster);
543 if(Toolkit::Cluster::ACTION_EXPAND == actionName)
545 GetImpl(cluster).DoExpandAction(attributes);
548 else if(Toolkit::Cluster::ACTION_COLLAPSE == actionName)
550 GetImpl(cluster).DoCollapseAction(attributes);
553 else if(Toolkit::Cluster::ACTION_TRANSFORM == actionName)
555 GetImpl(cluster).DoTransformAction(attributes);
562 } // namespace Internal
564 } // namespace Toolkit