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-toolkit/dali-toolkit.h>
28 #include "shared/multi-language-strings.h"
29 #include "shared/view.h"
32 using namespace Dali::Toolkit;
33 using namespace MultiLanguageStrings;
37 const char* const BACKGROUND_IMAGE = DEMO_IMAGE_DIR "grab-handle.png";
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;
50 const char* H_ALIGNMENT_STRING_TABLE[] =
57 const unsigned int H_ALIGNMENT_STRING_COUNT = sizeof( H_ALIGNMENT_STRING_TABLE ) / sizeof( H_ALIGNMENT_STRING_TABLE[0u] );
59 const char* V_ALIGNMENT_STRING_TABLE[] =
66 const unsigned int V_ALIGNMENT_STRING_COUNT = sizeof( V_ALIGNMENT_STRING_TABLE ) / sizeof( V_ALIGNMENT_STRING_TABLE[0u] );
68 int ConvertToEven(int value)
70 return (value % 2 == 0) ? value : (value + 1);
73 struct HSVColorConstraint
75 HSVColorConstraint(float hue, float saturation, float value)
77 saturation(saturation),
82 void operator()(Vector4& current, const PropertyInputContainer& inputs )
84 current = hsv2rgb(Vector4(inputs[0]->GetFloat(), saturation, value, current.a));
87 Vector4 hsv2rgb(Vector4 colorHSV)
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);
99 } // anonymous namespace
102 * @brief The main class of the demo.
104 class TextLabelExample : public ConnectionTracker
108 TextLabelExample( Application& application )
109 : mApplication( application ),
113 mPanGestureDetector(),
117 mHueAngleIndex( Property::INVALID_INDEX ),
118 mOverrideMixColorIndex( Property::INVALID_INDEX )
120 // Connect to the Application's Init signal
121 mApplication.InitSignal().Connect( this, &TextLabelExample::Create );
126 // Nothing to do here.
130 * One-time setup in response to Application InitSignal.
132 void Create( Application& application )
134 Stage stage = Stage::GetCurrent();
136 stage.KeyEventSignal().Connect(this, &TextLabelExample::OnKeyEvent);
137 Vector2 stageSize = stage.GetSize();
139 mContainer = Control::New();
140 mContainer.SetName( "Container" );
141 mContainer.SetParentOrigin( ParentOrigin::CENTER );
142 mLayoutSize = Vector2(stageSize.width*0.6f, stageSize.width*0.6f);
143 mContainer.SetSize( mLayoutSize );
144 mContainer.SetDrawMode( DrawMode::OVERLAY_2D );
145 stage.Add( mContainer );
147 // Resize the center layout when the corner is grabbed
148 mGrabCorner = ImageView::New( BACKGROUND_IMAGE );
149 mGrabCorner.SetName( "GrabCorner" );
150 mGrabCorner.SetAnchorPoint( AnchorPoint::TOP_CENTER );
151 mGrabCorner.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
152 mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
153 mContainer.Add( mGrabCorner );
155 mPanGestureDetector = PanGestureDetector::New();
156 mPanGestureDetector.Attach( mGrabCorner );
157 mPanGestureDetector.DetectedSignal().Connect( this, &TextLabelExample::OnPan );
159 mLabel = TextLabel::New( "A Quick Brown Fox Jumps Over The Lazy Dog" );
160 mLabel.SetName( "TextLabel" );
161 mLabel.SetAnchorPoint( AnchorPoint::TOP_LEFT );
162 mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
163 mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
164 mLabel.SetProperty( TextLabel::Property::MULTI_LINE, true );
165 mLabel.SetProperty( TextLabel::Property::TEXT_COLOR, Color::BLUE );
166 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 1.0f, 1.0f ) );
167 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
168 mLabel.SetBackgroundColor( Color::WHITE );
169 mContainer.Add( mLabel );
171 mHueAngleIndex = mLabel.RegisterProperty( "hue", 0.0f );
172 Renderer bgRenderer = mLabel.GetRendererAt(0);
173 mOverrideMixColorIndex = bgRenderer.GetPropertyIndex( ColorVisual::Property::MIX_COLOR );
175 Constraint constraint = Constraint::New<Vector4>( bgRenderer, mOverrideMixColorIndex, HSVColorConstraint(0.0f, 0.5f, 0.8f));
176 constraint.AddSource( Source( mLabel, mHueAngleIndex ) );
177 constraint.SetRemoveAction( Constraint::Discard );
180 Animation anim = Animation::New(50.0f);
181 anim.AnimateTo(Property(mLabel, mHueAngleIndex), 6.28318f);
182 anim.SetLooping(true);
185 Property::Value labelText = mLabel.GetProperty( TextLabel::Property::TEXT );
186 std::cout << "Displaying text: \"" << labelText.Get< std::string >() << "\"" << std::endl;
189 // Resize the text-label with pan gesture
190 void OnPan( Actor actor, const PanGesture& gesture )
192 // Reset mLayoutSize when the pan starts
193 if( gesture.state == Gesture::Started )
195 if( mLayoutSize.x < 2.0f )
197 mLayoutSize.x = 2.0f;
200 if( mLayoutSize.y < 2.0f )
202 mLayoutSize.y = 2.0f;
206 mLayoutSize.x += gesture.displacement.x * 2.0f;
207 mLayoutSize.y += gesture.displacement.y * 2.0f;
209 if( mLayoutSize.x >= 2.0f ||
210 mLayoutSize.y >= 2.0f )
212 // Avoid pixel mis-alignment issue
213 Vector2 clampedSize = Vector2( std::max( ConvertToEven( static_cast<int>( mLayoutSize.x )), 2 ),
214 std::max( ConvertToEven( static_cast<int>( mLayoutSize.y )), 2 ) );
216 mContainer.SetSize( clampedSize );
221 * Main key event handler
223 void OnKeyEvent(const KeyEvent& event)
225 if(event.state == KeyEvent::Down)
227 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
231 else if( event.IsCtrlModifier() )
233 switch( event.keyCode )
235 // Select rendering back-end
236 case KEY_ZERO: // fall through
239 mLabel.SetProperty( TextLabel::Property::RENDERING_BACKEND, event.keyCode - 10 );
242 case KEY_F: // Fill vertically
244 if( ResizePolicy::DIMENSION_DEPENDENCY == mLabel.GetResizePolicy(Dimension::HEIGHT) )
246 mLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
250 mLabel.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
254 case KEY_H: // Horizontal alignment
256 if( ++mAlignment >= H_ALIGNMENT_STRING_COUNT )
261 mLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, H_ALIGNMENT_STRING_TABLE[ mAlignment ] );
264 case KEY_V: // Vertical alignment
266 if( ++mAlignment >= V_ALIGNMENT_STRING_COUNT )
271 mLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, V_ALIGNMENT_STRING_TABLE[ mAlignment ] );
274 case KEY_M: // Multi-line
276 bool multiLine = mLabel.GetProperty<bool>( TextLabel::Property::MULTI_LINE );
277 mLabel.SetProperty( TextLabel::Property::MULTI_LINE, !multiLine );
280 case KEY_L: // Language
282 const Language& language = LANGUAGES[ mLanguageId ];
284 mLabel.SetProperty( TextLabel::Property::TEXT, language.text );
286 if( ++mLanguageId >= NUMBER_OF_LANGUAGES )
292 case KEY_S: // Shadow color
294 if( Color::BLACK == mLabel.GetProperty<Vector4>( TextLabel::Property::SHADOW_COLOR ) )
296 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::RED );
300 mLabel.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::BLACK );
304 case KEY_PLUS: // Increase shadow offset
306 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) + Vector2( 1.0f, 1.0f ) );
309 case KEY_MINUS: // Decrease shadow offset
311 mLabel.SetProperty( TextLabel::Property::SHADOW_OFFSET, mLabel.GetProperty<Vector2>( TextLabel::Property::SHADOW_OFFSET ) - Vector2( 1.0f, 1.0f ) );
322 Application& mApplication;
329 PanGestureDetector mPanGestureDetector;
333 unsigned int mLanguageId;
334 unsigned int mAlignment;
335 Property::Index mHueAngleIndex;
336 Property::Index mOverrideMixColorIndex;
339 void RunTest( Application& application )
341 TextLabelExample test( application );
343 application.MainLoop();
346 /** Entry point for Linux & Tizen applications */
347 int DALI_EXPORT_API main( int argc, char **argv )
349 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
351 RunTest( application );