text-label-example.cpp 26 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
/*
 * Copyright (c) 2022 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.
 *
 */

/**
 * @file text-label-example.cpp
 * @brief Usage of TextLabel control with style application.
 */

// EXTERNAL INCLUDES
#include <dali-toolkit/dali-toolkit.h>
#include <dali-toolkit/devel-api/controls/text-controls/text-label-devel.h>
#include <dali-toolkit/devel-api/text/text-enumerations-devel.h>
#include <dali/devel-api/actors/actor-devel.h>
#include <dali/devel-api/object/handle-devel.h>
#include <iostream>

// INTERNAL INCLUDES
#include "expanding-buttons.h"
#include "shared/multi-language-strings.h"
#include "shared/view.h"

using namespace Dali;
using namespace Dali::Toolkit;
using namespace MultiLanguageStrings;

namespace
{
const char* const BACKGROUND_IMAGE     = DEMO_IMAGE_DIR "grab-handle.png";
const char* const STYLE_SELECTED_IMAGE = DEMO_IMAGE_DIR "FontStyleButton_OK_03.png";

const char* BUTTON_IMAGES[] =
  {
    DEMO_IMAGE_DIR "FontStyleButton_Colour.png",
    DEMO_IMAGE_DIR "FontStyleButton_Outline.png",
    DEMO_IMAGE_DIR "FontStyleButton_Shadow.png",
    DEMO_IMAGE_DIR "FontStyleButton_Background.png"};

const unsigned int KEY_ZERO  = 10;
const unsigned int KEY_ONE   = 11;
const unsigned int KEY_A     = 38;
const unsigned int KEY_F     = 41;
const unsigned int KEY_H     = 43;
const unsigned int KEY_U     = 30;
const unsigned int KEY_V     = 55;
const unsigned int KEY_M     = 58;
const unsigned int KEY_L     = 46;
const unsigned int KEY_S     = 39;
const unsigned int KEY_PLUS  = 21;
const unsigned int KEY_MINUS = 20;

const char* H_ALIGNMENT_STRING_TABLE[] =
  {
    "BEGIN",
    "CENTER",
    "END"};

const unsigned int H_ALIGNMENT_STRING_COUNT = sizeof(H_ALIGNMENT_STRING_TABLE) / sizeof(H_ALIGNMENT_STRING_TABLE[0u]);

const char* V_ALIGNMENT_STRING_TABLE[] =
  {
    "TOP",
    "CENTER",
    "BOTTOM"};

const unsigned int V_ALIGNMENT_STRING_COUNT = sizeof(V_ALIGNMENT_STRING_TABLE) / sizeof(V_ALIGNMENT_STRING_TABLE[0u]);

enum StyleType
{
  TEXT_COLOR = 0,
  OUTLINE,
  SHADOW,
  BACKGROUND,
  NUMBER_OF_STYLES
};

const Vector4 AVAILABLE_COLORS[] =
  {
    Color::GREEN,
    Color::BLUE,
    Color::RED,
    Color::CYAN,
    Color::WHITE // Used as clear
};

const unsigned int NUMBER_OF_COLORS = sizeof(AVAILABLE_COLORS) / sizeof(AVAILABLE_COLORS[0u]);

int ConvertToEven(int value)
{
  return (value % 2 == 0) ? value : (value + 1);
}

struct HSVColorConstraint
{
  HSVColorConstraint(float hueParam, float saturationParam, float valueParam)
  : hue(hueParam),
    saturation(saturationParam),
    value(valueParam)
  {
  }

  void operator()(Vector3& current, const PropertyInputContainer& inputs)
  {
    current = hsv2rgb(Vector3(inputs[0]->GetFloat(), saturation, value));
  }

  Vector3 hsv2rgb(Vector3 colorHSV)
  {
    float r = colorHSV.z * (1 + colorHSV.y * (cos(colorHSV.x) - 1));
    float g = colorHSV.z * (1 + colorHSV.y * (cos(colorHSV.x - 2.09439) - 1));
    float b = colorHSV.z * (1 + colorHSV.y * (cos(colorHSV.x + 2.09439) - 1));
    return Vector3(r, g, b);
  }
  float hue;
  float saturation;
  float value;
};

const float   STYLE_BUTTON_POSTION_RELATIVE_TO_WINDOW = 0.9f;
const float   BUTTON_SIZE_RATIO_TO_WINDOW             = 0.1f;
const float   OUTLINE_WIDTH                           = 2.0f;
const Vector2 SHADOW_OFFSET                           = Vector2(2.0f, 2.0f);
const int     GAP_BETWEEN_BUTTONS                     = 3;

} // anonymous namespace

/**
 * @brief The main class of the demo.
 */
class TextLabelExample : public ConnectionTracker
{
public:
  TextLabelExample(Application& application)
  : mApplication(application),
    mLabel(),
    mSelectedColor(AVAILABLE_COLORS[0]),
    mStyleActivatedForColor(NUMBER_OF_STYLES),
    mContainer(),
    mGrabCorner(),
    mBorder(),
    mPanGestureDetector(),
    mLayoutSize(),
    mLanguageId(0u),
    mAlignment(0u),
    mHueAngleIndex(Property::INVALID_INDEX),
    mOverrideMixColorIndex(Property::INVALID_INDEX),
    mColorButtonsHidden(true),
    mCollapseColorsAndStyles(false)
  {
    // Connect to the Application's Init signal
    mApplication.InitSignal().Connect(this, &TextLabelExample::Create);

    // Set Style flags to inactive
    for(unsigned int i = TEXT_COLOR; i < NUMBER_OF_STYLES; i++)
    {
      mStyleActiveState[i]  = false;
      mCurrentStyleColor[i] = AVAILABLE_COLORS[NUMBER_OF_COLORS - 1];
    }
  }

  ~TextLabelExample()
  {
    // Nothing to do here.
  }

  // Clicking the expanding button shows the registered style buttons.
  void SetUpExpandingStyleButtons(Vector2 position)
  {
    mExpandingButtons = Demo::ExpandingButtons::New();
    mExpandingButtons.SetProperty(Actor::Property::POSITION, Vector2(mButtonSize.width, mWindowSize.height * STYLE_BUTTON_POSTION_RELATIVE_TO_WINDOW));
    mExpandingButtons.CollapsingSignal().Connect(this, &TextLabelExample::OnExpandingButtonCollapsing);
    mExpandingButtons.SetProperty(Actor::Property::SIZE, mButtonSize);
    // Creates the buttons to be expanded
    CreateStyleButtons();

    // Register the created buttons with the ExpandingButtons.
    for(unsigned int index = 0; index < NUMBER_OF_STYLES; index++)
    {
      mExpandingButtons.RegisterButton(mStyleButtons[index]);
    }
  }

  /**
   * One-time setup in response to Application InitSignal.
   */
  void Create(Application& application)
  {
    Window window = application.GetWindow();

    window.KeyEventSignal().Connect(this, &TextLabelExample::OnKeyEvent);
    mWindowSize = window.GetSize();
    mButtonSize = Size(mWindowSize.height * 0.1, mWindowSize.height * 0.1); // Button size 1/10 of window height

    mContainer = Control::New();
    mContainer.SetProperty(Dali::Actor::Property::NAME, "Container");
    mContainer.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
    auto size = std::min(mWindowSize.width, mWindowSize.height) * 0.6f;
    mLayoutSize = Vector2(size, size);
    mContainer.SetProperty(Actor::Property::SIZE, mLayoutSize);
    window.Add(mContainer);

    // Resize the center layout when the corner is grabbed
    mGrabCorner = ImageView::New(BACKGROUND_IMAGE);
    mGrabCorner.SetProperty(Dali::Actor::Property::NAME, "GrabCorner");
    mGrabCorner.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER);
    mGrabCorner.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_RIGHT);
    mGrabCorner.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS);
    mContainer.Add(mGrabCorner);

    mPanGestureDetector = PanGestureDetector::New();
    mPanGestureDetector.Attach(mGrabCorner);
    mPanGestureDetector.DetectedSignal().Connect(this, &TextLabelExample::OnPan);

    mLabel = TextLabel::New("\xF0\x9F\x98\x89 A Quick Brown Fox Jumps Over The Lazy Dog");

    mLabel.SetProperty(Dali::Actor::Property::NAME, "TextLabel");
    mLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
    mLabel.SetProperty(Actor::Property::SIZE, mLayoutSize);
    mLabel.SetProperty(TextLabel::Property::MULTI_LINE, true);
    mLabel.SetProperty(TextLabel::Property::TEXT_COLOR, Color::GREEN);
    mLabel.SetBackgroundColor(Color::WHITE);
    mContainer.Add(mLabel);

    // Clicking ExpandingButton shows the Registered Style buttons, clicking again hides them.
    Vector2 expandingButtonPosition(mButtonSize.width, mWindowSize.height * STYLE_BUTTON_POSTION_RELATIVE_TO_WINDOW);
    SetUpExpandingStyleButtons(expandingButtonPosition);
    window.Add(mExpandingButtons);

    // Add a border for the container so you can see the container is being resized while grabbing the handle.
    mBorder = Control::New();
    mBorder.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
    mBorder.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
    mBorder.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT);

    Dali::Property::Map border;
    border.Insert(Toolkit::Visual::Property::TYPE, Visual::BORDER);
    border.Insert(BorderVisual::Property::COLOR, Color::WHITE);
    border.Insert(BorderVisual::Property::SIZE, 3.f);
    mBorder.SetProperty(Control::Property::BACKGROUND, border);
    mContainer.Add(mBorder);
    mBorder.SetProperty(Actor::Property::VISIBLE, false);
    mGrabCorner.RaiseToTop();

    mHueAngleIndex      = mLabel.RegisterProperty("hue", 0.0f);
    Renderer bgRenderer = mLabel.GetRendererAt(0);

    Constraint constraint = Constraint::New<Vector3>(bgRenderer, VisualRenderer::Property::VISUAL_MIX_COLOR, HSVColorConstraint(0.0f, 0.5f, 0.8f));
    constraint.AddSource(Source(mLabel, mHueAngleIndex));
    constraint.SetRemoveAction(Constraint::DISCARD);
    constraint.Apply();

    Animation anim = Animation::New(50.0f);
    anim.AnimateTo(Property(mLabel, mHueAngleIndex), 6.28318f);
    anim.SetLooping(true);
    anim.Play();

    mContainer.RaiseToTop();
    mGrabCorner.RaiseToTop();

    Property::Value labelText = mLabel.GetProperty(TextLabel::Property::TEXT);
    std::cout << "Displaying text: \"" << labelText.Get<std::string>() << "\"" << std::endl;
  }

  // If the styling buttons should colapse (hide) then the color buttons should also hide.
  bool OnExpandingButtonCollapsing(Demo::ExpandingButtons button)
  {
    mCollapseColorsAndStyles = true;
    HideColorButtons();
    return true;
  }

  // Get the style type from the given button
  StyleType GetStyleTypeFromButton(Toolkit::Button button)
  {
    StyleType style = StyleType::TEXT_COLOR;

    if(button == mStyleButtons[StyleType::OUTLINE])
    {
      style = StyleType::OUTLINE;
    }
    else if(button == mStyleButtons[StyleType::SHADOW])
    {
      style = StyleType::SHADOW;
    }
    else if(button == mStyleButtons[StyleType::BACKGROUND])
    {
      style = StyleType::BACKGROUND;
    }
    return style;
  }

  // Style selected, show color buttons
  bool OnStyleButtonClicked(Toolkit::Button button)
  {
    StyleType selectedStyle = GetStyleTypeFromButton(button);
    if(mStyleActivatedForColor == selectedStyle)
    {
      HideColorButtons();
    }
    else
    {
      ResetColorButtons(mColorButtons, NUMBER_OF_COLORS);
      ShowColorButtons(selectedStyle);
    }
    return true;
  }

  // Set style to selected color
  bool OnColorSelected(Toolkit::Button button)
  {
    for(unsigned int index = 0; index < NUMBER_OF_COLORS; index++)
    {
      if(mColorButtons[index] == button)
      {
        mSelectedColor = AVAILABLE_COLORS[index];
      }
    }

    switch(mStyleActivatedForColor)
    {
      case TEXT_COLOR:
      {
        Animation animation = Animation::New(1.f);
        animation.AnimateTo(Property(mLabel, TextLabel::Property::TEXT_COLOR), mSelectedColor, AlphaFunction::LINEAR);
        mCurrentStyleColor[TEXT_COLOR] = mSelectedColor;
        animation.Play();
        break;
      }
      case OUTLINE:
      {
        Property::Map outlineMap;
        float         outlineWidth = OUTLINE_WIDTH;

        if(mStyleActiveState[OUTLINE])
        {
          outlineWidth = (Color::WHITE == mSelectedColor) ? 0.0f : OUTLINE_WIDTH; // toggles outline on/off
        }
        mStyleActiveState[OUTLINE] = (outlineWidth > 0.0f) ? true : false;

        outlineMap["color"]         = mSelectedColor;
        outlineMap["width"]         = outlineWidth;
        mCurrentStyleColor[OUTLINE] = mSelectedColor;
        mLabel.SetProperty(TextLabel::Property::OUTLINE, outlineMap);
        break;
      }
      case SHADOW:
      {
        Vector2         shadowOffset(SHADOW_OFFSET); // Will be set to zeros if color already set
        Property::Value value = mLabel.GetProperty(TextLabel::Property::SHADOW);
        Vector4         currentShadowColor;
        value.Get(currentShadowColor);

        if(mStyleActiveState[SHADOW])
        {
          // toggle shadow off ( zero offset ) if color is already set
          shadowOffset = (Color::WHITE == mSelectedColor) ? Vector2::ZERO : Vector2(SHADOW_OFFSET);
        }

        mStyleActiveState[SHADOW]  = (shadowOffset == Vector2::ZERO) ? false : true;
        mCurrentStyleColor[SHADOW] = mSelectedColor;

        Property::Map shadowMap;
        shadowMap.Insert("offset", shadowOffset);
        shadowMap.Insert("color", mSelectedColor);
        mLabel.SetProperty(TextLabel::Property::SHADOW, shadowMap);

        break;
      }
      case BACKGROUND:
      {
        Property::Map backgroundMap;
        auto          backgroundEnabled(true);

        if(mStyleActiveState[BACKGROUND])
        {
          backgroundEnabled = (Color::WHITE != mSelectedColor); // toggles background on/off
        }
        mStyleActiveState[BACKGROUND] = backgroundEnabled;

        backgroundMap["color"]         = mSelectedColor;
        backgroundMap["enable"]        = backgroundEnabled;
        mCurrentStyleColor[BACKGROUND] = mSelectedColor;
        mLabel.SetProperty(DevelTextLabel::Property::BACKGROUND, backgroundMap);

        break;
      }
      default:
        break;
    }

    return true;
  }

  // Set the inital color button that should be be selected.
  // If the style already has a color set then that should be used
  void SetInitialSelectedColorButton(StyleType styleButtonIndex)
  {
    Vector4 selectedColor = mCurrentStyleColor[styleButtonIndex];

    for(unsigned int i = 0; i < NUMBER_OF_COLORS; i++)
    {
      if(AVAILABLE_COLORS[i] == selectedColor)
      {
        if(mColorButtons[i])
        {
          mColorButtons[i].SetProperty(Toolkit::Button::Property::SELECTED, true);
        }
        break;
      }
    }
  }

  // Create a bar of color buttons that the user can select.
  void ShowColorButtons(StyleType styleButtonIndex)
  {
    mCollapseColorsAndStyles = false; // Request to show colors so reset flag
    mStyleActivatedForColor  = styleButtonIndex;

    for(unsigned int index = 0; index < NUMBER_OF_COLORS; index++)
    {
      if(!mColorButtonsAnimation)
      {
        mColorButtonsAnimation = Animation::New(0.15f);
        mColorButtonsAnimation.FinishedSignal().Connect(this, &TextLabelExample::OnColorButtonAnimationFinished);
      }

      // Create a color button
      if(!mColorButtons[index])
      {
        mColorButtons[index] = RadioButton::New();
        mColorButtons[index].SetProperty(Actor::Property::SIZE, mButtonSize);
        mColorButtons[index].ClickedSignal().Connect(this, &TextLabelExample::OnColorSelected);
        mColorButtons[index].SetProperty(Button::Property::TOGGLABLE, true);
        Property::Map propertyMap;
        propertyMap.Insert(Visual::Property::TYPE, Visual::COLOR);
        propertyMap.Insert(ColorVisual::Property::MIX_COLOR, AVAILABLE_COLORS[index]);
        mColorButtons[index].SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, propertyMap);
        mColorButtons[index].SetProperty(Toolkit::Button::Property::UNSELECTED_VISUAL, propertyMap);
        mColorButtons[index].SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER);
        mColorButtons[index].SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER);

        propertyMap.Insert(Visual::Property::TYPE, Visual::COLOR);
        propertyMap.Insert(ColorVisual::Property::MIX_COLOR, AVAILABLE_COLORS[index]);
        mColorButtons[index].SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, propertyMap);

        mColorButtons[index].SetProperty(Toolkit::Button::Property::SELECTED_VISUAL,
                                         Property::Map().Add(Visual::Property::TYPE, Visual::BORDER).Add(BorderVisual::Property::COLOR, Color::WHITE).Add(BorderVisual::Property::SIZE, 4.0f).Add(BorderVisual::Property::ANTI_ALIASING, true));

        // Use a white button with 50% transparency as a clear color button
        if(Color::WHITE == AVAILABLE_COLORS[index] && styleButtonIndex != StyleType::TEXT_COLOR)
        {
          mColorButtons[index].SetProperty(Actor::Property::OPACITY, 0.5f);

          mColorButtons[index].SetProperty(Toolkit::Button::Property::LABEL,
                                           Property::Map().Add(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT).Add(Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT, HorizontalAlignment::CENTER).Add(Toolkit::TextVisual::Property::TEXT, "off"));
        }
      }

      SetInitialSelectedColorButton(mStyleActivatedForColor);

      mColorButtons[index].Unparent();

      mStyleButtons[styleButtonIndex].Add(mColorButtons[index]);
      mColorButtons[index].Lower();

      // Position button using nice animation
      mColorButtons[index].SetProperty(Actor::Property::POSITION_Y, -GAP_BETWEEN_BUTTONS);
      float         desiredPosition      = -(mButtonSize.height + GAP_BETWEEN_BUTTONS) * (index);
      AlphaFunction focusedAlphaFunction = AlphaFunction(Vector2(0.32f, 0.08f), Vector2(0.38f, 1.72f));
      mColorButtonsAnimation.AnimateBy(Property(mColorButtons[index], Actor::Property::POSITION_Y), desiredPosition, focusedAlphaFunction);
    }

    mColorButtonsHidden = false;
    mColorButtonsAnimation.Play();
  }

  // Remove the color buttons when not being shown.
  void ResetColorButtons(Button buttons[], unsigned int numberOfButtons)
  {
    for(unsigned int index = 0; index < numberOfButtons; index++)
    {
      UnparentAndReset(buttons[index]);
    }
  }

  void OnColorButtonAnimationFinished(Animation& animation)
  {
    animation.Clear();
    if(mColorButtonsHidden)
    {
      ResetColorButtons(mColorButtons, NUMBER_OF_COLORS);
      animation.Reset(); // Handle reset
      if(mCollapseColorsAndStyles)
      {
        mExpandingButtons.Collapse();
      }
    }
  }

  // Create the style buttons that will expand from the expanding button.
  void CreateStyleButtons()
  {
    for(unsigned int index = 0; index < NUMBER_OF_STYLES; index++)
    {
      if(!mStyleButtons[index])
      {
        mStyleButtons[index] = PushButton::New();
        mStyleButtons[index].SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, BUTTON_IMAGES[index]);
        mStyleButtons[index].SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, STYLE_SELECTED_IMAGE);
        mStyleButtons[index].SetProperty(Dali::Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
        mStyleButtons[index].SetProperty(Actor::Property::SIZE, mButtonSize);
        mStyleButtons[index].ClickedSignal().Connect(this, &TextLabelExample::OnStyleButtonClicked);
      }
    }
  }

  // Animate away the color bar.
  void HideColorButtons()
  {
    if(!mColorButtonsHidden)
    {
      for(unsigned int index = 0; index < NUMBER_OF_COLORS; index++)
      {
        mColorButtonsAnimation.AnimateTo(Property(mColorButtons[index], Actor::Property::POSITION_Y), 0.0f);
      }
      mColorButtonsHidden = true;
      mColorButtonsAnimation.Play();
    }
    mStyleActivatedForColor = NUMBER_OF_STYLES;
  }

  //  Request the expanding button to collapse.
  void HideStyleAndColorButtons()
  {
    mCollapseColorsAndStyles = true;
    if(mColorButtonsHidden)
    {
      mExpandingButtons.Collapse();
    }
    else
    {
      HideColorButtons();
    }
  }

  // Resize the text-label with pan gesture
  void OnPan(Actor actor, const PanGesture& gesture)
  {
    // Reset mLayoutSize when the pan starts
    GestureState state = gesture.GetState();
    if(state == GestureState::STARTED)
    {
      if(mLayoutSize.x < 2.0f)
      {
        mLayoutSize.x = 2.0f;
      }

      if(mLayoutSize.y < 2.0f)
      {
        mLayoutSize.y = 2.0f;
      }

      // Only show the border during the panning
      mBorder.SetProperty(Actor::Property::VISIBLE, true);

      HideStyleAndColorButtons();
    }

    const Vector2& displacement = gesture.GetDisplacement();
    mLayoutSize.x += displacement.x * 2.0f;
    mLayoutSize.y += displacement.y * 2.0f;

    if(mLayoutSize.x >= 2.0f ||
       mLayoutSize.y >= 2.0f)
    {
      mLayoutSize.x = std::min(mLayoutSize.x, mWindowSize.width);
      mLayoutSize.y = std::min(mLayoutSize.y, mWindowSize.height * .9f);

      // Avoid pixel mis-alignment issue
      Vector2 clampedSize = Vector2(std::max(ConvertToEven(static_cast<int>(mLayoutSize.x)), 2),
                                    std::max(ConvertToEven(static_cast<int>(mLayoutSize.y)), 2));

      mContainer.SetProperty(Actor::Property::SIZE, clampedSize);
    }

    if(state == GestureState::CANCELLED || state == GestureState::FINISHED)
    {
      // Resize the text label to match the container size when panning is finished
      mLabel.SetProperty(Actor::Property::SIZE, mLayoutSize);
      mBorder.SetProperty(Actor::Property::VISIBLE, false);
    }
  }

  /**
   * Main key event handler
   */
  void OnKeyEvent(const KeyEvent& event)
  {
    if(event.GetState() == KeyEvent::DOWN)
    {
      if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
      {
        mApplication.Quit();
      }
      else if(event.IsCtrlModifier())
      {
        switch(event.GetKeyCode())
        {
          // Select rendering back-end
          case KEY_ZERO: // fall through
          case KEY_ONE:
          {
            mLabel.SetProperty(DevelTextLabel::Property::RENDERING_BACKEND, event.GetKeyCode() - 10);
            break;
          }
          case KEY_A: // Animate text colour
          {
            Animation animation = Animation::New(2.f);
            animation.AnimateTo(Property(mLabel, TextLabel::Property::TEXT_COLOR), Color::RED, AlphaFunction::SIN);
            animation.SetLoopCount(3);
            animation.Play();
            break;
          }
          case KEY_F: // Fill vertically
          {
            if(ResizePolicy::DIMENSION_DEPENDENCY == mLabel.GetResizePolicy(Dimension::HEIGHT))
            {
              mLabel.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT);
            }
            else
            {
              mLabel.SetResizePolicy(ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT);
            }
            break;
          }
          case KEY_H: // Horizontal alignment
          {
            if(++mAlignment >= H_ALIGNMENT_STRING_COUNT)
            {
              mAlignment = 0u;
            }

            mLabel.SetProperty(TextLabel::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[mAlignment]);
            break;
          }
          case KEY_V: // Vertical alignment
          {
            if(++mAlignment >= V_ALIGNMENT_STRING_COUNT)
            {
              mAlignment = 0u;
            }

            mLabel.SetProperty(TextLabel::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[mAlignment]);
            break;
          }
          case KEY_M: // Multi-line
          {
            bool multiLine = mLabel.GetProperty<bool>(TextLabel::Property::MULTI_LINE);
            mLabel.SetProperty(TextLabel::Property::MULTI_LINE, !multiLine);
            break;
          }
          case KEY_L: // Language
          {
            const Language& language = LANGUAGES[mLanguageId];

            mLabel.SetProperty(TextLabel::Property::TEXT, language.text);

            if(++mLanguageId >= NUMBER_OF_LANGUAGES)
            {
              mLanguageId = 0u;
            }
            break;
          }
          case KEY_S: // Shadow color
          {
            Property::Value value = mLabel.GetProperty(TextLabel::Property::SHADOW);
            Vector4         shadowColor;
            value.Get(shadowColor);
            Property::Map shadowMap;
            if(Color::BLACK == shadowColor)
            {
              shadowMap.Insert("color", Color::RED);
              mLabel.SetProperty(TextLabel::Property::SHADOW, shadowMap);
            }
            else
            {
              shadowMap.Insert("color", Color::BLACK);
              mLabel.SetProperty(TextLabel::Property::SHADOW, shadowMap);
            }
            break;
          }
          case KEY_U: // Markup
          {
            mLabel.SetProperty(TextLabel::Property::ENABLE_MARKUP, true);
            mLabel.SetProperty(TextLabel::Property::TEXT, "<font family='DejaVuSerif' size='18'>H<color value='blue'>ello</color> <font weight='bold'>world</font> demo</font>");
            break;
          }
          case KEY_PLUS: // Increase shadow offset
          {
            Property::Value value = mLabel.GetProperty(TextLabel::Property::SHADOW);
            Vector2         shadowOffset;
            value.Get(shadowOffset);
            shadowOffset += Vector2(1.0f, 1.0f);

            Property::Map shadowMap;
            shadowMap.Insert("offset", shadowOffset);
            mLabel.SetProperty(TextLabel::Property::SHADOW, shadowMap);
            break;
          }
          case KEY_MINUS: // Decrease shadow offset
          {
            Property::Value value = mLabel.GetProperty(TextLabel::Property::SHADOW);
            Vector2         shadowOffset;
            value.Get(shadowOffset);
            shadowOffset -= Vector2(1.0f, 1.0f);

            Property::Map shadowMap;
            shadowMap.Insert("offset", shadowOffset);
            mLabel.SetProperty(TextLabel::Property::SHADOW, shadowMap);
            break;
          }
        }
      }
    }
  }

private:
  Application& mApplication;

  TextLabel mLabel;

  Demo::ExpandingButtons mExpandingButtons;
  PushButton             mStyleButtons[NUMBER_OF_STYLES];
  bool                   mStyleActiveState[NUMBER_OF_STYLES];

  Vector4 mCurrentStyleColor[NUMBER_OF_STYLES];

  Vector4 mSelectedColor;

  Button mColorButtons[NUMBER_OF_COLORS];

  StyleType mStyleActivatedForColor; // The style that the color bar is connected to

  Control mContainer;
  Control mGrabCorner;
  Control mBorder;

  PanGestureDetector mPanGestureDetector;

  Vector2 mLayoutSize;

  Animation mColorButtonsAnimation;

  Size mWindowSize;
  Size mButtonSize;

  unsigned int    mLanguageId;
  unsigned int    mAlignment;
  Property::Index mHueAngleIndex;
  Property::Index mOverrideMixColorIndex;

  bool mColorButtonsHidden;
  bool mCollapseColorsAndStyles;
};

int DALI_EXPORT_API main(int argc, char** argv)
{
  Application      application = Application::New(&argc, &argv, DEMO_THEME_PATH);
  TextLabelExample test(application);
  application.MainLoop();
  return 0;
}