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