[dali_1.0.37] Merge branch '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
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 Vector3 INITIAL_OFFSCREEN_POSITION( 1000.0f, 0, -1000.0f );
164
165 static Vector3 DepthLayoutItemSizeFunctionPortrait(unsigned int numberOfColumns, float layoutWidth)
166 {
167   float width = (layoutWidth / static_cast<float>(numberOfColumns + 1)) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_PORTRAIT;
168
169   // 1x1 aspect ratio
170   return Vector3(width, width, width);
171 }
172
173 static Vector3 DepthLayoutItemSizeFunctionLandscape(unsigned int numberOfColumns, float layoutWidth)
174 {
175   float width = (layoutWidth / static_cast<float>(numberOfColumns + 1)) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_LANDSCAPE;
176
177   // 1x1 aspect ratio
178   return Vector3(width, width, width);
179 }
180
181 static float DepthLayoutBottomMarginFunctionPortrait(float layoutHeight)
182 {
183   return layoutHeight * DEPTH_LAYOUT_BOTTOM_MARGIN_FACTOR_PORTRAIT;
184 }
185
186 static float DepthLayoutBottomMarginFunctionLandscape(float layoutHeight)
187 {
188   return layoutHeight * DEPTH_LAYOUT_BOTTOM_MARGIN_FACTOR_LANDSCAPE;
189 }
190
191 }
192
193 /**
194  * This example shows how to use ItemView UI control.
195  * There are three layouts created for ItemView, i.e., Spiral, Depth and Grid.
196  * There is one button in the upper-left corner for quitting the application and
197  * another button in the upper-right corner for switching between different layouts.
198  */
199 class ItemViewExample : public ConnectionTracker, public ItemFactory
200 {
201 public:
202
203   enum Mode
204   {
205     MODE_NORMAL,
206     MODE_REMOVE,
207     MODE_REMOVE_MANY,
208     MODE_INSERT,
209     MODE_INSERT_MANY,
210     MODE_REPLACE,
211     MODE_REPLACE_MANY,
212     MODE_LAST
213   };
214
215   /**
216    * Constructor
217    * @param application class, stored as reference
218    */
219   ItemViewExample( Application& application )
220   : mApplication( application ),
221     mMode( MODE_NORMAL ),
222     mMenuShown( false ),
223     mOrientation( 0 ),
224     mCurrentLayout( SPIRAL_LAYOUT ),
225     mDurationSeconds( 1.0f ),
226     mAlphaFuncIndex( 0u )
227   {
228     // Connect to the Application's Init signal
229     mApplication.InitSignal().Connect(this, &ItemViewExample::OnInit);
230   }
231
232   /**
233    * This method gets called once the main loop of application is up and running
234    */
235   void OnInit(Application& app)
236   {
237     DemoHelper::RequestThemeChange();
238
239     Stage stage = Dali::Stage::GetCurrent();
240     stage.KeyEventSignal().Connect(this, &ItemViewExample::OnKeyEvent);
241
242     Vector2 stageSize = Stage::GetCurrent().GetSize();
243
244     // Create a border image shared by all the item actors
245     mBorderImage = ResourceImage::New(ITEM_BORDER_IMAGE_PATH);
246
247     // Creates a default view with a default tool bar.
248     // The view is added to the stage.
249     Layer contents = DemoHelper::CreateView( mApplication,
250                                              mView,
251                                              mToolBar,
252                                              BACKGROUND_IMAGE,
253                                              TOOLBAR_IMAGE,
254                                              "" );
255
256     mView.OrientationAnimationStartedSignal().Connect( this, &ItemViewExample::OnOrientationChanged );
257
258     // Create an edit mode button. (left of toolbar)
259     Toolkit::PushButton editButton = Toolkit::PushButton::New();
260     editButton.SetBackgroundImage( ResourceImage::New( EDIT_IMAGE ) );
261     editButton.ClickedSignal().Connect( this, &ItemViewExample::OnModeButtonClicked);
262     editButton.SetLeaveRequired( true );
263     mToolBar.AddControl( editButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalLeft, DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
264
265     // Create a layout toggle button. (right of toolbar)
266     mLayoutButton = Toolkit::PushButton::New();
267     mLayoutButton.SetBackgroundImage( ResourceImage::New( SPIRAL_LAYOUT_IMAGE ) );
268     mLayoutButton.ClickedSignal().Connect( this, &ItemViewExample::OnLayoutButtonClicked);
269     mLayoutButton.SetLeaveRequired( true );
270     mToolBar.AddControl( mLayoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
271
272     // Create a delete button (bottom right of screen)
273     mDeleteButton = Toolkit::PushButton::New();
274     mDeleteButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
275     mDeleteButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
276     mDeleteButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
277     mDeleteButton.SetDrawMode( DrawMode::OVERLAY );
278     mDeleteButton.SetButtonImage( ResourceImage::New( DELETE_IMAGE ) );
279     mDeleteButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_IMAGE ) );
280     mDeleteButton.SetSize( Vector2( stageSize.width * 0.15f, stageSize.width * 0.15f ) );
281     mDeleteButton.ClickedSignal().Connect( this, &ItemViewExample::OnDeleteButtonClicked);
282     mDeleteButton.SetLeaveRequired( true );
283     mDeleteButton.SetVisible( false );
284     stage.Add( mDeleteButton );
285
286     // Create an insert button (bottom right of screen)
287     mInsertButton = Toolkit::PushButton::New();
288     mInsertButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
289     mInsertButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
290     mInsertButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
291     mInsertButton.SetDrawMode( DrawMode::OVERLAY );
292     mInsertButton.SetButtonImage( ResourceImage::New( INSERT_IMAGE ) );
293     mInsertButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_IMAGE ) );
294     mInsertButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
295     mInsertButton.ClickedSignal().Connect( this, &ItemViewExample::OnInsertButtonClicked);
296     mInsertButton.SetLeaveRequired( true );
297     mInsertButton.SetVisible( false );
298     stage.Add( mInsertButton );
299
300     // Create an replace button (bottom right of screen)
301     mReplaceButton = Toolkit::PushButton::New();
302     mReplaceButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
303     mReplaceButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
304     mReplaceButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
305     mReplaceButton.SetDrawMode( DrawMode::OVERLAY );
306     mReplaceButton.SetButtonImage( ResourceImage::New( REPLACE_IMAGE ) );
307     mReplaceButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_IMAGE ) );
308     mReplaceButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
309     mReplaceButton.ClickedSignal().Connect( this, &ItemViewExample::OnReplaceButtonClicked);
310     mReplaceButton.SetLeaveRequired( true );
311     mReplaceButton.SetVisible( false );
312     stage.Add( mReplaceButton );
313
314     // Create the item view actor
315     mImageAtlas = CreateImageAtlas();
316     mItemView = ItemView::New(*this);
317     mItemView.SetRelayoutEnabled( false );
318     mItemView.SetParentOrigin(ParentOrigin::CENTER);
319     mItemView.SetAnchorPoint(AnchorPoint::CENTER);
320
321     // Display item view on the stage
322     stage.Add( mItemView );
323
324     // Create the layouts
325     mSpiralLayout = SpiralLayout::New();
326     mDepthLayout = DepthLayout::New();
327     mGridLayout = GridLayout::New();
328
329     // Add the layouts to item view
330     mItemView.AddLayout(*mSpiralLayout);
331     mItemView.AddLayout(*mDepthLayout);
332     mItemView.AddLayout(*mGridLayout);
333
334     mItemView.SetMinimumSwipeDistance(MIN_SWIPE_DISTANCE);
335     mItemView.SetMinimumSwipeSpeed(MIN_SWIPE_SPEED);
336
337     // Activate the spiral layout
338     UseLayout(mCurrentLayout, 0.0f);
339     mItemView.SetKeyboardFocusable( true );
340     KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &ItemViewExample::OnKeyboardPreFocusChange );
341
342     // Set the title and icon to the current layout
343     SetLayoutTitle();
344     SetLayoutImage();
345
346     // Store one 1x1 white image for multiple items to share for backgrounds:
347     mWhiteImage = BufferImage::WHITE();
348   }
349
350   Actor OnKeyboardPreFocusChange( Actor current, Actor proposed, Control::KeyboardFocusNavigationDirection direction )
351   {
352     if ( !current && !proposed  )
353     {
354       return mItemView;
355     }
356
357     return proposed;
358   }
359
360   /**
361    * Switch to a different item view layout
362    */
363   void UseLayout(int layoutId, float duration)
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 == SPIRAL_LAYOUT)
371     {
372       mSpiralLayout->SetRevolutionDistance(stageSize.height / Stage::GetCurrent().GetDpi().y * 45.0f);
373     }
374
375     if(layoutId == GRID_LAYOUT)
376     {
377       // Set up the grid layout according to the new orientation
378       float layoutWidth = Toolkit::IsHorizontal(mGridLayout->GetOrientation()) ? stageSize.height : stageSize.width;
379       float gridItemSize = (layoutWidth / mGridLayout->GetNumberOfColumns()) * 0.5f;
380       mGridLayout->SetScrollSpeedFactor(mGridLayout->GetNumberOfColumns() / gridItemSize);
381
382       float toolbarHeight = mToolBar.GetCurrentSize().y;
383       mGridLayout->SetTopMargin(toolbarHeight + mGridLayout->GetRowSpacing());
384     }
385
386     if(layoutId == DEPTH_LAYOUT)
387     {
388       // Set up the depth layout according to the new orientation
389       if(Toolkit::IsVertical(mDepthLayout->GetOrientation()))
390       {
391         mDepthLayout->SetRowSpacing(stageSize.height * DEPTH_LAYOUT_ROW_SPACING_FACTOR);
392         mDepthLayout->SetNumberOfRows(DEPTH_LAYOUT_ROWS_PORTRAIT);
393         mDepthLayout->SetTiltAngle( Degree( DEPTH_LAYOUT_TILT_ANGLE_PORTRAIT - mDepthLayout->GetNumberOfColumns() ) );
394         mDepthLayout->SetItemSizeFunction(DepthLayoutItemSizeFunctionPortrait);
395         mDepthLayout->SetBottomMarginFunction(DepthLayoutBottomMarginFunctionPortrait);
396       }
397       else
398       {
399         mDepthLayout->SetRowSpacing(stageSize.width * DEPTH_LAYOUT_ROW_SPACING_FACTOR);
400         mDepthLayout->SetNumberOfRows(DEPTH_LAYOUT_ROWS_LANDSCAPE);
401         mDepthLayout->SetTiltAngle( Degree( DEPTH_LAYOUT_TILT_ANGLE_LANDSCAPE - mDepthLayout->GetNumberOfColumns() ) );
402         mDepthLayout->SetItemSizeFunction(DepthLayoutItemSizeFunctionLandscape);
403         mDepthLayout->SetBottomMarginFunction(DepthLayoutBottomMarginFunctionLandscape);
404       }
405     }
406
407     // Enable anchoring for depth layout only
408     mItemView.SetAnchoring(layoutId == DEPTH_LAYOUT);
409
410     // Activate the layout
411     mItemView.ActivateLayout(layoutId, Vector3(stageSize.x, stageSize.y, stageSize.x), duration);
412   }
413
414   /**
415    * Orientation changed signal callback
416    * @param orientation
417    */
418   void OnOrientationChanged( View view, Animation& animation, const Orientation& orientation )
419   {
420     const unsigned int angle = orientation.GetDegrees();
421
422     // If orientation really changed
423     if( mOrientation != angle )
424     {
425       // Remember orientation
426       mOrientation = angle;
427
428       UseLayout(mCurrentLayout, mDurationSeconds);
429     }
430   }
431
432   bool OnLayoutButtonClicked( Toolkit::Button button )
433   {
434     // Switch to the next layout
435     mCurrentLayout = (mCurrentLayout + 1) % mItemView.GetLayoutCount();
436
437     UseLayout(mCurrentLayout, mDurationSeconds);
438
439     SetLayoutTitle();
440     SetLayoutImage();
441
442     return true;
443   }
444
445   bool OnModeButtonClicked( Toolkit::Button button )
446   {
447     SwitchToNextMode();
448
449     return true;
450   }
451
452   void SwitchToNextMode()
453   {
454     switch( mMode )
455     {
456       case MODE_REMOVE:
457       {
458         ExitRemoveMode();
459         mMode = MODE_REMOVE_MANY;
460         EnterRemoveManyMode();
461         break;
462       }
463
464       case MODE_REMOVE_MANY:
465       {
466         ExitRemoveManyMode();
467         mMode = MODE_INSERT;
468         EnterInsertMode();
469         break;
470       }
471
472       case MODE_INSERT:
473       {
474         ExitInsertMode();
475         mMode = MODE_INSERT_MANY;
476         EnterInsertManyMode();
477         break;
478       }
479
480       case MODE_INSERT_MANY:
481       {
482         ExitInsertManyMode();
483         mMode = MODE_REPLACE;
484         EnterReplaceMode();
485         break;
486       }
487
488       case MODE_REPLACE:
489       {
490         ExitReplaceMode();
491         mMode = MODE_REPLACE_MANY;
492         EnterReplaceManyMode();
493         break;
494       }
495
496       case MODE_REPLACE_MANY:
497       {
498         ExitReplaceManyMode();
499         mMode = MODE_NORMAL;
500         SetLayoutTitle();
501         break;
502       }
503
504       case MODE_NORMAL:
505       default:
506       {
507         mMode = MODE_REMOVE;
508         EnterRemoveMode();
509         break;
510       }
511     }
512   }
513
514   void EnterRemoveMode()
515   {
516     SetTitle("Edit: Remove");
517
518     mTapDetector = TapGestureDetector::New();
519
520     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
521     {
522       mTapDetector.Attach(mItemView.GetChildAt(i));
523     }
524
525     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::RemoveOnTap );
526   }
527
528   void ExitRemoveMode()
529   {
530     mTapDetector.Reset();
531   }
532
533   void RemoveOnTap( Actor actor, const TapGesture& tap )
534   {
535     mItemView.RemoveItem( mItemView.GetItemId(actor), 0.5f );
536   }
537
538   void EnterRemoveManyMode()
539   {
540     SetTitle("Edit: Remove Many");
541
542     mDeleteButton.SetVisible( true );
543
544     mTapDetector = TapGestureDetector::New();
545
546     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
547     {
548       Actor child = mItemView.GetChildAt( i );
549       Actor box = child.FindChildByName( "CheckBox" );
550
551       if( box )
552       {
553         mTapDetector.Attach( child );
554         box.SetVisible( true );
555       }
556     }
557
558     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
559   }
560
561   void ExitRemoveManyMode()
562   {
563     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
564     {
565       Actor child = mItemView.GetChildAt( i );
566       Actor box = child.FindChildByName( "CheckBox" );
567
568       if( box )
569       {
570         box.SetVisible( false );
571
572         Actor tick = box.FindChildByName( "Tick" );
573         if( tick )
574         {
575           tick.SetVisible( false );
576         }
577       }
578     }
579
580     mTapDetector.Reset();
581
582     mDeleteButton.SetVisible( false );
583   }
584
585   void SelectOnTap( Actor actor, const TapGesture& tap )
586   {
587     Actor tick = actor.FindChildByName( "Tick" );
588     if( tick )
589     {
590       tick.SetVisible( !tick.IsVisible() );
591     }
592   }
593
594   bool OnDeleteButtonClicked( Toolkit::Button button )
595   {
596     ItemIdContainer removeList;
597
598     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
599     {
600       Actor child = mItemView.GetChildAt( i );
601       Actor tick = child.FindChildByName( "Tick" );
602
603       if( tick && tick.IsVisible() )
604       {
605         removeList.push_back( mItemView.GetItemId(child) );
606       }
607     }
608
609     if( ! removeList.empty() )
610     {
611       mItemView.RemoveItems( removeList, 0.5f );
612     }
613
614     return true;
615   }
616
617   void EnterInsertMode()
618   {
619     SetTitle("Edit: Insert");
620
621     mTapDetector = TapGestureDetector::New();
622
623     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
624     {
625       mTapDetector.Attach( mItemView.GetChildAt(i) );
626     }
627
628     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::InsertOnTap );
629   }
630
631   void ExitInsertMode()
632   {
633     mTapDetector.Reset();
634   }
635
636   void InsertOnTap( Actor actor, const TapGesture& tap )
637   {
638     ItemId id = mItemView.GetItemId( actor );
639
640     Actor newActor = NewItem( rand() );
641
642     mItemView.InsertItem( Item(id,newActor), 0.5f );
643   }
644
645   void EnterInsertManyMode()
646   {
647     SetTitle("Edit: Insert Many");
648
649     mInsertButton.SetVisible( true );
650
651     mTapDetector = TapGestureDetector::New();
652
653     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
654     {
655       Actor child = mItemView.GetChildAt( i );
656       Actor box = child.FindChildByName( "CheckBox" );
657
658       if( box )
659       {
660         mTapDetector.Attach( child );
661         box.SetVisible( true );
662       }
663     }
664
665     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
666   }
667
668   void ExitInsertManyMode()
669   {
670     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
671     {
672       Actor child = mItemView.GetChildAt( i );
673       Actor box = child.FindChildByName( "CheckBox" );
674
675       if( box )
676       {
677         box.SetVisible( false );
678
679         Actor tick = box.FindChildByName( "Tick" );
680         if( tick )
681         {
682           tick.SetVisible( false );
683         }
684       }
685     }
686
687     mTapDetector.Reset();
688
689     mInsertButton.SetVisible( false );
690   }
691
692   bool OnInsertButtonClicked( Toolkit::Button button )
693   {
694     ItemContainer insertList;
695
696     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
697     {
698       Actor child = mItemView.GetChildAt( i );
699       Actor tick = child.FindChildByName( "Tick" );
700
701       if( tick && tick.IsVisible() )
702       {
703         insertList.push_back( Item( mItemView.GetItemId(child), NewItem(rand()) ) );
704       }
705     }
706
707     if( ! insertList.empty() )
708     {
709       mItemView.InsertItems( insertList, 0.5f );
710     }
711
712     return true;
713   }
714
715   void EnterReplaceMode()
716   {
717     SetTitle("Edit: Replace");
718
719     mTapDetector = TapGestureDetector::New();
720
721     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
722     {
723       mTapDetector.Attach(mItemView.GetChildAt(i));
724     }
725
726     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::ReplaceOnTap );
727   }
728
729   void ReplaceOnTap( Actor actor, const TapGesture& tap )
730   {
731     mItemView.ReplaceItem( Item( mItemView.GetItemId(actor), NewItem(rand()) ), 0.5f );
732   }
733
734   void ExitReplaceMode()
735   {
736     mTapDetector.Reset();
737   }
738
739   void EnterReplaceManyMode()
740   {
741     SetTitle("Edit: Replace Many");
742
743     mReplaceButton.SetVisible( true );
744
745     mTapDetector = TapGestureDetector::New();
746
747     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
748     {
749       Actor child = mItemView.GetChildAt( i );
750       Actor box = child.FindChildByName( "CheckBox" );
751
752       if( box )
753       {
754         mTapDetector.Attach( child );
755         box.SetVisible( true );
756       }
757     }
758
759     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
760   }
761
762   void ExitReplaceManyMode()
763   {
764     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
765     {
766       Actor child = mItemView.GetChildAt( i );
767       Actor box = child.FindChildByName( "CheckBox" );
768
769       if( box )
770       {
771         box.SetVisible( false );
772
773         Actor tick = box.FindChildByName( "Tick" );
774         if( tick )
775         {
776           tick.SetVisible( false );
777         }
778       }
779     }
780
781     mTapDetector.Reset();
782
783     mReplaceButton.SetVisible( false );
784   }
785
786   bool OnReplaceButtonClicked( Toolkit::Button button )
787   {
788     ItemContainer replaceList;
789
790     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
791     {
792       Actor child = mItemView.GetChildAt( i );
793       Actor tick = child.FindChildByName( "Tick" );
794
795       if( tick && tick.IsVisible() )
796       {
797         replaceList.push_back( Item( mItemView.GetItemId(child), NewItem(rand()) ) );
798       }
799     }
800
801     if( ! replaceList.empty() )
802     {
803       mItemView.ReplaceItems( replaceList, 0.5f );
804     }
805
806     return true;
807   }
808
809   void SetLayoutTitle()
810   {
811     if( MODE_NORMAL == mMode )
812     {
813       std::stringstream ss(APPLICATION_TITLE);
814       switch(mCurrentLayout)
815       {
816       case SPIRAL_LAYOUT:
817         ss << APPLICATION_TITLE << ": " << SPIRAL_LABEL;
818         break;
819       case GRID_LAYOUT:
820         ss << APPLICATION_TITLE << ": " << GRID_LABEL;
821         break;
822       case DEPTH_LAYOUT:
823         ss << APPLICATION_TITLE << ": " << DEPTH_LABEL;
824         break;
825       default:
826         break;
827       }
828       SetTitle(ss.str());
829     }
830   }
831
832   void SetLayoutImage()
833   {
834     if( mLayoutButton )
835     {
836       switch( mCurrentLayout )
837       {
838         case SPIRAL_LAYOUT:
839         {
840           mLayoutButton.SetBackgroundImage( ResourceImage::New( SPIRAL_LAYOUT_IMAGE ) );
841           break;
842         }
843
844         case GRID_LAYOUT:
845         {
846           mLayoutButton.SetBackgroundImage( ResourceImage::New( GRID_LAYOUT_IMAGE ) );
847           break;
848         }
849
850         case DEPTH_LAYOUT:
851         {
852           mLayoutButton.SetBackgroundImage( ResourceImage::New( DEPTH_LAYOUT_IMAGE ) );
853           break;
854         }
855
856         default:
857           break;
858       }
859     }
860   }
861
862 public: // From ItemFactory
863
864   /**
865    * Query the number of items available from the factory.
866    * The maximum available item has an ID of GetNumberOfItems() - 1.
867    */
868   virtual unsigned int GetNumberOfItems()
869   {
870     return NUM_IMAGES * 10;
871   }
872
873   /**
874    * Create an Actor to represent a visible item.
875    * @param itemId
876    * @return the created actor.
877    */
878   virtual Actor NewItem(unsigned int itemId)
879   {
880     // Create an image actor for this item
881     unsigned int imageId = itemId % NUM_IMAGES;
882     ImageActor::PixelArea pixelArea( (imageId%NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_WIDTH,
883                                      (imageId/NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_HEIGHT,
884                                       IMAGE_WIDTH,
885                                       IMAGE_HEIGHT );
886     Actor actor = ImageActor::New(mImageAtlas, pixelArea);
887     actor.SetPosition( INITIAL_OFFSCREEN_POSITION );
888
889     // Add a border image child actor
890     ImageActor borderActor = ImageActor::New(mBorderImage);
891     borderActor.SetParentOrigin( ParentOrigin::CENTER );
892     borderActor.SetAnchorPoint( AnchorPoint::CENTER );
893     borderActor.SetPosition( 0.f, 0.f, 1.f );
894     borderActor.SetStyle( ImageActor::STYLE_NINE_PATCH );
895     borderActor.SetNinePatchBorder( Vector4( ITEM_IMAGE_BORDER_LEFT, ITEM_IMAGE_BORDER_TOP, ITEM_IMAGE_BORDER_RIGHT, ITEM_IMAGE_BORDER_BOTTOM ) );
896     borderActor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR ); // darken with parent image-actor
897     borderActor.SetResizePolicy( SIZE_FIXED_OFFSET_FROM_PARENT, ALL_DIMENSIONS );
898     borderActor.SetSizeModeFactor( ITEM_BORDER_MARGIN_SIZE );
899     actor.Add(borderActor);
900     actor.SetKeyboardFocusable( true );
901
902     Vector3 spiralItemSize;
903     static_cast<ItemLayout&>(*mSpiralLayout).GetItemSize( 0u, Vector3( Stage::GetCurrent().GetSize() ), spiralItemSize );
904
905     // Add a checkbox child actor; invisible until edit-mode is enabled
906
907     ImageActor checkbox = ImageActor::New( mWhiteImage );
908     checkbox.SetRelayoutEnabled( false );
909     checkbox.SetName( "CheckBox" );
910     checkbox.SetColor( Vector4(0.0f,0.0f,0.0f,0.6f) );
911     checkbox.SetParentOrigin( ParentOrigin::TOP_RIGHT );
912     checkbox.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
913     checkbox.SetSize( spiralItemSize.width * 0.2f, spiralItemSize.width * 0.2f );
914     checkbox.SetPosition( -SELECTION_BORDER_WIDTH, SELECTION_BORDER_WIDTH );
915     checkbox.SetZ( 1.0f );
916     if( MODE_REMOVE_MANY  != mMode &&
917         MODE_INSERT_MANY  != mMode &&
918         MODE_REPLACE_MANY != mMode )
919     {
920       checkbox.SetVisible( false );
921     }
922     actor.Add( checkbox );
923
924     ImageActor tick = ImageActor::New( ResourceImage::New(SELECTED_IMAGE) );
925     tick.SetRelayoutEnabled( false );
926     tick.SetColorMode( USE_OWN_COLOR );
927     tick.SetName( "Tick" );
928     tick.SetParentOrigin( ParentOrigin::TOP_RIGHT );
929     tick.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
930     tick.SetSize( spiralItemSize.width * 0.2f, spiralItemSize.width * 0.2f );
931     tick.SetZ( 1.0f );
932     tick.SetVisible( false );
933     checkbox.Add( tick );
934
935     // Connect new items for various editing modes
936     if( mTapDetector )
937     {
938       mTapDetector.Attach( actor );
939     }
940
941     return actor;
942   }
943
944 private:
945
946   /**
947    * Create an Atlas to tile the images inside.
948    */
949   Atlas CreateImageAtlas()
950   {
951     const unsigned int atlas_width = IMAGE_WIDTH*NUM_IMAGE_PER_ROW_IN_ATLAS;
952     const unsigned int atlas_height = IMAGE_HEIGHT*ceil( static_cast<float>(NUM_IMAGES)/ static_cast<float>(NUM_IMAGE_PER_ROW_IN_ATLAS));
953     Atlas atlas = Atlas::New(atlas_width, atlas_height, Pixel::RGB888);
954
955     for( unsigned int i = 0; i < NUM_IMAGES; i++ )
956     {
957       atlas.Upload( IMAGE_PATHS[i], (i%NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_WIDTH, (i/NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_HEIGHT );
958     }
959
960     return atlas;
961   }
962
963   /**
964    * Sets/Updates the title of the View
965    * @param[in] title The new title for the view.
966    */
967   void SetTitle(const std::string& title)
968   {
969     if(!mTitleActor)
970     {
971       mTitleActor = DemoHelper::CreateToolBarLabel( "" );
972       // Add title to the tool bar.
973       mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HorizontalCenter );
974     }
975
976     mTitleActor.SetProperty( TextLabel::Property::TEXT, title );
977   }
978
979   void ShowMenu()
980   {
981     Stage stage = Stage::GetCurrent();
982     const float popupWidth = stage.GetSize().x * 0.75f;
983
984     mMenu = Toolkit::Popup::New();
985     mMenu.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
986     mMenu.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
987     mMenu.SetSize( 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     TextLabel text = TextLabel::New( "Duration" );
1005     text.SetAnchorPoint( ParentOrigin::TOP_LEFT );
1006     text.SetParentOrigin( ParentOrigin::TOP_LEFT );
1007     text.SetResizePolicy( FILL_TO_PARENT, WIDTH );
1008     text.SetResizePolicy( FIXED, HEIGHT );
1009     text.SetSize( Vector2( 0.0f, LABEL_TEXT_SIZE_Y ) );
1010     slider.Add( text );
1011
1012     Actor textContainer = Actor::New();
1013     textContainer.SetRelayoutEnabled( true );
1014     textContainer.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
1015     mAlphaFunctionText = TextLabel::New( ALPHA_FUNCTIONS_TEXT[mAlphaFuncIndex] );
1016     mAlphaFunctionText.SetAnchorPoint( ParentOrigin::CENTER );
1017     mAlphaFunctionText.SetParentOrigin( ParentOrigin::CENTER );
1018     textContainer.Add( mAlphaFunctionText );
1019     tableView.AddChild( textContainer, TableView::CellPosition( 1, 0 ) );
1020
1021     mTapDetector = TapGestureDetector::New();
1022     mTapDetector.Attach(mAlphaFunctionText);
1023     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::ChangeAlphaFunctionOnTap );
1024
1025     text = TextLabel::New( "Alpha Function" );
1026     text.SetAnchorPoint( ParentOrigin::TOP_LEFT );
1027     text.SetParentOrigin( ParentOrigin::TOP_LEFT );
1028     text.SetResizePolicy( FILL_TO_PARENT, WIDTH );
1029     text.SetSize( 0.0f, LABEL_TEXT_SIZE_Y );
1030     textContainer.Add( text );
1031
1032     mMenu.MarkDirtyForRelayout();
1033     mMenu.Show();
1034     mMenuShown = true;
1035   }
1036
1037   bool SliderValueChange( Toolkit::Slider slider, float value )
1038   {
1039     mDurationSeconds = value;
1040
1041     return true;
1042   }
1043
1044   void ChangeAlphaFunctionOnTap( Actor actor, const TapGesture& tap )
1045   {
1046     if( NUM_ALPHA_FUNCTIONS <= ++mAlphaFuncIndex )
1047     {
1048       mAlphaFuncIndex = 0;
1049     }
1050
1051     if( mAlphaFunctionText )
1052     {
1053       mAlphaFunctionText.SetProperty( TextLabel::Property::TEXT, std::string(ALPHA_FUNCTIONS_TEXT[mAlphaFuncIndex]) );
1054     }
1055
1056     if( mItemView )
1057     {
1058       mItemView.GetActiveLayout()->SetAlphaFunction( ALPHA_FUNCTIONS[mAlphaFuncIndex] );
1059     }
1060   }
1061
1062   void HideMenu()
1063   {
1064     mTapDetector.Reset();
1065
1066     if( mMenu )
1067     {
1068       mMenu.Hide();
1069       mMenu.Reset();
1070     }
1071
1072     mMenuShown = false;
1073   }
1074
1075   /**
1076    * Main key event handler
1077    */
1078   void OnKeyEvent(const KeyEvent& event)
1079   {
1080     if(event.state == KeyEvent::Down)
1081     {
1082       if( IsKey( event, DALI_KEY_MENU ) )
1083       {
1084         if( mMenuShown )
1085         {
1086           HideMenu();
1087         }
1088         else
1089         {
1090           ShowMenu();
1091         }
1092       }
1093       else if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
1094       {
1095         if( mMenuShown )
1096         {
1097           HideMenu();
1098         }
1099         else
1100         {
1101           mApplication.Quit();
1102         }
1103       }
1104     }
1105   }
1106
1107 private:
1108
1109   Application& mApplication;
1110   Mode mMode;
1111   bool mMenuShown;
1112
1113   Toolkit::View mView;
1114   unsigned int mOrientation;
1115
1116   Toolkit::ToolBar mToolBar;
1117   TextLabel mTitleActor;             ///< The Toolbar's Title.
1118
1119   ItemView mItemView;
1120   Image mBorderImage;
1121   Atlas mImageAtlas;
1122   unsigned int mCurrentLayout;
1123   float mDurationSeconds;
1124
1125   SpiralLayoutPtr mSpiralLayout;
1126   DepthLayoutPtr mDepthLayout;
1127   GridLayoutPtr mGridLayout;
1128
1129   Toolkit::Popup mMenu;
1130
1131   TapGestureDetector mTapDetector;
1132   Toolkit::PushButton mLayoutButton;
1133   Toolkit::PushButton mDeleteButton;
1134   Toolkit::PushButton mInsertButton;
1135   Toolkit::PushButton mReplaceButton;
1136
1137   unsigned int mAlphaFuncIndex;
1138   TextLabel mAlphaFunctionText;
1139   BufferImage mWhiteImage;
1140 };
1141
1142 void RunTest(Application& app)
1143 {
1144   ItemViewExample test(app);
1145
1146   app.MainLoop();
1147 }
1148
1149 int main(int argc, char **argv)
1150 {
1151   Application app = Application::New(&argc, &argv);
1152
1153   RunTest(app);
1154
1155   return 0;
1156 }