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