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>
25 #include <dali/public-api/text-abstraction/text-abstraction.h>
28 #include "center-layout.h"
29 #include "shared/multi-language-strings.h"
32 using namespace Dali::Toolkit;
33 using namespace MultiLanguageStrings;
37 const unsigned int KEY_ZERO = 10;
38 const unsigned int KEY_ONE = 11;
39 const unsigned int KEY_H = 43;
40 const unsigned int KEY_V = 55;
41 const unsigned int KEY_M = 58;
42 const unsigned int KEY_L = 46;
43 const unsigned int KEY_S = 39;
44 const unsigned int KEY_PLUS = 21;
45 const unsigned int KEY_MINUS = 20;
47 const char* H_ALIGNMENT_STRING_TABLE[] =
54 const unsigned int H_ALIGNMENT_STRING_COUNT = sizeof( H_ALIGNMENT_STRING_TABLE ) / sizeof( H_ALIGNMENT_STRING_TABLE[0u] );
56 const char* V_ALIGNMENT_STRING_TABLE[] =
63 const unsigned int V_ALIGNMENT_STRING_COUNT = sizeof( V_ALIGNMENT_STRING_TABLE ) / sizeof( V_ALIGNMENT_STRING_TABLE[0u] );
67 * @brief The main class of the demo.
69 class TextLabelExample : public ConnectionTracker
73 TextLabelExample( Application& application )
74 : mApplication( application ),
78 // Connect to the Application's Init signal
79 mApplication.InitSignal().Connect( this, &TextLabelExample::Create );
84 // Nothing to do here.
88 * One-time setup in response to Application InitSignal.
90 void Create( Application& application )
92 Stage stage = Stage::GetCurrent();
94 stage.SetBackgroundColor( Color::BLACK );
95 stage.KeyEventSignal().Connect(this, &TextLabelExample::OnKeyEvent);
96 Vector2 stageSize = stage.GetSize();
98 CenterLayout centerLayout = CenterLayout::New();
99 centerLayout.SetParentOrigin( ParentOrigin::CENTER );
100 centerLayout.SetSize( stageSize.width*0.6f, stageSize.width*0.6f );
101 stage.Add( centerLayout );
103 mLabel = TextLabel::New();
104 mLabel.SetBackgroundColor( Vector4(0.3f,0.3f,0.6f,1.0f) );
105 centerLayout.Add( mLabel );
107 mLabel.SetProperty( TextLabel::Property::MULTI_LINE, true );
108 mLabel.SetProperty( TextLabel::Property::TEXT, "A Quick Brown Fox Jumps Over The Lazy Dog" );
109 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 1.0f, 1.0f ) );
110 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
112 Property::Value labelText = mLabel.GetProperty( TextLabel::Property::TEXT );
113 std::cout << "Displaying text: \"" << labelText.Get< std::string >() << "\"" << std::endl;
117 * Main key event handler
119 void OnKeyEvent(const KeyEvent& event)
121 if(event.state == KeyEvent::Down)
123 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
127 else if( event.IsCtrlModifier() )
129 switch( event.keyCode )
131 case KEY_ZERO: // fall through
134 mLabel.SetProperty( TextLabel::Property::RENDERING_BACKEND, event.keyCode - 10 );
139 if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
144 mLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
149 if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
154 mLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
159 bool multiLine = mLabel.GetProperty<bool>( TextLabel::Property::MULTI_LINE );
160 mLabel.SetProperty( TextLabel::Property::MULTI_LINE, !multiLine );
165 const Language& language = LANGUAGES[ mLanguageId ];
167 mLabel.SetProperty( TextLabel::Property::TEXT, language.text );
169 if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
177 if( Color::BLACK == mLabel.GetProperty<Vector4>( TextLabel::Property::SHADOW_COLOR ) )
179 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::RED );
183 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
189 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
194 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
205 Application& mApplication;
209 unsigned int mLanguageId;
210 unsigned int mAlignment;
213 void RunTest( Application& application )
215 TextLabelExample test( application );
217 application.MainLoop();
220 /** Entry point for Linux & Tizen applications */
221 int main( int argc, char **argv )
223 Application application = Application::New( &argc, &argv );
225 RunTest( application );