cluster-impl.cpp 14.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
/*
 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

// CLASS HEADER
#include "cluster-impl.h"

// EXTERNAL INCLUDES
#include <algorithm>
#include <cstring> // for strcmp
#include <dali/public-api/animation/animation.h>
#include <dali/public-api/object/type-registry.h>
#include <dali/devel-api/object/type-registry-helper.h>
#include <dali/integration-api/debug.h>

// INTERNAL INCLUDES
#include "cluster-style.h"
#include "cluster-style-impl.h"

using namespace Dali;

namespace Dali
{

namespace Demo
{

namespace Internal
{

namespace
{

BaseHandle Create()
{
  Demo::ClusterStyleStandard s = Demo::ClusterStyleStandard::New( Demo::ClusterStyleStandard::ClusterStyle1 );
  return Demo::Cluster::New( s );
}

DALI_TYPE_REGISTRATION_BEGIN( Demo::Cluster, Toolkit::Control, Create )

DALI_ACTION_REGISTRATION( Demo, Cluster, "expand",    ACTION_EXPAND    )
DALI_ACTION_REGISTRATION( Demo, Cluster, "collapse",  ACTION_COLLAPSE  )
DALI_ACTION_REGISTRATION( Demo, Cluster, "transform", ACTION_TRANSFORM )

DALI_TYPE_REGISTRATION_END()

const float CLUSTER_STYLE_CONSTRAINT_DURATION = 1.0f;

}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Cluster
///////////////////////////////////////////////////////////////////////////////////////////////////

Dali::Demo::Cluster Cluster::New(Demo::ClusterStyle& style)
{
  // Create the implementation
  ClusterPtr cluster(new Cluster(style));

  // Pass ownership to CustomActor via derived handle
  Dali::Demo::Cluster handle(*cluster);

  // Second-phase init of the implementation
  // This can only be done after the CustomActor connection has been made...
  cluster->Initialize();

  return handle;
}

Cluster::Cluster(Demo::ClusterStyle& style)
: Toolkit::Internal::Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS | DISABLE_SIZE_NEGOTIATION ) ),
  mClusterStyle(style),
  mExpandedCount(0)
{
}

void Cluster::OnInitialize()
{
}

void Cluster::OnControlSizeSet( const Vector3& targetSize )
{
  mClusterSize = targetSize;
  GetImpl(mClusterStyle).SetClusterSize(targetSize);

  for(ChildInfoIter iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  {

    if((*iter).mActor)
    {
      mClusterStyle.ApplyStyle( (*iter).mActor,
                                (*iter).mPositionIndex,
                                AlphaFunction::EASE_OUT,
                                TimePeriod(0.f) );
    }
  }

  UpdateBackground(0.f);
  UpdateTitle(0.f);
}

Cluster::~Cluster()
{
}

void Cluster::AddChild( Actor child )
{
  // automatically add child with a position at end.
  AddChild( child, mChildren.size() );
}

void Cluster::AddChild( Actor child, unsigned int positionIndex )
{
  AddChildInfo( ChildInfo(child, positionIndex) );
}

void Cluster::AddChildAt( Actor child, unsigned int index )
{
  // automatically add child with a position at end.
  AddChild( child, mChildren.size() );
}

void Cluster::AddChildAt( Actor child, unsigned int positionIndex, unsigned int index )
{
  AddChildInfoAt( ChildInfo(child, positionIndex), index );
}

void Cluster::AddChildInfo( ChildInfo childInfo )
{
  AddChildInfoAt(childInfo, mChildren.size());
}

void Cluster::AddChildInfoAt( ChildInfo childInfo, unsigned int index )
{
  // check that the child is valid
  DALI_ASSERT_ALWAYS( childInfo.mActor );

  ChildInfoIter offset = index < mChildren.size() ? (mChildren.begin() + index) : mChildren.end();
  // now perform customization on this child.

  // adopt the child
  if(childInfo.mActor.GetParent() != Self())
  {
    Actor& child = childInfo.mActor;
    const float depth = std::distance(mChildren.begin(), offset);

    Property::Index depthProperty = child.GetPropertyIndex(Demo::Cluster::CLUSTER_ACTOR_DEPTH);
    if(depthProperty == Property::INVALID_INDEX)
    {
      child.RegisterProperty(Demo::Cluster::CLUSTER_ACTOR_DEPTH, depth);
    }

    // not added prior
    Self().Add( childInfo.mActor );
    mChildren.insert( offset, childInfo );

    // Use parent position plus relative position.
    child.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION_PLUS_LOCAL_POSITION );

    // remove old constraints
    child.RemoveConstraints();

    // apply new constraints to the child
    mClusterStyle.ApplyStyle(child, childInfo.mPositionIndex, AlphaFunction::EASE_OUT, TimePeriod(0.0f));
  }
  else
  {
    // already added.
    ChildInfoContainer mNewChildren;
    ChildInfoIter iter = mChildren.begin();
    float depth = 0.0f;

    for( ; iter != mChildren.end(); ++iter)
    {
      if(iter == offset)
      {
        SetDepth(childInfo, depth);
        depth++;
        // insert the new childInfo before offset.
        mNewChildren.push_back(childInfo);
      }
      // copy all children except the one that we wish to move.
      if((*iter).mActor != childInfo.mActor)
      {
        SetDepth(*iter, depth);
        depth++;
        mNewChildren.push_back(*iter);
      }
    } // end for.

    if(iter == offset)
    {
      SetDepth(childInfo, depth);
      // insert the new childInfo before offset (end).
      mNewChildren.push_back(childInfo);
    }

    mChildren = mNewChildren;

    // Todo somehow adjust their perceived depth.
  }
}

void Cluster::SetDepth( ChildInfo& childInfo, float depth )
{
  Property::Index depthProperty = childInfo.mActor.GetPropertyIndex(Demo::Cluster::CLUSTER_ACTOR_DEPTH);
  childInfo.mActor.SetProperty( depthProperty, depth );
}

ChildInfo Cluster::GetChildInfoAt( unsigned int index )
{
  // check if we have this position in the cluster
  if( index < mChildren.size() )
  {
    // return the child handle
    return mChildren[ index ];
  }

  // return an empty handle
  return ChildInfo();
}

Actor Cluster::GetChildAt( unsigned int index )
{
  // check if we have this position in the cluster
  if( index < mChildren.size() )
  {
    // return the child handle
    return mChildren[ index ].mActor;
  }

  // return an empty handle
  return Actor();
}

Actor Cluster::RemoveChildAt( unsigned int index )
{
  DALI_ASSERT_ALWAYS( index < mChildren.size() );

  ChildInfoIter iter = mChildren.begin() + index;
  Actor child = (*iter).mActor;
  mChildren.erase( iter );
  Self().Remove(child);
  // note: constraints will automatically be removed in OnControlChildRemove

  // update depths.
  float depth = 0.0f;

  for(ChildInfoIter iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  {
    SetDepth(*iter, depth);
    depth++;
  } // end for.

  return child;
}

void Cluster::ExpandChild( unsigned int index )
{
  if( index < mChildren.size() )
  {
    ChildInfo& childInfo = mChildren[ index ];
    DALI_ASSERT_ALWAYS(childInfo.mActor);

    if(!childInfo.mExpanded)
    {
      // expand child to a random position/angle.
      const Vector3 clusterSize = Self().GetCurrentSize();
      const float length = clusterSize.Length() * 0.1f;
      const float zOffset = 50.0f;
      const float angle = (rand()%360) * Math::PI / 180.0f;
      Vector3 position(sin(angle) * length, -cos(angle) * length, zOffset);
      const float scale(1.2f);
      const Radian rotate( Degree( (rand()%30) - 15 ) );

      position += childInfo.mActor.GetCurrentPosition();

      TransformChild(index,
                     position,
                     Vector3::ONE * scale,
                     Quaternion(rotate, Vector3::ZAXIS),
                     AlphaFunction::EASE_OUT,
                     TimePeriod(0.5f));
    }
  }
}

void Cluster::ExpandAllChildren()
{
  for(unsigned int index = 0;index < mChildren.size(); index++)
  {
    ExpandChild( index );
  }
}

void Cluster::CollapseChild( unsigned int index, bool front )
{
  if( index < mChildren.size() )
  {
    RestoreChild(index,
                 AlphaFunction::EASE_OUT,
                 TimePeriod(0.25f),
                 front);
  }
}

void Cluster::CollapseAllChildren( bool front )
{
  for(unsigned int index = 0;index < mChildren.size(); index++)
  {
    RestoreChild(index,
                 AlphaFunction::EASE_OUT,
                 TimePeriod(0.25f),
                 front);
  }
}

void Cluster::TransformChild( unsigned int index, const Vector3& position, const Vector3& scale, const Quaternion& rotation, AlphaFunction alpha, const TimePeriod& period )
{
  if( index < mChildren.size() )
  {
    ChildInfo& childInfo = mChildren[ index ];
    DALI_ASSERT_ALWAYS(childInfo.mActor);

    if(!childInfo.mExpanded)
    {
      Actor child = childInfo.mActor;
      childInfo.mExpanded = true;
      mExpandedCount++;

      child.RemoveConstraints();
      Animation animation = Animation::New(period.delaySeconds + period.durationSeconds);
      animation.AnimateTo( Property(child, Actor::Property::POSITION), position, AlphaFunction::EASE_OUT, period);
      animation.AnimateTo( Property(child, Actor::Property::SCALE), scale, AlphaFunction::EASE_OUT, period);
      animation.AnimateTo( Property(child, Actor::Property::ORIENTATION), rotation, AlphaFunction::EASE_OUT, period);
      animation.Play();
    }
  }
}

void Cluster::RestoreChild( unsigned int index, AlphaFunction alpha, const TimePeriod& period, bool front )
{
  if( index < mChildren.size() )
  {
    ChildInfo& childInfo = mChildren[ index ];
    DALI_ASSERT_ALWAYS(childInfo.mActor);

    if(childInfo.mExpanded)
    {
      Actor child = childInfo.mActor;
      childInfo.mExpanded = false;
      mExpandedCount--;
      mClusterStyle.ApplyStyle( child, childInfo.mPositionIndex, alpha, period );

      const unsigned int hideIndex = front ? mChildren.size() : 0;
      AddChildInfoAt(childInfo, hideIndex); // move child info to the back or front of the pack.
    }
  }
}

void Cluster::SetBackgroundImage( Actor image )
{
  // Replaces the background image.
  if(mBackgroundImage && mBackgroundImage.GetParent())
  {
    mBackgroundImage.GetParent().Remove(mBackgroundImage);
  }

  mBackgroundImage = image;
  Self().Add(mBackgroundImage);

  mBackgroundImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
  mBackgroundImage.SetParentOrigin( ParentOrigin::TOP_LEFT );

  UpdateBackground(0.0f);
}

void Cluster::SetTitle( Actor text )
{
  // Replaces the title actor.
  if(mTitle && mTitle.GetParent())
  {
    mTitle.GetParent().Remove( mTitle );
  }

  mTitle = text;
  Self().Add( mTitle );

  mTitle.SetAnchorPoint( AnchorPoint::TOP_LEFT );
  mTitle.SetParentOrigin( ParentOrigin::TOP_LEFT );

  UpdateTitle(0.0f);
}

void Cluster::SetStyle(Demo::ClusterStyle style)
{
  unsigned int previousChildrenNum = mChildren.size();
  mClusterStyle = style;
  GetImpl(mClusterStyle).SetClusterSize(mClusterSize);
  unsigned int newChildrenNum = mClusterStyle.GetMaximumNumberOfChildren();

  // New style supports less children (remove those that no longer belong)
  if(newChildrenNum < previousChildrenNum)
  {
    ChildInfoIter removeStart = mChildren.begin() + newChildrenNum;

    for(ChildInfoIter iter = removeStart; iter != mChildren.end(); ++iter)
    {
      Actor child = (*iter).mActor;
      child.RemoveConstraints();
      Self().Remove(child);
    }

    mChildren.erase( removeStart, mChildren.end() );
  }

  for(ChildInfoIter iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  {

    if((*iter).mActor)
    {
      mClusterStyle.ApplyStyle( (*iter).mActor,
                        (*iter).mPositionIndex,
                        AlphaFunction::EASE_OUT,
                        TimePeriod(CLUSTER_STYLE_CONSTRAINT_DURATION) );
    }
  }

  UpdateBackground(CLUSTER_STYLE_CONSTRAINT_DURATION);
  UpdateTitle(CLUSTER_STYLE_CONSTRAINT_DURATION);
}

Demo::ClusterStyle Cluster::GetStyle() const
{
  return mClusterStyle;
}

unsigned int Cluster::GetExpandedCount() const
{
  return mExpandedCount;
}

unsigned int Cluster::GetTotalCount() const
{
  return mChildren.size();
}

void Cluster::UpdateBackground(float duration)
{
  if (mBackgroundImage)
  {
    mClusterStyle.ApplyStyleToBackground(mBackgroundImage, AlphaFunction::EASE_OUT, TimePeriod(duration));
  }
}

void Cluster::UpdateTitle(float duration)
{
  if (mTitle)
  {
    mClusterStyle.ApplyStyleToTitle(mTitle, AlphaFunction::EASE_OUT, TimePeriod(duration));
  }
}

void Cluster::DoExpandAction(const PropertyValueContainer& attributes)
{
  if(attributes.size() >= 1)
  {
    for(PropertyValueConstIter iter = attributes.begin(); iter != attributes.end(); ++iter)
    {
      const Property::Value& value = *iter;

      DALI_ASSERT_ALWAYS(value.GetType() == Property::FLOAT);
      unsigned int index = value.Get<float>();
      ExpandChild( index );
    }
  }
  else
  {
    ExpandAllChildren();
  }
}

void Cluster::DoCollapseAction(const PropertyValueContainer& attributes)
{
  if(attributes.size() >= 1)
  {
    for(PropertyValueConstIter iter = attributes.begin(); iter != attributes.end(); ++iter)
    {
      const Property::Value& value = *iter;

      DALI_ASSERT_ALWAYS(value.GetType() == Property::FLOAT);
      unsigned int index = value.Get<float>();
      CollapseChild( index, false );
    }
  }
  else
  {
    CollapseAllChildren( false );
  }
}

void Cluster::DoTransformAction(const PropertyValueContainer& attributes)
{
  DALI_ASSERT_ALWAYS(attributes.size() >= 2);

  DALI_ASSERT_ALWAYS(attributes[0].GetType() == Property::FLOAT);
  unsigned int index = attributes[0].Get<float>();
  Vector3 position;
  Vector3 scale(Vector3::ONE);
  Quaternion rotation( Dali::ANGLE_0, Vector3::ZAXIS );

  DALI_ASSERT_ALWAYS(attributes[1].GetType() == Property::VECTOR3);
  attributes[1].Get(position);

  if(attributes.size()>2)
  {
    attributes[2].Get(scale);
  }

  if(attributes.size()>3)
  {
    attributes[3].Get(rotation);
  }

  // wrap index around -1 => size - 1
  index%= mChildren.size();

  TransformChild(index, position, scale, rotation, AlphaFunction::EASE_OUT, TimePeriod(0.5f));
}

void Cluster::OnControlChildRemove(Actor& child)
{
  child.RemoveConstraints();
}

bool Cluster::DoAction(BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes)
{
  bool ret = false;

  Dali::BaseHandle handle( object );

  Demo::Cluster cluster = Demo::Cluster::DownCast( handle );

  DALI_ASSERT_ALWAYS( cluster );

  if( 0 == strcmp( actionName.c_str(), ACTION_EXPAND ) )
  {
    GetImpl( cluster ).DoExpandAction( attributes );
    ret = true;
  }
  else if( 0 == strcmp( actionName.c_str(), ACTION_COLLAPSE ) )
  {
    GetImpl( cluster ).DoCollapseAction( attributes );
    ret = true;
  }
  else if( 0 == strcmp( actionName.c_str(), ACTION_TRANSFORM ) )
  {
    GetImpl( cluster ).DoTransformAction( attributes );
    ret = true;
  }

  return ret;
}

} // namespace Internal

} // namespace Demo

} // namespace Dali