Merge branch devel/master (1.0.49) into tizen
[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 <iostream>
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 "grab-handle.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     Stage stage = Stage::GetCurrent();
101
102     stage.KeyEventSignal().Connect(this, &TextLabelExample::OnKeyEvent);
103     Vector2 stageSize = stage.GetSize();
104
105     mContainer = Control::New();
106     mContainer.SetName( "Container" );
107     mContainer.SetParentOrigin( ParentOrigin::CENTER );
108     mLayoutSize = Vector2(stageSize.width*0.6f, stageSize.width*0.6f);
109     mContainer.SetSize( mLayoutSize );
110     mContainer.SetDrawMode( DrawMode::OVERLAY_2D );
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::TOP_CENTER );
117     mGrabCorner.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
118     mGrabCorner.SetBackgroundImage( ResourceImage::New( BACKGROUND_IMAGE ) );
119     mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
120     mContainer.Add( mGrabCorner );
121
122     mPanGestureDetector = PanGestureDetector::New();
123     mPanGestureDetector.Attach( mGrabCorner );
124     mPanGestureDetector.DetectedSignal().Connect( this, &TextLabelExample::OnPan );
125
126     mLabel = TextLabel::New( "A Quick Brown Fox Jumps Over The Lazy Dog" );
127     mLabel.SetName( "TextLabel" );
128     mLabel.SetAnchorPoint( AnchorPoint::TOP_LEFT );
129     mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
130     mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
131     mLabel.SetProperty( TextLabel::Property::MULTI_LINE, true );
132     mLabel.SetProperty( TextLabel::Property::TEXT_COLOR, Color::BLUE );
133     mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 1.0f, 1.0f ) );
134     mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
135     mLabel.SetBackgroundColor( Color::WHITE );
136     mContainer.Add( mLabel );
137
138     Property::Value labelText = mLabel.GetProperty( TextLabel::Property::TEXT );
139     std::cout << "Displaying text: \"" << labelText.Get< std::string >() << "\"" << std::endl;
140   }
141
142   // Resize the text-label with pan gesture
143   void OnPan( Actor actor, const PanGesture& gesture )
144   {
145     mLayoutSize.x += gesture.displacement.x * 2.0f;
146     mLayoutSize.y += gesture.displacement.y * 2.0f;
147
148     if( mLayoutSize.x >= 2.0f &&
149         mLayoutSize.y >= 2.0f )
150     {
151       // Avoid pixel mis-alignment issue
152       Vector2 clampedSize = Vector2( ConvertToEven(static_cast<int>(mLayoutSize.x)),
153                                      ConvertToEven(static_cast<int>(mLayoutSize.y)) );
154
155       mContainer.SetSize( clampedSize );
156     }
157   }
158
159   /**
160    * Main key event handler
161    */
162   void OnKeyEvent(const KeyEvent& event)
163   {
164     if(event.state == KeyEvent::Down)
165     {
166       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
167       {
168         mApplication.Quit();
169       }
170       else if( event.IsCtrlModifier() )
171       {
172         switch( event.keyCode )
173         {
174           // Select rendering back-end
175           case KEY_ZERO: // fall through
176           case KEY_ONE:
177           {
178             mLabel.SetProperty( TextLabel::Property::RENDERING_BACKEND, event.keyCode - 10 );
179             break;
180           }
181           case KEY_F: // Fill vertically
182           {
183             if( ResizePolicy::DIMENSION_DEPENDENCY == mLabel.GetResizePolicy(Dimension::HEIGHT) )
184             {
185               mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
186             }
187             else
188             {
189               mLabel.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
190             }
191             break;
192           }
193           case KEY_H: // Horizontal alignment
194           {
195             if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
196             {
197               mAlignment = 0u;
198             }
199
200             mLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
201             break;
202           }
203           case KEY_V: // Vertical alignment
204           {
205             if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
206             {
207               mAlignment = 0u;
208             }
209
210             mLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
211             break;
212           }
213           case KEY_M: // Multi-line
214           {
215             bool multiLine = mLabel.GetProperty<bool>( TextLabel::Property::MULTI_LINE );
216             mLabel.SetProperty( TextLabel::Property::MULTI_LINE, !multiLine );
217             break;
218           }
219           case KEY_L: // Language
220           {
221             const Language& language = LANGUAGES[ mLanguageId ];
222
223             mLabel.SetProperty( TextLabel::Property::TEXT, language.text );
224
225             if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
226             {
227               mLanguageId = 0u;
228             }
229             break;
230           }
231           case KEY_S: // Shadow color
232           {
233             if( Color::BLACK == mLabel.GetProperty<Vector4>( TextLabel::Property::SHADOW_COLOR ) )
234             {
235               mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::RED );
236             }
237             else
238             {
239               mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
240             }
241             break;
242           }
243           case KEY_PLUS: // Increase shadow offset
244           {
245             mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
246             break;
247           }
248           case KEY_MINUS: // Decrease shadow offset
249           {
250             mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
251             break;
252           }
253
254         }
255       }
256     }
257   }
258
259 private:
260
261   Application& mApplication;
262
263   TextLabel mLabel;
264
265   Control mContainer;
266   Control mGrabCorner;
267
268   PanGestureDetector mPanGestureDetector;
269
270   Vector2 mLayoutSize;
271
272   unsigned int mLanguageId;
273   unsigned int mAlignment;
274 };
275
276 void RunTest( Application& application )
277 {
278   TextLabelExample test( application );
279
280   application.MainLoop();
281 }
282
283 /** Entry point for Linux & Tizen applications */
284 int main( int argc, char **argv )
285 {
286   Application application = Application::New( &argc, &argv, DALI_DEMO_THEME_PATH );
287
288   RunTest( application );
289
290   return 0;
291 }