animated-gradient-card-active.cpp 31.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 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
/*
* Copyright (c) 2020 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.
*
*/
#include <dali-toolkit/dali-toolkit.h>
#include <dali-toolkit/devel-api/visuals/animated-gradient-visual-properties-devel.h>
#include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
#include <dali/dali.h>
#include <dali/integration-api/debug.h>
#include <iostream>

using namespace Dali;
using namespace Dali::Toolkit;

// This example shows how to create and display animated-gradient-effect
//
namespace
{
// The value for scale-change between wearable-mobile
// Can be changed on App-Create time
Vector2 WINDOW_SIZE          = Vector2(360.0f, 360.0f);
Vector2 SCALED_WINDOW_SIZE   = Vector2(1.0f, 1.0f);
Vector3 SCALED_WINDOW_SIZE_3 = Vector3(1.0f, 1.0f, 0.0f);
float   SCALED_WIDTH         = 1.0f;
float   SCALED_HEIGHT        = 1.0f;
float   FONT_SCALE           = 0.25f;

// const parameters for color and animation
const float   CHANGE_DURATION            = 0.2f;
const float   CARD_MOVE_DURATION         = 1.0f;
const float   LOADING_ONE_CYCLE_DURATION = 2.0f;
const int     LOADING_CYCLE_CNT          = 4;
const int     LOADING_CYCLE_DT           = 10;
const Vector4 DEFAULT_COLOR              = Vector4(0.5f, 0.5f, 0.5f, 1.0f);

const float PSEUDO_SCROLL_TIME   = 100.0f;
const float PSEUDO_SCROLL_OFFSET = 0.05f;

// const static parameters for cards when resolution is 360x360
const int     CARD_NUM                   = 3;
const Vector2 CARD_SIZE                  = Vector2(210.0f, 143.0f);
const Vector2 CARD_OFFSET                = Vector2(0.0f, 12.5f);
const Vector2 CARD_DIFF                  = Vector2(240.0f, 0.0f);
const float   CARD_BOUNCE_ANIMATION_RATE = 0.3f;

const Vector2 CARD_SIZE_BIG   = Vector2(292.0f, 199.0f);
const Vector2 CARD_BIG_OFFSET = Vector2(0.0f, -5.5f);
const float   CARD_MOVE_DIST  = 40.0f;

// const private parameters for each cards
const Vector4 CARD_COLOR_START_LIST[] =
  {
    Vector4(0x24, 0x2C, 0x93, 255.f) / 255.f,
    Vector4(0x7A, 0x1C, 0x9E, 255.f) / 255.f,
    Vector4(0xA9, 0x0C, 0x96, 255.f) / 255.f,
};
const Vector4 CARD_COLOR_END_LIST[] =
  {
    Vector4(0x04, 0x13, 0x23, 255.f) / 255.f,
    Vector4(0x28, 0x01, 0x45, 255.f) / 255.f,
    Vector4(0x37, 0x0A, 0x2E, 255.f) / 255.f,
};
const Vector4 CARD_COLOR_BACKGROUND_LIST[] =
  {
    Vector4(0x28, 0x2B, 0x6E, 255.f) / 255.f,
    Vector4(0x4D, 0x15, 0x61, 255.f) / 255.f,
    Vector4(0x70, 0x21, 0x61, 255.f) / 255.f,
};
const char* const CARD_IMAGE_LIST[] =
  {
    (DEMO_IMAGE_DIR "Card_01.png"),
    (DEMO_IMAGE_DIR "Card_02.png"),
    (DEMO_IMAGE_DIR "Card_03.png"),
};

// const parameters for add button position and size when resolution is 360x360x
const Vector2     BUTTON_ADD_SIZE     = Vector2(292.0f, 52.0f);
const Vector3     BUTTON_ADD_POSITION = Vector3(0.0f, 0.0f, 0.0f);
const char* const BUTTON_ADD_IMAGE(DEMO_IMAGE_DIR "Card_Add_Button.png");

// const parameters for string position and size and font-size when resolution is 360x360
const Vector2 LABEL_TICKET_SIZE         = Vector2(148.0f, 31.0f);
const Vector3 LABEL_TICKET_POSITION     = Vector3(0.0f, 72.5f, 0.0f);
const Vector4 LABEL_TICKET_FONT_COLOR   = Vector4(0.98f, 0.98f, 0.98f, 1.0f);
const float   LABEL_TICKET_FONT_SIZE    = 25.0f;
const Vector2 LABEL_HOLD_SIZE           = Vector2(180.0f, 60.0f);
const Vector3 LABEL_HOLD_POSITION       = Vector3(1.0f, 103.0f, 0.0f);
const Vector4 LABEL_HOLD_FONT_COLOR     = Vector4(0.98f, 0.98f, 0.98f, 1.0f);
const float   LABEL_HOLD_FONT_SIZE      = 25.0f;
const Vector2 LABEL_TERMINAL_SIZE       = Vector2(180.0f, 60.0f);
const Vector3 LABEL_TERMINAL_POSITION   = Vector3(1.0f, 133.0f, 0.0f);
const Vector4 LABEL_TERMINAL_FONT_COLOR = Vector4(0.98f, 0.98f, 0.98f, 1.0f);
const float   LABEL_TERMINAL_FONT_SIZE  = 25.0f;

// string string
const char* const LABEL_TICKET_STR("Select Ticket");
const char* const LABEL_HOLD_STR("Hold near");
const char* const LABEL_TERMINAL_STR("terminal");

class CardManager
{
public:
  CardManager()
  : mSize(0.0f, 0.0f),
    mOffset(0.0f, 0.0f),
    mDiff(0.0f, 0.0f),
    mCurIndex(0),
    mCurState(0)
  {
  }
  ~CardManager()
  {
  }

  void Init(Window& window)
  {
    mSize   = CARD_SIZE * SCALED_WIDTH;
    mOffset = CARD_OFFSET * SCALED_WIDTH;
    mDiff   = CARD_DIFF * SCALED_WIDTH;

    mCurIndex = 0;
    mCurState = 0;

    for(int k = 0; k < CARD_NUM; k++)
    {
      mPosition[k] = mOffset + mDiff * k;

      mColorStart[k]      = CARD_COLOR_START_LIST[k];
      mColorEnd[k]        = CARD_COLOR_END_LIST[k];
      mColorBackground[k] = CARD_COLOR_BACKGROUND_LIST[k];

      mImageUrl[k] = CARD_IMAGE_LIST[k];

      // Create an image view for this item
      mCard[k] = ImageView::New();
      {
        Property::Map propertyMap;
        propertyMap.Insert(Visual::Property::TYPE, Visual::IMAGE);
        propertyMap.Insert(ImageVisual::Property::URL, mImageUrl[k]);
        propertyMap.Insert(DevelVisual::Property::VISUAL_FITTING_MODE, DevelVisual::FILL);
        mCard[k].SetProperty(Toolkit::ImageView::Property::IMAGE, propertyMap);
      }

      mCard[k].SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
      mCard[k].SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
      mCard[k].SetProperty(Actor::Property::SIZE, Vector2(mSize.x, mSize.y));
      mCard[k].SetProperty(Actor::Property::POSITION, Vector2(mPosition[k].x, mPosition[k].y));

      window.Add(mCard[k]);
    }
  }

  bool MoveRight(float duration)
  {
    Animation anim = Animation::New(duration);
    for(int k = 0; k < CARD_NUM; k++)
    {
      if(mCurIndex == 0)
      {
        anim.AnimateBy(Property(mCard[k], Actor::Property::POSITION_X), mDiff.x * CARD_BOUNCE_ANIMATION_RATE, AlphaFunction::BOUNCE);
      }
      else
      {
        anim.AnimateBy(Property(mCard[k], Actor::Property::POSITION_X), mDiff.x, AlphaFunction::EASE_OUT);
      }
    }
    bool res = false;
    if(mCurIndex != 0)
    {
      mCurIndex = (mCurIndex - 1) % CARD_NUM;
      res       = true;
    }
    anim.Play();
    return res;
  }

  bool MoveLeft(float duration)
  {
    Animation anim = Animation::New(duration);
    for(int k = 0; k < CARD_NUM; k++)
    {
      if(mCurIndex == CARD_NUM - 1)
      {
        anim.AnimateBy(Property(mCard[k], Actor::Property::POSITION_X), -mDiff.x * CARD_BOUNCE_ANIMATION_RATE, AlphaFunction::BOUNCE);
      }
      else
      {
        anim.AnimateBy(Property(mCard[k], Actor::Property::POSITION_X), -mDiff.x, AlphaFunction::EASE_OUT);
      }
    }
    bool res = false;
    if(mCurIndex != CARD_NUM - 1)
    {
      mCurIndex = (mCurIndex + 1) % CARD_NUM;
      res       = true;
    }
    anim.Play();
    return res;
  }

  Vector4 GetColorStart(int index)
  {
    DALI_ASSERT_ALWAYS(index >= 0 && index < CARD_NUM);
    return mColorStart[index];
  }

  Vector4 GetColorEnd(int index)
  {
    DALI_ASSERT_ALWAYS(index >= 0 && index < CARD_NUM);
    return mColorEnd[index];
  }

  Vector4 GetColorBackground(int index)
  {
    DALI_ASSERT_ALWAYS(index >= 0 && index < CARD_NUM);
    return mColorBackground[index];
  }

  ImageView& operator[](int index)
  {
    DALI_ASSERT_ALWAYS(index >= 0 && index < CARD_NUM);
    return mCard[index];
  }

  ImageView   mCard[CARD_NUM];
  std::string mImageUrl[CARD_NUM];
  Vector4     mColorStart[CARD_NUM];
  Vector4     mColorEnd[CARD_NUM];
  Vector4     mColorBackground[CARD_NUM];
  Vector2     mPosition[CARD_NUM];
  Vector2     mSize;
  Vector2     mOffset;
  Vector2     mDiff;
  int         mCurIndex;
  int         mCurState;
};

} // unnamed namespace

// This example shows how to render animated gradients
//
class CardController : public ConnectionTracker
{
public:
  CardController(Application& application)
  : mApplication(application),
    mNormalColor(0.0f, 0.0f, 0.0f, 0.0f),
    mNormalStartColor(0.0f, 0.0f, 0.0f, 0.0f),
    mNormalEndColor(0.0f, 0.0f, 0.0f, 0.0f),
    mFirstTouchPos(0.0f, 0.0f),
    mLastTouchPos(0.0f, 0.0f),
    mCardDuration(0.0f),
    mDuration(0.0f),
    mLoadingTime(0.0f),
    mLoadingCount(0),
    mLoadingCountScale(0),
    mTickCount(0),
    mCancelSignal(false),
    mIsTouchedActor(false)
  {
    // Connect to the Application's Init signal
    mApplication.InitSignal().Connect(this, &CardController::Create);
  }

  ~CardController()
  {
    // Nothing to do here;
  }

  // The Init signal is received once (only) during the Application lifetime
  void Create(Application& application)
  {
    // Get a handle to the window
    mWindow = application.GetWindow();
    mWindow.KeyEventSignal().Connect(this, &CardController::OnKeyEvent);

    // Get current device's width and height.
    const Window::WindowSize windowSize = mWindow.GetSize();
    WINDOW_SIZE                         = Vector2(windowSize.GetWidth(), windowSize.GetHeight());
    SCALED_WINDOW_SIZE                  = WINDOW_SIZE / 360.0f;
    SCALED_WINDOW_SIZE_3                = Vector3(SCALED_WINDOW_SIZE.x, SCALED_WINDOW_SIZE.y, 0.0f);
    SCALED_WIDTH                        = SCALED_WINDOW_SIZE.x < SCALED_WINDOW_SIZE.y ? SCALED_WINDOW_SIZE.x : SCALED_WINDOW_SIZE.y;
    SCALED_HEIGHT                       = SCALED_WIDTH;

    // Note that this is heuristic value
    FONT_SCALE = 0.25f * WINDOW_SIZE.y / WINDOW_SIZE.x;

    mBackground = Control::New();
    mBackground.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
    mBackground.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
    mBackground.SetProperty(Actor::Property::SIZE, WINDOW_SIZE);

    mWindow.Add(mBackground);

    BuildParameter();
    InitMap();
    SetupCards();
    SetupActors();
    SetupAnimation();

    mWindow.GetRootLayer().TouchedSignal().Connect(this, &CardController::OnTouchLayer);
    Reset();
  }

  bool OnTouchCards(Actor actor, const TouchEvent& data)
  {
    if(data.GetPointCount() > 0)
    {
      if(data.GetState(0) == PointState::DOWN)
      {
        if(mCards.mCurState == 0)
        {
          mIsTouchedActor = false;
          if(mCards[mCards.mCurIndex] == actor)
          {
            mIsTouchedActor = true;
          }
          mCards.mCurState = 3;
          mTempTimer       = Timer::New(PSEUDO_SCROLL_TIME);
          mTempTimer.TickSignal().Connect(this, &CardController::OnDetectMotionLayer);
          mTempTimer.Start();

          mFirstTouchPos = data.GetScreenPosition(0);
          mLastTouchPos  = mFirstTouchPos;
        }
        else if(mCards.mCurState == 1)
        {
          mCancelSignal = true;
          return false;
        }
      }
      else
      {
        mLastTouchPos = data.GetScreenPosition(0);
      }
    }
    return true;
  }

  bool OnTouchLayer(Actor actor, const TouchEvent& data)
  {
    if(data.GetPointCount() > 0)
    {
      if(data.GetState(0) == PointState::DOWN)
      {
        if(mCards.mCurState == 0)
        {
          mIsTouchedActor  = false;
          mCards.mCurState = 3;
          mTempTimer       = Timer::New(PSEUDO_SCROLL_TIME);
          mTempTimer.TickSignal().Connect(this, &CardController::OnDetectMotionLayer);
          mTempTimer.Start();

          mFirstTouchPos = data.GetScreenPosition(0);
          mLastTouchPos  = mFirstTouchPos;
        }
      }
      else
      {
        mLastTouchPos = data.GetScreenPosition(0);
      }
    }
    return true;
  }

  // Heuristic Scroll View
  bool OnDetectMotionLayer()
  {
    if(mCards.mCurState == 3)
    {
      Vector2     diff        = (mLastTouchPos - mFirstTouchPos);
      float       offset      = PSEUDO_SCROLL_OFFSET;
      const float windowWidth = mWindow.GetSize().GetWidth();
      // Scroll to right
      if(diff.x > windowWidth * offset)
      {
        mCards.mCurState = 2;
        MoveRight();
        mCardChanger = Timer::New(mCardDuration * 1000.0f);
        mCardChanger.TickSignal().Connect(this, &CardController::OnTickLayer);
        mCardChanger.Start();
      }
      // Scroll to left
      else if(diff.x < -windowWidth * offset)
      {
        mCards.mCurState = 2;
        MoveLeft();
        mCardChanger = Timer::New(mCardDuration * 1000.0f);
        mCardChanger.TickSignal().Connect(this, &CardController::OnTickLayer);
        mCardChanger.Start();
      }
      // Not a scroll input
      else
      {
        // Run NFC Tag effect if we touch a card
        if(mIsTouchedActor)
        {
          mCards.mCurState = 1;
          RunAnimation(mCards.mCurIndex);
        }
        else
        {
          Reset();
        }
      }
    }
    return false;
  }

  bool OnTickLayer()
  {
    Reset();
    return false;
  }

  void OnKeyEvent(const KeyEvent& event)
  {
    if(event.GetState() == KeyEvent::DOWN)
    {
      if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
      {
        mApplication.Quit();
      }
    }
  }

private:
  // Utility function to make animation parameter map. return Property::Map
  Property::Value BuildMap(const Property::Value& start, const Property::Value& target, int dir, float duration, float delay, int repeat, float repeat_delay, int motion, int easing)
  {
    Property::Map map;

    map.Clear();
    map.Insert(DevelAnimatedGradientVisual::AnimationParameter::Property::START, start);
    map.Insert(DevelAnimatedGradientVisual::AnimationParameter::Property::TARGET, target);
    if(dir == 0)
    {
      map.Insert(DevelAnimatedGradientVisual::AnimationParameter::Property::DIRECTION, DevelAnimatedGradientVisual::AnimationParameter::DirectionType::FORWARD);
    }
    else
    {
      map.Insert(DevelAnimatedGradientVisual::AnimationParameter::Property::DIRECTION, DevelAnimatedGradientVisual::AnimationParameter::DirectionType::BACKWARD);
    }
    map.Insert(DevelAnimatedGradientVisual::AnimationParameter::Property::DURATION, duration);
    map.Insert(DevelAnimatedGradientVisual::AnimationParameter::Property::DELAY, delay);
    map.Insert(DevelAnimatedGradientVisual::AnimationParameter::Property::REPEAT, repeat);
    map.Insert(DevelAnimatedGradientVisual::AnimationParameter::Property::REPEAT_DELAY, repeat_delay);
    if(motion == 0)
    {
      map.Insert(DevelAnimatedGradientVisual::AnimationParameter::Property::MOTION_TYPE, DevelAnimatedGradientVisual::AnimationParameter::MotionType::LOOP);
    }
    else
    {
      map.Insert(DevelAnimatedGradientVisual::AnimationParameter::Property::MOTION_TYPE, DevelAnimatedGradientVisual::AnimationParameter::MotionType::MIRROR);
    }
    if(easing == 0)
    {
      map.Insert(DevelAnimatedGradientVisual::AnimationParameter::Property::EASING_TYPE, DevelAnimatedGradientVisual::AnimationParameter::EasingType::LINEAR);
    }
    else if(easing == 1)
    {
      map.Insert(DevelAnimatedGradientVisual::AnimationParameter::Property::EASING_TYPE, DevelAnimatedGradientVisual::AnimationParameter::EasingType::IN);
    }
    else if(easing == 2)
    {
      map.Insert(DevelAnimatedGradientVisual::AnimationParameter::Property::EASING_TYPE, DevelAnimatedGradientVisual::AnimationParameter::EasingType::OUT);
    }
    else
    {
      map.Insert(DevelAnimatedGradientVisual::AnimationParameter::Property::EASING_TYPE, DevelAnimatedGradientVisual::AnimationParameter::EasingType::IN_OUT);
    }

    return Property::Value(map);
  }

  // Setup background visual property during nothing action
  void InitMapNormal()
  {
    mBackgroundNormalMap.Clear();
    mBackgroundNormalMap.Insert(Visual::Property::TYPE, DevelVisual::ANIMATED_GRADIENT);

    mBackgroundNormalMap.Insert(Toolkit::DevelAnimatedGradientVisual::Property::START_POSITION, Vector2(-0.5, -0.5));
    mBackgroundNormalMap.Insert(Toolkit::DevelAnimatedGradientVisual::Property::END_POSITION, Vector2(0.5, 0.5));
    mBackgroundNormalMap.Insert(Toolkit::DevelAnimatedGradientVisual::Property::START_COLOR, mNormalColor);
    mBackgroundNormalMap.Insert(Toolkit::DevelAnimatedGradientVisual::Property::END_COLOR, mNormalColor);
    mBackgroundNormalMap.Insert(Toolkit::DevelAnimatedGradientVisual::Property::ROTATE_CENTER, Vector2(0.0f, 0.0f));
    mBackgroundNormalMap.Insert(Toolkit::DevelAnimatedGradientVisual::Property::ROTATE_AMOUNT, 0.0f);
    mBackgroundNormalMap.Insert(Toolkit::DevelAnimatedGradientVisual::Property::OFFSET, 0.0f);
  }

  // Setup background visual property during NFC tagging start
  void InitMapStart()
  {
    mBackgroundMapStart.Clear();
    mBackgroundMapStart.Insert(Visual::Property::TYPE, DevelVisual::ANIMATED_GRADIENT);

    mBackgroundMapStart.Insert(Toolkit::DevelAnimatedGradientVisual::Property::START_POSITION, Vector2(-0.5, -0.5));
    mBackgroundMapStart.Insert(Toolkit::DevelAnimatedGradientVisual::Property::END_POSITION, Vector2(0.5, 0.5));
    mBackgroundMapStart.Insert(Toolkit::DevelAnimatedGradientVisual::Property::START_COLOR, mNormalColor);
    mBackgroundMapStart.Insert(Toolkit::DevelAnimatedGradientVisual::Property::END_COLOR, mNormalColor);
    mBackgroundMapStart.Insert(Toolkit::DevelAnimatedGradientVisual::Property::ROTATE_CENTER, Vector2(0.0f, 0.0f));
    mBackgroundMapStart.Insert(Toolkit::DevelAnimatedGradientVisual::Property::ROTATE_AMOUNT, 0.0f);
    mBackgroundMapStart.Insert(Toolkit::DevelAnimatedGradientVisual::Property::OFFSET, BuildMap(0.0f, 2.0f, 0, mLoadingTime, 0.0f, -1, 0.0f, 0, 0));

    mColorAnimationStartStart = *(BuildMap(mNormalColor, Vector4(0, 0, 0, 0), 0, mDuration, 0.0f, 1, 0.0f, 0, 0).GetMap());
    mColorAnimationStartEnd   = *(BuildMap(mNormalColor, Vector4(0, 0, 0, 0), 0, mDuration, 0.0f, 1, 0.0f, 0, 0).GetMap());
  }

  // Setup background visual property during NFC tagging end
  void InitMapEnd()
  {
    mBackgroundMapEnd.Clear();
    mBackgroundMapEnd.Insert(Visual::Property::TYPE, DevelVisual::ANIMATED_GRADIENT);

    mBackgroundMapEnd.Insert(Toolkit::DevelAnimatedGradientVisual::Property::START_POSITION, Vector2(-0.5, -0.5));
    mBackgroundMapEnd.Insert(Toolkit::DevelAnimatedGradientVisual::Property::END_POSITION, Vector2(0.5, 0.5));
    mBackgroundMapEnd.Insert(Toolkit::DevelAnimatedGradientVisual::Property::START_COLOR, mNormalColor);
    mBackgroundMapEnd.Insert(Toolkit::DevelAnimatedGradientVisual::Property::END_COLOR, mNormalColor);
    mBackgroundMapEnd.Insert(Toolkit::DevelAnimatedGradientVisual::Property::ROTATE_CENTER, Vector2(0.0f, 0.0f));
    mBackgroundMapEnd.Insert(Toolkit::DevelAnimatedGradientVisual::Property::ROTATE_AMOUNT, 0.0f);
    mBackgroundMapEnd.Insert(Toolkit::DevelAnimatedGradientVisual::Property::OFFSET, BuildMap(0.0f, 2.0f, 0, mLoadingTime, 0.0f, -1, 0.0f, 0, 0));

    mColorAnimationEndStart = *(BuildMap(mNormalColor, Vector4(0, 0, 0, 0), 1, mDuration, 0.0f, 1, 0.0f, 0, 0).GetMap());
    mColorAnimationEndEnd   = *(BuildMap(mNormalColor, Vector4(0, 0, 0, 0), 1, mDuration, 0.0f, 1, 0.0f, 0, 0).GetMap());
  }

  // Setup background visual property during card change
  void InitMapMove()
  {
    mBackgroundMapMove.Clear();
    mBackgroundMapMove.Insert(Visual::Property::TYPE, DevelVisual::ANIMATED_GRADIENT);
    mBackgroundMapMove.Insert(Toolkit::DevelAnimatedGradientVisual::Property::SPREAD_TYPE, Toolkit::DevelAnimatedGradientVisual::SpreadType::CLAMP);

    mBackgroundMapMove.Insert(Toolkit::DevelAnimatedGradientVisual::Property::START_POSITION, Vector2(-0.5, 0.0));
    mBackgroundMapMove.Insert(Toolkit::DevelAnimatedGradientVisual::Property::END_POSITION, Vector2(0.5, 0.0));
    mBackgroundMapMove.Insert(Toolkit::DevelAnimatedGradientVisual::Property::START_COLOR, mNormalStartColor);
    mBackgroundMapMove.Insert(Toolkit::DevelAnimatedGradientVisual::Property::END_COLOR, mNormalEndColor);
    mBackgroundMapMove.Insert(Toolkit::DevelAnimatedGradientVisual::Property::ROTATE_CENTER, Vector2(0.0f, 0.0f));
    mBackgroundMapMove.Insert(Toolkit::DevelAnimatedGradientVisual::Property::ROTATE_AMOUNT, 0.0f);
    mBackgroundMapMove.Insert(Toolkit::DevelAnimatedGradientVisual::Property::OFFSET, BuildMap(-1.0f, 1.0f, 0, mCardDuration, 0.0f, 1, 0.0f, 0, 2));
  }

  void InitMap()
  {
    InitMapNormal();
    InitMapStart();
    InitMapEnd();
    InitMapMove();
  }

  // Setup const parameter values
  void BuildParameter()
  {
    mNormalColor       = DEFAULT_COLOR;
    mCardDuration      = CARD_MOVE_DURATION;
    mDuration          = CHANGE_DURATION;
    mLoadingTime       = LOADING_ONE_CYCLE_DURATION;
    mLoadingCount      = LOADING_CYCLE_CNT;
    mLoadingCountScale = LOADING_CYCLE_DT;

    mNormalStartColor = mNormalColor;
    mNormalEndColor   = mNormalColor;
  }

  void BuildAnimation()
  {
    InitMap();
  }

  void SetupCards()
  {
    mCards.Init(mWindow);
    for(int k = 0; k < CARD_NUM; k++)
    {
      mCards[k].TouchedSignal().Connect(this, &CardController::OnTouchCards);
    }
    mNormalStartColor = mCards.GetColorBackground(mCards.mCurIndex);
    mNormalEndColor   = mCards.GetColorBackground(mCards.mCurIndex);
  }

  // Create and Add to window
  void SetupActors()
  {
    mAddButton = ImageView::New();
    mAddButton.SetImage(BUTTON_ADD_IMAGE);
    mAddButton.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER);
    mAddButton.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER);
    mAddButton.SetProperty(Actor::Property::SIZE, BUTTON_ADD_SIZE * SCALED_WIDTH);
    mAddButton.SetProperty(Actor::Property::POSITION, BUTTON_ADD_POSITION * SCALED_WIDTH);

    mLabel1 = TextLabel::New(LABEL_TICKET_STR);
    mLabel1.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER);
    mLabel1.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER);
    mLabel1.SetProperty(Actor::Property::SIZE, LABEL_TICKET_SIZE * SCALED_WIDTH);
    mLabel1.SetProperty(Actor::Property::POSITION, LABEL_TICKET_POSITION * SCALED_WIDTH);
    mLabel1.SetProperty(Actor::Property::VISIBLE, true);
    mLabel1.SetProperty(TextLabel::Property::TEXT_COLOR, LABEL_TICKET_FONT_COLOR);
    mLabel1.SetProperty(TextLabel::Property::POINT_SIZE, LABEL_TICKET_FONT_SIZE * FONT_SCALE);
    mLabel1.SetProperty(TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER");
    mLabel1.SetProperty(TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER");

    mLabel2 = TextLabel::New(LABEL_HOLD_STR);
    mLabel2.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
    mLabel2.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
    mLabel2.SetProperty(Actor::Property::SIZE, LABEL_HOLD_SIZE * SCALED_WIDTH);
    mLabel2.SetProperty(Actor::Property::POSITION, LABEL_HOLD_POSITION * SCALED_WIDTH);
    mLabel2.SetProperty(Actor::Property::VISIBLE, false);
    mLabel2.SetProperty(TextLabel::Property::TEXT_COLOR, LABEL_HOLD_FONT_COLOR);
    mLabel2.SetProperty(TextLabel::Property::POINT_SIZE, LABEL_HOLD_FONT_SIZE * FONT_SCALE);
    mLabel2.SetProperty(TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER");
    mLabel2.SetProperty(TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER");

    mLabel3 = TextLabel::New(LABEL_TERMINAL_STR);
    mLabel3.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
    mLabel3.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
    mLabel3.SetProperty(Actor::Property::SIZE, LABEL_TERMINAL_SIZE * SCALED_WIDTH);
    mLabel3.SetProperty(Actor::Property::POSITION, LABEL_TERMINAL_POSITION * SCALED_WIDTH);
    mLabel3.SetProperty(Actor::Property::VISIBLE, false);
    mLabel3.SetProperty(TextLabel::Property::TEXT_COLOR, LABEL_TERMINAL_FONT_COLOR);
    mLabel3.SetProperty(TextLabel::Property::POINT_SIZE, LABEL_TERMINAL_FONT_SIZE * FONT_SCALE);
    mLabel3.SetProperty(TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER");
    mLabel3.SetProperty(TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER");

    mWindow.Add(mAddButton);
    mWindow.Add(mLabel1);
    mWindow.Add(mLabel2);
    mWindow.Add(mLabel3);
  }

  void SetupAnimation()
  {
    mMoveFront = Animation::New(mDuration);
    mMoveBack  = Animation::New(mDuration);
  }

  // Run animations when 'index' card active
  void RunAnimation(int index)
  {
    //set animated background color here
    mColorAnimationStartStart[DevelAnimatedGradientVisual::AnimationParameter::Property::START] = mNormalStartColor;
    mColorAnimationStartEnd[DevelAnimatedGradientVisual::AnimationParameter::Property::START]   = mNormalStartColor;
    mColorAnimationEndStart[DevelAnimatedGradientVisual::AnimationParameter::Property::START]   = mNormalStartColor;
    mColorAnimationEndEnd[DevelAnimatedGradientVisual::AnimationParameter::Property::START]     = mNormalStartColor;

    mColorAnimationStartStart[DevelAnimatedGradientVisual::AnimationParameter::Property::TARGET] = mCards.GetColorStart(index);
    mColorAnimationStartEnd[DevelAnimatedGradientVisual::AnimationParameter::Property::TARGET]   = mCards.GetColorEnd(index);
    mColorAnimationEndStart[DevelAnimatedGradientVisual::AnimationParameter::Property::TARGET]   = mCards.GetColorStart(index);
    mColorAnimationEndEnd[DevelAnimatedGradientVisual::AnimationParameter::Property::TARGET]     = mCards.GetColorEnd(index);

    mBackgroundMapStart[Toolkit::DevelAnimatedGradientVisual::Property::START_COLOR] = mColorAnimationStartStart;
    mBackgroundMapStart[Toolkit::DevelAnimatedGradientVisual::Property::END_COLOR]   = mColorAnimationStartEnd;
    mBackgroundMapEnd[Toolkit::DevelAnimatedGradientVisual::Property::START_COLOR]   = mColorAnimationEndStart;
    mBackgroundMapEnd[Toolkit::DevelAnimatedGradientVisual::Property::END_COLOR]     = mColorAnimationEndEnd;

    if(index == 1)
    {
      // Rotate background gradient
      mBackgroundMapStart[Toolkit::DevelAnimatedGradientVisual::Property::ROTATE_AMOUNT] = BuildMap(0.0f, Math::PI * 2.0f, 0, mLoadingTime, 0.0f, -1, 0.0f, 0, 0);
    }
    else if(index == 2)
    {
      // Rotate background gradient more slow
      mBackgroundMapStart[Toolkit::DevelAnimatedGradientVisual::Property::ROTATE_AMOUNT] = BuildMap(0.0f, Math::PI * 2.0f, 0, mLoadingTime * 2.0, 0.0f, -1, 0.0f, 0, 0);
    }
    else
    {
      mBackgroundMapStart[Toolkit::DevelAnimatedGradientVisual::Property::ROTATE_AMOUNT] = 0.0f;
    }

    mBackground.SetProperty(Control::Property::BACKGROUND, mBackgroundMapStart);

    mTickCount         = 0;
    mBackgroundChanger = Timer::New(mLoadingTime * 1000.0f / mLoadingCountScale);
    mBackgroundChanger.TickSignal().Connect(this, &CardController::OnTickBackground);
    mBackgroundChanger.Start();

    mMoveFront.AnimateTo(Property(mCards[index], Actor::Property::SIZE_WIDTH), CARD_SIZE_BIG.x * SCALED_WIDTH);
    mMoveFront.AnimateTo(Property(mCards[index], Actor::Property::SIZE_HEIGHT), CARD_SIZE_BIG.y * SCALED_HEIGHT);
    mMoveFront.AnimateTo(Property(mCards[index], Actor::Property::POSITION_Y), CARD_BIG_OFFSET.y * SCALED_HEIGHT);
    mMoveBack.AnimateTo(Property(mCards[index], Actor::Property::SIZE_WIDTH), CARD_SIZE.x * SCALED_WIDTH);
    mMoveBack.AnimateTo(Property(mCards[index], Actor::Property::SIZE_HEIGHT), CARD_SIZE.y * SCALED_HEIGHT);
    mMoveBack.AnimateTo(Property(mCards[index], Actor::Property::POSITION_Y), CARD_OFFSET.y * SCALED_HEIGHT);
    for(int i = 0; i < index; i++)
    {
      mMoveFront.AnimateBy(Property(mCards[i], Actor::Property::POSITION_X), -CARD_MOVE_DIST * SCALED_WIDTH);
      mMoveBack.AnimateBy(Property(mCards[i], Actor::Property::POSITION_X), CARD_MOVE_DIST * SCALED_WIDTH);
    }
    for(int i = index + 1; i < CARD_NUM; i++)
    {
      mMoveFront.AnimateBy(Property(mCards[i], Actor::Property::POSITION_X), CARD_MOVE_DIST * SCALED_WIDTH);
      mMoveBack.AnimateBy(Property(mCards[i], Actor::Property::POSITION_X), -CARD_MOVE_DIST * SCALED_WIDTH);
    }
    mMoveFront.AnimateTo(Property(mAddButton, Actor::Property::POSITION_Y), BUTTON_ADD_SIZE.y * SCALED_HEIGHT);
    mMoveBack.AnimateTo(Property(mAddButton, Actor::Property::POSITION_Y), 0.f * SCALED_HEIGHT);

    mMoveFront.AnimateTo(Property(mLabel1, Actor::Property::VISIBLE), false);
    mMoveFront.AnimateTo(Property(mLabel2, Actor::Property::VISIBLE), true);
    mMoveFront.AnimateTo(Property(mLabel3, Actor::Property::VISIBLE), true);
    mMoveBack.AnimateTo(Property(mLabel1, Actor::Property::VISIBLE), true);
    mMoveBack.AnimateTo(Property(mLabel2, Actor::Property::VISIBLE), false);
    mMoveBack.AnimateTo(Property(mLabel3, Actor::Property::VISIBLE), false);

    mMoveFront.Play();
  }
  bool OnTickBackground()
  {
    mTickCount++;
    if(mCancelSignal || mTickCount >= mLoadingCount * mLoadingCountScale)
    {
      if(mCards.mCurState == 1)
      {
        mCards.mCurState = 2;
        mBackground.SetProperty(Control::Property::BACKGROUND, mBackgroundMapEnd);
        mMoveBack.Play();
        mBackgroundChanger.SetInterval(mDuration * 1000.0f);
        return true;
      }
      else
      {
        Reset();
        return false;
      }
    }
    return true;
  }
  void MoveRight()
  {
    if(mCards.MoveRight(mCardDuration))
    {
      // Set smooth background color change here
      mNormalEndColor                                                                 = mCards.GetColorBackground(mCards.mCurIndex);
      mBackgroundMapMove[Toolkit::DevelAnimatedGradientVisual::Property::START_COLOR] = mNormalEndColor;
      mBackgroundMapMove[Toolkit::DevelAnimatedGradientVisual::Property::END_COLOR]   = mNormalStartColor;

      (*mBackgroundMapMove[Toolkit::DevelAnimatedGradientVisual::Property::OFFSET].GetMap())[DevelAnimatedGradientVisual::AnimationParameter::Property::DIRECTION] = Property::Value(DevelAnimatedGradientVisual::AnimationParameter::DirectionType::BACKWARD);

      mBackground.SetProperty(Control::Property::BACKGROUND, mBackgroundMapMove);
    }
  }
  void MoveLeft()
  {
    if(mCards.MoveLeft(mCardDuration))
    {
      //Set smooth background color change here
      mNormalEndColor                                                                 = mCards.GetColorBackground(mCards.mCurIndex);
      mBackgroundMapMove[Toolkit::DevelAnimatedGradientVisual::Property::START_COLOR] = mNormalStartColor;
      mBackgroundMapMove[Toolkit::DevelAnimatedGradientVisual::Property::END_COLOR]   = mNormalEndColor;

      (*mBackgroundMapMove[Toolkit::DevelAnimatedGradientVisual::Property::OFFSET].GetMap())[DevelAnimatedGradientVisual::AnimationParameter::Property::DIRECTION] = Property::Value(DevelAnimatedGradientVisual::AnimationParameter::DirectionType::FORWARD);

      mBackground.SetProperty(Control::Property::BACKGROUND, mBackgroundMapMove);
    }
  }

  void Reset()
  {
    mCards.mCurState = 0;
    mCancelSignal    = false;
    mMoveFront.Clear();
    mMoveBack.Clear();

    mNormalStartColor                                                                 = mNormalEndColor;
    mBackgroundNormalMap[Toolkit::DevelAnimatedGradientVisual::Property::START_COLOR] = mNormalStartColor;
    mBackgroundNormalMap[Toolkit::DevelAnimatedGradientVisual::Property::END_COLOR]   = mNormalEndColor;
    mBackground.SetProperty(Control::Property::BACKGROUND, mBackgroundNormalMap);
  }

private:
  Application& mApplication;
  Window       mWindow;

  CardManager mCards;

  Control mBackground;

  ImageView mAddButton;
  TextLabel mLabel1;
  TextLabel mLabel2;
  TextLabel mLabel3;

  Timer mBackgroundChanger;
  Timer mCardChanger;
  Timer mTempTimer;

  Animation mMoveFront;
  Animation mMoveBack;

  // Property for background animated gradient visual
  Property::Map mBackgroundNormalMap;
  Property::Map mBackgroundMapStart;
  Property::Map mBackgroundMapEnd;
  Property::Map mBackgroundMapMove;

  // Property for animation of color in animated gradient visual
  Property::Map mColorAnimationStartStart;
  Property::Map mColorAnimationStartEnd;
  Property::Map mColorAnimationEndStart;
  Property::Map mColorAnimationEndEnd;

  Vector4 mNormalColor;
  Vector4 mNormalStartColor;
  Vector4 mNormalEndColor;

  Vector2 mFirstTouchPos;
  Vector2 mLastTouchPos;

  float mCardDuration;
  float mDuration;
  float mLoadingTime;
  int   mLoadingCount;
  int   mLoadingCountScale;
  int   mTickCount;

  bool mCancelSignal;
  bool mIsTouchedActor;
};

int DALI_EXPORT_API main(int argc, char** argv)
{
  Application application = Application::New(&argc, &argv);

  CardController test(application);

  application.MainLoop();

  return 0;
}