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