fc89fa890ba2c3b46b4e8117e8721663baec67df
[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_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;
48
49   const char* H_ALIGNMENT_STRING_TABLE[] =
50   {
51     "BEGIN",
52     "CENTER",
53     "END"
54   };
55
56   const unsigned int H_ALIGNMENT_STRING_COUNT = sizeof( H_ALIGNMENT_STRING_TABLE ) / sizeof( H_ALIGNMENT_STRING_TABLE[0u] );
57
58   const char* V_ALIGNMENT_STRING_TABLE[] =
59   {
60     "TOP",
61     "CENTER",
62     "BOTTOM"
63   };
64
65   const unsigned int V_ALIGNMENT_STRING_COUNT = sizeof( V_ALIGNMENT_STRING_TABLE ) / sizeof( V_ALIGNMENT_STRING_TABLE[0u] );
66
67   int ConvertToEven(int value)
68   {
69     return (value % 2 == 0) ? value : (value + 1);
70   }
71 }
72
73 /**
74  * @brief The main class of the demo.
75  */
76 class TextLabelExample : public ConnectionTracker
77 {
78 public:
79
80   TextLabelExample( Application& application )
81   : mApplication( application ),
82     mLanguageId( 0u ),
83     mAlignment( 0u )
84   {
85     // Connect to the Application's Init signal
86     mApplication.InitSignal().Connect( this, &TextLabelExample::Create );
87   }
88
89   ~TextLabelExample()
90   {
91     // Nothing to do here.
92   }
93
94   /**
95    * One-time setup in response to Application InitSignal.
96    */
97   void Create( Application& application )
98   {
99     DemoHelper::RequestThemeChange();
100
101     Stage stage = Stage::GetCurrent();
102
103     stage.KeyEventSignal().Connect(this, &TextLabelExample::OnKeyEvent);
104     Vector2 stageSize = stage.GetSize();
105
106     mContainer = Control::New();
107     mContainer.SetName( "Container" );
108     mContainer.SetParentOrigin( ParentOrigin::CENTER );
109     mLayoutSize = Vector2(stageSize.width*0.6f, stageSize.width*0.6f);
110     mContainer.SetSize( mLayoutSize );
111     mContainer.SetBackgroundImage( ResourceImage::New( BACKGROUND_IMAGE ) );
112     mContainer.GetChildAt(0).SetZ(-1.0f);
113     stage.Add( mContainer );
114
115     // Resize the center layout when the corner is grabbed
116     mGrabCorner = Control::New();
117     mGrabCorner.SetName( "GrabCorner" );
118     mGrabCorner.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
119     mGrabCorner.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
120     mGrabCorner.SetSize( Vector2(stageSize.width*0.1f, stageSize.width*0.1f) );
121     mGrabCorner.SetZ(1.0f);
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::DIMENSION_DEPENDENCY, 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     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           case KEY_ZERO: // fall through
175           case KEY_ONE:
176           {
177             mLabel.SetProperty( TextLabel::Property::RENDERING_BACKEND, event.keyCode - 10 );
178             break;
179           }
180           case KEY_H:
181           {
182             if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
183             {
184               mAlignment = 0u;
185             }
186
187             mLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
188             break;
189           }
190           case KEY_V:
191           {
192             if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
193             {
194               mAlignment = 0u;
195             }
196
197             mLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
198             break;
199           }
200           case KEY_M:
201           {
202             bool multiLine = mLabel.GetProperty<bool>( TextLabel::Property::MULTI_LINE );
203             mLabel.SetProperty( TextLabel::Property::MULTI_LINE, !multiLine );
204             break;
205           }
206           case KEY_L:
207           {
208             const Language& language = LANGUAGES[ mLanguageId ];
209
210             mLabel.SetProperty( TextLabel::Property::TEXT, language.text );
211
212             if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
213             {
214               mLanguageId = 0u;
215             }
216             break;
217           }
218           case KEY_S:
219           {
220             if( Color::BLACK == mLabel.GetProperty<Vector4>( TextLabel::Property::SHADOW_COLOR ) )
221             {
222               mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::RED );
223             }
224             else
225             {
226               mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
227             }
228             break;
229           }
230           case KEY_PLUS:
231           {
232             mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
233             break;
234           }
235           case KEY_MINUS:
236           {
237             mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
238             break;
239           }
240
241         }
242       }
243     }
244   }
245
246 private:
247
248   Application& mApplication;
249
250   TextLabel mLabel;
251
252   Control mContainer;
253   Actor mGrabCorner;
254
255   PanGestureDetector mPanGestureDetector;
256
257   Vector2 mLayoutSize;
258
259   unsigned int mLanguageId;
260   unsigned int mAlignment;
261 };
262
263 void RunTest( Application& application )
264 {
265   TextLabelExample test( application );
266
267   application.MainLoop();
268 }
269
270 /** Entry point for Linux & Tizen applications */
271 int main( int argc, char **argv )
272 {
273   Application application = Application::New( &argc, &argv );
274
275   RunTest( application );
276
277   return 0;
278 }