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