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