Add shadow toggle and displacement function to text-label example
[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 "center-layout.h"
29 #include "shared/multi-language-strings.h"
30
31 using namespace Dali;
32 using namespace Dali::Toolkit;
33 using namespace MultiLanguageStrings;
34
35 namespace
36 {
37   const unsigned int KEY_ZERO = 10;
38   const unsigned int KEY_ONE = 11;
39   const unsigned int KEY_H = 43;
40   const unsigned int KEY_V = 55;
41   const unsigned int KEY_M = 58;
42   const unsigned int KEY_L = 46;
43   const unsigned int KEY_S = 39;
44   const unsigned int KEY_PLUS = 21;
45   const unsigned int KEY_MINUS = 20;
46
47   const char* H_ALIGNMENT_STRING_TABLE[] =
48   {
49     "BEGIN",
50     "CENTER",
51     "END"
52   };
53
54   const unsigned int H_ALIGNMENT_STRING_COUNT = sizeof( H_ALIGNMENT_STRING_TABLE ) / sizeof( H_ALIGNMENT_STRING_TABLE[0u] );
55
56   const char* V_ALIGNMENT_STRING_TABLE[] =
57   {
58     "TOP",
59     "CENTER",
60     "BOTTOM"
61   };
62
63   const unsigned int V_ALIGNMENT_STRING_COUNT = sizeof( V_ALIGNMENT_STRING_TABLE ) / sizeof( V_ALIGNMENT_STRING_TABLE[0u] );
64 }
65
66 /**
67  * @brief The main class of the demo.
68  */
69 class TextLabelExample : public ConnectionTracker
70 {
71 public:
72
73   TextLabelExample( Application& application )
74   : mApplication( application ),
75     mLanguageId( 0u ),
76     mAlignment( 0u )
77   {
78     // Connect to the Application's Init signal
79     mApplication.InitSignal().Connect( this, &TextLabelExample::Create );
80   }
81
82   ~TextLabelExample()
83   {
84     // Nothing to do here.
85   }
86
87   /**
88    * One-time setup in response to Application InitSignal.
89    */
90   void Create( Application& application )
91   {
92     Stage stage = Stage::GetCurrent();
93
94     stage.SetBackgroundColor( Color::BLACK );
95     stage.KeyEventSignal().Connect(this, &TextLabelExample::OnKeyEvent);
96     Vector2 stageSize = stage.GetSize();
97
98     CenterLayout centerLayout = CenterLayout::New();
99     centerLayout.SetParentOrigin( ParentOrigin::CENTER );
100     centerLayout.SetSize( stageSize.width*0.6f, stageSize.width*0.6f );
101     stage.Add( centerLayout );
102
103     mLabel = TextLabel::New();
104     mLabel.SetBackgroundColor( Vector4(0.3f,0.3f,0.6f,1.0f) );
105     centerLayout.Add( mLabel );
106
107     mLabel.SetProperty( TextLabel::Property::MULTI_LINE, true );
108     mLabel.SetProperty( TextLabel::Property::TEXT, "A Quick Brown Fox Jumps Over The Lazy Dog" );
109     mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 1.0f, 1.0f ) );
110     mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
111
112     Property::Value labelText = mLabel.GetProperty( TextLabel::Property::TEXT );
113     std::cout << "Displaying text: \"" << labelText.Get< std::string >() << "\"" << std::endl;
114   }
115
116   /**
117    * Main key event handler
118    */
119   void OnKeyEvent(const KeyEvent& event)
120   {
121     if(event.state == KeyEvent::Down)
122     {
123       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
124       {
125         mApplication.Quit();
126       }
127       else if( event.IsCtrlModifier() )
128       {
129         switch( event.keyCode )
130         {
131           case KEY_ZERO: // fall through
132           case KEY_ONE:
133           {
134             mLabel.SetProperty( TextLabel::Property::RENDERING_BACKEND, event.keyCode - 10 );
135             break;
136           }
137           case KEY_H:
138           {
139             if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
140             {
141               mAlignment = 0u;
142             }
143
144             mLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
145             break;
146           }
147           case KEY_V:
148           {
149             if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
150             {
151               mAlignment = 0u;
152             }
153
154             mLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
155             break;
156           }
157           case KEY_M:
158           {
159             bool multiLine = mLabel.GetProperty<bool>( TextLabel::Property::MULTI_LINE );
160             mLabel.SetProperty( TextLabel::Property::MULTI_LINE, !multiLine );
161             break;
162           }
163           case KEY_L:
164           {
165             const Language& language = LANGUAGES[ mLanguageId ];
166
167             mLabel.SetProperty( TextLabel::Property::TEXT, language.text );
168
169             if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
170             {
171               mLanguageId = 0u;
172             }
173             break;
174           }
175           case KEY_S:
176           {
177             if( Color::BLACK == mLabel.GetProperty<Vector4>( TextLabel::Property::SHADOW_COLOR ) )
178             {
179               mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::RED );
180             }
181             else
182             {
183               mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
184             }
185             break;
186           }
187           case KEY_PLUS:
188           {
189             mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
190             break;
191           }
192           case KEY_MINUS:
193           {
194             mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
195             break;
196           }
197
198         }
199       }
200     }
201   }
202
203 private:
204
205   Application& mApplication;
206
207   TextLabel mLabel;
208
209   unsigned int mLanguageId;
210   unsigned int mAlignment;
211 };
212
213 void RunTest( Application& application )
214 {
215   TextLabelExample test( application );
216
217   application.MainLoop();
218 }
219
220 /** Entry point for Linux & Tizen applications */
221 int main( int argc, char **argv )
222 {
223   Application application = Application::New( &argc, &argv );
224
225   RunTest( application );
226
227   return 0;
228 }