Remove ResourceImage usage from demos
[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 = DEMO_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 = ImageView::New( BACKGROUND_IMAGE );
115     mGrabCorner.SetName( "GrabCorner" );
116     mGrabCorner.SetAnchorPoint( AnchorPoint::TOP_CENTER );
117     mGrabCorner.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
118     mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
119     mContainer.Add( mGrabCorner );
120
121     mPanGestureDetector = PanGestureDetector::New();
122     mPanGestureDetector.Attach( mGrabCorner );
123     mPanGestureDetector.DetectedSignal().Connect( this, &TextLabelExample::OnPan );
124
125     mLabel = TextLabel::New( "A Quick Brown Fox Jumps Over The Lazy Dog" );
126     mLabel.SetName( "TextLabel" );
127     mLabel.SetAnchorPoint( AnchorPoint::TOP_LEFT );
128     mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
129     mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
130     mLabel.SetProperty( TextLabel::Property::MULTI_LINE, true );
131     mLabel.SetProperty( TextLabel::Property::TEXT_COLOR, Color::BLUE );
132     mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 1.0f, 1.0f ) );
133     mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
134     mLabel.SetBackgroundColor( Color::WHITE );
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     // Reset mLayoutSize when the pan starts
145     if( gesture.state == Gesture::Started )
146     {
147       if( mLayoutSize.x < 2.0f )
148       {
149         mLayoutSize.x = 2.0f;
150       }
151
152       if( mLayoutSize.y < 2.0f )
153       {
154         mLayoutSize.y = 2.0f;
155       }
156     }
157
158     mLayoutSize.x += gesture.displacement.x * 2.0f;
159     mLayoutSize.y += gesture.displacement.y * 2.0f;
160
161     if( mLayoutSize.x >= 2.0f ||
162         mLayoutSize.y >= 2.0f )
163     {
164       // Avoid pixel mis-alignment issue
165       Vector2 clampedSize = Vector2( std::max( ConvertToEven( static_cast<int>( mLayoutSize.x )), 2 ),
166                                      std::max( ConvertToEven( static_cast<int>( mLayoutSize.y )), 2 ) );
167
168       mContainer.SetSize( clampedSize );
169     }
170   }
171
172   /**
173    * Main key event handler
174    */
175   void OnKeyEvent(const KeyEvent& event)
176   {
177     if(event.state == KeyEvent::Down)
178     {
179       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
180       {
181         mApplication.Quit();
182       }
183       else if( event.IsCtrlModifier() )
184       {
185         switch( event.keyCode )
186         {
187           // Select rendering back-end
188           case KEY_ZERO: // fall through
189           case KEY_ONE:
190           {
191             mLabel.SetProperty( TextLabel::Property::RENDERING_BACKEND, event.keyCode - 10 );
192             break;
193           }
194           case KEY_F: // Fill vertically
195           {
196             if( ResizePolicy::DIMENSION_DEPENDENCY == mLabel.GetResizePolicy(Dimension::HEIGHT) )
197             {
198               mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
199             }
200             else
201             {
202               mLabel.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
203             }
204             break;
205           }
206           case KEY_H: // Horizontal alignment
207           {
208             if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
209             {
210               mAlignment = 0u;
211             }
212
213             mLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
214             break;
215           }
216           case KEY_V: // Vertical alignment
217           {
218             if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
219             {
220               mAlignment = 0u;
221             }
222
223             mLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
224             break;
225           }
226           case KEY_M: // Multi-line
227           {
228             bool multiLine = mLabel.GetProperty<bool>( TextLabel::Property::MULTI_LINE );
229             mLabel.SetProperty( TextLabel::Property::MULTI_LINE, !multiLine );
230             break;
231           }
232           case KEY_L: // Language
233           {
234             const Language& language = LANGUAGES[ mLanguageId ];
235
236             mLabel.SetProperty( TextLabel::Property::TEXT, language.text );
237
238             if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
239             {
240               mLanguageId = 0u;
241             }
242             break;
243           }
244           case KEY_S: // Shadow color
245           {
246             if( Color::BLACK == mLabel.GetProperty<Vector4>( TextLabel::Property::SHADOW_COLOR ) )
247             {
248               mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::RED );
249             }
250             else
251             {
252               mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
253             }
254             break;
255           }
256           case KEY_PLUS: // Increase shadow offset
257           {
258             mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
259             break;
260           }
261           case KEY_MINUS: // Decrease shadow offset
262           {
263             mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
264             break;
265           }
266
267         }
268       }
269     }
270   }
271
272 private:
273
274   Application& mApplication;
275
276   TextLabel mLabel;
277
278   Control mContainer;
279   Control mGrabCorner;
280
281   PanGestureDetector mPanGestureDetector;
282
283   Vector2 mLayoutSize;
284
285   unsigned int mLanguageId;
286   unsigned int mAlignment;
287 };
288
289 void RunTest( Application& application )
290 {
291   TextLabelExample test( application );
292
293   application.MainLoop();
294 }
295
296 /** Entry point for Linux & Tizen applications */
297 int DALI_EXPORT_API main( int argc, char **argv )
298 {
299   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
300
301   RunTest( application );
302
303   return 0;
304 }