Merge branch 'tizen' into devel/new_mesh
[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 #include "shared/view.h"
30
31 using namespace Dali;
32 using namespace Dali::Toolkit;
33 using namespace MultiLanguageStrings;
34
35 namespace
36 {
37   const char* const BACKGROUND_IMAGE = DALI_IMAGE_DIR "button-up.9.png";
38
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;
49
50   const char* H_ALIGNMENT_STRING_TABLE[] =
51   {
52     "BEGIN",
53     "CENTER",
54     "END"
55   };
56
57   const unsigned int H_ALIGNMENT_STRING_COUNT = sizeof( H_ALIGNMENT_STRING_TABLE ) / sizeof( H_ALIGNMENT_STRING_TABLE[0u] );
58
59   const char* V_ALIGNMENT_STRING_TABLE[] =
60   {
61     "TOP",
62     "CENTER",
63     "BOTTOM"
64   };
65
66   const unsigned int V_ALIGNMENT_STRING_COUNT = sizeof( V_ALIGNMENT_STRING_TABLE ) / sizeof( V_ALIGNMENT_STRING_TABLE[0u] );
67
68   int ConvertToEven(int value)
69   {
70     return (value % 2 == 0) ? value : (value + 1);
71   }
72 }
73
74 /**
75  * @brief The main class of the demo.
76  */
77 class TextLabelExample : public ConnectionTracker
78 {
79 public:
80
81   TextLabelExample( Application& application )
82   : mApplication( application ),
83     mLanguageId( 0u ),
84     mAlignment( 0u )
85   {
86     // Connect to the Application's Init signal
87     mApplication.InitSignal().Connect( this, &TextLabelExample::Create );
88   }
89
90   ~TextLabelExample()
91   {
92     // Nothing to do here.
93   }
94
95   /**
96    * One-time setup in response to Application InitSignal.
97    */
98   void Create( Application& application )
99   {
100     DemoHelper::RequestThemeChange();
101
102     Stage stage = Stage::GetCurrent();
103
104     stage.KeyEventSignal().Connect(this, &TextLabelExample::OnKeyEvent);
105     Vector2 stageSize = stage.GetSize();
106
107     mContainer = Control::New();
108     mContainer.SetName( "Container" );
109     mContainer.SetParentOrigin( ParentOrigin::CENTER );
110     mLayoutSize = Vector2(stageSize.width*0.6f, stageSize.width*0.6f);
111     mContainer.SetSize( mLayoutSize );
112     stage.Add( mContainer );
113
114     // Resize the center layout when the corner is grabbed
115     mGrabCorner = Control::New();
116     mGrabCorner.SetName( "GrabCorner" );
117     mGrabCorner.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
118     mGrabCorner.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
119     mGrabCorner.SetSize( Vector2(stageSize.width*0.1f, stageSize.width*0.1f) );
120     mGrabCorner.SetZ(1.0f);
121     mGrabCorner.SetBackgroundColor( Color::YELLOW );
122     mContainer.Add( mGrabCorner );
123
124     mPanGestureDetector = PanGestureDetector::New();
125     mPanGestureDetector.Attach( mGrabCorner );
126     mPanGestureDetector.DetectedSignal().Connect( this, &TextLabelExample::OnPan );
127
128     mLabel = TextLabel::New( "A Quick Brown Fox Jumps Over The Lazy Dog" );
129     mLabel.SetName( "TextLabel" );
130     mLabel.SetAnchorPoint( AnchorPoint::TOP_LEFT );
131     mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
132     mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
133     mLabel.SetProperty( TextLabel::Property::MULTI_LINE, true );
134     mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 1.0f, 1.0f ) );
135     mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
136     mLabel.SetBackgroundColor( Vector4(0.3f,0.3f,0.6f,1.0f) );
137     mContainer.Add( mLabel );
138
139     Property::Value labelText = mLabel.GetProperty( TextLabel::Property::TEXT );
140     std::cout << "Displaying text: \"" << labelText.Get< std::string >() << "\"" << std::endl;
141   }
142
143   // Resize the text-label with pan gesture
144   void OnPan( Actor actor, const PanGesture& gesture )
145   {
146     mLayoutSize.x += gesture.displacement.x * 2.0f;
147     mLayoutSize.y += gesture.displacement.y * 2.0f;
148
149     if( mLayoutSize.x >= 2.0f &&
150         mLayoutSize.y >= 2.0f )
151     {
152       // Avoid pixel mis-alignment issue
153       Vector2 clampedSize = Vector2( ConvertToEven(static_cast<int>(mLayoutSize.x)),
154                                      ConvertToEven(static_cast<int>(mLayoutSize.y)) );
155
156       mContainer.SetSize( clampedSize );
157     }
158   }
159
160   /**
161    * Main key event handler
162    */
163   void OnKeyEvent(const KeyEvent& event)
164   {
165     if(event.state == KeyEvent::Down)
166     {
167       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
168       {
169         mApplication.Quit();
170       }
171       else if( event.IsCtrlModifier() )
172       {
173         switch( event.keyCode )
174         {
175           // Select rendering back-end
176           case KEY_ZERO: // fall through
177           case KEY_ONE:
178           {
179             mLabel.SetProperty( TextLabel::Property::RENDERING_BACKEND, event.keyCode - 10 );
180             break;
181           }
182           case KEY_F: // Fill vertically
183           {
184             if( ResizePolicy::DIMENSION_DEPENDENCY == mLabel.GetResizePolicy(Dimension::HEIGHT) )
185             {
186               mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
187             }
188             else
189             {
190               mLabel.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
191             }
192             break;
193           }
194           case KEY_H: // Horizontal alignment
195           {
196             if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
197             {
198               mAlignment = 0u;
199             }
200
201             mLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
202             break;
203           }
204           case KEY_V: // Vertical alignment
205           {
206             if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
207             {
208               mAlignment = 0u;
209             }
210
211             mLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
212             break;
213           }
214           case KEY_M: // Multi-line
215           {
216             bool multiLine = mLabel.GetProperty<bool>( TextLabel::Property::MULTI_LINE );
217             mLabel.SetProperty( TextLabel::Property::MULTI_LINE, !multiLine );
218             break;
219           }
220           case KEY_L: // Language
221           {
222             const Language& language = LANGUAGES[ mLanguageId ];
223
224             mLabel.SetProperty( TextLabel::Property::TEXT, language.text );
225
226             if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
227             {
228               mLanguageId = 0u;
229             }
230             break;
231           }
232           case KEY_S: // Shadow color
233           {
234             if( Color::BLACK == mLabel.GetProperty<Vector4>( TextLabel::Property::SHADOW_COLOR ) )
235             {
236               mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::RED );
237             }
238             else
239             {
240               mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
241             }
242             break;
243           }
244           case KEY_PLUS: // Increase shadow offset
245           {
246             mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
247             break;
248           }
249           case KEY_MINUS: // Decrease shadow offset
250           {
251             mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
252             break;
253           }
254
255         }
256       }
257     }
258   }
259
260 private:
261
262   Application& mApplication;
263
264   TextLabel mLabel;
265
266   Control mContainer;
267   Control mGrabCorner;
268
269   PanGestureDetector mPanGestureDetector;
270
271   Vector2 mLayoutSize;
272
273   unsigned int mLanguageId;
274   unsigned int mAlignment;
275 };
276
277 void RunTest( Application& application )
278 {
279   TextLabelExample test( application );
280
281   application.MainLoop();
282 }
283
284 /** Entry point for Linux & Tizen applications */
285 int main( int argc, char **argv )
286 {
287   Application application = Application::New( &argc, &argv );
288
289   RunTest( application );
290
291   return 0;
292 }