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