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 "shared/multi-language-strings.h"
31 using namespace Dali::Toolkit;
32 using namespace MultiLanguageStrings;
36 const char* const BACKGROUND_IMAGE = DALI_IMAGE_DIR "button-up.9.png";
38 const unsigned int KEY_ZERO = 10;
39 const unsigned int KEY_ONE = 11;
40 const unsigned int KEY_H = 43;
41 const unsigned int KEY_V = 55;
42 const unsigned int KEY_M = 58;
43 const unsigned int KEY_L = 46;
44 const unsigned int KEY_S = 39;
45 const unsigned int KEY_PLUS = 21;
46 const unsigned int KEY_MINUS = 20;
48 const char* H_ALIGNMENT_STRING_TABLE[] =
55 const unsigned int H_ALIGNMENT_STRING_COUNT = sizeof( H_ALIGNMENT_STRING_TABLE ) / sizeof( H_ALIGNMENT_STRING_TABLE[0u] );
57 const char* V_ALIGNMENT_STRING_TABLE[] =
64 const unsigned int V_ALIGNMENT_STRING_COUNT = sizeof( V_ALIGNMENT_STRING_TABLE ) / sizeof( V_ALIGNMENT_STRING_TABLE[0u] );
66 int ConvertToEven(int value)
68 return (value % 2 == 0) ? value : (value + 1);
73 * @brief The main class of the demo.
75 class TextLabelExample : public ConnectionTracker
79 TextLabelExample( Application& application )
80 : mApplication( application ),
84 // Connect to the Application's Init signal
85 mApplication.InitSignal().Connect( this, &TextLabelExample::Create );
90 // Nothing to do here.
94 * One-time setup in response to Application InitSignal.
96 void Create( Application& application )
98 Stage stage = Stage::GetCurrent();
100 stage.KeyEventSignal().Connect(this, &TextLabelExample::OnKeyEvent);
101 Vector2 stageSize = stage.GetSize();
103 mContainer = Control::New();
104 mContainer.SetName( "Container" );
105 mContainer.SetParentOrigin( ParentOrigin::CENTER );
106 mContainer.SetResizePolicy( FIXED, ALL_DIMENSIONS );
107 mLayoutSize = Vector2(stageSize.width*0.6f, stageSize.width*0.6f);
108 mContainer.SetPreferredSize( mLayoutSize );
109 mContainer.SetBackgroundImage( ResourceImage::New( BACKGROUND_IMAGE ) );
110 mContainer.GetChildAt(0).SetZ(-1.0f);
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::BOTTOM_RIGHT );
117 mGrabCorner.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
118 mGrabCorner.SetResizePolicy( FIXED, ALL_DIMENSIONS );
119 mGrabCorner.SetPreferredSize( Vector2(stageSize.width*0.1f, stageSize.width*0.1f) );
120 mGrabCorner.SetZ(1.0f);
121 mContainer.Add( mGrabCorner );
123 mPanGestureDetector = PanGestureDetector::New();
124 mPanGestureDetector.Attach( mGrabCorner );
125 mPanGestureDetector.DetectedSignal().Connect( this, &TextLabelExample::OnPan );
127 mLabel = TextLabel::New( "A Quick Brown Fox Jumps Over The Lazy Dog" );
128 mLabel.SetName( "TextLabel" );
129 mLabel.SetAnchorPoint( AnchorPoint::TOP_LEFT );
130 mLabel.SetResizePolicy( FILL_TO_PARENT, HEIGHT );
131 mLabel.SetProperty( TextLabel::Property::MULTI_LINE, true );
132 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 1.0f, 1.0f ) );
133 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
134 mContainer.Add( mLabel );
136 Property::Value labelText = mLabel.GetProperty( TextLabel::Property::TEXT );
137 std::cout << "Displaying text: \"" << labelText.Get< std::string >() << "\"" << std::endl;
140 // Resize the text-label with pan gesture
141 void OnPan( Actor actor, const PanGesture& gesture )
143 mLayoutSize.x += gesture.displacement.x * 2.0f;
144 mLayoutSize.y += gesture.displacement.y * 2.0f;
146 if( mLayoutSize.x >= 2.0f &&
147 mLayoutSize.y >= 2.0f )
149 // Avoid pixel mis-alignment issue
150 Vector2 clampedSize = Vector2( ConvertToEven(static_cast<int>(mLayoutSize.x)),
151 ConvertToEven(static_cast<int>(mLayoutSize.y)) );
153 mContainer.SetPreferredSize( clampedSize );
158 * Main key event handler
160 void OnKeyEvent(const KeyEvent& event)
162 if(event.state == KeyEvent::Down)
164 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
168 else if( event.IsCtrlModifier() )
170 switch( event.keyCode )
172 case KEY_ZERO: // fall through
175 mLabel.SetProperty( TextLabel::Property::RENDERING_BACKEND, event.keyCode - 10 );
180 if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
185 mLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
190 if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
195 mLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
200 bool multiLine = mLabel.GetProperty<bool>( TextLabel::Property::MULTI_LINE );
201 mLabel.SetProperty( TextLabel::Property::MULTI_LINE, !multiLine );
206 const Language& language = LANGUAGES[ mLanguageId ];
208 mLabel.SetProperty( TextLabel::Property::TEXT, language.text );
210 if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
218 if( Color::BLACK == mLabel.GetProperty<Vector4>( TextLabel::Property::SHADOW_COLOR ) )
220 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::RED );
224 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
230 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
235 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
246 Application& mApplication;
253 PanGestureDetector mPanGestureDetector;
257 unsigned int mLanguageId;
258 unsigned int mAlignment;
261 void RunTest( Application& application )
263 TextLabelExample test( application );
265 application.MainLoop();
268 /** Entry point for Linux & Tizen applications */
269 int main( int argc, char **argv )
271 Application application = Application::New( &argc, &argv );
273 RunTest( application );