2 * Copyright (c) 2014 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.
18 #include <dali-toolkit/dali-toolkit.h>
23 using Dali::Toolkit::TextLabel;
27 const char* IMAGE_PATH[] = {
28 DEMO_IMAGE_DIR "application-icon-0.png",
29 DEMO_IMAGE_DIR "application-icon-100.png",
30 DEMO_IMAGE_DIR "application-icon-101.png",
31 DEMO_IMAGE_DIR "application-icon-102.png",
32 DEMO_IMAGE_DIR "application-icon-103.png",
33 DEMO_IMAGE_DIR "application-icon-104.png",
34 DEMO_IMAGE_DIR "application-icon-105.png",
35 DEMO_IMAGE_DIR "application-icon-106.png",
36 DEMO_IMAGE_DIR "application-icon-107.png",
37 DEMO_IMAGE_DIR "application-icon-108.png",
38 DEMO_IMAGE_DIR "application-icon-109.png",
39 DEMO_IMAGE_DIR "application-icon-10.png",
40 DEMO_IMAGE_DIR "application-icon-110.png",
41 DEMO_IMAGE_DIR "application-icon-111.png",
42 DEMO_IMAGE_DIR "application-icon-112.png",
43 DEMO_IMAGE_DIR "application-icon-113.png",
44 DEMO_IMAGE_DIR "application-icon-114.png",
45 DEMO_IMAGE_DIR "application-icon-115.png",
46 DEMO_IMAGE_DIR "application-icon-116.png",
47 DEMO_IMAGE_DIR "application-icon-117.png",
48 DEMO_IMAGE_DIR "application-icon-118.png",
49 DEMO_IMAGE_DIR "application-icon-119.png",
50 DEMO_IMAGE_DIR "application-icon-11.png",
51 DEMO_IMAGE_DIR "application-icon-120.png",
52 DEMO_IMAGE_DIR "application-icon-121.png",
53 DEMO_IMAGE_DIR "application-icon-122.png",
54 DEMO_IMAGE_DIR "application-icon-123.png",
55 DEMO_IMAGE_DIR "application-icon-124.png",
56 DEMO_IMAGE_DIR "application-icon-125.png",
57 DEMO_IMAGE_DIR "application-icon-126.png",
58 DEMO_IMAGE_DIR "application-icon-127.png",
59 DEMO_IMAGE_DIR "application-icon-128.png",
60 DEMO_IMAGE_DIR "application-icon-129.png",
61 DEMO_IMAGE_DIR "application-icon-12.png",
62 DEMO_IMAGE_DIR "application-icon-130.png",
63 DEMO_IMAGE_DIR "application-icon-131.png",
64 DEMO_IMAGE_DIR "application-icon-132.png",
65 DEMO_IMAGE_DIR "application-icon-133.png",
66 DEMO_IMAGE_DIR "application-icon-134.png",
67 DEMO_IMAGE_DIR "application-icon-135.png",
68 DEMO_IMAGE_DIR "application-icon-136.png",
69 DEMO_IMAGE_DIR "application-icon-137.png",
70 DEMO_IMAGE_DIR "application-icon-138.png",
71 DEMO_IMAGE_DIR "application-icon-139.png",
72 DEMO_IMAGE_DIR "application-icon-13.png",
73 DEMO_IMAGE_DIR "application-icon-140.png",
74 DEMO_IMAGE_DIR "application-icon-141.png",
75 DEMO_IMAGE_DIR "application-icon-142.png",
76 DEMO_IMAGE_DIR "application-icon-143.png",
77 DEMO_IMAGE_DIR "application-icon-144.png",
78 DEMO_IMAGE_DIR "application-icon-145.png",
79 DEMO_IMAGE_DIR "application-icon-146.png",
80 DEMO_IMAGE_DIR "application-icon-147.png",
81 DEMO_IMAGE_DIR "application-icon-14.png",
82 DEMO_IMAGE_DIR "application-icon-15.png",
83 DEMO_IMAGE_DIR "application-icon-16.png",
84 DEMO_IMAGE_DIR "application-icon-17.png",
85 DEMO_IMAGE_DIR "application-icon-18.png",
86 DEMO_IMAGE_DIR "application-icon-19.png",
87 DEMO_IMAGE_DIR "application-icon-1.png",
88 DEMO_IMAGE_DIR "application-icon-20.png",
89 DEMO_IMAGE_DIR "application-icon-21.png",
90 DEMO_IMAGE_DIR "application-icon-22.png",
91 DEMO_IMAGE_DIR "application-icon-23.png",
92 DEMO_IMAGE_DIR "application-icon-24.png",
93 DEMO_IMAGE_DIR "application-icon-25.png",
94 DEMO_IMAGE_DIR "application-icon-26.png",
95 DEMO_IMAGE_DIR "application-icon-27.png",
96 DEMO_IMAGE_DIR "application-icon-28.png",
97 DEMO_IMAGE_DIR "application-icon-29.png",
98 DEMO_IMAGE_DIR "application-icon-2.png",
99 DEMO_IMAGE_DIR "application-icon-30.png",
100 DEMO_IMAGE_DIR "application-icon-31.png",
101 DEMO_IMAGE_DIR "application-icon-32.png",
102 DEMO_IMAGE_DIR "application-icon-33.png",
103 DEMO_IMAGE_DIR "application-icon-34.png",
104 DEMO_IMAGE_DIR "application-icon-35.png",
105 DEMO_IMAGE_DIR "application-icon-36.png",
106 DEMO_IMAGE_DIR "application-icon-37.png",
107 DEMO_IMAGE_DIR "application-icon-38.png",
108 DEMO_IMAGE_DIR "application-icon-39.png",
109 DEMO_IMAGE_DIR "application-icon-3.png",
110 DEMO_IMAGE_DIR "application-icon-40.png",
111 DEMO_IMAGE_DIR "application-icon-41.png",
112 DEMO_IMAGE_DIR "application-icon-42.png",
113 DEMO_IMAGE_DIR "application-icon-43.png",
114 DEMO_IMAGE_DIR "application-icon-44.png",
115 DEMO_IMAGE_DIR "application-icon-45.png",
116 DEMO_IMAGE_DIR "application-icon-46.png",
117 DEMO_IMAGE_DIR "application-icon-47.png",
118 DEMO_IMAGE_DIR "application-icon-48.png",
119 DEMO_IMAGE_DIR "application-icon-49.png",
120 DEMO_IMAGE_DIR "application-icon-4.png",
121 DEMO_IMAGE_DIR "application-icon-50.png",
122 DEMO_IMAGE_DIR "application-icon-51.png",
123 DEMO_IMAGE_DIR "application-icon-52.png",
124 DEMO_IMAGE_DIR "application-icon-53.png",
125 DEMO_IMAGE_DIR "application-icon-54.png",
126 DEMO_IMAGE_DIR "application-icon-55.png",
127 DEMO_IMAGE_DIR "application-icon-56.png",
128 DEMO_IMAGE_DIR "application-icon-57.png",
129 DEMO_IMAGE_DIR "application-icon-58.png",
130 DEMO_IMAGE_DIR "application-icon-59.png",
131 DEMO_IMAGE_DIR "application-icon-5.png",
132 DEMO_IMAGE_DIR "application-icon-60.png",
133 DEMO_IMAGE_DIR "application-icon-61.png",
134 DEMO_IMAGE_DIR "application-icon-62.png",
135 DEMO_IMAGE_DIR "application-icon-63.png",
136 DEMO_IMAGE_DIR "application-icon-64.png",
137 DEMO_IMAGE_DIR "application-icon-65.png",
138 DEMO_IMAGE_DIR "application-icon-66.png",
139 DEMO_IMAGE_DIR "application-icon-67.png",
140 DEMO_IMAGE_DIR "application-icon-68.png",
141 DEMO_IMAGE_DIR "application-icon-69.png",
142 DEMO_IMAGE_DIR "application-icon-6.png",
143 DEMO_IMAGE_DIR "application-icon-70.png",
144 DEMO_IMAGE_DIR "application-icon-71.png",
145 DEMO_IMAGE_DIR "application-icon-72.png",
146 DEMO_IMAGE_DIR "application-icon-73.png",
147 DEMO_IMAGE_DIR "application-icon-74.png",
148 DEMO_IMAGE_DIR "application-icon-75.png",
149 DEMO_IMAGE_DIR "application-icon-76.png",
150 DEMO_IMAGE_DIR "application-icon-77.png",
151 DEMO_IMAGE_DIR "application-icon-78.png",
152 DEMO_IMAGE_DIR "application-icon-79.png",
153 DEMO_IMAGE_DIR "application-icon-7.png",
154 DEMO_IMAGE_DIR "application-icon-80.png",
155 DEMO_IMAGE_DIR "application-icon-81.png",
156 DEMO_IMAGE_DIR "application-icon-82.png",
157 DEMO_IMAGE_DIR "application-icon-83.png",
158 DEMO_IMAGE_DIR "application-icon-84.png",
159 DEMO_IMAGE_DIR "application-icon-85.png",
160 DEMO_IMAGE_DIR "application-icon-86.png",
161 DEMO_IMAGE_DIR "application-icon-87.png",
162 DEMO_IMAGE_DIR "application-icon-88.png",
163 DEMO_IMAGE_DIR "application-icon-89.png",
164 DEMO_IMAGE_DIR "application-icon-8.png",
165 DEMO_IMAGE_DIR "application-icon-90.png",
166 DEMO_IMAGE_DIR "application-icon-91.png",
167 DEMO_IMAGE_DIR "application-icon-92.png",
168 DEMO_IMAGE_DIR "application-icon-93.png",
169 DEMO_IMAGE_DIR "application-icon-94.png",
170 DEMO_IMAGE_DIR "application-icon-95.png",
171 DEMO_IMAGE_DIR "application-icon-96.png",
172 DEMO_IMAGE_DIR "application-icon-97.png",
173 DEMO_IMAGE_DIR "application-icon-98.png",
174 DEMO_IMAGE_DIR "application-icon-99.png",
175 DEMO_IMAGE_DIR "application-icon-9.png",
180 * Random words used as unique application names
182 const char* DEMO_APPS_NAMES[] =
337 // this code comes from command-line-options.cpp. the reason it's here is to
338 // keep consistent the extra-help formatting when '--help' used
340 void PrintHelp( const char * const opt, const char * const optDescription)
342 const std::ios_base::fmtflags flags = std::cout.flags();
343 std::cout << std::left << " -";
344 std::cout.width( 18 );
346 std::cout << optDescription;
347 std::cout << std::endl;
348 std::cout.flags( flags );
351 const float PAGE_SCALE_FACTOR_X ( 0.95f );
352 const float PAGE_SCALE_FACTOR_Y ( 0.95f );
353 const float PAGE_DURATION_SCALE_FACTOR ( 2.0f ); // time-scale factor, larger = animation is slower
355 const float DEFAULT_OPT_ROW_COUNT ( 5 );
356 const float DEFAULT_OPT_COL_COUNT ( 4 );
357 const float DEFAULT_OPT_PAGE_COUNT ( 10 );
358 const bool DEFAULT_OPT_USETABLEVIEW ( false );
362 // This example is a benchmark that mimics the paged applications list of the homescreen app
364 class HomescreenBenchmark : public ConnectionTracker
368 // Config structure passed to the constructor. It makes easier to increase number
369 // of setup parameters if needed
373 mRows( DEFAULT_OPT_ROW_COUNT ),
374 mCols( DEFAULT_OPT_COL_COUNT ),
375 mPageCount( DEFAULT_OPT_PAGE_COUNT ),
376 mUseTableView( DEFAULT_OPT_USETABLEVIEW )
386 // animation script data
389 ScriptData( int pages, float duration, bool flick )
392 mDuration = duration;
396 int mPages; // number of pages to scroll
397 float mDuration; // duration
398 bool mFlick; // use flick or 'one-by-one' scroll
401 HomescreenBenchmark( Application& application, const Config& config )
402 : mApplication( application ),
406 // Connect to the Application's Init signal
407 mApplication.InitSignal().Connect( this, &HomescreenBenchmark::Create );
410 ~HomescreenBenchmark()
412 // Nothing to do here;
415 // The Init signal is received once (only) during the Application lifetime
416 void Create( Application& application )
418 // create benchmark script
421 // Get a handle to the stage
422 Stage stage = Stage::GetCurrent();
424 mScrollParent = Actor::New();
425 mScrollParent.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
426 mScrollParent.SetAnchorPoint( AnchorPoint::CENTER );
427 mScrollParent.SetParentOrigin( ParentOrigin::CENTER );
430 Toolkit::ImageView background = Toolkit::ImageView::New( DEMO_IMAGE_DIR "background-3.jpg");
431 Stage::GetCurrent().Add(background);
432 background.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
433 background.SetAnchorPoint( AnchorPoint::CENTER );
434 background.SetParentOrigin( ParentOrigin::CENTER );
438 stage.Add(mScrollParent);
440 // Respond to a click anywhere on the stage
441 stage.GetRootLayer().TouchSignal().Connect( this, &HomescreenBenchmark::OnTouch );
444 bool OnTouch( Actor actor, const TouchData& touch )
446 // quit the application
453 // create root page actor
456 if( mConfig.mUseTableView )
458 Toolkit::TableView tableView = Toolkit::TableView::New( mConfig.mRows, mConfig.mCols );
459 tableView.SetBackgroundColor( Vector4( 0.0f, 0.0f, 0.0f, 0.5f ) );
460 pageActor = tableView;
464 pageActor = Toolkit::Control::New();
465 pageActor.SetProperty( Toolkit::Control::Property::BACKGROUND_COLOR, Vector4( 0.0f, 0.0f, 0.0f, 0.5f ) );
468 pageActor.SetParentOrigin( ParentOrigin::CENTER );
469 pageActor.SetAnchorPoint( AnchorPoint::CENTER );
470 pageActor.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
471 pageActor.SetSizeModeFactor(Vector3( PAGE_SCALE_FACTOR_X, PAGE_SCALE_FACTOR_Y, 1.0f ) );
475 void AddIconsToPage(Actor page)
477 Size stageSize( Stage::GetCurrent().GetSize() );
478 const float scaledHeight = stageSize.y * PAGE_SCALE_FACTOR_Y;
479 const float scaledWidth = stageSize.x * PAGE_SCALE_FACTOR_X;
481 const float PADDING = stageSize.y / 64.0f;
482 const float ROW_HEIGHT = ( scaledHeight - (PADDING*2.0f) ) / (float)mConfig.mRows;
483 const float COL_WIDTH = ( scaledWidth - (PADDING*2.0f) ) / (float)mConfig.mCols;
485 // the image/label area tries to make sure the positioning will be relative to previous sibling
486 const float IMAGE_AREA = 0.60f;
487 const float LABEL_AREA = 0.50f;
489 static int currentIconIndex = 0;
491 Vector2 dpi = Stage::GetCurrent().GetDpi();
493 for( int y = 0; y < mConfig.mRows; ++y )
495 for( int x = 0; x < mConfig.mCols; ++x )
497 // create parent icon view
498 Toolkit::Control iconView = Toolkit::Control::New();
499 iconView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
500 iconView.SetParentOrigin( ParentOrigin::TOP_LEFT );
502 if( !mConfig.mUseTableView )
504 float rowX = x * COL_WIDTH + PADDING;
505 float rowY = y * ROW_HEIGHT + PADDING;
506 iconView.SetSize( Vector3( COL_WIDTH, ROW_HEIGHT, 1.0f ) );
507 iconView.SetPosition( Vector3( rowX, rowY, 0.0f ) );
511 iconView.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
512 iconView.SetSizeScalePolicy( SizeScalePolicy::FIT_WITH_ASPECT_RATIO );
516 Toolkit::ImageView imageView = Toolkit::ImageView::New( IMAGE_PATH[currentIconIndex] );
517 imageView.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
518 imageView.SetSizeScalePolicy( SizeScalePolicy::FIT_WITH_ASPECT_RATIO );
519 imageView.SetAnchorPoint( AnchorPoint::CENTER );
520 imageView.SetParentOrigin( ParentOrigin::CENTER );
521 imageView.SetSizeModeFactor( Vector3(IMAGE_AREA, IMAGE_AREA, 1.0f) );
524 Toolkit::TextLabel textLabel = Toolkit::TextLabel::New( DEMO_APPS_NAMES[currentIconIndex] );
525 textLabel.SetAnchorPoint( AnchorPoint::TOP_CENTER );
526 textLabel.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
528 textLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
529 textLabel.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, Vector4( 1.0f, 1.0f, 1.0f, 1.0f ) );
530 textLabel.SetProperty( Toolkit::TextLabel::Property::POINT_SIZE, (((float)( ROW_HEIGHT * LABEL_AREA ) * 72.0f) / (dpi.y))*0.25f );
531 textLabel.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
532 textLabel.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "TOP" );
534 iconView.Add( imageView );
535 imageView.Add( textLabel );
539 if( !IMAGE_PATH[ ++currentIconIndex ] )
541 currentIconIndex = 0;
549 const int lastPage = mConfig.mPageCount-1;
550 const int halfA = lastPage/2;
551 const int halfB = lastPage/2 + lastPage%2;
552 mScriptFrameData.push_back( ScriptData( lastPage, 1.5f, true));
553 mScriptFrameData.push_back( ScriptData( -lastPage, 1.5f, true ));
554 mScriptFrameData.push_back( ScriptData( halfA, 1.0f, true ));
555 mScriptFrameData.push_back( ScriptData( halfB, 1.0f, true ));
556 mScriptFrameData.push_back( ScriptData( -lastPage, 0.5f, false ));
557 mScriptFrameData.push_back( ScriptData( halfA, 0.5f, false ));
558 mScriptFrameData.push_back( ScriptData( halfB, 1.0f, true ));
559 mScriptFrameData.push_back( ScriptData( -halfA, 1.0f, true ));
560 mScriptFrameData.push_back( ScriptData( 1, 0.1f, true ));
561 mScriptFrameData.push_back( ScriptData( -1, 0.1f, true ));
562 mScriptFrameData.push_back( ScriptData( 1, 0.1f, true ));
563 mScriptFrameData.push_back( ScriptData( -1, 0.1f, true ));
564 mScriptFrameData.push_back( ScriptData( 1, 0.1f, true ));
565 mScriptFrameData.push_back( ScriptData( -1, 0.1f, true ));
566 mScriptFrameData.push_back( ScriptData( halfA, 1.0f, true ));
571 Vector3 stageSize( Stage::GetCurrent().GetSize() );
572 const float SCALED_HEIGHT = stageSize.y * PAGE_SCALE_FACTOR_Y;
574 for( int i = 0; i < mConfig.mPageCount; ++i )
577 Actor page = AddPage();
580 AddIconsToPage(page);
582 // move page 'a little bit up'
583 page.SetParentOrigin( ParentOrigin::CENTER );
584 page.SetAnchorPoint( AnchorPoint::CENTER );
585 page.SetPosition( Vector3(stageSize.x*i, -0.30f*( stageSize.y-SCALED_HEIGHT ), 0.0f) );
586 mScrollParent.Add( page );
589 mScrollParent.SetOpacity( 0.0f );
590 mScrollParent.SetScale( Vector3(0.0f, 0.0f, 0.0f) );
598 mShowAnimation = Animation::New(1.0f);
599 mShowAnimation.AnimateTo( Property(mScrollParent, Actor::Property::COLOR_ALPHA), 1.0f, AlphaFunction::EASE_IN_OUT );
600 mShowAnimation.AnimateTo( Property(mScrollParent, Actor::Property::SCALE), Vector3(1.0f, 1.0f, 1.0f), AlphaFunction::EASE_IN_OUT );
601 mShowAnimation.FinishedSignal().Connect( this, &HomescreenBenchmark::OnAnimationEnd );
602 mShowAnimation.Play();
605 void ScrollPages(int pages, float duration, bool flick)
607 duration *= PAGE_DURATION_SCALE_FACTOR;
608 Vector3 stageSize( Stage::GetCurrent().GetSize() );
609 mScrollAnimation = Animation::New(duration);
612 mScrollAnimation.AnimateBy( Property(mScrollParent, Actor::Property::POSITION), Vector3(-stageSize.x*pages, 0.0f, 0.0f), AlphaFunction::EASE_IN_OUT );
616 for( int i = 0; i < abs(pages); ++i )
618 mScrollAnimation.AnimateBy( Property(mScrollParent, Actor::Property::POSITION), Vector3(pages < 0 ? stageSize.x : -stageSize.x, 0.0f, 0.0f), AlphaFunction::EASE_IN_OUT, TimePeriod(duration*i, duration) );
621 mScrollAnimation.FinishedSignal().Connect( this, &HomescreenBenchmark::OnAnimationEnd );
622 mScrollAnimation.Play();
623 mCurrentPage += pages;
626 void OnAnimationEnd( Animation& source )
628 if( source == mShowAnimation )
630 ScriptData& frame = mScriptFrameData[0];
631 ScrollPages( frame.mPages, frame.mDuration, frame.mFlick );
634 else if( mScriptFrame < mScriptFrameData.size() && source == mScrollAnimation )
636 ScriptData& frame = mScriptFrameData[mScriptFrame];
637 ScrollPages( frame.mPages, frame.mDuration, frame.mFlick );
647 Application& mApplication;
651 Animation mShowAnimation;
652 Animation mScrollAnimation;
656 std::vector<ScriptData> mScriptFrameData;
662 void RunTest( Application& application, const HomescreenBenchmark::Config& config, bool printHelpAndExit )
664 HomescreenBenchmark test( application, config );
666 if( printHelpAndExit )
668 PrintHelp( "c<num>", " Number of columns");
669 PrintHelp( "r<num>", " Number of rows");
670 PrintHelp( "p<num>", " Number of pages ( must be greater than 1 )");
671 PrintHelp( "-use-tableview", " Uses TableView for layouting");
675 application.MainLoop();
678 // Entry point for Linux & Tizen applications
680 int DALI_EXPORT_API main( int argc, char **argv )
683 HomescreenBenchmark::Config config;
685 bool printHelpAndExit = false;
687 for( int i(1) ; i < argc; ++i )
689 std::string arg( argv[i] );
690 if( arg.compare( 0, 2, "-r" ) == 0 )
692 config.mRows = atoi( arg.substr( 2 ).c_str() );
694 else if( arg.compare( 0, 2, "-c" ) == 0 )
696 config.mCols = atoi( arg.substr( 2 ).c_str() );
698 else if( arg.compare( 0, 2, "-p" ) == 0 )
700 config.mPageCount = atoi( arg.substr( 2 ).c_str() );
702 else if( arg.compare( "--use-tableview" ) == 0 )
704 config.mUseTableView = true;
706 else if( arg.compare( "--help" ) == 0 )
708 printHelpAndExit = true;
713 Application application = Application::New( &argc, &argv );
715 RunTest( application, config, printHelpAndExit );