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