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/devel-api/controls/text-controls/text-label-devel.h>
26 #include <dali-toolkit/dali-toolkit.h>
30 #include "shared/multi-language-strings.h"
31 #include "shared/view.h"
34 using namespace Dali::Toolkit;
35 using namespace MultiLanguageStrings;
39 const char* const BACKGROUND_IMAGE = DEMO_IMAGE_DIR "grab-handle.png";
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;
53 const char* H_ALIGNMENT_STRING_TABLE[] =
60 const unsigned int H_ALIGNMENT_STRING_COUNT = sizeof( H_ALIGNMENT_STRING_TABLE ) / sizeof( H_ALIGNMENT_STRING_TABLE[0u] );
62 const char* V_ALIGNMENT_STRING_TABLE[] =
69 const unsigned int V_ALIGNMENT_STRING_COUNT = sizeof( V_ALIGNMENT_STRING_TABLE ) / sizeof( V_ALIGNMENT_STRING_TABLE[0u] );
71 int ConvertToEven(int value)
73 return (value % 2 == 0) ? value : (value + 1);
76 struct HSVColorConstraint
78 HSVColorConstraint(float hue, float saturation, float value)
80 saturation(saturation),
85 void operator()(Vector3& current, const PropertyInputContainer& inputs )
87 current = hsv2rgb(Vector3(inputs[0]->GetFloat(), saturation, value));
90 Vector3 hsv2rgb(Vector3 colorHSV)
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);
102 } // anonymous namespace
105 * @brief The main class of the demo.
107 class TextLabelExample : public ConnectionTracker
111 TextLabelExample( Application& application )
112 : mApplication( application ),
116 mPanGestureDetector(),
120 mHueAngleIndex( Property::INVALID_INDEX ),
121 mOverrideMixColorIndex( Property::INVALID_INDEX )
123 // Connect to the Application's Init signal
124 mApplication.InitSignal().Connect( this, &TextLabelExample::Create );
129 // Nothing to do here.
133 * One-time setup in response to Application InitSignal.
135 void Create( Application& application )
137 Stage stage = Stage::GetCurrent();
139 stage.KeyEventSignal().Connect(this, &TextLabelExample::OnKeyEvent);
140 Vector2 stageSize = stage.GetSize();
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 );
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 );
158 mPanGestureDetector = PanGestureDetector::New();
159 mPanGestureDetector.Attach( mGrabCorner );
160 mPanGestureDetector.DetectedSignal().Connect( this, &TextLabelExample::OnPan );
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 );
174 mHueAngleIndex = mLabel.RegisterProperty( "hue", 0.0f );
175 Renderer bgRenderer = mLabel.GetRendererAt(0);
176 mOverrideMixColorIndex = DevelHandle::GetPropertyIndex( bgRenderer, ColorVisual::Property::MIX_COLOR );
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 );
183 Animation anim = Animation::New(50.0f);
184 anim.AnimateTo(Property(mLabel, mHueAngleIndex), 6.28318f);
185 anim.SetLooping(true);
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 );
194 Property::Value labelText = mLabel.GetProperty( TextLabel::Property::TEXT );
195 std::cout << "Displaying text: \"" << labelText.Get< std::string >() << "\"" << std::endl;
198 // Resize the text-label with pan gesture
199 void OnPan( Actor actor, const PanGesture& gesture )
201 // Reset mLayoutSize when the pan starts
202 if( gesture.state == Gesture::Started )
204 if( mLayoutSize.x < 2.0f )
206 mLayoutSize.x = 2.0f;
209 if( mLayoutSize.y < 2.0f )
211 mLayoutSize.y = 2.0f;
215 mLayoutSize.x += gesture.displacement.x * 2.0f;
216 mLayoutSize.y += gesture.displacement.y * 2.0f;
218 if( mLayoutSize.x >= 2.0f ||
219 mLayoutSize.y >= 2.0f )
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 ) );
225 mContainer.SetSize( clampedSize );
230 * Main key event handler
232 void OnKeyEvent(const KeyEvent& event)
234 if(event.state == KeyEvent::Down)
236 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
240 else if( event.IsCtrlModifier() )
242 switch( event.keyCode )
244 // Select rendering back-end
245 case KEY_ZERO: // fall through
248 mLabel.SetProperty( TextLabel::Property::RENDERING_BACKEND, event.keyCode - 10 );
251 case KEY_A: // Animate text colour
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 );
259 case KEY_F: // Fill vertically
261 if( ResizePolicy::DIMENSION_DEPENDENCY == mLabel.GetResizePolicy(Dimension::HEIGHT) )
263 mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
267 mLabel.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
271 case KEY_H: // Horizontal alignment
273 if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
278 mLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
281 case KEY_V: // Vertical alignment
283 if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
288 mLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
291 case KEY_M: // Multi-line
293 bool multiLine = mLabel.GetProperty<bool>( TextLabel::Property::MULTI_LINE );
294 mLabel.SetProperty( TextLabel::Property::MULTI_LINE, !multiLine );
297 case KEY_L: // Language
299 const Language& language = LANGUAGES[ mLanguageId ];
301 mLabel.SetProperty( TextLabel::Property::TEXT, language.text );
303 if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
309 case KEY_S: // Shadow color
311 if( Color::BLACK == mLabel.GetProperty<Vector4>( TextLabel::Property::SHADOW_COLOR ) )
313 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::RED );
317 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
321 case KEY_PLUS: // Increase shadow offset
323 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
326 case KEY_MINUS: // Decrease shadow offset
328 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
339 Application& mApplication;
346 PanGestureDetector mPanGestureDetector;
350 unsigned int mLanguageId;
351 unsigned int mAlignment;
352 Property::Index mHueAngleIndex;
353 Property::Index mOverrideMixColorIndex;
356 void RunTest( Application& application )
358 TextLabelExample test( application );
360 application.MainLoop();
363 /** Entry point for Linux & Tizen applications */
364 int DALI_EXPORT_API main( int argc, char **argv )
366 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
368 RunTest( application );