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"
29 #include "shared/view.h"
32 using namespace Dali::Toolkit;
33 using namespace MultiLanguageStrings;
37 const char* const BACKGROUND_IMAGE = DALI_IMAGE_DIR "button-up.9.png";
39 const unsigned int KEY_ZERO = 10;
40 const unsigned int KEY_ONE = 11;
41 const unsigned int KEY_H = 43;
42 const unsigned int KEY_V = 55;
43 const unsigned int KEY_M = 58;
44 const unsigned int KEY_L = 46;
45 const unsigned int KEY_S = 39;
46 const unsigned int KEY_PLUS = 21;
47 const unsigned int KEY_MINUS = 20;
49 const char* H_ALIGNMENT_STRING_TABLE[] =
56 const unsigned int H_ALIGNMENT_STRING_COUNT = sizeof( H_ALIGNMENT_STRING_TABLE ) / sizeof( H_ALIGNMENT_STRING_TABLE[0u] );
58 const char* V_ALIGNMENT_STRING_TABLE[] =
65 const unsigned int V_ALIGNMENT_STRING_COUNT = sizeof( V_ALIGNMENT_STRING_TABLE ) / sizeof( V_ALIGNMENT_STRING_TABLE[0u] );
67 int ConvertToEven(int value)
69 return (value % 2 == 0) ? value : (value + 1);
74 * @brief The main class of the demo.
76 class TextLabelExample : public ConnectionTracker
80 TextLabelExample( Application& application )
81 : mApplication( application ),
85 // Connect to the Application's Init signal
86 mApplication.InitSignal().Connect( this, &TextLabelExample::Create );
91 // Nothing to do here.
95 * One-time setup in response to Application InitSignal.
97 void Create( Application& application )
99 DemoHelper::RequestThemeChange();
101 Stage stage = Stage::GetCurrent();
103 stage.KeyEventSignal().Connect(this, &TextLabelExample::OnKeyEvent);
104 Vector2 stageSize = stage.GetSize();
106 mContainer = Control::New();
107 mContainer.SetName( "Container" );
108 mContainer.SetParentOrigin( ParentOrigin::CENTER );
109 mContainer.SetResizePolicy( FIXED, ALL_DIMENSIONS );
110 mLayoutSize = Vector2(stageSize.width*0.6f, stageSize.width*0.6f);
111 mContainer.SetSize( mLayoutSize );
112 mContainer.SetBackgroundImage( ResourceImage::New( BACKGROUND_IMAGE ) );
113 mContainer.GetChildAt(0).SetZ(-1.0f);
114 stage.Add( mContainer );
116 // Resize the center layout when the corner is grabbed
117 mGrabCorner = Control::New();
118 mGrabCorner.SetName( "GrabCorner" );
119 mGrabCorner.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
120 mGrabCorner.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
121 mGrabCorner.SetResizePolicy( FIXED, ALL_DIMENSIONS );
122 mGrabCorner.SetSize( Vector2(stageSize.width*0.1f, stageSize.width*0.1f) );
123 mGrabCorner.SetZ(1.0f);
124 mContainer.Add( mGrabCorner );
126 mPanGestureDetector = PanGestureDetector::New();
127 mPanGestureDetector.Attach( mGrabCorner );
128 mPanGestureDetector.DetectedSignal().Connect( this, &TextLabelExample::OnPan );
130 mLabel = TextLabel::New( "A Quick Brown Fox Jumps Over The Lazy Dog" );
131 mLabel.SetName( "TextLabel" );
132 mLabel.SetAnchorPoint( AnchorPoint::TOP_LEFT );
133 mLabel.SetResizePolicy( FILL_TO_PARENT, WIDTH );
134 mLabel.SetResizePolicy( DIMENSION_DEPENDENCY, HEIGHT );
135 mLabel.SetProperty( TextLabel::Property::MULTI_LINE, true );
136 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 1.0f, 1.0f ) );
137 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
138 mContainer.Add( mLabel );
140 Property::Value labelText = mLabel.GetProperty( TextLabel::Property::TEXT );
141 std::cout << "Displaying text: \"" << labelText.Get< std::string >() << "\"" << std::endl;
144 // Resize the text-label with pan gesture
145 void OnPan( Actor actor, const PanGesture& gesture )
147 mLayoutSize.x += gesture.displacement.x * 2.0f;
148 mLayoutSize.y += gesture.displacement.y * 2.0f;
150 if( mLayoutSize.x >= 2.0f &&
151 mLayoutSize.y >= 2.0f )
153 // Avoid pixel mis-alignment issue
154 Vector2 clampedSize = Vector2( ConvertToEven(static_cast<int>(mLayoutSize.x)),
155 ConvertToEven(static_cast<int>(mLayoutSize.y)) );
157 mContainer.SetSize( clampedSize );
162 * Main key event handler
164 void OnKeyEvent(const KeyEvent& event)
166 if(event.state == KeyEvent::Down)
168 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
172 else if( event.IsCtrlModifier() )
174 switch( event.keyCode )
176 case KEY_ZERO: // fall through
179 mLabel.SetProperty( TextLabel::Property::RENDERING_BACKEND, event.keyCode - 10 );
184 if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
189 mLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
194 if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
199 mLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
204 bool multiLine = mLabel.GetProperty<bool>( TextLabel::Property::MULTI_LINE );
205 mLabel.SetProperty( TextLabel::Property::MULTI_LINE, !multiLine );
210 const Language& language = LANGUAGES[ mLanguageId ];
212 mLabel.SetProperty( TextLabel::Property::TEXT, language.text );
214 if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
222 if( Color::BLACK == mLabel.GetProperty<Vector4>( TextLabel::Property::SHADOW_COLOR ) )
224 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::RED );
228 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
234 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
239 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
250 Application& mApplication;
257 PanGestureDetector mPanGestureDetector;
261 unsigned int mLanguageId;
262 unsigned int mAlignment;
265 void RunTest( Application& application )
267 TextLabelExample test( application );
269 application.MainLoop();
272 /** Entry point for Linux & Tizen applications */
273 int main( int argc, char **argv )
275 Application application = Application::New( &argc, &argv );
277 RunTest( application );