Update following the changes of Blending&Culling options
[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 "shared/view.h"
20
21 #include <dali/dali.h>
22 #include <dali-toolkit/dali-toolkit.h>
23
24 using namespace Dali;
25 using namespace Dali::Toolkit;
26
27 namespace
28 {
29
30 enum AllImagesLayouts
31 {
32   SPIRAL_LAYOUT,
33   DEPTH_LAYOUT,
34   GRID_LAYOUT,
35
36   NUMBER_OF_LAYOUTS
37 };
38
39 const char* IMAGE_PATHS[] = {
40     DEMO_IMAGE_DIR "gallery-medium-1.jpg",
41     DEMO_IMAGE_DIR "gallery-medium-2.jpg",
42     DEMO_IMAGE_DIR "gallery-medium-3.jpg",
43     DEMO_IMAGE_DIR "gallery-medium-4.jpg",
44     DEMO_IMAGE_DIR "gallery-medium-5.jpg",
45     DEMO_IMAGE_DIR "gallery-medium-6.jpg",
46     DEMO_IMAGE_DIR "gallery-medium-7.jpg",
47     DEMO_IMAGE_DIR "gallery-medium-8.jpg",
48     DEMO_IMAGE_DIR "gallery-medium-9.jpg",
49     DEMO_IMAGE_DIR "gallery-medium-10.jpg",
50     DEMO_IMAGE_DIR "gallery-medium-11.jpg",
51     DEMO_IMAGE_DIR "gallery-medium-12.jpg",
52     DEMO_IMAGE_DIR "gallery-medium-13.jpg",
53     DEMO_IMAGE_DIR "gallery-medium-14.jpg",
54     DEMO_IMAGE_DIR "gallery-medium-15.jpg",
55     DEMO_IMAGE_DIR "gallery-medium-16.jpg",
56     DEMO_IMAGE_DIR "gallery-medium-17.jpg",
57     DEMO_IMAGE_DIR "gallery-medium-18.jpg",
58     DEMO_IMAGE_DIR "gallery-medium-19.jpg",
59     DEMO_IMAGE_DIR "gallery-medium-20.jpg",
60     DEMO_IMAGE_DIR "gallery-medium-21.jpg",
61     DEMO_IMAGE_DIR "gallery-medium-22.jpg",
62     DEMO_IMAGE_DIR "gallery-medium-23.jpg",
63     DEMO_IMAGE_DIR "gallery-medium-24.jpg",
64     DEMO_IMAGE_DIR "gallery-medium-25.jpg",
65     DEMO_IMAGE_DIR "gallery-medium-26.jpg",
66     DEMO_IMAGE_DIR "gallery-medium-27.jpg",
67     DEMO_IMAGE_DIR "gallery-medium-28.jpg",
68     DEMO_IMAGE_DIR "gallery-medium-29.jpg",
69     DEMO_IMAGE_DIR "gallery-medium-30.jpg",
70     DEMO_IMAGE_DIR "gallery-medium-31.jpg",
71     DEMO_IMAGE_DIR "gallery-medium-32.jpg",
72     DEMO_IMAGE_DIR "gallery-medium-33.jpg",
73     DEMO_IMAGE_DIR "gallery-medium-34.jpg",
74     DEMO_IMAGE_DIR "gallery-medium-35.jpg",
75     DEMO_IMAGE_DIR "gallery-medium-36.jpg",
76     DEMO_IMAGE_DIR "gallery-medium-37.jpg",
77     DEMO_IMAGE_DIR "gallery-medium-38.jpg",
78     DEMO_IMAGE_DIR "gallery-medium-39.jpg",
79     DEMO_IMAGE_DIR "gallery-medium-40.jpg",
80     DEMO_IMAGE_DIR "gallery-medium-41.jpg",
81     DEMO_IMAGE_DIR "gallery-medium-42.jpg",
82     DEMO_IMAGE_DIR "gallery-medium-43.jpg",
83     DEMO_IMAGE_DIR "gallery-medium-44.jpg",
84     DEMO_IMAGE_DIR "gallery-medium-45.jpg",
85     DEMO_IMAGE_DIR "gallery-medium-46.jpg",
86     DEMO_IMAGE_DIR "gallery-medium-47.jpg",
87     DEMO_IMAGE_DIR "gallery-medium-48.jpg",
88     DEMO_IMAGE_DIR "gallery-medium-49.jpg",
89     DEMO_IMAGE_DIR "gallery-medium-50.jpg",
90     DEMO_IMAGE_DIR "gallery-medium-51.jpg",
91     DEMO_IMAGE_DIR "gallery-medium-52.jpg",
92     DEMO_IMAGE_DIR "gallery-medium-53.jpg",
93 };
94
95 const unsigned int NUM_IMAGES = sizeof(IMAGE_PATHS) / sizeof(char*);
96
97 const char* BACKGROUND_IMAGE( "" );
98 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
99 const char* EDIT_IMAGE( DEMO_IMAGE_DIR "icon-edit.png" );
100 const char* EDIT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-edit-selected.png" );
101 const char* SPIRAL_LAYOUT_IMAGE( DEMO_IMAGE_DIR "icon-item-view-layout-spiral.png" );
102 const char* SPIRAL_LAYOUT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-item-view-layout-spiral-selected.png" );
103 const char* GRID_LAYOUT_IMAGE( DEMO_IMAGE_DIR "icon-item-view-layout-grid.png" );
104 const char* GRID_LAYOUT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-item-view-layout-grid-selected.png" );
105 const char* DEPTH_LAYOUT_IMAGE( DEMO_IMAGE_DIR "icon-item-view-layout-depth.png" );
106 const char* DEPTH_LAYOUT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-item-view-layout-depth-selected.png" );
107 const char* DELETE_IMAGE( DEMO_IMAGE_DIR "icon-delete.png" );
108 const char* DELETE_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-delete-selected.png" );
109 const char* REPLACE_IMAGE( DEMO_IMAGE_DIR "icon-replace.png" );
110 const char* REPLACE_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-replace-selected.png" );
111 const char* INSERT_IMAGE( DEMO_IMAGE_DIR "icon-insert.png" );
112 const char* INSERT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-insert-selected.png" );
113 const char* SELECTED_IMAGE( DEMO_IMAGE_DIR "item-select-check.png" );
114 const char* APPLICATION_TITLE( "ItemView" );
115
116 const char* SPIRAL_LABEL("Spiral");
117 const char* GRID_LABEL("Grid");
118 const char* DEPTH_LABEL("Depth");
119
120 const float ITEM_BORDER_SIZE = 2.0f;
121
122 const float DEPTH_LAYOUT_ITEM_SIZE_FACTOR_PORTRAIT = 1.0f;
123 const float DEPTH_LAYOUT_ITEM_SIZE_FACTOR_LANDSCAPE = 0.8f;
124 const float DEPTH_LAYOUT_COLUMNS = 3.0f;
125
126 const float MIN_SWIPE_DISTANCE = 15.0f;
127 const float MIN_SWIPE_SPEED = 5.0f;
128
129 const float SELECTION_BORDER_WIDTH = 3.0f;
130 const float BUTTON_BORDER = -10.0f;
131 const float MENU_OPTION_HEIGHT(140.0f);
132 const float LABEL_TEXT_SIZE_Y = 20.0f;
133
134 const Vector3 INITIAL_OFFSCREEN_POSITION( 1000.0f, 0, -1000.0f );
135
136 static Vector3 DepthLayoutItemSizeFunctionPortrait( float layoutWidth )
137 {
138   float width = ( layoutWidth / ( DEPTH_LAYOUT_COLUMNS + 1.0f ) ) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_PORTRAIT;
139
140   // 1x1 aspect ratio
141   return Vector3(width, width, width);
142 }
143
144 static Vector3 DepthLayoutItemSizeFunctionLandscape( float layoutWidth )
145 {
146   float width = ( layoutWidth / ( DEPTH_LAYOUT_COLUMNS + 1.0f ) ) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_LANDSCAPE;
147
148   // 1x1 aspect ratio
149   return Vector3(width, width, width);
150 }
151
152 } // unnamed namespace
153
154 /**
155  * This example shows how to use ItemView UI control.
156  * There are three layouts created for ItemView, i.e., Spiral, Depth and Grid.
157  * There is one button in the upper-left corner for quitting the application and
158  * another button in the upper-right corner for switching between different layouts.
159  */
160 class ItemViewExample : public ConnectionTracker, public ItemFactory
161 {
162 public:
163
164   enum Mode
165   {
166     MODE_NORMAL,
167     MODE_REMOVE,
168     MODE_REMOVE_MANY,
169     MODE_INSERT,
170     MODE_INSERT_MANY,
171     MODE_REPLACE,
172     MODE_REPLACE_MANY,
173     MODE_LAST
174   };
175
176   /**
177    * Constructor
178    * @param application class, stored as reference
179    */
180   ItemViewExample( Application& application )
181   : mApplication( application ),
182     mMode( MODE_NORMAL ),
183     mOrientation( 0 ),
184     mCurrentLayout( SPIRAL_LAYOUT ),
185     mDurationSeconds( 0.25f )
186   {
187     // Connect to the Application's Init signal
188     mApplication.InitSignal().Connect(this, &ItemViewExample::OnInit);
189   }
190
191   /**
192    * This method gets called once the main loop of application is up and running
193    */
194   void OnInit(Application& app)
195   {
196     Stage stage = Dali::Stage::GetCurrent();
197     stage.KeyEventSignal().Connect(this, &ItemViewExample::OnKeyEvent);
198     stage.GetRootLayer().SetBehavior(Layer::LAYER_3D);
199
200     Vector2 stageSize = Stage::GetCurrent().GetSize();
201
202     // Creates a default view with a default tool bar.
203     // The view is added to the stage.
204
205     Layer contents = DemoHelper::CreateView( mApplication,
206                                              mView,
207                                              mToolBar,
208                                              BACKGROUND_IMAGE,
209                                              TOOLBAR_IMAGE,
210                                              "" );
211
212     //app.GetWindow().GetOrientation().ChangedSignal().Connect( this, &ItemViewExample::OnOrientationChanged );
213
214     // Create an edit mode button. (left of toolbar)
215     Toolkit::PushButton editButton = Toolkit::PushButton::New();
216     editButton.SetUnselectedImage( EDIT_IMAGE );
217     editButton.SetSelectedImage( EDIT_IMAGE_SELECTED );
218     editButton.ClickedSignal().Connect( this, &ItemViewExample::OnModeButtonClicked);
219     editButton.SetLeaveRequired( true );
220     mToolBar.AddControl( editButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalLeft, DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
221
222     // Create a layout toggle button. (right of toolbar)
223     mLayoutButton = Toolkit::PushButton::New();
224     mLayoutButton.SetUnselectedImage( SPIRAL_LAYOUT_IMAGE );
225     mLayoutButton.SetSelectedImage(SPIRAL_LAYOUT_IMAGE_SELECTED );
226     mLayoutButton.ClickedSignal().Connect( this, &ItemViewExample::OnLayoutButtonClicked);
227     mLayoutButton.SetLeaveRequired( true );
228     mToolBar.AddControl( mLayoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
229
230     // Create a delete button (bottom right of screen)
231     mDeleteButton = Toolkit::PushButton::New();
232     mDeleteButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
233     mDeleteButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
234     mDeleteButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
235     mDeleteButton.SetDrawMode( DrawMode::OVERLAY_2D );
236     mDeleteButton.SetUnselectedImage( DELETE_IMAGE );
237     mDeleteButton.SetSelectedImage( DELETE_IMAGE_SELECTED );
238     mDeleteButton.SetBackgroundImage( TOOLBAR_IMAGE );
239     mDeleteButton.SetSize( Vector2( stageSize.width * 0.15f, stageSize.width * 0.15f ) );
240     mDeleteButton.ClickedSignal().Connect( this, &ItemViewExample::OnDeleteButtonClicked);
241     mDeleteButton.SetLeaveRequired( true );
242     mDeleteButton.SetVisible( false );
243     stage.Add( mDeleteButton );
244
245     // Create an insert button (bottom right of screen)
246     mInsertButton = Toolkit::PushButton::New();
247     mInsertButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
248     mInsertButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
249     mInsertButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
250     mInsertButton.SetDrawMode( DrawMode::OVERLAY_2D );
251     mInsertButton.SetUnselectedImage( INSERT_IMAGE );
252     mInsertButton.SetSelectedImage( INSERT_IMAGE_SELECTED );
253     mInsertButton.SetBackgroundImage( TOOLBAR_IMAGE );
254     mInsertButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
255     mInsertButton.ClickedSignal().Connect( this, &ItemViewExample::OnInsertButtonClicked);
256     mInsertButton.SetLeaveRequired( true );
257     mInsertButton.SetVisible( false );
258     stage.Add( mInsertButton );
259
260     // Create an replace button (bottom right of screen)
261     mReplaceButton = Toolkit::PushButton::New();
262     mReplaceButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
263     mReplaceButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
264     mReplaceButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
265     mReplaceButton.SetDrawMode( DrawMode::OVERLAY_2D );
266     mReplaceButton.SetUnselectedImage( REPLACE_IMAGE );
267     mReplaceButton.SetSelectedImage( REPLACE_IMAGE_SELECTED );
268     mReplaceButton.SetBackgroundImage( TOOLBAR_IMAGE );
269     mReplaceButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
270     mReplaceButton.ClickedSignal().Connect( this, &ItemViewExample::OnReplaceButtonClicked);
271     mReplaceButton.SetLeaveRequired( true );
272     mReplaceButton.SetVisible( false );
273     stage.Add( mReplaceButton );
274
275     // Create the item view actor
276     mItemView = ItemView::New(*this);
277     mItemView.SetParentOrigin(ParentOrigin::CENTER);
278     mItemView.SetAnchorPoint(AnchorPoint::CENTER);
279
280     // Display item view on the stage
281     stage.Add( mItemView );
282     stage.GetRootLayer().SetBehavior( Layer::LAYER_3D );
283
284     // Create the layouts
285     mSpiralLayout = DefaultItemLayout::New( DefaultItemLayout::SPIRAL );
286     mDepthLayout = DefaultItemLayout::New( DefaultItemLayout::DEPTH );
287     mGridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
288
289     // Add the layouts to item view
290     mItemView.AddLayout(*mSpiralLayout);
291     mItemView.AddLayout(*mDepthLayout);
292     mItemView.AddLayout(*mGridLayout);
293
294     mItemView.SetMinimumSwipeDistance(MIN_SWIPE_DISTANCE);
295     mItemView.SetMinimumSwipeSpeed(MIN_SWIPE_SPEED);
296
297     // Activate the spiral layout
298     SetLayout( mCurrentLayout );
299     mItemView.SetKeyboardFocusable( true );
300     KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &ItemViewExample::OnKeyboardPreFocusChange );
301
302     // Set the title and icon to the current layout
303     SetLayoutTitle();
304     SetLayoutImage();
305   }
306
307   Actor OnKeyboardPreFocusChange( Actor current, Actor proposed, Control::KeyboardFocus::Direction direction )
308   {
309     if ( !current && !proposed  )
310     {
311       return mItemView;
312     }
313
314     return proposed;
315   }
316
317   /**
318    * Animate to a different layout
319    */
320   void ChangeLayout()
321   {
322     Animation animation = Animation::New( mDurationSeconds );
323     animation.FinishedSignal().Connect( this, &ItemViewExample::AnimationFinished );
324     animation.AnimateTo( Property( mItemView, Actor::Property::COLOR_ALPHA ), 0.0f );
325     animation.Play();
326   }
327
328   void AnimationFinished( Animation& )
329   {
330     SetLayout( mCurrentLayout );
331
332     Animation animation = Animation::New( mDurationSeconds );
333     animation.AnimateTo( Property( mItemView, Actor::Property::COLOR_ALPHA ), 1.0f );
334     animation.Play();
335   }
336
337   /**
338    * Switch to a different item view layout
339    */
340   void SetLayout( int layoutId )
341   {
342     Stage stage = Dali::Stage::GetCurrent();
343     switch( mCurrentLayout )
344     {
345       case SPIRAL_LAYOUT:
346       case DEPTH_LAYOUT:
347       {
348         stage.GetRootLayer().SetBehavior(Layer::LAYER_3D);
349         break;
350       }
351       case GRID_LAYOUT:
352       {
353         stage.GetRootLayer().SetBehavior(Layer::LAYER_2D);
354         break;
355       }
356     }
357
358     // Set the new orientation to the layout
359     mItemView.GetLayout(layoutId)->SetOrientation(static_cast<ControlOrientation::Type>(mOrientation / 90));
360
361     Vector2 stageSize = Stage::GetCurrent().GetSize();
362
363     if(layoutId == DEPTH_LAYOUT)
364     {
365       // Set up the depth layout according to the new orientation
366       if(Toolkit::IsVertical(mDepthLayout->GetOrientation()))
367       {
368         mDepthLayout->SetItemSize( DepthLayoutItemSizeFunctionPortrait( stageSize.width ) );
369       }
370       else
371       {
372         mDepthLayout->SetItemSize( DepthLayoutItemSizeFunctionLandscape( stageSize.height ) );
373       }
374     }
375
376     // Enable anchoring for depth layout only
377     mItemView.SetAnchoring(layoutId == DEPTH_LAYOUT);
378
379     // Activate the layout
380     mItemView.ActivateLayout( layoutId, Vector3(stageSize.x, stageSize.y, stageSize.x), 0.0f );
381   }
382
383   /**
384    * Orientation changed signal callback
385    * @param orientation
386    */
387   void OnOrientationChanged( Orientation orientation )
388   {
389     const unsigned int angle = orientation.GetDegrees();
390
391     // If orientation really changed
392     if( mOrientation != angle )
393     {
394       // Remember orientation
395       mOrientation = angle;
396
397       SetLayout( mCurrentLayout );
398     }
399   }
400
401   bool OnLayoutButtonClicked( Toolkit::Button button )
402   {
403     // Switch to the next layout
404     mCurrentLayout = (mCurrentLayout + 1) % mItemView.GetLayoutCount();
405
406     ChangeLayout();
407
408     SetLayoutTitle();
409     SetLayoutImage();
410
411     return true;
412   }
413
414   bool OnModeButtonClicked( Toolkit::Button button )
415   {
416     SwitchToNextMode();
417
418     return true;
419   }
420
421   void SwitchToNextMode()
422   {
423     switch( mMode )
424     {
425       case MODE_REMOVE:
426       {
427         ExitRemoveMode();
428         mMode = MODE_REMOVE_MANY;
429         EnterRemoveManyMode();
430         break;
431       }
432
433       case MODE_REMOVE_MANY:
434       {
435         ExitRemoveManyMode();
436         mMode = MODE_INSERT;
437         EnterInsertMode();
438         break;
439       }
440
441       case MODE_INSERT:
442       {
443         ExitInsertMode();
444         mMode = MODE_INSERT_MANY;
445         EnterInsertManyMode();
446         break;
447       }
448
449       case MODE_INSERT_MANY:
450       {
451         ExitInsertManyMode();
452         mMode = MODE_REPLACE;
453         EnterReplaceMode();
454         break;
455       }
456
457       case MODE_REPLACE:
458       {
459         ExitReplaceMode();
460         mMode = MODE_REPLACE_MANY;
461         EnterReplaceManyMode();
462         break;
463       }
464
465       case MODE_REPLACE_MANY:
466       {
467         ExitReplaceManyMode();
468         mMode = MODE_NORMAL;
469         SetLayoutTitle();
470         break;
471       }
472
473       case MODE_NORMAL:
474       default:
475       {
476         mMode = MODE_REMOVE;
477         EnterRemoveMode();
478         break;
479       }
480     }
481   }
482
483   void EnterRemoveMode()
484   {
485     SetTitle("Edit: Remove");
486
487     mTapDetector = TapGestureDetector::New();
488
489     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
490     {
491       mTapDetector.Attach(mItemView.GetChildAt(i));
492     }
493
494     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::RemoveOnTap );
495   }
496
497   void ExitRemoveMode()
498   {
499     mTapDetector.Reset();
500   }
501
502   void RemoveOnTap( Actor actor, const TapGesture& tap )
503   {
504     mItemView.RemoveItem( mItemView.GetItemId(actor), 0.5f );
505   }
506
507   void EnterRemoveManyMode()
508   {
509     SetTitle("Edit: Remove Many");
510
511     mDeleteButton.SetVisible( true );
512
513     mTapDetector = TapGestureDetector::New();
514
515     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
516     {
517       Actor child = mItemView.GetChildAt( i );
518       Actor box = child.FindChildByName( "CheckBox" );
519
520       if( box )
521       {
522         mTapDetector.Attach( child );
523         box.SetVisible( true );
524       }
525     }
526
527     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
528   }
529
530   void ExitRemoveManyMode()
531   {
532     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
533     {
534       Actor child = mItemView.GetChildAt( i );
535       Actor box = child.FindChildByName( "CheckBox" );
536
537       if( box )
538       {
539         box.SetVisible( false );
540
541         Actor tick = box.FindChildByName( "Tick" );
542         if( tick )
543         {
544           tick.SetVisible( false );
545         }
546       }
547     }
548
549     mTapDetector.Reset();
550
551     mDeleteButton.SetVisible( false );
552   }
553
554   void SelectOnTap( Actor actor, const TapGesture& tap )
555   {
556     Actor tick = actor.FindChildByName( "Tick" );
557     if( tick )
558     {
559       tick.SetVisible( !tick.IsVisible() );
560     }
561   }
562
563   bool OnDeleteButtonClicked( Toolkit::Button button )
564   {
565     ItemIdContainer removeList;
566
567     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
568     {
569       Actor child = mItemView.GetChildAt( i );
570       Actor tick = child.FindChildByName( "Tick" );
571
572       if( tick && tick.IsVisible() )
573       {
574         removeList.push_back( mItemView.GetItemId(child) );
575       }
576     }
577
578     if( ! removeList.empty() )
579     {
580       mItemView.RemoveItems( removeList, 0.5f );
581     }
582
583     return true;
584   }
585
586   void EnterInsertMode()
587   {
588     SetTitle("Edit: Insert");
589
590     mTapDetector = TapGestureDetector::New();
591
592     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
593     {
594       mTapDetector.Attach( mItemView.GetChildAt(i) );
595     }
596
597     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::InsertOnTap );
598   }
599
600   void ExitInsertMode()
601   {
602     mTapDetector.Reset();
603   }
604
605   void InsertOnTap( Actor actor, const TapGesture& tap )
606   {
607     ItemId id = mItemView.GetItemId( actor );
608
609     Actor newActor = NewItem( rand() );
610
611     mItemView.InsertItem( Item(id,newActor), 0.5f );
612   }
613
614   void EnterInsertManyMode()
615   {
616     SetTitle("Edit: Insert Many");
617
618     mInsertButton.SetVisible( true );
619
620     mTapDetector = TapGestureDetector::New();
621
622     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
623     {
624       Actor child = mItemView.GetChildAt( i );
625       Actor box = child.FindChildByName( "CheckBox" );
626
627       if( box )
628       {
629         mTapDetector.Attach( child );
630         box.SetVisible( true );
631       }
632     }
633
634     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
635   }
636
637   void ExitInsertManyMode()
638   {
639     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
640     {
641       Actor child = mItemView.GetChildAt( i );
642       Actor box = child.FindChildByName( "CheckBox" );
643
644       if( box )
645       {
646         box.SetVisible( false );
647
648         Actor tick = box.FindChildByName( "Tick" );
649         if( tick )
650         {
651           tick.SetVisible( false );
652         }
653       }
654     }
655
656     mTapDetector.Reset();
657
658     mInsertButton.SetVisible( false );
659   }
660
661   bool OnInsertButtonClicked( Toolkit::Button button )
662   {
663     ItemContainer insertList;
664
665     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
666     {
667       Actor child = mItemView.GetChildAt( i );
668       Actor tick = child.FindChildByName( "Tick" );
669
670       if( tick && tick.IsVisible() )
671       {
672         insertList.push_back( Item( mItemView.GetItemId(child), NewItem(rand()) ) );
673       }
674     }
675
676     if( ! insertList.empty() )
677     {
678       mItemView.InsertItems( insertList, 0.5f );
679     }
680
681     return true;
682   }
683
684   void EnterReplaceMode()
685   {
686     SetTitle("Edit: Replace");
687
688     mTapDetector = TapGestureDetector::New();
689
690     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
691     {
692       mTapDetector.Attach(mItemView.GetChildAt(i));
693     }
694
695     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::ReplaceOnTap );
696   }
697
698   void ReplaceOnTap( Actor actor, const TapGesture& tap )
699   {
700     mItemView.ReplaceItem( Item( mItemView.GetItemId(actor), NewItem(rand()) ), 0.5f );
701   }
702
703   void ExitReplaceMode()
704   {
705     mTapDetector.Reset();
706   }
707
708   void EnterReplaceManyMode()
709   {
710     SetTitle("Edit: Replace Many");
711
712     mReplaceButton.SetVisible( true );
713
714     mTapDetector = TapGestureDetector::New();
715
716     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
717     {
718       Actor child = mItemView.GetChildAt( i );
719       Actor box = child.FindChildByName( "CheckBox" );
720
721       if( box )
722       {
723         mTapDetector.Attach( child );
724         box.SetVisible( true );
725       }
726     }
727
728     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
729   }
730
731   void ExitReplaceManyMode()
732   {
733     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
734     {
735       Actor child = mItemView.GetChildAt( i );
736       Actor box = child.FindChildByName( "CheckBox" );
737
738       if( box )
739       {
740         box.SetVisible( false );
741
742         Actor tick = box.FindChildByName( "Tick" );
743         if( tick )
744         {
745           tick.SetVisible( false );
746         }
747       }
748     }
749
750     mTapDetector.Reset();
751
752     mReplaceButton.SetVisible( false );
753   }
754
755   bool OnReplaceButtonClicked( Toolkit::Button button )
756   {
757     ItemContainer replaceList;
758
759     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
760     {
761       Actor child = mItemView.GetChildAt( i );
762       Actor tick = child.FindChildByName( "Tick" );
763
764       if( tick && tick.IsVisible() )
765       {
766         replaceList.push_back( Item( mItemView.GetItemId(child), NewItem(rand()) ) );
767       }
768     }
769
770     if( ! replaceList.empty() )
771     {
772       mItemView.ReplaceItems( replaceList, 0.5f );
773     }
774
775     return true;
776   }
777
778   void SetLayoutTitle()
779   {
780     if( MODE_NORMAL == mMode )
781     {
782       std::stringstream ss(APPLICATION_TITLE);
783       switch(mCurrentLayout)
784       {
785       case SPIRAL_LAYOUT:
786         ss << APPLICATION_TITLE << ": " << SPIRAL_LABEL;
787         break;
788       case GRID_LAYOUT:
789         ss << APPLICATION_TITLE << ": " << GRID_LABEL;
790         break;
791       case DEPTH_LAYOUT:
792         ss << APPLICATION_TITLE << ": " << DEPTH_LABEL;
793         break;
794       default:
795         break;
796       }
797       SetTitle(ss.str());
798     }
799   }
800
801   void SetLayoutImage()
802   {
803     if( mLayoutButton )
804     {
805       switch( mCurrentLayout )
806       {
807         case SPIRAL_LAYOUT:
808         {
809           mLayoutButton.SetUnselectedImage( SPIRAL_LAYOUT_IMAGE );
810           mLayoutButton.SetSelectedImage( SPIRAL_LAYOUT_IMAGE_SELECTED );
811           break;
812         }
813
814         case GRID_LAYOUT:
815         {
816           mLayoutButton.SetUnselectedImage( GRID_LAYOUT_IMAGE );
817           mLayoutButton.SetSelectedImage( GRID_LAYOUT_IMAGE_SELECTED );
818           break;
819         }
820
821         case DEPTH_LAYOUT:
822         {
823           mLayoutButton.SetUnselectedImage( DEPTH_LAYOUT_IMAGE );
824           mLayoutButton.SetSelectedImage( DEPTH_LAYOUT_IMAGE_SELECTED );
825           break;
826         }
827
828         default:
829           break;
830       }
831     }
832   }
833
834 public: // From ItemFactory
835
836   /**
837    * Query the number of items available from the factory.
838    * The maximum available item has an ID of GetNumberOfItems() - 1.
839    */
840   virtual unsigned int GetNumberOfItems()
841   {
842     return NUM_IMAGES * 10;
843   }
844
845   /**
846    * Create an Actor to represent a visible item.
847    * @param itemId
848    * @return the created actor.
849    */
850   virtual Actor NewItem(unsigned int itemId)
851   {
852     // Create an image view for this item
853     ImageView actor = ImageView::New( IMAGE_PATHS[ itemId % NUM_IMAGES ] );
854     actor.SetZ( 0.0f );
855     actor.SetPosition( INITIAL_OFFSCREEN_POSITION );
856
857     // Add a border image child actor
858     ImageView borderActor = ImageView::New();
859     borderActor.SetParentOrigin( ParentOrigin::CENTER );
860     borderActor.SetAnchorPoint( AnchorPoint::CENTER );
861     borderActor.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::ALL_DIMENSIONS );
862     borderActor.SetSizeModeFactor( Vector3( 2.0f * ITEM_BORDER_SIZE, 2.0f * ITEM_BORDER_SIZE, 0.0f ) );
863     borderActor.SetColorMode( USE_PARENT_COLOR );
864
865     Property::Map borderProperty;
866     borderProperty.Insert( "rendererType", "borderRenderer" );
867     borderProperty.Insert( "borderColor", Color::WHITE );
868     borderProperty.Insert( "borderSize", ITEM_BORDER_SIZE );
869     borderProperty.Insert( "antiAliasing", true );
870     borderActor.SetProperty( ImageView::Property::IMAGE, borderProperty );
871
872     actor.Add(borderActor);
873
874     actor.SetKeyboardFocusable( true );
875
876     Vector3 spiralItemSize;
877     static_cast<ItemLayout&>(*mSpiralLayout).GetItemSize( 0u, Vector3( Stage::GetCurrent().GetSize() ), spiralItemSize );
878
879     // Add a checkbox child actor; invisible until edit-mode is enabled
880     ImageView checkbox = ImageView::New();
881     checkbox.SetName( "CheckBox" );
882     checkbox.SetColorMode( USE_PARENT_COLOR );
883     checkbox.SetParentOrigin( ParentOrigin::TOP_RIGHT );
884     checkbox.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
885     checkbox.SetSize( spiralItemSize.width * 0.2f, spiralItemSize.width * 0.2f );
886     checkbox.SetPosition( -SELECTION_BORDER_WIDTH, SELECTION_BORDER_WIDTH );
887     checkbox.SetZ( 0.1f );
888
889     Property::Map solidColorProperty;
890     solidColorProperty.Insert( "rendererType", "colorRenderer" );
891     solidColorProperty.Insert( "blendColor", Vector4(0.f, 0.f, 0.f, 0.6f) );
892     checkbox.SetProperty( ImageView::Property::IMAGE, solidColorProperty );
893
894     if( MODE_REMOVE_MANY  != mMode &&
895         MODE_INSERT_MANY  != mMode &&
896         MODE_REPLACE_MANY != mMode )
897     {
898       checkbox.SetVisible( false );
899     }
900     borderActor.Add( checkbox );
901
902     ImageView tick = ImageView::New( SELECTED_IMAGE );
903     tick.SetName( "Tick" );
904     tick.SetColorMode( USE_PARENT_COLOR );
905     tick.SetParentOrigin( ParentOrigin::TOP_RIGHT );
906     tick.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
907     tick.SetSize( spiralItemSize.width * 0.2f, spiralItemSize.width * 0.2f );
908     tick.SetZ( 0.2f );
909     tick.SetVisible( false );
910     checkbox.Add( tick );
911
912     // Connect new items for various editing modes
913     if( mTapDetector )
914     {
915       mTapDetector.Attach( actor );
916     }
917
918     return actor;
919   }
920
921 private:
922
923   /**
924    * Sets/Updates the title of the View
925    * @param[in] title The new title for the view.
926    */
927   void SetTitle(const std::string& title)
928   {
929     if(!mTitleActor)
930     {
931       mTitleActor = DemoHelper::CreateToolBarLabel( "" );
932       // Add title to the tool bar.
933       mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HorizontalCenter );
934     }
935
936     mTitleActor.SetProperty( TextLabel::Property::TEXT, title );
937   }
938
939   /**
940    * Main key event handler
941    */
942   void OnKeyEvent(const KeyEvent& event)
943   {
944     if(event.state == KeyEvent::Down)
945     {
946       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
947       {
948         mApplication.Quit();
949       }
950     }
951   }
952
953 private:
954
955   Application& mApplication;
956   Mode mMode;
957
958   Toolkit::Control mView;
959   unsigned int mOrientation;
960
961   Toolkit::ToolBar mToolBar;
962   TextLabel mTitleActor;             ///< The Toolbar's Title.
963
964   ItemView mItemView;
965   unsigned int mCurrentLayout;
966   float mDurationSeconds;
967
968   ItemLayoutPtr mSpiralLayout;
969   ItemLayoutPtr mDepthLayout;
970   ItemLayoutPtr mGridLayout;
971
972   TapGestureDetector mTapDetector;
973   Toolkit::PushButton mLayoutButton;
974   Toolkit::PushButton mDeleteButton;
975   Toolkit::PushButton mInsertButton;
976   Toolkit::PushButton mReplaceButton;
977 };
978
979 void RunTest(Application& app)
980 {
981   ItemViewExample test(app);
982
983   app.MainLoop();
984 }
985
986 int main(int argc, char **argv)
987 {
988   Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
989
990   RunTest(app);
991
992   return 0;
993 }