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