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