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