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