Merge remote-tracking branch 'origin/tizen' into new_text
[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.SetResizePolicy( FILL_TO_PARENT, HEIGHT );
131     mLabel.SetProperty( TextLabel::Property::MULTI_LINE, true );
132     mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 1.0f, 1.0f ) );
133     mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
134     mContainer.Add( mLabel );
135
136     Property::Value labelText = mLabel.GetProperty( TextLabel::Property::TEXT );
137     std::cout << "Displaying text: \"" << labelText.Get< std::string >() << "\"" << std::endl;
138   }
139
140   // Resize the text-label with pan gesture
141   void OnPan( Actor actor, const PanGesture& gesture )
142   {
143     mLayoutSize.x += gesture.displacement.x * 2.0f;
144     mLayoutSize.y += gesture.displacement.y * 2.0f;
145
146     if( mLayoutSize.x >= 2.0f &&
147         mLayoutSize.y >= 2.0f )
148     {
149       // Avoid pixel mis-alignment issue
150       Vector2 clampedSize = Vector2( ConvertToEven(static_cast<int>(mLayoutSize.x)),
151                                      ConvertToEven(static_cast<int>(mLayoutSize.y)) );
152
153       mContainer.SetPreferredSize( clampedSize );
154     }
155   }
156
157   /**
158    * Main key event handler
159    */
160   void OnKeyEvent(const KeyEvent& event)
161   {
162     if(event.state == KeyEvent::Down)
163     {
164       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
165       {
166         mApplication.Quit();
167       }
168       else if( event.IsCtrlModifier() )
169       {
170         switch( event.keyCode )
171         {
172           case KEY_ZERO: // fall through
173           case KEY_ONE:
174           {
175             mLabel.SetProperty( TextLabel::Property::RENDERING_BACKEND, event.keyCode - 10 );
176             break;
177           }
178           case KEY_H:
179           {
180             if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
181             {
182               mAlignment = 0u;
183             }
184
185             mLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
186             break;
187           }
188           case KEY_V:
189           {
190             if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
191             {
192               mAlignment = 0u;
193             }
194
195             mLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
196             break;
197           }
198           case KEY_M:
199           {
200             bool multiLine = mLabel.GetProperty<bool>( TextLabel::Property::MULTI_LINE );
201             mLabel.SetProperty( TextLabel::Property::MULTI_LINE, !multiLine );
202             break;
203           }
204           case KEY_L:
205           {
206             const Language& language = LANGUAGES[ mLanguageId ];
207
208             mLabel.SetProperty( TextLabel::Property::TEXT, language.text );
209
210             if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
211             {
212               mLanguageId = 0u;
213             }
214             break;
215           }
216           case KEY_S:
217           {
218             if( Color::BLACK == mLabel.GetProperty<Vector4>( TextLabel::Property::SHADOW_COLOR ) )
219             {
220               mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::RED );
221             }
222             else
223             {
224               mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
225             }
226             break;
227           }
228           case KEY_PLUS:
229           {
230             mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
231             break;
232           }
233           case KEY_MINUS:
234           {
235             mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
236             break;
237           }
238
239         }
240       }
241     }
242   }
243
244 private:
245
246   Application& mApplication;
247
248   TextLabel mLabel;
249
250   Control mContainer;
251   Actor mGrabCorner;
252
253   PanGestureDetector mPanGestureDetector;
254
255   Vector2 mLayoutSize;
256
257   unsigned int mLanguageId;
258   unsigned int mAlignment;
259 };
260
261 void RunTest( Application& application )
262 {
263   TextLabelExample test( application );
264
265   application.MainLoop();
266 }
267
268 /** Entry point for Linux & Tizen applications */
269 int main( int argc, char **argv )
270 {
271   Application application = Application::New( &argc, &argv );
272
273   RunTest( application );
274
275   return 0;
276 }