2 * Copyright (c) 2016 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-toolkit/devel-api/controls/text-controls/text-label-devel.h>
26 #include <dali-toolkit/dali-toolkit.h>
30 #include "shared/multi-language-strings.h"
31 #include "shared/view.h"
34 using namespace Dali::Toolkit;
35 using namespace MultiLanguageStrings;
39 const char* const BACKGROUND_IMAGE = DEMO_IMAGE_DIR "grab-handle.png";
41 const unsigned int KEY_ZERO = 10;
42 const unsigned int KEY_ONE = 11;
43 const unsigned int KEY_A = 38;
44 const unsigned int KEY_F = 41;
45 const unsigned int KEY_H = 43;
46 const unsigned int KEY_U = 30;
47 const unsigned int KEY_V = 55;
48 const unsigned int KEY_M = 58;
49 const unsigned int KEY_L = 46;
50 const unsigned int KEY_S = 39;
51 const unsigned int KEY_PLUS = 21;
52 const unsigned int KEY_MINUS = 20;
54 const char* H_ALIGNMENT_STRING_TABLE[] =
61 const unsigned int H_ALIGNMENT_STRING_COUNT = sizeof( H_ALIGNMENT_STRING_TABLE ) / sizeof( H_ALIGNMENT_STRING_TABLE[0u] );
63 const char* V_ALIGNMENT_STRING_TABLE[] =
70 const unsigned int V_ALIGNMENT_STRING_COUNT = sizeof( V_ALIGNMENT_STRING_TABLE ) / sizeof( V_ALIGNMENT_STRING_TABLE[0u] );
72 int ConvertToEven(int value)
74 return (value % 2 == 0) ? value : (value + 1);
77 struct HSVColorConstraint
79 HSVColorConstraint(float hue, float saturation, float value)
81 saturation(saturation),
86 void operator()(Vector3& current, const PropertyInputContainer& inputs )
88 current = hsv2rgb(Vector3(inputs[0]->GetFloat(), saturation, value));
91 Vector3 hsv2rgb(Vector3 colorHSV)
93 float r=colorHSV.z*(1+colorHSV.y*(cos(colorHSV.x)-1));
94 float g=colorHSV.z*(1+colorHSV.y*(cos(colorHSV.x-2.09439)-1));
95 float b=colorHSV.z*(1+colorHSV.y*(cos(colorHSV.x+2.09439)-1));
96 return Vector3(r, g, b);
103 } // anonymous namespace
106 * @brief The main class of the demo.
108 class TextLabelExample : public ConnectionTracker
112 TextLabelExample( Application& application )
113 : mApplication( application ),
117 mPanGestureDetector(),
121 mHueAngleIndex( Property::INVALID_INDEX ),
122 mOverrideMixColorIndex( Property::INVALID_INDEX )
124 // Connect to the Application's Init signal
125 mApplication.InitSignal().Connect( this, &TextLabelExample::Create );
130 // Nothing to do here.
134 * One-time setup in response to Application InitSignal.
136 void Create( Application& application )
138 Stage stage = Stage::GetCurrent();
140 stage.KeyEventSignal().Connect(this, &TextLabelExample::OnKeyEvent);
141 Vector2 stageSize = stage.GetSize();
143 mContainer = Control::New();
144 mContainer.SetName( "Container" );
145 mContainer.SetParentOrigin( ParentOrigin::CENTER );
146 mLayoutSize = Vector2(stageSize.width*0.6f, stageSize.width*0.6f);
147 mContainer.SetSize( mLayoutSize );
148 mContainer.SetDrawMode( DrawMode::OVERLAY_2D );
149 stage.Add( mContainer );
151 // Resize the center layout when the corner is grabbed
152 mGrabCorner = ImageView::New( BACKGROUND_IMAGE );
153 mGrabCorner.SetName( "GrabCorner" );
154 mGrabCorner.SetAnchorPoint( AnchorPoint::TOP_CENTER );
155 mGrabCorner.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
156 mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
157 mContainer.Add( mGrabCorner );
159 mPanGestureDetector = PanGestureDetector::New();
160 mPanGestureDetector.Attach( mGrabCorner );
161 mPanGestureDetector.DetectedSignal().Connect( this, &TextLabelExample::OnPan );
163 mLabel = TextLabel::New( "A Quick Brown Fox Jumps Over The Lazy Dog" );
165 mLabel.SetName( "TextLabel" );
166 mLabel.SetAnchorPoint( AnchorPoint::TOP_LEFT );
167 mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
168 mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
169 mLabel.SetProperty( TextLabel::Property::MULTI_LINE, true );
170 mLabel.SetProperty( DevelTextLabel::Property::TEXT_COLOR_ANIMATABLE, Color::GREEN );
171 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 1.0f, 1.0f ) );
172 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
173 mLabel.SetBackgroundColor( Color::WHITE );
174 mContainer.Add( mLabel );
176 mHueAngleIndex = mLabel.RegisterProperty( "hue", 0.0f );
177 Renderer bgRenderer = mLabel.GetRendererAt(0);
178 mOverrideMixColorIndex = DevelHandle::GetPropertyIndex( bgRenderer, ColorVisual::Property::MIX_COLOR );
180 Constraint constraint = Constraint::New<Vector3>( bgRenderer, mOverrideMixColorIndex, HSVColorConstraint(0.0f, 0.5f, 0.8f));
181 constraint.AddSource( Source( mLabel, mHueAngleIndex ) );
182 constraint.SetRemoveAction( Constraint::Discard );
185 Animation anim = Animation::New(50.0f);
186 anim.AnimateTo(Property(mLabel, mHueAngleIndex), 6.28318f);
187 anim.SetLooping(true);
190 // Animate the text color 3 times from source color to RED
191 Animation animation = Animation::New( 2.f );
192 animation.AnimateTo( Property( mLabel, DevelTextLabel::Property::TEXT_COLOR_ANIMATABLE ), Color::YELLOW, AlphaFunction::SIN );
193 animation.SetLoopCount( 3 );
196 Property::Value labelText = mLabel.GetProperty( TextLabel::Property::TEXT );
197 std::cout << "Displaying text: \"" << labelText.Get< std::string >() << "\"" << std::endl;
200 // Resize the text-label with pan gesture
201 void OnPan( Actor actor, const PanGesture& gesture )
203 // Reset mLayoutSize when the pan starts
204 if( gesture.state == Gesture::Started )
206 if( mLayoutSize.x < 2.0f )
208 mLayoutSize.x = 2.0f;
211 if( mLayoutSize.y < 2.0f )
213 mLayoutSize.y = 2.0f;
217 mLayoutSize.x += gesture.displacement.x * 2.0f;
218 mLayoutSize.y += gesture.displacement.y * 2.0f;
220 if( mLayoutSize.x >= 2.0f ||
221 mLayoutSize.y >= 2.0f )
223 // Avoid pixel mis-alignment issue
224 Vector2 clampedSize = Vector2( std::max( ConvertToEven( static_cast<int>( mLayoutSize.x )), 2 ),
225 std::max( ConvertToEven( static_cast<int>( mLayoutSize.y )), 2 ) );
227 mContainer.SetSize( clampedSize );
232 * Main key event handler
234 void OnKeyEvent(const KeyEvent& event)
236 if(event.state == KeyEvent::Down)
238 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
242 else if( event.IsCtrlModifier() )
244 switch( event.keyCode )
246 // Select rendering back-end
247 case KEY_ZERO: // fall through
250 mLabel.SetProperty( TextLabel::Property::RENDERING_BACKEND, event.keyCode - 10 );
253 case KEY_A: // Animate text colour
255 Animation animation = Animation::New( 2.f );
256 animation.AnimateTo( Property( mLabel, DevelTextLabel::Property::TEXT_COLOR_ANIMATABLE ), Color::RED, AlphaFunction::SIN );
257 animation.SetLoopCount( 3 );
261 case KEY_F: // Fill vertically
263 if( ResizePolicy::DIMENSION_DEPENDENCY == mLabel.GetResizePolicy(Dimension::HEIGHT) )
265 mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
269 mLabel.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
273 case KEY_H: // Horizontal alignment
275 if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
280 mLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
283 case KEY_V: // Vertical alignment
285 if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
290 mLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
293 case KEY_M: // Multi-line
295 bool multiLine = mLabel.GetProperty<bool>( TextLabel::Property::MULTI_LINE );
296 mLabel.SetProperty( TextLabel::Property::MULTI_LINE, !multiLine );
299 case KEY_L: // Language
301 const Language& language = LANGUAGES[ mLanguageId ];
303 mLabel.SetProperty( TextLabel::Property::TEXT, language.text );
305 if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
311 case KEY_S: // Shadow color
313 if( Color::BLACK == mLabel.GetProperty<Vector4>( TextLabel::Property::SHADOW_COLOR ) )
315 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::RED );
319 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
323 case KEY_U: // Markup
325 mLabel.SetProperty( TextLabel::Property::ENABLE_MARKUP, true );
326 mLabel.SetProperty( TextLabel::Property::TEXT, "<font family='DejaVuSerif' size='18'>H<color value='blue'>ello</color> <font weight='bold'>world</font> demo</font>" );
329 case KEY_PLUS: // Increase shadow offset
331 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
334 case KEY_MINUS: // Decrease shadow offset
336 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
347 Application& mApplication;
354 PanGestureDetector mPanGestureDetector;
358 unsigned int mLanguageId;
359 unsigned int mAlignment;
360 Property::Index mHueAngleIndex;
361 Property::Index mOverrideMixColorIndex;
364 void RunTest( Application& application )
366 TextLabelExample test( application );
368 application.MainLoop();
371 /** Entry point for Linux & Tizen applications */
372 int DALI_EXPORT_API main( int argc, char **argv )
374 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
376 RunTest( application );