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