2 * Copyright (c) 2017 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 * @file text-label-example.cpp
20 * @brief Basic usage of TextLabel control
24 #include <dali/devel-api/object/handle-devel.h>
25 #include <dali/devel-api/actors/actor-devel.h>
26 #include <dali-toolkit/devel-api/controls/buttons/button-devel.h>
27 #include <dali-toolkit/dali-toolkit.h>
31 #include "shared/multi-language-strings.h"
32 #include "shared/view.h"
35 using namespace Dali::Toolkit;
36 using namespace MultiLanguageStrings;
40 const char* const BACKGROUND_IMAGE = DEMO_IMAGE_DIR "grab-handle.png";
41 const char* const STYLES_IMAGE = DEMO_IMAGE_DIR "FontStyleButton_Main.png";
42 const char* const TICK_IMAGE_IMAGE = DEMO_IMAGE_DIR "FontStyleButton_OK_02.png";
43 const char* const STYLE_SELECTED_IMAGE = DEMO_IMAGE_DIR "FontStyleButton_OK_03.png";
45 const char* BUTTON_IMAGES[] =
47 DEMO_IMAGE_DIR "FontStyleButton_Colour.png",
48 DEMO_IMAGE_DIR "FontStyleButton_Outline.png",
49 DEMO_IMAGE_DIR "FontStyleButton_Shadow.png"
52 const unsigned int KEY_ZERO = 10;
53 const unsigned int KEY_ONE = 11;
54 const unsigned int KEY_A = 38;
55 const unsigned int KEY_F = 41;
56 const unsigned int KEY_H = 43;
57 const unsigned int KEY_U = 30;
58 const unsigned int KEY_V = 55;
59 const unsigned int KEY_M = 58;
60 const unsigned int KEY_L = 46;
61 const unsigned int KEY_S = 39;
62 const unsigned int KEY_PLUS = 21;
63 const unsigned int KEY_MINUS = 20;
65 const char* H_ALIGNMENT_STRING_TABLE[] =
72 const unsigned int H_ALIGNMENT_STRING_COUNT = sizeof( H_ALIGNMENT_STRING_TABLE ) / sizeof( H_ALIGNMENT_STRING_TABLE[0u] );
74 const char* V_ALIGNMENT_STRING_TABLE[] =
81 const unsigned int V_ALIGNMENT_STRING_COUNT = sizeof( V_ALIGNMENT_STRING_TABLE ) / sizeof( V_ALIGNMENT_STRING_TABLE[0u] );
91 const Vector4 AVAILABLE_COLORS[] =
99 const unsigned int NUMBER_OF_COLORS = sizeof( AVAILABLE_COLORS ) / sizeof( AVAILABLE_COLORS[0u] );
101 int ConvertToEven(int value)
103 return (value % 2 == 0) ? value : (value + 1);
106 struct HSVColorConstraint
108 HSVColorConstraint(float hue, float saturation, float value)
110 saturation(saturation),
115 void operator()(Vector3& current, const PropertyInputContainer& inputs )
117 current = hsv2rgb(Vector3(inputs[0]->GetFloat(), saturation, value));
120 Vector3 hsv2rgb(Vector3 colorHSV)
122 float r=colorHSV.z*(1+colorHSV.y*(cos(colorHSV.x)-1));
123 float g=colorHSV.z*(1+colorHSV.y*(cos(colorHSV.x-2.09439)-1));
124 float b=colorHSV.z*(1+colorHSV.y*(cos(colorHSV.x+2.09439)-1));
125 return Vector3(r, g, b);
132 const float STYLE_BUTTON_POSTION_RELATIVE_TO_STAGE = 0.9f;
133 const float BUTTON_SIZE_RATIO_TO_STAGE = 0.1f;
134 const float OUTLINE_WIDTH = 2.0f;
135 const Vector2 SHADOW_OFFSET = Vector2( 2.0f, 2.0f );
138 } // anonymous namespace
141 * @brief The main class of the demo.
143 class TextLabelExample : public ConnectionTracker
147 TextLabelExample( Application& application )
148 : mApplication( application ),
150 mShadowActive( false ),
151 mOutlineActive( false ),
152 mSelectedColor(AVAILABLE_COLORS[0]),
156 mPanGestureDetector(),
160 mHueAngleIndex( Property::INVALID_INDEX ),
161 mOverrideMixColorIndex( Property::INVALID_INDEX )
164 // Connect to the Application's Init signal
165 mApplication.InitSignal().Connect( this, &TextLabelExample::Create );
170 // Nothing to do here.
174 * One-time setup in response to Application InitSignal.
176 void Create( Application& application )
178 Stage stage = Stage::GetCurrent();
180 stage.KeyEventSignal().Connect(this, &TextLabelExample::OnKeyEvent);
181 mStageSize = stage.GetSize();
183 mButtonSize = Size( mStageSize.height * 0.1, mStageSize.height * 0.1 ); // Button size 1/10 of stage height
185 mContainer = Control::New();
186 mContainer.SetName( "Container" );
187 mContainer.SetParentOrigin( ParentOrigin::CENTER );
188 mLayoutSize = Vector2(mStageSize.width*0.6f, mStageSize.width*0.6f);
189 mContainer.SetSize( mLayoutSize );
190 mContainer.SetDrawMode( DrawMode::OVERLAY_2D );
191 stage.Add( mContainer );
193 // Resize the center layout when the corner is grabbed
194 mGrabCorner = ImageView::New( BACKGROUND_IMAGE );
195 mGrabCorner.SetName( "GrabCorner" );
196 mGrabCorner.SetAnchorPoint( AnchorPoint::TOP_CENTER );
197 mGrabCorner.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
198 mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
199 mContainer.Add( mGrabCorner );
201 mPanGestureDetector = PanGestureDetector::New();
202 mPanGestureDetector.Attach( mGrabCorner );
203 mPanGestureDetector.DetectedSignal().Connect( this, &TextLabelExample::OnPan );
205 mLabel = TextLabel::New( "\xF0\x9F\x98\x89 A Quick Brown Fox Jumps Over The Lazy Dog" );
207 mLabel.SetName( "TextLabel" );
208 mLabel.SetAnchorPoint( AnchorPoint::TOP_LEFT );
209 mLabel.SetSize(mLayoutSize);
210 mLabel.SetProperty( TextLabel::Property::MULTI_LINE, true );
211 mLabel.SetProperty( TextLabel::Property::TEXT_COLOR, Color::GREEN );
212 mLabel.SetBackgroundColor( Color::WHITE );
213 mContainer.Add( mLabel );
215 // Create style activate button
216 mStyleMenuButton = PushButton::New();
217 mStyleMenuButton.SetPosition( mButtonSize.width, mStageSize.height * STYLE_BUTTON_POSTION_RELATIVE_TO_STAGE );
218 mStyleMenuButton.SetSize( mButtonSize );
219 mStyleMenuButton.SetProperty( Button::Property::TOGGLABLE, true );
220 mStyleMenuButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, STYLES_IMAGE );
221 mStyleMenuButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, TICK_IMAGE_IMAGE );
223 mStyleMenuButton.ClickedSignal().Connect( this, &TextLabelExample::OnStyleButtonClicked );
224 stage.Add( mStyleMenuButton );
226 // Add a border for the container so you can see the container is being resized while grabbing the handle.
227 mBorder = Control::New();
228 mBorder.SetAnchorPoint( AnchorPoint::TOP_LEFT );
229 mBorder.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
230 mBorder.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
232 Dali::Property::Map border;
233 border.Insert( Toolkit::Visual::Property::TYPE, Visual::BORDER );
234 border.Insert( BorderVisual::Property::COLOR, Color::WHITE );
235 border.Insert( BorderVisual::Property::SIZE, 2.f );
236 mBorder.SetProperty( Control::Property::BACKGROUND, border );
237 mContainer.Add( mBorder );
238 mBorder.SetVisible(false);
240 mGrabCorner.RaiseToTop();
242 mHueAngleIndex = mLabel.RegisterProperty( "hue", 0.0f );
243 Renderer bgRenderer = mLabel.GetRendererAt(0);
244 mOverrideMixColorIndex = DevelHandle::GetPropertyIndex( bgRenderer, ColorVisual::Property::MIX_COLOR );
246 Constraint constraint = Constraint::New<Vector3>( bgRenderer, mOverrideMixColorIndex, HSVColorConstraint(0.0f, 0.5f, 0.8f));
247 constraint.AddSource( Source( mLabel, mHueAngleIndex ) );
248 constraint.SetRemoveAction( Constraint::Discard );
251 Animation anim = Animation::New(50.0f);
252 anim.AnimateTo(Property(mLabel, mHueAngleIndex), 6.28318f);
253 anim.SetLooping(true);
256 // Animate the text color 3 times from source color to Yellow
257 Animation animation = Animation::New( 2.f );
258 animation.AnimateTo( Property( mLabel, TextLabel::Property::TEXT_COLOR ), Color::YELLOW, AlphaFunction::SIN );
259 animation.SetLoopCount( 3 );
262 Property::Value labelText = mLabel.GetProperty( TextLabel::Property::TEXT );
263 std::cout << "Displaying text: \"" << labelText.Get< std::string >() << "\"" << std::endl;
266 // Depending on button pressed, apply the style to the text label
267 bool OnStyleSelected( Toolkit::Button button )
269 if( button == mStyleButtons[ StyleType::TEXT_COLOR ] )
271 Animation animation = Animation::New( 2.f );
272 animation.AnimateTo( Property( mLabel, TextLabel::Property::TEXT_COLOR ), mSelectedColor, AlphaFunction::LINEAR );
275 else if( button == mStyleButtons[ StyleType::OUTLINE ] )
277 Property::Map outlineMap;
278 float outlineWidth = OUTLINE_WIDTH;
282 outlineWidth = ( mOutlineColor == mSelectedColor ) ? 0.0f : OUTLINE_WIDTH ; // toggles outline on/off
284 mOutlineActive = ( outlineWidth > 0.0f ) ? true : false;
286 mOutlineColor = mSelectedColor;
287 outlineMap["color"] = mOutlineColor;
288 outlineMap["width"] = outlineWidth;
289 mLabel.SetProperty( TextLabel::Property::OUTLINE, outlineMap );
291 else if( button == mStyleButtons[ StyleType::SHADOW ] )
293 Vector2 shadowOffset( SHADOW_OFFSET ); // Will be set to zeros if color already set
294 Property::Value value = mLabel.GetProperty( TextLabel::Property::SHADOW_COLOR );
295 Vector4 currentShadowColor;
296 value.Get( currentShadowColor );
300 // toggle shadow off ( zero offset ) if color is already set
301 shadowOffset = ( currentShadowColor == mSelectedColor ) ? Vector2::ZERO : Vector2( SHADOW_OFFSET );
304 mShadowActive = ( shadowOffset == Vector2::ZERO ) ? false : true;
306 Property::Map shadowMap;
307 shadowMap.Insert( "offset", shadowOffset );
308 shadowMap.Insert( "color", mSelectedColor );
309 shadowMap.Insert( "blurRadius", 2.0f );
310 mLabel.SetProperty( TextLabel::Property::SHADOW, shadowMap );
315 bool OnColorSelected( Toolkit::Button button )
317 for( unsigned int index = 0; index < NUMBER_OF_COLORS; index++)
319 if ( mColorButtons[index] == button )
321 mSelectedColor = AVAILABLE_COLORS[ index ];
328 void ShowColorButtons()
330 for( unsigned int index = 0; index < NUMBER_OF_COLORS; index++)
332 mColorButtons[index] = RadioButton::New();
333 mColorButtons[index].SetPosition( mButtonSize.width, mStageSize.height * STYLE_BUTTON_POSTION_RELATIVE_TO_STAGE - ( mButtonSize.width * (index+1) ) );
334 mColorButtons[index].SetSize( mButtonSize );
335 mColorButtons[index].ClickedSignal().Connect( this, &TextLabelExample::OnColorSelected );
336 mColorButtons[index].SetProperty( Button::Property::TOGGLABLE, true );
337 Property::Map propertyMap;
338 propertyMap.Insert(Visual::Property::TYPE, Visual::COLOR);
339 propertyMap.Insert(ColorVisual::Property::MIX_COLOR, AVAILABLE_COLORS[ index ]);
340 mColorButtons[index].SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, propertyMap );
341 mColorButtons[index].SetProperty( Toolkit::DevelButton::Property::UNSELECTED_VISUAL, propertyMap );
343 propertyMap.Insert(Visual::Property::TYPE, Visual::COLOR);
344 propertyMap.Insert(ColorVisual::Property::MIX_COLOR, AVAILABLE_COLORS[ index ]);
345 mColorButtons[index].SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, propertyMap );
347 mColorButtons[index].SetProperty( Toolkit::DevelButton::Property::SELECTED_VISUAL,
348 Property::Map().Add( Toolkit::Visual::Property::TYPE, Visual::BORDER )
349 .Add( BorderVisual::Property::COLOR, Color::WHITE )
350 .Add( BorderVisual::Property::SIZE, 2.0f )
351 .Add( BorderVisual::Property::ANTI_ALIASING, true ) );
353 Stage::GetCurrent().Add( mColorButtons[index] );
358 void HideColorButtons()
360 for( unsigned int index = 0; index < NUMBER_OF_COLORS; index++)
362 UnparentAndReset( mColorButtons[index] );
366 void HideStyleButtons()
368 for( unsigned int index = 0; index < NUMBER_OF_STYLES; index++)
370 UnparentAndReset( mStyleButtons[index] );
374 bool OnStyleButtonClicked( Toolkit::Button button )
376 if ( button.GetProperty( Toolkit::Button::Property::SELECTED ).Get<bool>() )
378 for ( unsigned int index = 0; index < NUMBER_OF_STYLES; index++ )
380 mStyleButtons[index] = PushButton::New();
381 mStyleButtons[index].SetPosition( mButtonSize.width + ( mButtonSize.width * (index+1) ), mStageSize.height * STYLE_BUTTON_POSTION_RELATIVE_TO_STAGE );
382 mStyleButtons[index].SetSize( mButtonSize );
383 mStyleButtons[index].SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, BUTTON_IMAGES[ index ] );
384 mStyleButtons[index].SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, STYLE_SELECTED_IMAGE );
385 mStyleButtons[index].ClickedSignal().Connect( this, &TextLabelExample::OnStyleSelected );
386 Stage::GetCurrent().Add( mStyleButtons[index] );
392 // hide menu and colors
399 // Resize the text-label with pan gesture
400 void OnPan( Actor actor, const PanGesture& gesture )
402 // Reset mLayoutSize when the pan starts
403 if( gesture.state == Gesture::Started )
405 if( mLayoutSize.x < 2.0f )
407 mLayoutSize.x = 2.0f;
410 if( mLayoutSize.y < 2.0f )
412 mLayoutSize.y = 2.0f;
415 // Only show the border during the panning
416 mBorder.SetVisible(true);
419 mLayoutSize.x += gesture.displacement.x * 2.0f;
420 mLayoutSize.y += gesture.displacement.y * 2.0f;
422 if( mLayoutSize.x >= 2.0f ||
423 mLayoutSize.y >= 2.0f )
425 mLayoutSize.x = std::min ( mLayoutSize.x, mStageSize.width );
426 mLayoutSize.y = std::min ( mLayoutSize.y, mStageSize.height*.9f );
428 // Avoid pixel mis-alignment issue
429 Vector2 clampedSize = Vector2( std::max( ConvertToEven( static_cast<int>( mLayoutSize.x )), 2 ),
430 std::max( ConvertToEven( static_cast<int>( mLayoutSize.y )), 2 ) );
432 mContainer.SetSize( clampedSize );
435 if( gesture.state == Gesture::Cancelled || gesture.state == Gesture::Finished )
437 // Resize the text label to match the container size when panning is finished
438 mLabel.SetSize(mLayoutSize);
439 mBorder.SetVisible(false);
444 * Main key event handler
446 void OnKeyEvent(const KeyEvent& event)
448 if(event.state == KeyEvent::Down)
450 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
454 else if( event.IsCtrlModifier() )
456 switch( event.keyCode )
458 // Select rendering back-end
459 case KEY_ZERO: // fall through
462 mLabel.SetProperty( TextLabel::Property::RENDERING_BACKEND, event.keyCode - 10 );
465 case KEY_A: // Animate text colour
467 Animation animation = Animation::New( 2.f );
468 animation.AnimateTo( Property( mLabel, TextLabel::Property::TEXT_COLOR ), Color::RED, AlphaFunction::SIN );
469 animation.SetLoopCount( 3 );
473 case KEY_F: // Fill vertically
475 if( ResizePolicy::DIMENSION_DEPENDENCY == mLabel.GetResizePolicy(Dimension::HEIGHT) )
477 mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
481 mLabel.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
485 case KEY_H: // Horizontal alignment
487 if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
492 mLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
495 case KEY_V: // Vertical alignment
497 if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
502 mLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
505 case KEY_M: // Multi-line
507 bool multiLine = mLabel.GetProperty<bool>( TextLabel::Property::MULTI_LINE );
508 mLabel.SetProperty( TextLabel::Property::MULTI_LINE, !multiLine );
511 case KEY_L: // Language
513 const Language& language = LANGUAGES[ mLanguageId ];
515 mLabel.SetProperty( TextLabel::Property::TEXT, language.text );
517 if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
523 case KEY_S: // Shadow color
525 if( Color::BLACK == mLabel.GetProperty<Vector4>( TextLabel::Property::SHADOW_COLOR ) )
527 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::RED );
531 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
535 case KEY_U: // Markup
537 mLabel.SetProperty( TextLabel::Property::ENABLE_MARKUP, true );
538 mLabel.SetProperty( TextLabel::Property::TEXT, "<font family='DejaVuSerif' size='18'>H<color value='blue'>ello</color> <font weight='bold'>world</font> demo</font>" );
541 case KEY_PLUS: // Increase shadow offset
543 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
546 case KEY_MINUS: // Decrease shadow offset
548 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
559 Application& mApplication;
563 PushButton mStyleMenuButton;
564 PushButton mStyleButtons[ NUMBER_OF_STYLES ];
567 Vector4 mSelectedColor;
568 Vector4 mOutlineColor; // Store outline as Vector4 whilst TextLabel Outline Property returns a string when using GetProperty
569 Button mColorButtons[ NUMBER_OF_COLORS ];
575 PanGestureDetector mPanGestureDetector;
582 unsigned int mLanguageId;
583 unsigned int mAlignment;
584 Property::Index mHueAngleIndex;
585 Property::Index mOverrideMixColorIndex;
588 int DALI_EXPORT_API main( int argc, char **argv )
590 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
591 TextLabelExample test( application );
592 application.MainLoop();