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