2 * Copyright (c) 2017 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-memory-profiling-example.cpp
20 * @brief Memory consumption profiling for TextLabel
24 #include <dali/dali.h>
25 #include <dali-toolkit/dali-toolkit.h>
26 #include <dali-toolkit/devel-api/controls/buttons/button-devel.h>
27 #include <dali-toolkit/devel-api/controls/navigation-view/navigation-view.h>
30 #include "shared/view.h"
33 using namespace Dali::Toolkit;
41 SINGLE_COLOR_TEXT_WITH_STYLE,
42 SINGLE_COLOR_TEXT_WITH_EMOJI,
43 SINGLE_COLOR_TEXT_WITH_STYLE_EMOJI,
45 MULTI_COLOR_TEXT_WITH_STYLE,
46 MULTI_COLOR_TEXT_WITH_EMOJI,
47 MULTI_COLOR_TEXT_WITH_STYLE_EMOJI,
48 SMALL_TEXT_IN_LARGE_TEXT_LABEL,
52 std::string TEXT_TYPE_STRING[ NUMBER_OF_TYPES ] =
55 "Single color text with style",
56 "Single color text with emoji",
57 "Single color text with style and emoji",
59 "Multi color text with style",
60 "Multi color text with emoji",
61 "Multi color text with style and emoji",
62 "Small text in large Text Label"
65 const int NUMBER_OF_LABELS = 500;
67 const char* BACKGROUND_IMAGE( "" );
68 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
69 const char* BACK_IMAGE( DEMO_IMAGE_DIR "icon-change.png" );
70 const char* BACK_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-change-selected.png" );
71 const char* INDICATOR_IMAGE( DEMO_IMAGE_DIR "loading.png" );
73 } // anonymous namespace
76 * @brief The main class of the demo.
78 class TextMemoryProfilingExample : public ConnectionTracker, public Toolkit::ItemFactory
82 TextMemoryProfilingExample( Application& application )
83 : mApplication( application ),
84 mCurrentTextStyle( SINGLE_COLOR_TEXT )
86 // Connect to the Application's Init signal
87 mApplication.InitSignal().Connect( this, &TextMemoryProfilingExample::Create );
90 ~TextMemoryProfilingExample()
92 // Nothing to do here.
96 * @brief Create a text label in the given type
98 TextLabel SetupTextLabel( int type )
100 TextLabel label = TextLabel::New();
101 label.SetAnchorPoint( ParentOrigin::TOP_LEFT );
102 label.SetAnchorPoint( AnchorPoint::TOP_LEFT );
103 label.SetProperty( TextLabel::Property::TEXT_COLOR, Color::BLACK );
104 label.SetProperty( TextLabel::Property::POINT_SIZE, 12.0f );
105 label.SetProperty( TextLabel::Property::SHADOW_COLOR, Color::YELLOW );
106 label.SetProperty( TextLabel::Property::ENABLE_MARKUP, true );
108 Vector2 stageSize = Stage::GetCurrent().GetSize();
109 label.SetPosition( Vector3( Random::Range( 0.0f, stageSize.x ), Random::Range( 0.0f, stageSize.y ), 0.0f) );
113 case SINGLE_COLOR_TEXT:
115 label.SetProperty( TextLabel::Property::TEXT, "A Quick Brown Fox Jumps Over The Lazy Dog" );
116 label.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 0.0f, 0.0f ) );
119 case SINGLE_COLOR_TEXT_WITH_STYLE:
121 label.SetProperty( TextLabel::Property::TEXT, "A Quick Brown Fox Jumps Over The Lazy Dog" );
122 label.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 2.0f, 2.0f ) );
125 case SINGLE_COLOR_TEXT_WITH_EMOJI:
127 label.SetProperty( TextLabel::Property::TEXT, "\xF0\x9F\x98\x81 A Quick Brown Fox Jumps Over The Lazy Dog" );
128 label.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 0.0f, 0.0f ) );
131 case SINGLE_COLOR_TEXT_WITH_STYLE_EMOJI:
133 label.SetProperty( TextLabel::Property::TEXT, "\xF0\x9F\x98\x81 A Quick Brown Fox Jumps Over The Lazy Dog" );
134 label.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 2.0f, 2.0f ) );
137 case MULTI_COLOR_TEXT:
139 label.SetProperty( TextLabel::Property::TEXT, "A <color value='cyan'>Quick Brown Fox</color> Jumps Over The <color value='yellow'>Lazy Dog</color>" );
140 label.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 0.0f, 0.0f ) );
143 case MULTI_COLOR_TEXT_WITH_STYLE:
145 label.SetProperty( TextLabel::Property::TEXT, "A <color value='cyan'>Quick Brown Fox</color> Jumps Over The <color value='yellow'>Lazy Dog</color>" );
146 label.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 2.0f, 2.0f ) );
149 case MULTI_COLOR_TEXT_WITH_EMOJI:
151 label.SetProperty( TextLabel::Property::TEXT, " \xF0\x9F\x98\x81 A <color value='cyan'>Quick Brown Fox</color> Jumps Over The <color value='yellow'>Lazy Dog</color>" );
152 label.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 0.0f, 0.0f ) );
155 case MULTI_COLOR_TEXT_WITH_STYLE_EMOJI:
157 label.SetProperty( TextLabel::Property::TEXT, " \xF0\x9F\x98\x81 A <color value='cyan'>Quick Brown Fox</color> Jumps Over The <color value='yellow'>Lazy Dog</color>" );
158 label.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 2.0f, 2.0f ) );
161 case SMALL_TEXT_IN_LARGE_TEXT_LABEL:
163 label.SetProperty( TextLabel::Property::TEXT, "A Quick Brown Fox Jumps Over The Lazy Dog" );
164 label.SetProperty( TextLabel::Property::SHADOW_OFFSET, Vector2( 0.0f, 0.0f ) );
165 label.SetSize(stageSize.x, stageSize.y * 0.25f); // Set the text label in larger size
176 * @brief Create the main menu
178 void CreateMainMenu()
180 Stage stage = Stage::GetCurrent();
181 Vector2 stageSize = stage.GetSize();
183 mTapDetector = TapGestureDetector::New();
184 mTapDetector.DetectedSignal().Connect( this, &TextMemoryProfilingExample::OnTap );
186 // Create an item view for the main menu
187 mItemView = ItemView::New( *this );
189 mItemView.SetParentOrigin( ParentOrigin::CENTER );
190 mItemView.SetAnchorPoint( AnchorPoint::CENTER );
192 mLayout = DefaultItemLayout::New( DefaultItemLayout::LIST );
193 mLayout->SetItemSize( Vector3( stageSize.width, 60.0f, 0.0f ) );
195 mItemView.AddLayout( *mLayout );
197 // Activate the layout
198 mItemView.ActivateLayout( 0, Vector3( stageSize ), 0.0f );
202 * @brief Return the number of items in the main menu
204 virtual unsigned int GetNumberOfItems()
206 return NUMBER_OF_TYPES;
210 * @brief Create new item for the main menu
212 virtual Actor NewItem( unsigned int itemId )
214 TextLabel label = TextLabel::New( TEXT_TYPE_STRING[itemId] );
215 label.SetStyleName( "BuilderLabel" );
216 label.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
218 // Hook up tap detector
219 mTapDetector.Attach( label );
225 * @brief Create text labels for memory profiling
227 void CreateTextLabels( int type )
229 Stage stage = Stage::GetCurrent();
231 // Render tasks may have been setup last load so remove them
232 RenderTaskList taskList = stage.GetRenderTaskList();
233 if( taskList.GetTaskCount() > 1 )
235 typedef std::vector<RenderTask> Collection;
236 typedef Collection::iterator ColIter;
239 for( unsigned int i = 1; i < taskList.GetTaskCount(); ++i )
241 tasks.push_back( taskList.GetTask(i) );
244 for( ColIter iter = tasks.begin(); iter != tasks.end(); ++iter )
246 taskList.RemoveTask(*iter);
249 RenderTask defaultTask = taskList.GetTask( 0 );
250 defaultTask.SetSourceActor( stage.GetRootLayer() );
251 defaultTask.SetTargetFrameBuffer( FrameBufferImage() );
254 // Delete any existing text labels
255 unsigned int numChildren = mLayer.GetChildCount();
257 for( unsigned int i = 0; i < numChildren; ++i )
259 mLayer.Remove( mLayer.GetChildAt( 0 ) );
262 mLayer.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
263 mLayer.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
264 mLayer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
265 mLayer.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::HEIGHT );
266 mLayer.SetSizeModeFactor( Vector3( 0.0f, -DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight, 0.0f ) );
268 mNavigationView.Push( mLayer );
270 // Create new text labels
271 for ( int i = 0; i < NUMBER_OF_LABELS; i++ )
273 TextLabel label = SetupTextLabel( type );
277 mTitle.SetProperty( TextLabel::Property::TEXT, "Run memps on target" );
281 * @brief One-time setup in response to Application InitSignal.
283 void Create( Application& application )
285 Stage stage = Stage::GetCurrent();
287 stage.KeyEventSignal().Connect(this, &TextMemoryProfilingExample::OnKeyEvent);
289 Layer contents = DemoHelper::CreateView( mApplication,
296 mTitle = DemoHelper::CreateToolBarLabel( "" );
297 mTitle.SetProperty( TextLabel::Property::TEXT, "Select the type of text" );
299 // Add title to the tool bar.
300 mToolBar.AddControl( mTitle, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HorizontalCenter );
302 // Create a layer to contain dynamically created text labels
303 mLayer = Layer::New();
305 mIndicator = Toolkit::ImageView::New(INDICATOR_IMAGE);
306 mIndicator.SetParentOrigin( ParentOrigin::CENTER );
307 mIndicator.SetAnchorPoint( AnchorPoint::CENTER );
308 mIndicator.SetProperty( Actor::Property::VISIBLE, false );
310 // Create a back button in the left of toolbar
311 PushButton backButton = PushButton::New();
312 backButton.SetProperty( DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, BACK_IMAGE );
313 backButton.SetProperty( DevelButton::Property::SELECTED_BACKGROUND_VISUAL, BACK_IMAGE_SELECTED );
314 backButton.ClickedSignal().Connect( this, &TextMemoryProfilingExample::OnBackButtonPressed );
315 backButton.SetLeaveRequired( true );
316 mToolBar.AddControl( backButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Alignment::HorizontalLeft, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
318 // Create a navigation view to navigate different types of text labels
319 mNavigationView = NavigationView::New();
320 mNavigationView.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
321 mNavigationView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
322 mNavigationView.SetBackgroundColor( Color::WHITE );
323 stage.Add( mNavigationView );
326 mNavigationView.Push( mItemView );
328 mItemView.Add(mIndicator);
330 PropertyNotification notification = mIndicator.AddPropertyNotification( Actor::Property::VISIBLE, GreaterThanCondition(0.01f) );
331 notification.NotifySignal().Connect( this, &TextMemoryProfilingExample::OnIndicatorVisible );
335 * @brief Main key event handler
337 void OnKeyEvent( const KeyEvent& event )
339 if( event.state == KeyEvent::Down )
341 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
343 ReturnToPreviousScreen();
349 * @brief Tap gesture handler
351 void OnTap( Actor actor, const TapGesture& tap )
353 mCurrentTextStyle = mItemView.GetItemId( actor );
355 // Show the loading indicator
356 mIndicator.SetProperty( Actor::Property::VISIBLE, true );
364 mAnimation = Animation::New( 0.8f );
365 mAnimation.AnimateBy( Property( mIndicator, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(180.0f) ), Vector3::ZAXIS ) );
366 mAnimation.SetLooping( true );
371 * @brief Property notification handler
373 void OnIndicatorVisible( PropertyNotification& source )
375 CreateTextLabels( mCurrentTextStyle );
377 // Hide the loading indicator
379 mIndicator.SetProperty( Actor::Property::VISIBLE, false );
383 * @brief Button signal handler
385 bool OnBackButtonPressed( Toolkit::Button button )
387 ReturnToPreviousScreen();
392 * @brief Returns to the previous screen
394 void ReturnToPreviousScreen()
396 if ( mItemView.OnStage() )
398 // Quit the application if it is in the main menu
403 // Return to the main menu
404 mNavigationView.Pop();
406 mTitle.SetProperty( TextLabel::Property::TEXT, "Select type of text to test" );
412 Application& mApplication;
414 ItemLayoutPtr mLayout;
416 NavigationView mNavigationView;
421 ImageView mIndicator;
422 Animation mAnimation;
426 TapGestureDetector mTapDetector;
428 unsigned int mCurrentTextStyle;
431 int DALI_EXPORT_API main( int argc, char **argv )
433 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
434 TextMemoryProfilingExample test( application );
435 application.MainLoop();