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 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, shadowOffset );
307 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, mSelectedColor );
312 bool OnColorSelected( Toolkit::Button button )
314 for( unsigned int index = 0; index < NUMBER_OF_COLORS; index++)
316 if ( mColorButtons[index] == button )
318 mSelectedColor = AVAILABLE_COLORS[ index ];
325 void ShowColorButtons()
327 for( unsigned int index = 0; index < NUMBER_OF_COLORS; index++)
329 mColorButtons[index] = RadioButton::New();
330 mColorButtons[index].SetPosition( mButtonSize.width, mStageSize.height * STYLE_BUTTON_POSTION_RELATIVE_TO_STAGE - ( mButtonSize.width * (index+1) ) );
331 mColorButtons[index].SetSize( mButtonSize );
332 mColorButtons[index].ClickedSignal().Connect( this, &TextLabelExample::OnColorSelected );
333 mColorButtons[index].SetProperty( Button::Property::TOGGLABLE, true );
334 Property::Map propertyMap;
335 propertyMap.Insert(Visual::Property::TYPE, Visual::COLOR);
336 propertyMap.Insert(ColorVisual::Property::MIX_COLOR, AVAILABLE_COLORS[ index ]);
337 mColorButtons[index].SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, propertyMap );
338 mColorButtons[index].SetProperty( Toolkit::DevelButton::Property::UNSELECTED_VISUAL, propertyMap );
340 propertyMap.Insert(Visual::Property::TYPE, Visual::COLOR);
341 propertyMap.Insert(ColorVisual::Property::MIX_COLOR, AVAILABLE_COLORS[ index ]);
342 mColorButtons[index].SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, propertyMap );
344 mColorButtons[index].SetProperty( Toolkit::DevelButton::Property::SELECTED_VISUAL,
345 Property::Map().Add( Toolkit::Visual::Property::TYPE, Visual::BORDER )
346 .Add( BorderVisual::Property::COLOR, Color::WHITE )
347 .Add( BorderVisual::Property::SIZE, 2.0f )
348 .Add( BorderVisual::Property::ANTI_ALIASING, true ) );
350 Stage::GetCurrent().Add( mColorButtons[index] );
355 void HideColorButtons()
357 for( unsigned int index = 0; index < NUMBER_OF_COLORS; index++)
359 UnparentAndReset( mColorButtons[index] );
363 void HideStyleButtons()
365 for( unsigned int index = 0; index < NUMBER_OF_STYLES; index++)
367 UnparentAndReset( mStyleButtons[index] );
371 bool OnStyleButtonClicked( Toolkit::Button button )
373 if ( button.GetProperty( Toolkit::Button::Property::SELECTED ).Get<bool>() )
375 for ( unsigned int index = 0; index < NUMBER_OF_STYLES; index++ )
377 mStyleButtons[index] = PushButton::New();
378 mStyleButtons[index].SetPosition( mButtonSize.width + ( mButtonSize.width * (index+1) ), mStageSize.height * STYLE_BUTTON_POSTION_RELATIVE_TO_STAGE );
379 mStyleButtons[index].SetSize( mButtonSize );
380 mStyleButtons[index].SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, BUTTON_IMAGES[ index ] );
381 mStyleButtons[index].SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, STYLE_SELECTED_IMAGE );
382 mStyleButtons[index].ClickedSignal().Connect( this, &TextLabelExample::OnStyleSelected );
383 Stage::GetCurrent().Add( mStyleButtons[index] );
389 // hide menu and colors
396 // Resize the text-label with pan gesture
397 void OnPan( Actor actor, const PanGesture& gesture )
399 // Reset mLayoutSize when the pan starts
400 if( gesture.state == Gesture::Started )
402 if( mLayoutSize.x < 2.0f )
404 mLayoutSize.x = 2.0f;
407 if( mLayoutSize.y < 2.0f )
409 mLayoutSize.y = 2.0f;
412 // Only show the border during the panning
413 mBorder.SetVisible(true);
416 mLayoutSize.x += gesture.displacement.x * 2.0f;
417 mLayoutSize.y += gesture.displacement.y * 2.0f;
419 if( mLayoutSize.x >= 2.0f ||
420 mLayoutSize.y >= 2.0f )
422 mLayoutSize.x = std::min ( mLayoutSize.x, mStageSize.width );
423 mLayoutSize.y = std::min ( mLayoutSize.y, mStageSize.height*.9f );
425 // Avoid pixel mis-alignment issue
426 Vector2 clampedSize = Vector2( std::max( ConvertToEven( static_cast<int>( mLayoutSize.x )), 2 ),
427 std::max( ConvertToEven( static_cast<int>( mLayoutSize.y )), 2 ) );
429 mContainer.SetSize( clampedSize );
432 if( gesture.state == Gesture::Cancelled || gesture.state == Gesture::Finished )
434 // Resize the text label to match the container size when panning is finished
435 mLabel.SetSize(mLayoutSize);
436 mBorder.SetVisible(false);
441 * Main key event handler
443 void OnKeyEvent(const KeyEvent& event)
445 if(event.state == KeyEvent::Down)
447 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
451 else if( event.IsCtrlModifier() )
453 switch( event.keyCode )
455 // Select rendering back-end
456 case KEY_ZERO: // fall through
459 mLabel.SetProperty( TextLabel::Property::RENDERING_BACKEND, event.keyCode - 10 );
462 case KEY_A: // Animate text colour
464 Animation animation = Animation::New( 2.f );
465 animation.AnimateTo( Property( mLabel, TextLabel::Property::TEXT_COLOR ), Color::RED, AlphaFunction::SIN );
466 animation.SetLoopCount( 3 );
470 case KEY_F: // Fill vertically
472 if( ResizePolicy::DIMENSION_DEPENDENCY == mLabel.GetResizePolicy(Dimension::HEIGHT) )
474 mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
478 mLabel.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
482 case KEY_H: // Horizontal alignment
484 if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
489 mLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
492 case KEY_V: // Vertical alignment
494 if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
499 mLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
502 case KEY_M: // Multi-line
504 bool multiLine = mLabel.GetProperty<bool>( TextLabel::Property::MULTI_LINE );
505 mLabel.SetProperty( TextLabel::Property::MULTI_LINE, !multiLine );
508 case KEY_L: // Language
510 const Language& language = LANGUAGES[ mLanguageId ];
512 mLabel.SetProperty( TextLabel::Property::TEXT, language.text );
514 if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
520 case KEY_S: // Shadow color
522 if( Color::BLACK == mLabel.GetProperty<Vector4>( TextLabel::Property::SHADOW_COLOR ) )
524 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::RED );
528 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
532 case KEY_U: // Markup
534 mLabel.SetProperty( TextLabel::Property::ENABLE_MARKUP, true );
535 mLabel.SetProperty( TextLabel::Property::TEXT, "<font family='DejaVuSerif' size='18'>H<color value='blue'>ello</color> <font weight='bold'>world</font> demo</font>" );
538 case KEY_PLUS: // Increase shadow offset
540 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
543 case KEY_MINUS: // Decrease shadow offset
545 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
556 Application& mApplication;
560 PushButton mStyleMenuButton;
561 PushButton mStyleButtons[ NUMBER_OF_STYLES ];
564 Vector4 mSelectedColor;
565 Vector4 mOutlineColor; // Store outline as Vector4 whilst TextLabel Outline Property returns a string when using GetProperty
566 Button mColorButtons[ NUMBER_OF_COLORS ];
572 PanGestureDetector mPanGestureDetector;
579 unsigned int mLanguageId;
580 unsigned int mAlignment;
581 Property::Index mHueAngleIndex;
582 Property::Index mOverrideMixColorIndex;
585 int DALI_EXPORT_API main( int argc, char **argv )
587 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
588 TextLabelExample test( application );
589 application.MainLoop();