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