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