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