2 * Copyright (c) 2015 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-toolkit/dali-toolkit.h>
28 #include "shared/multi-language-strings.h"
29 #include "shared/view.h"
32 using namespace Dali::Toolkit;
33 using namespace MultiLanguageStrings;
37 const char* const BACKGROUND_IMAGE = DEMO_IMAGE_DIR "grab-handle.png";
39 const unsigned int KEY_ZERO = 10;
40 const unsigned int KEY_ONE = 11;
41 const unsigned int KEY_F = 41;
42 const unsigned int KEY_H = 43;
43 const unsigned int KEY_V = 55;
44 const unsigned int KEY_M = 58;
45 const unsigned int KEY_L = 46;
46 const unsigned int KEY_S = 39;
47 const unsigned int KEY_PLUS = 21;
48 const unsigned int KEY_MINUS = 20;
50 const char* H_ALIGNMENT_STRING_TABLE[] =
57 const unsigned int H_ALIGNMENT_STRING_COUNT = sizeof( H_ALIGNMENT_STRING_TABLE ) / sizeof( H_ALIGNMENT_STRING_TABLE[0u] );
59 const char* V_ALIGNMENT_STRING_TABLE[] =
66 const unsigned int V_ALIGNMENT_STRING_COUNT = sizeof( V_ALIGNMENT_STRING_TABLE ) / sizeof( V_ALIGNMENT_STRING_TABLE[0u] );
68 int ConvertToEven(int value)
70 return (value % 2 == 0) ? value : (value + 1);
75 * @brief The main class of the demo.
77 class TextLabelExample : public ConnectionTracker
81 TextLabelExample( Application& application )
82 : mApplication( application ),
86 // Connect to the Application's Init signal
87 mApplication.InitSignal().Connect( this, &TextLabelExample::Create );
92 // Nothing to do here.
96 * One-time setup in response to Application InitSignal.
98 void Create( Application& application )
100 Stage stage = Stage::GetCurrent();
102 stage.KeyEventSignal().Connect(this, &TextLabelExample::OnKeyEvent);
103 Vector2 stageSize = stage.GetSize();
105 mContainer = Control::New();
106 mContainer.SetName( "Container" );
107 mContainer.SetParentOrigin( ParentOrigin::CENTER );
108 mLayoutSize = Vector2(stageSize.width*0.6f, stageSize.width*0.6f);
109 mContainer.SetSize( mLayoutSize );
110 mContainer.SetDrawMode( DrawMode::OVERLAY_2D );
111 stage.Add( mContainer );
113 // Resize the center layout when the corner is grabbed
114 mGrabCorner = Control::New();
115 mGrabCorner.SetName( "GrabCorner" );
116 mGrabCorner.SetAnchorPoint( AnchorPoint::TOP_CENTER );
117 mGrabCorner.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
118 mGrabCorner.SetBackgroundImage( ResourceImage::New( BACKGROUND_IMAGE ) );
119 mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
120 mContainer.Add( mGrabCorner );
122 mPanGestureDetector = PanGestureDetector::New();
123 mPanGestureDetector.Attach( mGrabCorner );
124 mPanGestureDetector.DetectedSignal().Connect( this, &TextLabelExample::OnPan );
126 mLabel = TextLabel::New( "A Quick Brown Fox Jumps Over The Lazy Dog" );
127 mLabel.SetName( "TextLabel" );
128 mLabel.SetAnchorPoint( AnchorPoint::TOP_LEFT );
129 mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
130 mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
131 mLabel.SetProperty( TextLabel::Property::MULTI_LINE, true );
132 mLabel.SetProperty( TextLabel::Property::TEXT_COLOR, Color::BLUE );
133 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 1.0f, 1.0f ) );
134 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
135 mLabel.SetBackgroundColor( Color::WHITE );
136 mContainer.Add( mLabel );
138 Property::Value labelText = mLabel.GetProperty( TextLabel::Property::TEXT );
139 std::cout << "Displaying text: \"" << labelText.Get< std::string >() << "\"" << std::endl;
142 // Resize the text-label with pan gesture
143 void OnPan( Actor actor, const PanGesture& gesture )
145 // Reset mLayoutSize when the pan starts
146 if( gesture.state == Gesture::Started )
148 if( mLayoutSize.x < 2.0f )
150 mLayoutSize.x = 2.0f;
153 if( mLayoutSize.y < 2.0f )
155 mLayoutSize.y = 2.0f;
159 mLayoutSize.x += gesture.displacement.x * 2.0f;
160 mLayoutSize.y += gesture.displacement.y * 2.0f;
162 if( mLayoutSize.x >= 2.0f ||
163 mLayoutSize.y >= 2.0f )
165 // Avoid pixel mis-alignment issue
166 Vector2 clampedSize = Vector2( std::max( ConvertToEven( static_cast<int>( mLayoutSize.x )), 2 ),
167 std::max( ConvertToEven( static_cast<int>( mLayoutSize.y )), 2 ) );
169 mContainer.SetSize( clampedSize );
174 * Main key event handler
176 void OnKeyEvent(const KeyEvent& event)
178 if(event.state == KeyEvent::Down)
180 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
184 else if( event.IsCtrlModifier() )
186 switch( event.keyCode )
188 // Select rendering back-end
189 case KEY_ZERO: // fall through
192 mLabel.SetProperty( TextLabel::Property::RENDERING_BACKEND, event.keyCode - 10 );
195 case KEY_F: // Fill vertically
197 if( ResizePolicy::DIMENSION_DEPENDENCY == mLabel.GetResizePolicy(Dimension::HEIGHT) )
199 mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
203 mLabel.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
207 case KEY_H: // Horizontal alignment
209 if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
214 mLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
217 case KEY_V: // Vertical alignment
219 if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
224 mLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
227 case KEY_M: // Multi-line
229 bool multiLine = mLabel.GetProperty<bool>( TextLabel::Property::MULTI_LINE );
230 mLabel.SetProperty( TextLabel::Property::MULTI_LINE, !multiLine );
233 case KEY_L: // Language
235 const Language& language = LANGUAGES[ mLanguageId ];
237 mLabel.SetProperty( TextLabel::Property::TEXT, language.text );
239 if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
245 case KEY_S: // Shadow color
247 if( Color::BLACK == mLabel.GetProperty<Vector4>( TextLabel::Property::SHADOW_COLOR ) )
249 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::RED );
253 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
257 case KEY_PLUS: // Increase shadow offset
259 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
262 case KEY_MINUS: // Decrease shadow offset
264 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
275 Application& mApplication;
282 PanGestureDetector mPanGestureDetector;
286 unsigned int mLanguageId;
287 unsigned int mAlignment;
290 void RunTest( Application& application )
292 TextLabelExample test( application );
294 application.MainLoop();
297 /** Entry point for Linux & Tizen applications */
298 int DALI_EXPORT_API main( int argc, char **argv )
300 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
302 RunTest( application );