a8476bfcc3cd0fbb90bde299520483c7a1433e28
[platform/core/uifw/dali-demo.git] / examples / text-label / text-label-example.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 /**
19  * @file text-label-example.cpp
20  * @brief Basic usage of TextLabel control
21  */
22
23 // EXTERNAL INCLUDES
24 #include <dali-toolkit/dali-toolkit.h>
25 #include <dali/public-api/text-abstraction/text-abstraction.h>
26
27 // INTERNAL INCLUDES
28 #include "shared/multi-language-strings.h"
29
30 using namespace Dali;
31 using namespace Dali::Toolkit;
32 using namespace MultiLanguageStrings;
33
34 namespace
35 {
36   const char* const BACKGROUND_IMAGE = DALI_IMAGE_DIR "button-up.9.png";
37
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;
47
48   const char* H_ALIGNMENT_STRING_TABLE[] =
49   {
50     "BEGIN",
51     "CENTER",
52     "END"
53   };
54
55   const unsigned int H_ALIGNMENT_STRING_COUNT = sizeof( H_ALIGNMENT_STRING_TABLE ) / sizeof( H_ALIGNMENT_STRING_TABLE[0u] );
56
57   const char* V_ALIGNMENT_STRING_TABLE[] =
58   {
59     "TOP",
60     "CENTER",
61     "BOTTOM"
62   };
63
64   const unsigned int V_ALIGNMENT_STRING_COUNT = sizeof( V_ALIGNMENT_STRING_TABLE ) / sizeof( V_ALIGNMENT_STRING_TABLE[0u] );
65
66   int ConvertToEven(int value)
67   {
68     return (value % 2 == 0) ? value : (value + 1);
69   }
70 }
71
72 /**
73  * @brief The main class of the demo.
74  */
75 class TextLabelExample : public ConnectionTracker
76 {
77 public:
78
79   TextLabelExample( Application& application )
80   : mApplication( application ),
81     mLanguageId( 0u ),
82     mAlignment( 0u )
83   {
84     // Connect to the Application's Init signal
85     mApplication.InitSignal().Connect( this, &TextLabelExample::Create );
86   }
87
88   ~TextLabelExample()
89   {
90     // Nothing to do here.
91   }
92
93   /**
94    * One-time setup in response to Application InitSignal.
95    */
96   void Create( Application& application )
97   {
98     Stage stage = Stage::GetCurrent();
99
100     stage.KeyEventSignal().Connect(this, &TextLabelExample::OnKeyEvent);
101     Vector2 stageSize = stage.GetSize();
102
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 );
112
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 );
122
123     mPanGestureDetector = PanGestureDetector::New();
124     mPanGestureDetector.Attach( mGrabCorner );
125     mPanGestureDetector.DetectedSignal().Connect( this, &TextLabelExample::OnPan );
126
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.SetDimensionDependency( HEIGHT, WIDTH );
131     mLabel.SetResizePolicy( FILL_TO_PARENT, WIDTH );
132     mLabel.SetProperty( TextLabel::Property::MULTI_LINE, true );
133     mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 1.0f, 1.0f ) );
134     mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
135     mContainer.Add( mLabel );
136
137     Property::Value labelText = mLabel.GetProperty( TextLabel::Property::TEXT );
138     std::cout << "Displaying text: \"" << labelText.Get< std::string >() << "\"" << std::endl;
139   }
140
141   // Resize the text-label with pan gesture
142   void OnPan( Actor actor, const PanGesture& gesture )
143   {
144     mLayoutSize.x += gesture.displacement.x * 2.0f;
145     mLayoutSize.y += gesture.displacement.y * 2.0f;
146
147     if( mLayoutSize.x >= 2.0f &&
148         mLayoutSize.y >= 2.0f )
149     {
150       // Avoid pixel mis-alignment issue
151       Vector2 clampedSize = Vector2( ConvertToEven(static_cast<int>(mLayoutSize.x)),
152                                      ConvertToEven(static_cast<int>(mLayoutSize.y)) );
153
154       mContainer.SetPreferredSize( clampedSize );
155     }
156   }
157
158   /**
159    * Main key event handler
160    */
161   void OnKeyEvent(const KeyEvent& event)
162   {
163     if(event.state == KeyEvent::Down)
164     {
165       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
166       {
167         mApplication.Quit();
168       }
169       else if( event.IsCtrlModifier() )
170       {
171         switch( event.keyCode )
172         {
173           case KEY_ZERO: // fall through
174           case KEY_ONE:
175           {
176             mLabel.SetProperty( TextLabel::Property::RENDERING_BACKEND, event.keyCode - 10 );
177             break;
178           }
179           case KEY_H:
180           {
181             if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
182             {
183               mAlignment = 0u;
184             }
185
186             mLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
187             break;
188           }
189           case KEY_V:
190           {
191             if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
192             {
193               mAlignment = 0u;
194             }
195
196             mLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
197             break;
198           }
199           case KEY_M:
200           {
201             bool multiLine = mLabel.GetProperty<bool>( TextLabel::Property::MULTI_LINE );
202             mLabel.SetProperty( TextLabel::Property::MULTI_LINE, !multiLine );
203             break;
204           }
205           case KEY_L:
206           {
207             const Language& language = LANGUAGES[ mLanguageId ];
208
209             mLabel.SetProperty( TextLabel::Property::TEXT, language.text );
210
211             if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
212             {
213               mLanguageId = 0u;
214             }
215             break;
216           }
217           case KEY_S:
218           {
219             if( Color::BLACK == mLabel.GetProperty<Vector4>( TextLabel::Property::SHADOW_COLOR ) )
220             {
221               mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::RED );
222             }
223             else
224             {
225               mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
226             }
227             break;
228           }
229           case KEY_PLUS:
230           {
231             mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
232             break;
233           }
234           case KEY_MINUS:
235           {
236             mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
237             break;
238           }
239
240         }
241       }
242     }
243   }
244
245 private:
246
247   Application& mApplication;
248
249   TextLabel mLabel;
250
251   Control mContainer;
252   Actor mGrabCorner;
253
254   PanGestureDetector mPanGestureDetector;
255
256   Vector2 mLayoutSize;
257
258   unsigned int mLanguageId;
259   unsigned int mAlignment;
260 };
261
262 void RunTest( Application& application )
263 {
264   TextLabelExample test( application );
265
266   application.MainLoop();
267 }
268
269 /** Entry point for Linux & Tizen applications */
270 int main( int argc, char **argv )
271 {
272   Application application = Application::New( &argc, &argv );
273
274   RunTest( application );
275
276   return 0;
277 }