TextLabel demo updates
[platform/core/uifw/dali-demo.git] / examples / text-label / text-label-example.cpp
1 /*
2  * Copyright (c) 2016 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/devel-api/object/handle-devel.h>
25 #include <dali-toolkit/devel-api/controls/text-controls/text-label-devel.h>
26 #include <dali-toolkit/dali-toolkit.h>
27 #include <iostream>
28
29 // INTERNAL INCLUDES
30 #include "shared/multi-language-strings.h"
31 #include "shared/view.h"
32
33 using namespace Dali;
34 using namespace Dali::Toolkit;
35 using namespace MultiLanguageStrings;
36
37 namespace
38 {
39 const char* const BACKGROUND_IMAGE = DEMO_IMAGE_DIR "grab-handle.png";
40
41 const unsigned int KEY_ZERO = 10;
42 const unsigned int KEY_ONE = 11;
43 const unsigned int KEY_A = 38;
44 const unsigned int KEY_F = 41;
45 const unsigned int KEY_H = 43;
46 const unsigned int KEY_U = 30;
47 const unsigned int KEY_V = 55;
48 const unsigned int KEY_M = 58;
49 const unsigned int KEY_L = 46;
50 const unsigned int KEY_S = 39;
51 const unsigned int KEY_PLUS = 21;
52 const unsigned int KEY_MINUS = 20;
53
54 const char* H_ALIGNMENT_STRING_TABLE[] =
55 {
56   "BEGIN",
57   "CENTER",
58   "END"
59 };
60
61 const unsigned int H_ALIGNMENT_STRING_COUNT = sizeof( H_ALIGNMENT_STRING_TABLE ) / sizeof( H_ALIGNMENT_STRING_TABLE[0u] );
62
63 const char* V_ALIGNMENT_STRING_TABLE[] =
64 {
65   "TOP",
66   "CENTER",
67   "BOTTOM"
68 };
69
70 const unsigned int V_ALIGNMENT_STRING_COUNT = sizeof( V_ALIGNMENT_STRING_TABLE ) / sizeof( V_ALIGNMENT_STRING_TABLE[0u] );
71
72 int ConvertToEven(int value)
73 {
74   return (value % 2 == 0) ? value : (value + 1);
75 }
76
77 struct HSVColorConstraint
78 {
79   HSVColorConstraint(float hue, float saturation, float value)
80   : hue(hue),
81     saturation(saturation),
82     value(value)
83   {
84   }
85
86   void operator()(Vector3& current, const PropertyInputContainer& inputs )
87   {
88     current = hsv2rgb(Vector3(inputs[0]->GetFloat(), saturation, value));
89   }
90
91   Vector3 hsv2rgb(Vector3 colorHSV)
92   {
93     float r=colorHSV.z*(1+colorHSV.y*(cos(colorHSV.x)-1));
94     float g=colorHSV.z*(1+colorHSV.y*(cos(colorHSV.x-2.09439)-1));
95     float b=colorHSV.z*(1+colorHSV.y*(cos(colorHSV.x+2.09439)-1));
96     return Vector3(r, g, b);
97   }
98   float hue;
99   float saturation;
100   float value;
101 };
102
103 } // anonymous namespace
104
105 /**
106  * @brief The main class of the demo.
107  */
108 class TextLabelExample : public ConnectionTracker
109 {
110 public:
111
112   TextLabelExample( Application& application )
113   : mApplication( application ),
114     mLabel(),
115     mContainer(),
116     mGrabCorner(),
117     mPanGestureDetector(),
118     mLayoutSize(),
119     mLanguageId( 0u ),
120     mAlignment( 0u ),
121     mHueAngleIndex( Property::INVALID_INDEX ),
122     mOverrideMixColorIndex( Property::INVALID_INDEX )
123   {
124     // Connect to the Application's Init signal
125     mApplication.InitSignal().Connect( this, &TextLabelExample::Create );
126   }
127
128   ~TextLabelExample()
129   {
130     // Nothing to do here.
131   }
132
133   /**
134    * One-time setup in response to Application InitSignal.
135    */
136   void Create( Application& application )
137   {
138     Stage stage = Stage::GetCurrent();
139
140     stage.KeyEventSignal().Connect(this, &TextLabelExample::OnKeyEvent);
141     Vector2 stageSize = stage.GetSize();
142
143     mContainer = Control::New();
144     mContainer.SetName( "Container" );
145     mContainer.SetParentOrigin( ParentOrigin::CENTER );
146     mLayoutSize = Vector2(stageSize.width*0.6f, stageSize.width*0.6f);
147     mContainer.SetSize( mLayoutSize );
148     mContainer.SetDrawMode( DrawMode::OVERLAY_2D );
149     stage.Add( mContainer );
150
151     // Resize the center layout when the corner is grabbed
152     mGrabCorner = ImageView::New( BACKGROUND_IMAGE );
153     mGrabCorner.SetName( "GrabCorner" );
154     mGrabCorner.SetAnchorPoint( AnchorPoint::TOP_CENTER );
155     mGrabCorner.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
156     mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
157     mContainer.Add( mGrabCorner );
158
159     mPanGestureDetector = PanGestureDetector::New();
160     mPanGestureDetector.Attach( mGrabCorner );
161     mPanGestureDetector.DetectedSignal().Connect( this, &TextLabelExample::OnPan );
162
163     mLabel = TextLabel::New( "A Quick Brown Fox Jumps Over The Lazy Dog" );
164
165     mLabel.SetName( "TextLabel" );
166     mLabel.SetAnchorPoint( AnchorPoint::TOP_LEFT );
167     mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
168     mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
169     mLabel.SetProperty( TextLabel::Property::MULTI_LINE, true );
170     mLabel.SetProperty( DevelTextLabel::Property::TEXT_COLOR_ANIMATABLE, Color::GREEN );
171     mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 1.0f, 1.0f ) );
172     mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
173     mLabel.SetBackgroundColor( Color::WHITE );
174     mContainer.Add( mLabel );
175
176     mHueAngleIndex = mLabel.RegisterProperty( "hue", 0.0f );
177     Renderer bgRenderer = mLabel.GetRendererAt(0);
178     mOverrideMixColorIndex = DevelHandle::GetPropertyIndex( bgRenderer, ColorVisual::Property::MIX_COLOR );
179
180     Constraint constraint = Constraint::New<Vector3>( bgRenderer, mOverrideMixColorIndex, HSVColorConstraint(0.0f, 0.5f, 0.8f));
181     constraint.AddSource( Source( mLabel, mHueAngleIndex ) );
182     constraint.SetRemoveAction( Constraint::Discard );
183     constraint.Apply();
184
185     Animation anim = Animation::New(50.0f);
186     anim.AnimateTo(Property(mLabel, mHueAngleIndex), 6.28318f);
187     anim.SetLooping(true);
188     anim.Play();
189
190     // Animate the text color 3 times from source color to RED
191     Animation animation = Animation::New( 2.f );
192     animation.AnimateTo( Property( mLabel, DevelTextLabel::Property::TEXT_COLOR_ANIMATABLE ), Color::YELLOW, AlphaFunction::SIN );
193     animation.SetLoopCount( 3 );
194     animation.Play();
195
196     Property::Value labelText = mLabel.GetProperty( TextLabel::Property::TEXT );
197     std::cout << "Displaying text: \"" << labelText.Get< std::string >() << "\"" << std::endl;
198   }
199
200   // Resize the text-label with pan gesture
201   void OnPan( Actor actor, const PanGesture& gesture )
202   {
203     // Reset mLayoutSize when the pan starts
204     if( gesture.state == Gesture::Started )
205     {
206       if( mLayoutSize.x < 2.0f )
207       {
208         mLayoutSize.x = 2.0f;
209       }
210
211       if( mLayoutSize.y < 2.0f )
212       {
213         mLayoutSize.y = 2.0f;
214       }
215     }
216
217     mLayoutSize.x += gesture.displacement.x * 2.0f;
218     mLayoutSize.y += gesture.displacement.y * 2.0f;
219
220     if( mLayoutSize.x >= 2.0f ||
221         mLayoutSize.y >= 2.0f )
222     {
223       // Avoid pixel mis-alignment issue
224       Vector2 clampedSize = Vector2( std::max( ConvertToEven( static_cast<int>( mLayoutSize.x )), 2 ),
225                                      std::max( ConvertToEven( static_cast<int>( mLayoutSize.y )), 2 ) );
226
227       mContainer.SetSize( clampedSize );
228     }
229   }
230
231   /**
232    * Main key event handler
233    */
234   void OnKeyEvent(const KeyEvent& event)
235   {
236     if(event.state == KeyEvent::Down)
237     {
238       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
239       {
240         mApplication.Quit();
241       }
242       else if( event.IsCtrlModifier() )
243       {
244         switch( event.keyCode )
245         {
246           // Select rendering back-end
247           case KEY_ZERO: // fall through
248           case KEY_ONE:
249           {
250             mLabel.SetProperty( TextLabel::Property::RENDERING_BACKEND, event.keyCode - 10 );
251             break;
252           }
253           case KEY_A: // Animate text colour
254           {
255             Animation animation = Animation::New( 2.f );
256             animation.AnimateTo( Property( mLabel, DevelTextLabel::Property::TEXT_COLOR_ANIMATABLE ), Color::RED, AlphaFunction::SIN );
257             animation.SetLoopCount( 3 );
258             animation.Play();
259             break;
260           }
261           case KEY_F: // Fill vertically
262           {
263             if( ResizePolicy::DIMENSION_DEPENDENCY == mLabel.GetResizePolicy(Dimension::HEIGHT) )
264             {
265               mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
266             }
267             else
268             {
269               mLabel.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
270             }
271             break;
272           }
273           case KEY_H: // Horizontal alignment
274           {
275             if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
276             {
277               mAlignment = 0u;
278             }
279
280             mLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
281             break;
282           }
283           case KEY_V: // Vertical alignment
284           {
285             if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
286             {
287               mAlignment = 0u;
288             }
289
290             mLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
291             break;
292           }
293           case KEY_M: // Multi-line
294           {
295             bool multiLine = mLabel.GetProperty<bool>( TextLabel::Property::MULTI_LINE );
296             mLabel.SetProperty( TextLabel::Property::MULTI_LINE, !multiLine );
297             break;
298           }
299           case KEY_L: // Language
300           {
301             const Language& language = LANGUAGES[ mLanguageId ];
302
303             mLabel.SetProperty( TextLabel::Property::TEXT, language.text );
304
305             if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
306             {
307               mLanguageId = 0u;
308             }
309             break;
310           }
311           case KEY_S: // Shadow color
312           {
313             if( Color::BLACK == mLabel.GetProperty<Vector4>( TextLabel::Property::SHADOW_COLOR ) )
314             {
315               mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::RED );
316             }
317             else
318             {
319               mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
320             }
321             break;
322           }
323           case KEY_U: // Markup
324           {
325             mLabel.SetProperty( TextLabel::Property::ENABLE_MARKUP, true );
326             mLabel.SetProperty( TextLabel::Property::TEXT, "<font family='DejaVuSerif' size='18'>H<color value='blue'>ello</color> <font weight='bold'>world</font> demo</font>" );
327             break;
328           }
329           case KEY_PLUS: // Increase shadow offset
330           {
331             mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
332             break;
333           }
334           case KEY_MINUS: // Decrease shadow offset
335           {
336             mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
337             break;
338           }
339
340         }
341       }
342     }
343   }
344
345 private:
346
347   Application& mApplication;
348
349   TextLabel mLabel;
350
351   Control mContainer;
352   Control mGrabCorner;
353
354   PanGestureDetector mPanGestureDetector;
355
356   Vector2 mLayoutSize;
357
358   unsigned int mLanguageId;
359   unsigned int mAlignment;
360   Property::Index mHueAngleIndex;
361   Property::Index mOverrideMixColorIndex;
362 };
363
364 void RunTest( Application& application )
365 {
366   TextLabelExample test( application );
367
368   application.MainLoop();
369 }
370
371 /** Entry point for Linux & Tizen applications */
372 int DALI_EXPORT_API main( int argc, char **argv )
373 {
374   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
375
376   RunTest( application );
377
378   return 0;
379 }