2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file text-label-example.cpp
20 * @brief Basic usage of TextLabel control
24 #include <dali/devel-api/object/handle-devel.h>
25 #include <dali-toolkit/dali-toolkit.h>
29 #include "shared/multi-language-strings.h"
30 #include "shared/view.h"
33 using namespace Dali::Toolkit;
34 using namespace MultiLanguageStrings;
38 const char* const BACKGROUND_IMAGE = DEMO_IMAGE_DIR "grab-handle.png";
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;
51 const char* H_ALIGNMENT_STRING_TABLE[] =
58 const unsigned int H_ALIGNMENT_STRING_COUNT = sizeof( H_ALIGNMENT_STRING_TABLE ) / sizeof( H_ALIGNMENT_STRING_TABLE[0u] );
60 const char* V_ALIGNMENT_STRING_TABLE[] =
67 const unsigned int V_ALIGNMENT_STRING_COUNT = sizeof( V_ALIGNMENT_STRING_TABLE ) / sizeof( V_ALIGNMENT_STRING_TABLE[0u] );
69 int ConvertToEven(int value)
71 return (value % 2 == 0) ? value : (value + 1);
74 struct HSVColorConstraint
76 HSVColorConstraint(float hue, float saturation, float value)
78 saturation(saturation),
83 void operator()(Vector3& current, const PropertyInputContainer& inputs )
85 current = hsv2rgb(Vector3(inputs[0]->GetFloat(), saturation, value));
88 Vector3 hsv2rgb(Vector3 colorHSV)
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);
100 } // anonymous namespace
103 * @brief The main class of the demo.
105 class TextLabelExample : public ConnectionTracker
109 TextLabelExample( Application& application )
110 : mApplication( application ),
114 mPanGestureDetector(),
118 mHueAngleIndex( Property::INVALID_INDEX ),
119 mOverrideMixColorIndex( Property::INVALID_INDEX )
121 // Connect to the Application's Init signal
122 mApplication.InitSignal().Connect( this, &TextLabelExample::Create );
127 // Nothing to do here.
131 * One-time setup in response to Application InitSignal.
133 void Create( Application& application )
135 Stage stage = Stage::GetCurrent();
137 stage.KeyEventSignal().Connect(this, &TextLabelExample::OnKeyEvent);
138 Vector2 stageSize = stage.GetSize();
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 );
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 );
156 mPanGestureDetector = PanGestureDetector::New();
157 mPanGestureDetector.Attach( mGrabCorner );
158 mPanGestureDetector.DetectedSignal().Connect( this, &TextLabelExample::OnPan );
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 );
172 mHueAngleIndex = mLabel.RegisterProperty( "hue", 0.0f );
173 Renderer bgRenderer = mLabel.GetRendererAt(0);
174 mOverrideMixColorIndex = DevelHandle::GetPropertyIndex( bgRenderer, ColorVisual::Property::MIX_COLOR );
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 );
181 Animation anim = Animation::New(50.0f);
182 anim.AnimateTo(Property(mLabel, mHueAngleIndex), 6.28318f);
183 anim.SetLooping(true);
186 Property::Value labelText = mLabel.GetProperty( TextLabel::Property::TEXT );
187 std::cout << "Displaying text: \"" << labelText.Get< std::string >() << "\"" << std::endl;
190 // Resize the text-label with pan gesture
191 void OnPan( Actor actor, const PanGesture& gesture )
193 // Reset mLayoutSize when the pan starts
194 if( gesture.state == Gesture::Started )
196 if( mLayoutSize.x < 2.0f )
198 mLayoutSize.x = 2.0f;
201 if( mLayoutSize.y < 2.0f )
203 mLayoutSize.y = 2.0f;
207 mLayoutSize.x += gesture.displacement.x * 2.0f;
208 mLayoutSize.y += gesture.displacement.y * 2.0f;
210 if( mLayoutSize.x >= 2.0f ||
211 mLayoutSize.y >= 2.0f )
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 ) );
217 mContainer.SetSize( clampedSize );
222 * Main key event handler
224 void OnKeyEvent(const KeyEvent& event)
226 if(event.state == KeyEvent::Down)
228 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
232 else if( event.IsCtrlModifier() )
234 switch( event.keyCode )
236 // Select rendering back-end
237 case KEY_ZERO: // fall through
240 mLabel.SetProperty( TextLabel::Property::RENDERING_BACKEND, event.keyCode - 10 );
243 case KEY_F: // Fill vertically
245 if( ResizePolicy::DIMENSION_DEPENDENCY == mLabel.GetResizePolicy(Dimension::HEIGHT) )
247 mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
251 mLabel.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
255 case KEY_H: // Horizontal alignment
257 if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
262 mLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
265 case KEY_V: // Vertical alignment
267 if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
272 mLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
275 case KEY_M: // Multi-line
277 bool multiLine = mLabel.GetProperty<bool>( TextLabel::Property::MULTI_LINE );
278 mLabel.SetProperty( TextLabel::Property::MULTI_LINE, !multiLine );
281 case KEY_L: // Language
283 const Language& language = LANGUAGES[ mLanguageId ];
285 mLabel.SetProperty( TextLabel::Property::TEXT, language.text );
287 if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
293 case KEY_S: // Shadow color
295 if( Color::BLACK == mLabel.GetProperty<Vector4>( TextLabel::Property::SHADOW_COLOR ) )
297 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::RED );
301 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
305 case KEY_PLUS: // Increase shadow offset
307 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
310 case KEY_MINUS: // Decrease shadow offset
312 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
323 Application& mApplication;
330 PanGestureDetector mPanGestureDetector;
334 unsigned int mLanguageId;
335 unsigned int mAlignment;
336 Property::Index mHueAngleIndex;
337 Property::Index mOverrideMixColorIndex;
340 void RunTest( Application& application )
342 TextLabelExample test( application );
344 application.MainLoop();
347 /** Entry point for Linux & Tizen applications */
348 int DALI_EXPORT_API main( int argc, char **argv )
350 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
352 RunTest( application );