Merge "Fix the scalability issue on dali-demo" into tizen
[platform/core/uifw/dali-demo.git] / examples / item-view / item-view-example.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <sstream>
19 #include <iostream>
20 #include <vector>
21 #include <string>
22 #include <algorithm>
23 #include <cstdlib> // rand
24 #include "../shared/view.h"
25
26 #include <dali/dali.h>
27 #include <dali-toolkit/dali-toolkit.h>
28
29 using namespace std;
30 using namespace Dali;
31 using namespace Dali::Toolkit;
32
33 namespace
34 {
35
36 enum AllImagesLayouts
37 {
38   SPIRAL_LAYOUT,
39   DEPTH_LAYOUT,
40   GRID_LAYOUT,
41
42   NUMBER_OF_LAYOUTS
43 };
44
45 const char* IMAGE_PATHS[] = {
46     DALI_IMAGE_DIR "gallery-medium-1.jpg",
47     DALI_IMAGE_DIR "gallery-medium-2.jpg",
48     DALI_IMAGE_DIR "gallery-medium-3.jpg",
49     DALI_IMAGE_DIR "gallery-medium-4.jpg",
50     DALI_IMAGE_DIR "gallery-medium-5.jpg",
51     DALI_IMAGE_DIR "gallery-medium-6.jpg",
52     DALI_IMAGE_DIR "gallery-medium-7.jpg",
53     DALI_IMAGE_DIR "gallery-medium-8.jpg",
54     DALI_IMAGE_DIR "gallery-medium-9.jpg",
55     DALI_IMAGE_DIR "gallery-medium-10.jpg",
56     DALI_IMAGE_DIR "gallery-medium-11.jpg",
57     DALI_IMAGE_DIR "gallery-medium-12.jpg",
58     DALI_IMAGE_DIR "gallery-medium-13.jpg",
59     DALI_IMAGE_DIR "gallery-medium-14.jpg",
60     DALI_IMAGE_DIR "gallery-medium-15.jpg",
61     DALI_IMAGE_DIR "gallery-medium-16.jpg",
62     DALI_IMAGE_DIR "gallery-medium-17.jpg",
63     DALI_IMAGE_DIR "gallery-medium-18.jpg",
64     DALI_IMAGE_DIR "gallery-medium-19.jpg",
65     DALI_IMAGE_DIR "gallery-medium-20.jpg",
66     DALI_IMAGE_DIR "gallery-medium-21.jpg",
67     DALI_IMAGE_DIR "gallery-medium-22.jpg",
68     DALI_IMAGE_DIR "gallery-medium-23.jpg",
69     DALI_IMAGE_DIR "gallery-medium-24.jpg",
70     DALI_IMAGE_DIR "gallery-medium-25.jpg",
71     DALI_IMAGE_DIR "gallery-medium-26.jpg",
72     DALI_IMAGE_DIR "gallery-medium-27.jpg",
73     DALI_IMAGE_DIR "gallery-medium-28.jpg",
74     DALI_IMAGE_DIR "gallery-medium-29.jpg",
75     DALI_IMAGE_DIR "gallery-medium-30.jpg",
76     DALI_IMAGE_DIR "gallery-medium-31.jpg",
77     DALI_IMAGE_DIR "gallery-medium-32.jpg",
78     DALI_IMAGE_DIR "gallery-medium-33.jpg",
79     DALI_IMAGE_DIR "gallery-medium-34.jpg",
80     DALI_IMAGE_DIR "gallery-medium-35.jpg",
81     DALI_IMAGE_DIR "gallery-medium-36.jpg",
82     DALI_IMAGE_DIR "gallery-medium-37.jpg",
83     DALI_IMAGE_DIR "gallery-medium-38.jpg",
84     DALI_IMAGE_DIR "gallery-medium-39.jpg",
85     DALI_IMAGE_DIR "gallery-medium-40.jpg",
86     DALI_IMAGE_DIR "gallery-medium-41.jpg",
87     DALI_IMAGE_DIR "gallery-medium-42.jpg",
88     DALI_IMAGE_DIR "gallery-medium-43.jpg",
89     DALI_IMAGE_DIR "gallery-medium-44.jpg",
90     DALI_IMAGE_DIR "gallery-medium-45.jpg",
91     DALI_IMAGE_DIR "gallery-medium-46.jpg",
92     DALI_IMAGE_DIR "gallery-medium-47.jpg",
93     DALI_IMAGE_DIR "gallery-medium-48.jpg",
94     DALI_IMAGE_DIR "gallery-medium-49.jpg",
95     DALI_IMAGE_DIR "gallery-medium-50.jpg",
96     DALI_IMAGE_DIR "gallery-medium-51.jpg",
97     DALI_IMAGE_DIR "gallery-medium-52.jpg",
98     DALI_IMAGE_DIR "gallery-medium-53.jpg",
99 };
100
101 const unsigned int NUM_IMAGES = sizeof(IMAGE_PATHS) / sizeof(char*);
102
103 AlphaFunction ALPHA_FUNCTIONS[] = { AlphaFunctions::Linear,
104                                     AlphaFunctions::EaseIn,
105                                     AlphaFunctions::EaseOut };
106
107 const unsigned int NUM_ALPHA_FUNCTIONS = sizeof(ALPHA_FUNCTIONS) / sizeof(AlphaFunction);
108
109 const char* ALPHA_FUNCTIONS_TEXT[] = { "Linear",
110                                        "EaseIn",
111                                        "EaseOut" };
112
113 const char* BACKGROUND_IMAGE( "" );
114 const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
115 const char* EDIT_IMAGE( DALI_IMAGE_DIR "icon-edit.png" );
116 const char* SPIRAL_LAYOUT_IMAGE( DALI_IMAGE_DIR "icon-item-view-layout-spiral.png" );
117 const char* GRID_LAYOUT_IMAGE( DALI_IMAGE_DIR "icon-item-view-layout-grid.png" );
118 const char* DEPTH_LAYOUT_IMAGE( DALI_IMAGE_DIR "icon-item-view-layout-depth.png" );
119 const char* DELETE_IMAGE( DALI_IMAGE_DIR "icon-delete.png" );
120 const char* REPLACE_IMAGE( DALI_IMAGE_DIR "icon-replace.png" );
121 const char* INSERT_IMAGE( DALI_IMAGE_DIR "icon-insert.png" );
122 const char* SELECTED_IMAGE( DALI_IMAGE_DIR "item-select-check.png" );
123 const char* APPLICATION_TITLE( "ItemView" );
124
125 const char* SPIRAL_LABEL("Spiral");
126 const char* GRID_LABEL("Grid");
127 const char* DEPTH_LABEL("Depth");
128
129 const char* ITEM_BORDER_IMAGE_PATH( DALI_IMAGE_DIR "frame-128x128.png" );
130 const Vector3 ITEM_BORDER_MARGIN_SIZE(24, 24, 0);
131
132 // These values depend on the border image
133 const float ITEM_IMAGE_BORDER_LEFT   = 13.0f;
134 const float ITEM_IMAGE_BORDER_RIGHT  = 13.0f;
135 const float ITEM_IMAGE_BORDER_TOP    = 13.0f;
136 const float ITEM_IMAGE_BORDER_BOTTOM = 13.0f;
137
138 const unsigned int DEPTH_LAYOUT_ROWS_PORTRAIT = 26;
139 const unsigned int DEPTH_LAYOUT_ROWS_LANDSCAPE = 16;
140 const float DEPTH_LAYOUT_TILT_ANGLE_PORTRAIT = 25.0f;
141 const float DEPTH_LAYOUT_TILT_ANGLE_LANDSCAPE = 21.0f;
142 const float DEPTH_LAYOUT_ROW_SPACING_FACTOR = 0.1f;
143 const float DEPTH_LAYOUT_ITEM_SIZE_FACTOR_PORTRAIT = 1.0f;
144 const float DEPTH_LAYOUT_ITEM_SIZE_FACTOR_LANDSCAPE = 0.8f;
145 const float DEPTH_LAYOUT_BOTTOM_MARGIN_FACTOR_PORTRAIT = 0.2f;
146 const float DEPTH_LAYOUT_BOTTOM_MARGIN_FACTOR_LANDSCAPE = 0.1f;
147
148 const float SPIRAL_LAYOUT_REVOLUTION_NUMBER_PORTRAIT = 4.5f;
149 const float SPIRAL_LAYOUT_REVOLUTION_NUMBER_LANDSCAPE = 2.5f;
150
151 const float DEPTH_LAYOUT_HEIGHT_SCALE = 20.0f;
152 const float MIN_SWIPE_DISTANCE = 15.0f;
153 const float MIN_SWIPE_SPEED = 5.0f;
154
155 const float SELECTION_BORDER_WIDTH = 3.0f;
156 const float BUTTON_BORDER = -10.0f;
157 const float MENU_OPTION_HEIGHT(140.0f);
158 const float LABEL_TEXT_SIZE_Y = 20.0f;
159
160 const char*             DEFAULT_TEXT_STYLE_FONT_FAMILY("HelveticaNue");
161 const char*             DEFAULT_TEXT_STYLE_FONT_STYLE("Regular");
162 const PointSize         DEFAULT_TEXT_STYLE_POINT_SIZE( 5.0f );
163 const TextStyle::Weight DEFAULT_TEXT_STYLE_WEIGHT(Dali::TextStyle::MEDIUM);
164 const Vector4           DEFAULT_TEXT_STYLE_COLOR(1.0f, 1.0f, 1.0f, 1.0f);
165
166 const Vector3 INITIAL_OFFSCREEN_POSITION( 1000.0f, 0, -1000.0f );
167
168 struct BorderSizeConstraintFunction
169 {
170   Vector3 operator()(const Vector3&       current,
171                      const PropertyInput& parentSize)
172   {
173     return parentSize.GetVector3() + ITEM_BORDER_MARGIN_SIZE;
174   }
175 };
176
177 static Vector3 DepthLayoutItemSizeFunctionPortrait(unsigned int numberOfColumns, float layoutWidth)
178 {
179   float width = (layoutWidth / static_cast<float>(numberOfColumns + 1)) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_PORTRAIT;
180
181   // 1x1 aspect ratio
182   return Vector3(width, width, width);
183 }
184
185 static Vector3 DepthLayoutItemSizeFunctionLandscape(unsigned int numberOfColumns, float layoutWidth)
186 {
187   float width = (layoutWidth / static_cast<float>(numberOfColumns + 1)) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_LANDSCAPE;
188
189   // 1x1 aspect ratio
190   return Vector3(width, width, width);
191 }
192
193 static float DepthLayoutBottomMarginFunctionPortrait(float layoutHeight)
194 {
195   return layoutHeight * DEPTH_LAYOUT_BOTTOM_MARGIN_FACTOR_PORTRAIT;
196 }
197
198 static float DepthLayoutBottomMarginFunctionLandscape(float layoutHeight)
199 {
200   return layoutHeight * DEPTH_LAYOUT_BOTTOM_MARGIN_FACTOR_LANDSCAPE;
201 }
202
203 }
204
205 /**
206  * This example shows how to use ItemView UI control.
207  * There are three layouts created for ItemView, i.e., Spiral, Depth and Grid.
208  * There is one button in the upper-left corner for quitting the application and
209  * another button in the upper-right corner for switching between different layouts.
210  */
211 class ItemViewExample : public ConnectionTracker, public ItemFactory
212 {
213 public:
214
215   enum Mode
216   {
217     MODE_NORMAL,
218     MODE_REMOVE,
219     MODE_REMOVE_MANY,
220     MODE_INSERT,
221     MODE_INSERT_MANY,
222     MODE_REPLACE,
223     MODE_REPLACE_MANY,
224     MODE_LAST
225   };
226
227   /**
228    * Constructor
229    * @param application class, stored as reference
230    */
231   ItemViewExample( Application& application )
232   : mApplication( application ),
233     mMode( MODE_NORMAL ),
234     mMenuShown( false ),
235     mOrientation( 0 ),
236     mCurrentLayout( SPIRAL_LAYOUT ),
237     mDurationSeconds( 1.0f ),
238     mAlphaFuncIndex( 0u )
239   {
240     // Connect to the Application's Init signal
241     mApplication.InitSignal().Connect(this, &ItemViewExample::OnInit);
242   }
243
244   /**
245    * This method gets called once the main loop of application is up and running
246    */
247   void OnInit(Application& app)
248   {
249     Stage stage = Dali::Stage::GetCurrent();
250     stage.KeyEventSignal().Connect(this, &ItemViewExample::OnKeyEvent);
251
252     Vector2 stageSize = Stage::GetCurrent().GetSize();
253
254     // Create a border image shared by all the item actors
255     mBorderImage = Image::New(ITEM_BORDER_IMAGE_PATH);
256
257     // Creates a default view with a default tool bar.
258     // The view is added to the stage.
259     Layer contents = DemoHelper::CreateView( mApplication,
260                                              mView,
261                                              mToolBar,
262                                              BACKGROUND_IMAGE,
263                                              TOOLBAR_IMAGE,
264                                              "" );
265
266     mView.OrientationAnimationStartedSignal().Connect( this, &ItemViewExample::OnOrientationChanged );
267
268     // Create an edit mode button. (left of toolbar)
269     Toolkit::PushButton editButton = Toolkit::PushButton::New();
270     editButton.SetBackgroundImage( Image::New( EDIT_IMAGE ) );
271     editButton.ClickedSignal().Connect( this, &ItemViewExample::OnModeButtonClicked);
272     editButton.SetLeaveRequired( true );
273     mToolBar.AddControl( editButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalLeft, DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
274
275     // Create a layout toggle button. (right of toolbar)
276     mLayoutButton = Toolkit::PushButton::New();
277     mLayoutButton.SetBackgroundImage( Image::New( SPIRAL_LAYOUT_IMAGE ) );
278     mLayoutButton.ClickedSignal().Connect( this, &ItemViewExample::OnLayoutButtonClicked);
279     mLayoutButton.SetLeaveRequired( true );
280     mToolBar.AddControl( mLayoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
281
282     // Create a delete button (bottom right of screen)
283     mDeleteButton = Toolkit::PushButton::New();
284     mDeleteButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
285     mDeleteButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
286     mDeleteButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
287     mDeleteButton.SetDrawMode( DrawMode::OVERLAY );
288     mDeleteButton.SetBackgroundImage( Image::New( TOOLBAR_IMAGE ) );
289     mDeleteButton.SetButtonImage( Image::New( DELETE_IMAGE ) );
290     mDeleteButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
291     mDeleteButton.ClickedSignal().Connect( this, &ItemViewExample::OnDeleteButtonClicked);
292     mDeleteButton.SetLeaveRequired( true );
293     mDeleteButton.SetVisible( false );
294     stage.Add( mDeleteButton );
295
296     // Create an insert button (bottom right of screen)
297     mInsertButton = Toolkit::PushButton::New();
298     mInsertButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
299     mInsertButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
300     mInsertButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
301     mInsertButton.SetDrawMode( DrawMode::OVERLAY );
302     mInsertButton.SetBackgroundImage( Image::New( TOOLBAR_IMAGE ) );
303     mInsertButton.SetButtonImage( Image::New( INSERT_IMAGE ) );
304     mInsertButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
305     mInsertButton.ClickedSignal().Connect( this, &ItemViewExample::OnInsertButtonClicked);
306     mInsertButton.SetLeaveRequired( true );
307     mInsertButton.SetVisible( false );
308     stage.Add( mInsertButton );
309
310     // Create an replace button (bottom right of screen)
311     mReplaceButton = Toolkit::PushButton::New();
312     mReplaceButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
313     mReplaceButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
314     mReplaceButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
315     mReplaceButton.SetDrawMode( DrawMode::OVERLAY );
316     mReplaceButton.SetBackgroundImage( Image::New( TOOLBAR_IMAGE ) );
317     mReplaceButton.SetButtonImage( Image::New( REPLACE_IMAGE ) );
318     mReplaceButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
319     mReplaceButton.ClickedSignal().Connect( this, &ItemViewExample::OnReplaceButtonClicked);
320     mReplaceButton.SetLeaveRequired( true );
321     mReplaceButton.SetVisible( false );
322     stage.Add( mReplaceButton );
323
324     // Create the item view actor
325     mItemView = ItemView::New(*this);
326     mItemView.SetParentOrigin(ParentOrigin::CENTER);
327     mItemView.SetAnchorPoint(AnchorPoint::CENTER);
328
329     // Display item view on the stage
330     stage.Add( mItemView );
331
332     // Create the layouts
333     mSpiralLayout = SpiralLayout::New();
334     mDepthLayout = DepthLayout::New();
335     mGridLayout = GridLayout::New();
336
337     // Add the layouts to item view
338     mItemView.AddLayout(*mSpiralLayout);
339     mItemView.AddLayout(*mDepthLayout);
340     mItemView.AddLayout(*mGridLayout);
341
342     mItemView.SetMinimumSwipeDistance(MIN_SWIPE_DISTANCE);
343     mItemView.SetMinimumSwipeSpeed(MIN_SWIPE_SPEED);
344
345     // Activate the spiral layout
346     UseLayout(mCurrentLayout, 0.0f);
347     mItemView.SetKeyboardFocusable( true );
348     KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &ItemViewExample::OnKeyboardPreFocusChange );
349
350     // Set the title and icon to the current layout
351     SetLayoutTitle();
352     SetLayoutImage();
353   }
354
355   Actor OnKeyboardPreFocusChange( Actor current, Actor proposed, Control::KeyboardFocusNavigationDirection direction )
356   {
357     if ( !current && !proposed  )
358     {
359       return mItemView;
360     }
361
362     return proposed;
363   }
364
365   /**
366    * Switch to a different item view layout
367    */
368   void UseLayout(int layoutId, float duration)
369   {
370     // Set the new orientation to the layout
371     mItemView.GetLayout(layoutId)->SetOrientation(static_cast<ControlOrientation::Type>(mOrientation / 90));
372
373     Vector2 stageSize = Stage::GetCurrent().GetSize();
374
375     if(layoutId == SPIRAL_LAYOUT)
376     {
377       mSpiralLayout->SetRevolutionDistance(stageSize.height / Stage::GetCurrent().GetDpi().y * 45.0f);
378     }
379
380     if(layoutId == GRID_LAYOUT)
381     {
382       // Set up the grid layout according to the new orientation
383       float layoutWidth = Toolkit::IsHorizontal(mGridLayout->GetOrientation()) ? stageSize.height : stageSize.width;
384       float gridItemSize = (layoutWidth / mGridLayout->GetNumberOfColumns()) * 0.5f;
385       mGridLayout->SetScrollSpeedFactor(mGridLayout->GetNumberOfColumns() / gridItemSize);
386
387       float toolbarHeight = mToolBar.GetCurrentSize().y;
388       mGridLayout->SetTopMargin(toolbarHeight + mGridLayout->GetRowSpacing());
389     }
390
391     if(layoutId == DEPTH_LAYOUT)
392     {
393       // Set up the depth layout according to the new orientation
394       if(Toolkit::IsVertical(mDepthLayout->GetOrientation()))
395       {
396         mDepthLayout->SetRowSpacing(stageSize.height * DEPTH_LAYOUT_ROW_SPACING_FACTOR);
397         mDepthLayout->SetNumberOfRows(DEPTH_LAYOUT_ROWS_PORTRAIT);
398         mDepthLayout->SetTiltAngle( Degree( DEPTH_LAYOUT_TILT_ANGLE_PORTRAIT - mDepthLayout->GetNumberOfColumns() ) );
399         mDepthLayout->SetItemSizeFunction(DepthLayoutItemSizeFunctionPortrait);
400         mDepthLayout->SetBottomMarginFunction(DepthLayoutBottomMarginFunctionPortrait);
401       }
402       else
403       {
404         mDepthLayout->SetRowSpacing(stageSize.width * DEPTH_LAYOUT_ROW_SPACING_FACTOR);
405         mDepthLayout->SetNumberOfRows(DEPTH_LAYOUT_ROWS_LANDSCAPE);
406         mDepthLayout->SetTiltAngle( Degree( DEPTH_LAYOUT_TILT_ANGLE_LANDSCAPE - mDepthLayout->GetNumberOfColumns() ) );
407         mDepthLayout->SetItemSizeFunction(DepthLayoutItemSizeFunctionLandscape);
408         mDepthLayout->SetBottomMarginFunction(DepthLayoutBottomMarginFunctionLandscape);
409       }
410     }
411
412     // Enable anchoring for depth layout only
413     mItemView.SetAnchoring(layoutId == DEPTH_LAYOUT);
414
415     // Activate the layout
416     mItemView.ActivateLayout(layoutId, Vector3(stageSize.x, stageSize.y, stageSize.x), duration);
417   }
418
419   /**
420    * Orientation changed signal callback
421    * @param orientation
422    */
423   void OnOrientationChanged( View view, Animation& animation, const Orientation& orientation )
424   {
425     const unsigned int angle = orientation.GetDegrees();
426
427     // If orientation really changed
428     if( mOrientation != angle )
429     {
430       // Remember orientation
431       mOrientation = angle;
432
433       UseLayout(mCurrentLayout, mDurationSeconds);
434     }
435   }
436
437   bool OnLayoutButtonClicked( Toolkit::Button button )
438   {
439     // Switch to the next layout
440     mCurrentLayout = (mCurrentLayout + 1) % mItemView.GetLayoutCount();
441
442     UseLayout(mCurrentLayout, mDurationSeconds);
443
444     SetLayoutTitle();
445     SetLayoutImage();
446
447     return true;
448   }
449
450   bool OnModeButtonClicked( Toolkit::Button button )
451   {
452     SwitchToNextMode();
453
454     return true;
455   }
456
457   void SwitchToNextMode()
458   {
459     switch( mMode )
460     {
461       case MODE_REMOVE:
462       {
463         ExitRemoveMode();
464         mMode = MODE_REMOVE_MANY;
465         EnterRemoveManyMode();
466         break;
467       }
468
469       case MODE_REMOVE_MANY:
470       {
471         ExitRemoveManyMode();
472         mMode = MODE_INSERT;
473         EnterInsertMode();
474         break;
475       }
476
477       case MODE_INSERT:
478       {
479         ExitInsertMode();
480         mMode = MODE_INSERT_MANY;
481         EnterInsertManyMode();
482         break;
483       }
484
485       case MODE_INSERT_MANY:
486       {
487         ExitInsertManyMode();
488         mMode = MODE_REPLACE;
489         EnterReplaceMode();
490         break;
491       }
492
493       case MODE_REPLACE:
494       {
495         ExitReplaceMode();
496         mMode = MODE_REPLACE_MANY;
497         EnterReplaceManyMode();
498         break;
499       }
500
501       case MODE_REPLACE_MANY:
502       {
503         ExitReplaceManyMode();
504         mMode = MODE_NORMAL;
505         SetLayoutTitle();
506         break;
507       }
508
509       case MODE_NORMAL:
510       default:
511       {
512         mMode = MODE_REMOVE;
513         EnterRemoveMode();
514         break;
515       }
516     }
517   }
518
519   void EnterRemoveMode()
520   {
521     SetTitle("Edit: Remove");
522
523     mTapDetector = TapGestureDetector::New();
524
525     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
526     {
527       mTapDetector.Attach(mItemView.GetChildAt(i));
528     }
529
530     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::RemoveOnTap );
531   }
532
533   void ExitRemoveMode()
534   {
535     mTapDetector.Reset();
536   }
537
538   void RemoveOnTap( Actor actor, TapGesture tap )
539   {
540     mItemView.RemoveItem( mItemView.GetItemId(actor), 0.5f );
541   }
542
543   void EnterRemoveManyMode()
544   {
545     SetTitle("Edit: Remove Many");
546
547     mDeleteButton.SetVisible( true );
548
549     mTapDetector = TapGestureDetector::New();
550
551     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
552     {
553       Actor child = mItemView.GetChildAt( i );
554       Actor box = child.FindChildByName( "CheckBox" );
555
556       if( box )
557       {
558         mTapDetector.Attach( child );
559         box.SetVisible( true );
560       }
561     }
562
563     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
564   }
565
566   void ExitRemoveManyMode()
567   {
568     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
569     {
570       Actor child = mItemView.GetChildAt( i );
571       Actor box = child.FindChildByName( "CheckBox" );
572
573       if( box )
574       {
575         box.SetVisible( false );
576
577         Actor tick = box.FindChildByName( "Tick" );
578         if( tick )
579         {
580           tick.SetVisible( false );
581         }
582       }
583     }
584
585     mTapDetector.Reset();
586
587     mDeleteButton.SetVisible( false );
588   }
589
590   void SelectOnTap( Actor actor, TapGesture tap )
591   {
592     Actor tick = actor.FindChildByName( "Tick" );
593     if( tick )
594     {
595       tick.SetVisible( !tick.IsVisible() );
596     }
597   }
598
599   bool OnDeleteButtonClicked( Toolkit::Button button )
600   {
601     ItemIdContainer removeList;
602
603     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
604     {
605       Actor child = mItemView.GetChildAt( i );
606       Actor tick = child.FindChildByName( "Tick" );
607
608       if( tick && tick.IsVisible() )
609       {
610         removeList.push_back( mItemView.GetItemId(child) );
611       }
612     }
613
614     if( ! removeList.empty() )
615     {
616       mItemView.RemoveItems( removeList, 0.5f );
617     }
618
619     return true;
620   }
621
622   void EnterInsertMode()
623   {
624     SetTitle("Edit: Insert");
625
626     mTapDetector = TapGestureDetector::New();
627
628     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
629     {
630       mTapDetector.Attach( mItemView.GetChildAt(i) );
631     }
632
633     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::InsertOnTap );
634   }
635
636   void ExitInsertMode()
637   {
638     mTapDetector.Reset();
639   }
640
641   void InsertOnTap( Actor actor, TapGesture tap )
642   {
643     ItemId id = mItemView.GetItemId( actor );
644
645     Actor newActor = NewItem( rand() );
646
647     mItemView.InsertItem( Item(id,newActor), 0.5f );
648   }
649
650   void EnterInsertManyMode()
651   {
652     SetTitle("Edit: Insert Many");
653
654     mInsertButton.SetVisible( true );
655
656     mTapDetector = TapGestureDetector::New();
657
658     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
659     {
660       Actor child = mItemView.GetChildAt( i );
661       Actor box = child.FindChildByName( "CheckBox" );
662
663       if( box )
664       {
665         mTapDetector.Attach( child );
666         box.SetVisible( true );
667       }
668     }
669
670     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
671   }
672
673   void ExitInsertManyMode()
674   {
675     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
676     {
677       Actor child = mItemView.GetChildAt( i );
678       Actor box = child.FindChildByName( "CheckBox" );
679
680       if( box )
681       {
682         box.SetVisible( false );
683
684         Actor tick = box.FindChildByName( "Tick" );
685         if( tick )
686         {
687           tick.SetVisible( false );
688         }
689       }
690     }
691
692     mTapDetector.Reset();
693
694     mInsertButton.SetVisible( false );
695   }
696
697   bool OnInsertButtonClicked( Toolkit::Button button )
698   {
699     ItemContainer insertList;
700
701     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
702     {
703       Actor child = mItemView.GetChildAt( i );
704       Actor tick = child.FindChildByName( "Tick" );
705
706       if( tick && tick.IsVisible() )
707       {
708         insertList.push_back( Item( mItemView.GetItemId(child), NewItem(rand()) ) );
709       }
710     }
711
712     if( ! insertList.empty() )
713     {
714       mItemView.InsertItems( insertList, 0.5f );
715     }
716
717     return true;
718   }
719
720   void EnterReplaceMode()
721   {
722     SetTitle("Edit: Replace");
723
724     mTapDetector = TapGestureDetector::New();
725
726     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
727     {
728       mTapDetector.Attach(mItemView.GetChildAt(i));
729     }
730
731     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::ReplaceOnTap );
732   }
733
734   void ReplaceOnTap( Actor actor, TapGesture tap )
735   {
736     mItemView.ReplaceItem( Item( mItemView.GetItemId(actor), NewItem(rand()) ), 0.5f );
737   }
738
739   void ExitReplaceMode()
740   {
741     mTapDetector.Reset();
742   }
743
744   void EnterReplaceManyMode()
745   {
746     SetTitle("Edit: Replace Many");
747
748     mReplaceButton.SetVisible( true );
749
750     mTapDetector = TapGestureDetector::New();
751
752     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
753     {
754       Actor child = mItemView.GetChildAt( i );
755       Actor box = child.FindChildByName( "CheckBox" );
756
757       if( box )
758       {
759         mTapDetector.Attach( child );
760         box.SetVisible( true );
761       }
762     }
763
764     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
765   }
766
767   void ExitReplaceManyMode()
768   {
769     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
770     {
771       Actor child = mItemView.GetChildAt( i );
772       Actor box = child.FindChildByName( "CheckBox" );
773
774       if( box )
775       {
776         box.SetVisible( false );
777
778         Actor tick = box.FindChildByName( "Tick" );
779         if( tick )
780         {
781           tick.SetVisible( false );
782         }
783       }
784     }
785
786     mTapDetector.Reset();
787
788     mReplaceButton.SetVisible( false );
789   }
790
791   bool OnReplaceButtonClicked( Toolkit::Button button )
792   {
793     ItemContainer replaceList;
794
795     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
796     {
797       Actor child = mItemView.GetChildAt( i );
798       Actor tick = child.FindChildByName( "Tick" );
799
800       if( tick && tick.IsVisible() )
801       {
802         replaceList.push_back( Item( mItemView.GetItemId(child), NewItem(rand()) ) );
803       }
804     }
805
806     if( ! replaceList.empty() )
807     {
808       mItemView.ReplaceItems( replaceList, 0.5f );
809     }
810
811     return true;
812   }
813
814   void SetLayoutTitle()
815   {
816     if( MODE_NORMAL == mMode )
817     {
818       std::stringstream ss(APPLICATION_TITLE);
819       switch(mCurrentLayout)
820       {
821       case SPIRAL_LAYOUT:
822         ss << APPLICATION_TITLE << ": " << SPIRAL_LABEL;
823         break;
824       case GRID_LAYOUT:
825         ss << APPLICATION_TITLE << ": " << GRID_LABEL;
826         break;
827       case DEPTH_LAYOUT:
828         ss << APPLICATION_TITLE << ": " << DEPTH_LABEL;
829         break;
830       default:
831         break;
832       }
833       SetTitle(ss.str());
834     }
835   }
836
837   void SetLayoutImage()
838   {
839     if( mLayoutButton )
840     {
841       switch( mCurrentLayout )
842       {
843         case SPIRAL_LAYOUT:
844         {
845           mLayoutButton.SetBackgroundImage( Image::New( SPIRAL_LAYOUT_IMAGE ) );
846           break;
847         }
848
849         case GRID_LAYOUT:
850         {
851           mLayoutButton.SetBackgroundImage( Image::New( GRID_LAYOUT_IMAGE ) );
852           break;
853         }
854
855         case DEPTH_LAYOUT:
856         {
857           mLayoutButton.SetBackgroundImage( Image::New( DEPTH_LAYOUT_IMAGE ) );
858           break;
859         }
860
861         default:
862           break;
863       }
864     }
865   }
866
867 public: // From ItemFactory
868
869   /**
870    * Query the number of items available from the factory.
871    * The maximum available item has an ID of GetNumberOfItems() - 1.
872    */
873   virtual unsigned int GetNumberOfItems()
874   {
875     return NUM_IMAGES * 10;
876   }
877
878   /**
879    * Create an Actor to represent a visible item.
880    * @param itemId
881    * @return the created actor.
882    */
883   virtual Actor NewItem(unsigned int itemId)
884   {
885     // Create an image actor for this item
886     Image image = Image::New( IMAGE_PATHS[itemId % NUM_IMAGES] );
887     Actor actor = ImageActor::New(image);
888     actor.SetPosition( INITIAL_OFFSCREEN_POSITION );
889
890     // Add a border image child actor
891     ImageActor borderActor = ImageActor::New(mBorderImage);
892     borderActor.SetParentOrigin( ParentOrigin::CENTER );
893     borderActor.SetAnchorPoint( AnchorPoint::CENTER );
894     borderActor.SetPosition( 0.f, 0.f, 1.f );
895     borderActor.SetStyle( ImageActor::STYLE_NINE_PATCH );
896     borderActor.SetNinePatchBorder( Vector4( ITEM_IMAGE_BORDER_LEFT, ITEM_IMAGE_BORDER_TOP, ITEM_IMAGE_BORDER_RIGHT, ITEM_IMAGE_BORDER_BOTTOM ) );
897     borderActor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR ); // darken with parent image-actor
898
899     Constraint constraint = Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), BorderSizeConstraintFunction() );
900     borderActor.ApplyConstraint(constraint);
901     actor.Add(borderActor);
902     actor.SetKeyboardFocusable( true );
903
904     Vector3 spiralItemSize;
905     static_cast<ItemLayout&>(*mSpiralLayout).GetItemSize( 0u, Vector3( Stage::GetCurrent().GetSize() ), spiralItemSize );
906
907     // Add a checkbox child actor; invisible until edit-mode is enabled
908
909     ImageActor checkbox = ImageActor::New( BitmapImage::WHITE() );
910     checkbox.SetName( "CheckBox" );
911     checkbox.SetColor( Vector4(0.0f,0.0f,0.0f,0.6f) );
912     checkbox.SetParentOrigin( ParentOrigin::TOP_RIGHT );
913     checkbox.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
914     checkbox.SetSize( spiralItemSize.width * 0.2f, spiralItemSize.width * 0.2f );
915     checkbox.SetPosition( -SELECTION_BORDER_WIDTH, SELECTION_BORDER_WIDTH );
916     checkbox.SetZ( 1.0f );
917     if( MODE_REMOVE_MANY  != mMode &&
918         MODE_INSERT_MANY  != mMode &&
919         MODE_REPLACE_MANY != mMode )
920     {
921       checkbox.SetVisible( false );
922     }
923     actor.Add( checkbox );
924
925     ImageActor tick = ImageActor::New( Image::New(SELECTED_IMAGE) );
926     tick.SetColorMode( USE_OWN_COLOR );
927     tick.SetName( "Tick" );
928     tick.SetParentOrigin( ParentOrigin::TOP_RIGHT );
929     tick.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
930     tick.SetSize( spiralItemSize.width * 0.2f, spiralItemSize.width * 0.2f );
931     tick.SetZ( 1.0f );
932     tick.SetVisible( false );
933     checkbox.Add( tick );
934
935     // Connect new items for various editing modes
936     if( mTapDetector )
937     {
938       mTapDetector.Attach( actor );
939     }
940
941     return actor;
942   }
943
944 private:
945
946   /**
947    * Sets/Updates the title of the View
948    * @param[in] title The new title for the view.
949    */
950   void SetTitle(const std::string& title)
951   {
952     if(!mTitleActor)
953     {
954       mTitleActor = TextView::New();
955       // Add title to the tool bar.
956       mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HorizontalCenter );
957     }
958
959     Font font = Font::New();
960     mTitleActor.SetText( title );
961     mTitleActor.SetSize( font.MeasureText( title ) );
962     mTitleActor.SetStyleToCurrentText(DemoHelper::GetDefaultTextStyle());
963   }
964
965   void ShowMenu()
966   {
967     Stage stage = Stage::GetCurrent();
968     const float popupWidth = stage.GetSize().x * 0.75f;
969
970     mMenu = Toolkit::Popup::New();
971     mMenu.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
972     mMenu.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
973     mMenu.OutsideTouchedSignal().Connect( this, &ItemViewExample::HideMenu );
974     stage.Add( mMenu );
975
976     TableView tableView = TableView::New( 0, 0 );
977     Vector2 tableSize = Vector2( popupWidth, MENU_OPTION_HEIGHT * 2 );
978     tableView.SetSize( tableSize );
979     mMenu.Add( tableView );
980
981     Slider slider = Slider::New();
982     slider.SetProperty( Slider::LOWER_BOUND_PROPERTY, 0.0f );
983     slider.SetProperty( Slider::UPPER_BOUND_PROPERTY, 3.0f );
984     slider.SetProperty( Slider::VALUE_PROPERTY, mDurationSeconds );
985     slider.SetProperty( Slider::VALUE_PRECISION_PROPERTY, 2 );
986     slider.SetProperty( Slider::SHOW_POPUP_PROPERTY, true );
987     slider.ValueChangedSignal().Connect( this, &ItemViewExample::SliderValueChange );
988     tableView.AddChild( slider, TableView::CellPosition( 0, 0 ) );
989     tableView.SetRelativeHeight( 0, 0.5f );
990
991     TextStyle defaultTextStyle;
992     defaultTextStyle.SetFontName(DEFAULT_TEXT_STYLE_FONT_FAMILY);
993     defaultTextStyle.SetFontStyle(DEFAULT_TEXT_STYLE_FONT_STYLE);
994     defaultTextStyle.SetFontPointSize(DEFAULT_TEXT_STYLE_POINT_SIZE);
995     defaultTextStyle.SetWeight(DEFAULT_TEXT_STYLE_WEIGHT);
996     defaultTextStyle.SetTextColor(DEFAULT_TEXT_STYLE_COLOR);
997
998     TextView text = TextView::New( "Duration" );
999     text.SetAnchorPoint( ParentOrigin::TOP_LEFT );
1000     text.SetParentOrigin( ParentOrigin::TOP_LEFT );
1001     text.SetTextAlignment( Dali::Toolkit::Alignment::HorizontalLeft );
1002     text.SetStyleToCurrentText( defaultTextStyle );
1003     text.SetSize( 0.0f, LABEL_TEXT_SIZE_Y );
1004     text.ApplyConstraint( Dali::Constraint::New<float>( Dali::Actor::SIZE_WIDTH, Dali::ParentSource( Dali::Actor::SIZE_WIDTH ), Dali::EqualToConstraint() ) );
1005     text.SetZ( -0.9f );
1006     slider.Add( text );
1007
1008     Actor textContainer = Actor::New();
1009     mAlphaFunctionText = TextView::New( ALPHA_FUNCTIONS_TEXT[mAlphaFuncIndex] );
1010     mAlphaFunctionText.SetAnchorPoint( ParentOrigin::CENTER );
1011     mAlphaFunctionText.SetParentOrigin( ParentOrigin::CENTER );
1012     mAlphaFunctionText.SetTextAlignment( Toolkit::Alignment::VerticalCenter );
1013     textContainer.Add( mAlphaFunctionText );
1014     tableView.AddChild( textContainer, TableView::CellPosition( 1, 0 ) );
1015     tableView.SetRelativeHeight( 0, 0.5f );
1016
1017     mTapDetector = TapGestureDetector::New();
1018     mTapDetector.Attach(mAlphaFunctionText);
1019     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::ChangeAlphaFunctionOnTap );
1020
1021     text = TextView::New( "Alpha Function" );
1022     text.SetAnchorPoint( ParentOrigin::TOP_LEFT );
1023     text.SetParentOrigin( ParentOrigin::TOP_LEFT );
1024     text.SetTextAlignment( Dali::Toolkit::Alignment::HorizontalLeft );
1025     text.SetStyleToCurrentText( defaultTextStyle );
1026     text.SetSize( 0.0f, LABEL_TEXT_SIZE_Y );
1027     text.ApplyConstraint( Dali::Constraint::New<float>( Dali::Actor::SIZE_WIDTH, Dali::ParentSource( Dali::Actor::SIZE_WIDTH ), Dali::EqualToConstraint() ) );
1028     textContainer.Add( text );
1029
1030     mMenu.Show();
1031     mMenuShown = true;
1032   }
1033
1034   bool SliderValueChange( Toolkit::Slider slider, float value )
1035   {
1036     mDurationSeconds = value;
1037
1038     return true;
1039   }
1040
1041   void ChangeAlphaFunctionOnTap( Actor actor, TapGesture tap )
1042   {
1043     if( NUM_ALPHA_FUNCTIONS <= ++mAlphaFuncIndex )
1044     {
1045       mAlphaFuncIndex = 0;
1046     }
1047
1048     if( mAlphaFunctionText )
1049     {
1050       mAlphaFunctionText.SetText( ALPHA_FUNCTIONS_TEXT[mAlphaFuncIndex] );
1051     }
1052
1053     if( mItemView )
1054     {
1055       mItemView.GetActiveLayout()->SetAlphaFunction( ALPHA_FUNCTIONS[mAlphaFuncIndex] );
1056     }
1057   }
1058
1059   void HideMenu()
1060   {
1061     mTapDetector.Reset();
1062
1063     if( mMenu )
1064     {
1065       mMenu.Hide();
1066       mMenu.Reset();
1067     }
1068
1069     mMenuShown = false;
1070   }
1071
1072   /**
1073    * Main key event handler
1074    */
1075   void OnKeyEvent(const KeyEvent& event)
1076   {
1077     if(event.state == KeyEvent::Down)
1078     {
1079       if( IsKey( event, DALI_KEY_MENU ) )
1080       {
1081         if( mMenuShown )
1082         {
1083           HideMenu();
1084         }
1085         else
1086         {
1087           ShowMenu();
1088         }
1089       }
1090       else if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
1091       {
1092         if( mMenuShown )
1093         {
1094           HideMenu();
1095         }
1096         else
1097         {
1098           mApplication.Quit();
1099         }
1100       }
1101     }
1102   }
1103
1104 private:
1105
1106   Application& mApplication;
1107   Mode mMode;
1108   bool mMenuShown;
1109
1110   Toolkit::View mView;
1111   unsigned int mOrientation;
1112
1113   Toolkit::ToolBar mToolBar;
1114   TextView mTitleActor;             ///< The Toolbar's Title.
1115
1116   ItemView mItemView;
1117   Image mBorderImage;
1118   unsigned int mCurrentLayout;
1119   float mDurationSeconds;
1120
1121   SpiralLayoutPtr mSpiralLayout;
1122   DepthLayoutPtr mDepthLayout;
1123   GridLayoutPtr mGridLayout;
1124
1125   Toolkit::Popup mMenu;
1126
1127   TapGestureDetector mTapDetector;
1128   Toolkit::PushButton mLayoutButton;
1129   Toolkit::PushButton mDeleteButton;
1130   Toolkit::PushButton mInsertButton;
1131   Toolkit::PushButton mReplaceButton;
1132
1133   unsigned int mAlphaFuncIndex;
1134   TextView mAlphaFunctionText;
1135 };
1136
1137 void RunTest(Application& app)
1138 {
1139   ItemViewExample test(app);
1140
1141   app.MainLoop();
1142 }
1143
1144 int main(int argc, char **argv)
1145 {
1146   Application app = Application::New(&argc, &argv);
1147
1148   RunTest(app);
1149
1150   return 0;
1151 }