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/devel-api/actors/actor-devel.h>
26 #include <dali-toolkit/devel-api/controls/text-controls/text-label-devel.h>
27 #include <dali-toolkit/dali-toolkit.h>
31 #include "shared/multi-language-strings.h"
32 #include "shared/view.h"
35 using namespace Dali::Toolkit;
36 using namespace MultiLanguageStrings;
40 const char* const BACKGROUND_IMAGE = DEMO_IMAGE_DIR "grab-handle.png";
42 const unsigned int KEY_ZERO = 10;
43 const unsigned int KEY_ONE = 11;
44 const unsigned int KEY_A = 38;
45 const unsigned int KEY_F = 41;
46 const unsigned int KEY_H = 43;
47 const unsigned int KEY_U = 30;
48 const unsigned int KEY_V = 55;
49 const unsigned int KEY_M = 58;
50 const unsigned int KEY_L = 46;
51 const unsigned int KEY_S = 39;
52 const unsigned int KEY_PLUS = 21;
53 const unsigned int KEY_MINUS = 20;
55 const char* H_ALIGNMENT_STRING_TABLE[] =
62 const unsigned int H_ALIGNMENT_STRING_COUNT = sizeof( H_ALIGNMENT_STRING_TABLE ) / sizeof( H_ALIGNMENT_STRING_TABLE[0u] );
64 const char* V_ALIGNMENT_STRING_TABLE[] =
71 const unsigned int V_ALIGNMENT_STRING_COUNT = sizeof( V_ALIGNMENT_STRING_TABLE ) / sizeof( V_ALIGNMENT_STRING_TABLE[0u] );
73 int ConvertToEven(int value)
75 return (value % 2 == 0) ? value : (value + 1);
78 struct HSVColorConstraint
80 HSVColorConstraint(float hue, float saturation, float value)
82 saturation(saturation),
87 void operator()(Vector3& current, const PropertyInputContainer& inputs )
89 current = hsv2rgb(Vector3(inputs[0]->GetFloat(), saturation, value));
92 Vector3 hsv2rgb(Vector3 colorHSV)
94 float r=colorHSV.z*(1+colorHSV.y*(cos(colorHSV.x)-1));
95 float g=colorHSV.z*(1+colorHSV.y*(cos(colorHSV.x-2.09439)-1));
96 float b=colorHSV.z*(1+colorHSV.y*(cos(colorHSV.x+2.09439)-1));
97 return Vector3(r, g, b);
104 } // anonymous namespace
107 * @brief The main class of the demo.
109 class TextLabelExample : public ConnectionTracker
113 TextLabelExample( Application& application )
114 : mApplication( application ),
119 mPanGestureDetector(),
123 mHueAngleIndex( Property::INVALID_INDEX ),
124 mOverrideMixColorIndex( Property::INVALID_INDEX )
126 // Connect to the Application's Init signal
127 mApplication.InitSignal().Connect( this, &TextLabelExample::Create );
132 // Nothing to do here.
136 * One-time setup in response to Application InitSignal.
138 void Create( Application& application )
140 Stage stage = Stage::GetCurrent();
142 stage.KeyEventSignal().Connect(this, &TextLabelExample::OnKeyEvent);
143 Vector2 stageSize = stage.GetSize();
145 mContainer = Control::New();
146 mContainer.SetName( "Container" );
147 mContainer.SetParentOrigin( ParentOrigin::CENTER );
148 mLayoutSize = Vector2(stageSize.width*0.6f, stageSize.width*0.6f);
149 mContainer.SetSize( mLayoutSize );
150 mContainer.SetDrawMode( DrawMode::OVERLAY_2D );
151 stage.Add( mContainer );
153 // Resize the center layout when the corner is grabbed
154 mGrabCorner = ImageView::New( BACKGROUND_IMAGE );
155 mGrabCorner.SetName( "GrabCorner" );
156 mGrabCorner.SetAnchorPoint( AnchorPoint::TOP_CENTER );
157 mGrabCorner.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
158 mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
159 mContainer.Add( mGrabCorner );
161 mPanGestureDetector = PanGestureDetector::New();
162 mPanGestureDetector.Attach( mGrabCorner );
163 mPanGestureDetector.DetectedSignal().Connect( this, &TextLabelExample::OnPan );
165 mLabel = TextLabel::New( "A Quick Brown Fox Jumps Over The Lazy Dog" );
167 mLabel.SetName( "TextLabel" );
168 mLabel.SetAnchorPoint( AnchorPoint::TOP_LEFT );
169 mLabel.SetSize(mLayoutSize);
170 mLabel.SetProperty( TextLabel::Property::MULTI_LINE, true );
171 mLabel.SetProperty( DevelTextLabel::Property::TEXT_COLOR_ANIMATABLE, Color::GREEN );
172 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 1.0f, 1.0f ) );
173 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
174 mLabel.SetBackgroundColor( Color::WHITE );
175 mContainer.Add( mLabel );
177 // Add a border for the container so you can see the container is being resized while grabbing the handle.
178 mBorder = Control::New();
179 mBorder.SetAnchorPoint( AnchorPoint::TOP_LEFT );
180 mBorder.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
181 mBorder.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
183 Dali::Property::Map border;
184 border.Insert( Visual::Property::TYPE, Visual::BORDER );
185 border.Insert( BorderVisual::Property::COLOR, Color::WHITE );
186 border.Insert( BorderVisual::Property::SIZE, 2.f );
187 mBorder.SetProperty( Control::Property::BACKGROUND, border );
188 mContainer.Add( mBorder );
189 mBorder.SetVisible(false);
191 DevelActor::RaiseToTop(mGrabCorner);
193 mHueAngleIndex = mLabel.RegisterProperty( "hue", 0.0f );
194 Renderer bgRenderer = mLabel.GetRendererAt(0);
195 mOverrideMixColorIndex = DevelHandle::GetPropertyIndex( bgRenderer, ColorVisual::Property::MIX_COLOR );
197 Constraint constraint = Constraint::New<Vector3>( bgRenderer, mOverrideMixColorIndex, HSVColorConstraint(0.0f, 0.5f, 0.8f));
198 constraint.AddSource( Source( mLabel, mHueAngleIndex ) );
199 constraint.SetRemoveAction( Constraint::Discard );
202 Animation anim = Animation::New(50.0f);
203 anim.AnimateTo(Property(mLabel, mHueAngleIndex), 6.28318f);
204 anim.SetLooping(true);
207 // Animate the text color 3 times from source color to RED
208 Animation animation = Animation::New( 2.f );
209 animation.AnimateTo( Property( mLabel, DevelTextLabel::Property::TEXT_COLOR_ANIMATABLE ), Color::YELLOW, AlphaFunction::SIN );
210 animation.SetLoopCount( 3 );
213 Property::Value labelText = mLabel.GetProperty( TextLabel::Property::TEXT );
214 std::cout << "Displaying text: \"" << labelText.Get< std::string >() << "\"" << std::endl;
217 // Resize the text-label with pan gesture
218 void OnPan( Actor actor, const PanGesture& gesture )
220 // Reset mLayoutSize when the pan starts
221 if( gesture.state == Gesture::Started )
223 if( mLayoutSize.x < 2.0f )
225 mLayoutSize.x = 2.0f;
228 if( mLayoutSize.y < 2.0f )
230 mLayoutSize.y = 2.0f;
233 // Only show the border during the panning
234 mBorder.SetVisible(true);
237 mLayoutSize.x += gesture.displacement.x * 2.0f;
238 mLayoutSize.y += gesture.displacement.y * 2.0f;
240 if( mLayoutSize.x >= 2.0f ||
241 mLayoutSize.y >= 2.0f )
243 // Avoid pixel mis-alignment issue
244 Vector2 clampedSize = Vector2( std::max( ConvertToEven( static_cast<int>( mLayoutSize.x )), 2 ),
245 std::max( ConvertToEven( static_cast<int>( mLayoutSize.y )), 2 ) );
247 mContainer.SetSize( clampedSize );
250 if( gesture.state == Gesture::Cancelled || gesture.state == Gesture::Finished )
252 // Resize the text label to match the container size when panning is finished
253 mLabel.SetSize(mLayoutSize);
254 mBorder.SetVisible(false);
259 * Main key event handler
261 void OnKeyEvent(const KeyEvent& event)
263 if(event.state == KeyEvent::Down)
265 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
269 else if( event.IsCtrlModifier() )
271 switch( event.keyCode )
273 // Select rendering back-end
274 case KEY_ZERO: // fall through
277 mLabel.SetProperty( TextLabel::Property::RENDERING_BACKEND, event.keyCode - 10 );
280 case KEY_A: // Animate text colour
282 Animation animation = Animation::New( 2.f );
283 animation.AnimateTo( Property( mLabel, DevelTextLabel::Property::TEXT_COLOR_ANIMATABLE ), Color::RED, AlphaFunction::SIN );
284 animation.SetLoopCount( 3 );
288 case KEY_F: // Fill vertically
290 if( ResizePolicy::DIMENSION_DEPENDENCY == mLabel.GetResizePolicy(Dimension::HEIGHT) )
292 mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
296 mLabel.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
300 case KEY_H: // Horizontal alignment
302 if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
307 mLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
310 case KEY_V: // Vertical alignment
312 if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
317 mLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
320 case KEY_M: // Multi-line
322 bool multiLine = mLabel.GetProperty<bool>( TextLabel::Property::MULTI_LINE );
323 mLabel.SetProperty( TextLabel::Property::MULTI_LINE, !multiLine );
326 case KEY_L: // Language
328 const Language& language = LANGUAGES[ mLanguageId ];
330 mLabel.SetProperty( TextLabel::Property::TEXT, language.text );
332 if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
338 case KEY_S: // Shadow color
340 if( Color::BLACK == mLabel.GetProperty<Vector4>( TextLabel::Property::SHADOW_COLOR ) )
342 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::RED );
346 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
350 case KEY_U: // Markup
352 mLabel.SetProperty( TextLabel::Property::ENABLE_MARKUP, true );
353 mLabel.SetProperty( TextLabel::Property::TEXT, "<font family='DejaVuSerif' size='18'>H<color value='blue'>ello</color> <font weight='bold'>world</font> demo</font>" );
356 case KEY_PLUS: // Increase shadow offset
358 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
361 case KEY_MINUS: // Decrease shadow offset
363 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
374 Application& mApplication;
382 PanGestureDetector mPanGestureDetector;
386 unsigned int mLanguageId;
387 unsigned int mAlignment;
388 Property::Index mHueAngleIndex;
389 Property::Index mOverrideMixColorIndex;
392 void RunTest( Application& application )
394 TextLabelExample test( application );
396 application.MainLoop();
399 /** Entry point for Linux & Tizen applications */
400 int DALI_EXPORT_API main( int argc, char **argv )
402 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
404 RunTest( application );