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