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