Change FittingMode on Item view demos.
[platform/core/uifw/dali-demo.git] / examples / item-view / item-view-example.cpp
1 /*
2  * Copyright (c) 2018 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 "shared/view.h"
20
21 #include <dali/dali.h>
22 #include <dali-toolkit/dali-toolkit.h>
23 #include <dali-toolkit/devel-api/controls/buttons/button-devel.h>
24 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
25
26 using namespace Dali;
27 using namespace Dali::Toolkit;
28
29 namespace
30 {
31
32 enum AllImagesLayouts
33 {
34   SPIRAL_LAYOUT,
35   DEPTH_LAYOUT,
36   GRID_LAYOUT,
37
38   NUMBER_OF_LAYOUTS
39 };
40
41 const char* IMAGE_PATHS[] = {
42     DEMO_IMAGE_DIR "gallery-medium-1.jpg",
43     DEMO_IMAGE_DIR "gallery-medium-2.jpg",
44     DEMO_IMAGE_DIR "gallery-medium-3.jpg",
45     DEMO_IMAGE_DIR "gallery-medium-4.jpg",
46     DEMO_IMAGE_DIR "gallery-medium-5.jpg",
47     DEMO_IMAGE_DIR "gallery-medium-6.jpg",
48     DEMO_IMAGE_DIR "gallery-medium-7.jpg",
49     DEMO_IMAGE_DIR "gallery-medium-8.jpg",
50     DEMO_IMAGE_DIR "gallery-medium-9.jpg",
51     DEMO_IMAGE_DIR "gallery-medium-10.jpg",
52     DEMO_IMAGE_DIR "gallery-medium-11.jpg",
53     DEMO_IMAGE_DIR "gallery-medium-12.jpg",
54     DEMO_IMAGE_DIR "gallery-medium-13.jpg",
55     DEMO_IMAGE_DIR "gallery-medium-14.jpg",
56     DEMO_IMAGE_DIR "gallery-medium-15.jpg",
57     DEMO_IMAGE_DIR "gallery-medium-16.jpg",
58     DEMO_IMAGE_DIR "gallery-medium-17.jpg",
59     DEMO_IMAGE_DIR "gallery-medium-18.jpg",
60     DEMO_IMAGE_DIR "gallery-medium-19.jpg",
61     DEMO_IMAGE_DIR "gallery-medium-20.jpg",
62     DEMO_IMAGE_DIR "gallery-medium-21.jpg",
63     DEMO_IMAGE_DIR "gallery-medium-22.jpg",
64     DEMO_IMAGE_DIR "gallery-medium-23.jpg",
65     DEMO_IMAGE_DIR "gallery-medium-24.jpg",
66     DEMO_IMAGE_DIR "gallery-medium-25.jpg",
67     DEMO_IMAGE_DIR "gallery-medium-26.jpg",
68     DEMO_IMAGE_DIR "gallery-medium-27.jpg",
69     DEMO_IMAGE_DIR "gallery-medium-28.jpg",
70     DEMO_IMAGE_DIR "gallery-medium-29.jpg",
71     DEMO_IMAGE_DIR "gallery-medium-30.jpg",
72     DEMO_IMAGE_DIR "gallery-medium-31.jpg",
73     DEMO_IMAGE_DIR "gallery-medium-32.jpg",
74     DEMO_IMAGE_DIR "gallery-medium-33.jpg",
75     DEMO_IMAGE_DIR "gallery-medium-34.jpg",
76     DEMO_IMAGE_DIR "gallery-medium-35.jpg",
77     DEMO_IMAGE_DIR "gallery-medium-36.jpg",
78     DEMO_IMAGE_DIR "gallery-medium-37.jpg",
79     DEMO_IMAGE_DIR "gallery-medium-38.jpg",
80     DEMO_IMAGE_DIR "gallery-medium-39.jpg",
81     DEMO_IMAGE_DIR "gallery-medium-40.jpg",
82     DEMO_IMAGE_DIR "gallery-medium-41.jpg",
83     DEMO_IMAGE_DIR "gallery-medium-42.jpg",
84     DEMO_IMAGE_DIR "gallery-medium-43.jpg",
85     DEMO_IMAGE_DIR "gallery-medium-44.jpg",
86     DEMO_IMAGE_DIR "gallery-medium-45.jpg",
87     DEMO_IMAGE_DIR "gallery-medium-46.jpg",
88     DEMO_IMAGE_DIR "gallery-medium-47.jpg",
89     DEMO_IMAGE_DIR "gallery-medium-48.jpg",
90     DEMO_IMAGE_DIR "gallery-medium-49.jpg",
91     DEMO_IMAGE_DIR "gallery-medium-50.jpg",
92     DEMO_IMAGE_DIR "gallery-medium-51.jpg",
93     DEMO_IMAGE_DIR "gallery-medium-52.jpg",
94     DEMO_IMAGE_DIR "gallery-medium-53.jpg",
95 };
96
97 const unsigned int NUM_IMAGES = sizeof(IMAGE_PATHS) / sizeof(char*);
98
99 const char* BACKGROUND_IMAGE( "" );
100 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
101 const char* EDIT_IMAGE( DEMO_IMAGE_DIR "icon-edit.png" );
102 const char* EDIT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-edit-selected.png" );
103 const char* SPIRAL_LAYOUT_IMAGE( DEMO_IMAGE_DIR "icon-item-view-layout-spiral.png" );
104 const char* SPIRAL_LAYOUT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-item-view-layout-spiral-selected.png" );
105 const char* GRID_LAYOUT_IMAGE( DEMO_IMAGE_DIR "icon-item-view-layout-grid.png" );
106 const char* GRID_LAYOUT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-item-view-layout-grid-selected.png" );
107 const char* DEPTH_LAYOUT_IMAGE( DEMO_IMAGE_DIR "icon-item-view-layout-depth.png" );
108 const char* DEPTH_LAYOUT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-item-view-layout-depth-selected.png" );
109 const char* DELETE_IMAGE( DEMO_IMAGE_DIR "icon-delete.png" );
110 const char* DELETE_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-delete-selected.png" );
111 const char* REPLACE_IMAGE( DEMO_IMAGE_DIR "icon-replace.png" );
112 const char* REPLACE_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-replace-selected.png" );
113 const char* INSERT_IMAGE( DEMO_IMAGE_DIR "icon-insert.png" );
114 const char* INSERT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-insert-selected.png" );
115 const char* SELECTED_IMAGE( DEMO_IMAGE_DIR "item-select-check.png" );
116 const char* APPLICATION_TITLE( "ItemView" );
117
118 const char* SPIRAL_LABEL("Spiral");
119 const char* GRID_LABEL("Grid");
120 const char* DEPTH_LABEL("Depth");
121
122 const float ITEM_BORDER_SIZE = 2.0f;
123
124 const float DEPTH_LAYOUT_ITEM_SIZE_FACTOR_PORTRAIT = 1.0f;
125 const float DEPTH_LAYOUT_ITEM_SIZE_FACTOR_LANDSCAPE = 0.8f;
126 const float DEPTH_LAYOUT_COLUMNS = 3.0f;
127
128 const float MIN_SWIPE_DISTANCE = 15.0f;
129 const float MIN_SWIPE_SPEED = 5.0f;
130
131 const float SELECTION_BORDER_WIDTH = 3.0f;
132 const float BUTTON_BORDER = -10.0f;
133 const float MENU_OPTION_HEIGHT(140.0f);
134 const float LABEL_TEXT_SIZE_Y = 20.0f;
135
136 const Vector3 INITIAL_OFFSCREEN_POSITION( 1000.0f, 0, -1000.0f );
137
138 const float SCROLL_TO_ITEM_ANIMATION_TIME = 5.f;
139
140 static Vector3 DepthLayoutItemSizeFunctionPortrait( float layoutWidth )
141 {
142   float width = ( layoutWidth / ( DEPTH_LAYOUT_COLUMNS + 1.0f ) ) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_PORTRAIT;
143
144   // 1x1 aspect ratio
145   return Vector3(width, width, width);
146 }
147
148 static Vector3 DepthLayoutItemSizeFunctionLandscape( float layoutWidth )
149 {
150   float width = ( layoutWidth / ( DEPTH_LAYOUT_COLUMNS + 1.0f ) ) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_LANDSCAPE;
151
152   // 1x1 aspect ratio
153   return Vector3(width, width, width);
154 }
155
156 } // unnamed namespace
157
158 /**
159  * This example shows how to use ItemView UI control.
160  * There are three layouts created for ItemView, i.e., Spiral, Depth and Grid.
161  * There is one button in the upper-left corner for quitting the application and
162  * another button in the upper-right corner for switching between different layouts.
163  */
164 class ItemViewExample : public ConnectionTracker, public ItemFactory
165 {
166 public:
167
168   enum Mode
169   {
170     MODE_NORMAL,
171     MODE_REMOVE,
172     MODE_REMOVE_MANY,
173     MODE_INSERT,
174     MODE_INSERT_MANY,
175     MODE_REPLACE,
176     MODE_REPLACE_MANY,
177     MODE_LAST
178   };
179
180   /**
181    * Constructor
182    * @param application class, stored as reference
183    */
184   ItemViewExample( Application& application )
185   : mApplication( application ),
186     mMode( MODE_NORMAL ),
187     mOrientation( 0 ),
188     mCurrentLayout( SPIRAL_LAYOUT ),
189     mDurationSeconds( 0.25f )
190   {
191     // Connect to the Application's Init signal
192     mApplication.InitSignal().Connect(this, &ItemViewExample::OnInit);
193   }
194
195   /**
196    * This method gets called once the main loop of application is up and running
197    */
198   void OnInit(Application& app)
199   {
200     Stage stage = Dali::Stage::GetCurrent();
201     stage.KeyEventSignal().Connect(this, &ItemViewExample::OnKeyEvent);
202     stage.GetRootLayer().SetBehavior(Layer::LAYER_3D);
203
204     Vector2 stageSize = Stage::GetCurrent().GetSize();
205
206     // Creates a default view with a default tool bar.
207     // The view is added to the stage.
208
209     Layer contents = DemoHelper::CreateView( mApplication,
210                                              mView,
211                                              mToolBar,
212                                              BACKGROUND_IMAGE,
213                                              TOOLBAR_IMAGE,
214                                              "" );
215
216     // Create an edit mode button. (left of toolbar)
217     Toolkit::PushButton editButton = Toolkit::PushButton::New();
218     editButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, EDIT_IMAGE );
219     editButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, EDIT_IMAGE_SELECTED );
220     editButton.ClickedSignal().Connect( this, &ItemViewExample::OnModeButtonClicked);
221     editButton.SetLeaveRequired( true );
222     mToolBar.AddControl( editButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalLeft, DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
223
224     // Create a layout toggle button. (right of toolbar)
225     mLayoutButton = Toolkit::PushButton::New();
226     mLayoutButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, SPIRAL_LAYOUT_IMAGE );
227     mLayoutButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, SPIRAL_LAYOUT_IMAGE_SELECTED );
228     mLayoutButton.ClickedSignal().Connect( this, &ItemViewExample::OnLayoutButtonClicked);
229     mLayoutButton.SetLeaveRequired( true );
230     mToolBar.AddControl( mLayoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
231
232     // Create a delete button (bottom right of screen)
233     mDeleteButton = Toolkit::PushButton::New();
234     mDeleteButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
235     mDeleteButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
236     mDeleteButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
237     mDeleteButton.SetDrawMode( DrawMode::OVERLAY_2D );
238     mDeleteButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, DELETE_IMAGE );
239     mDeleteButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, DELETE_IMAGE_SELECTED );
240     mDeleteButton.SetProperty( Toolkit::Control::Property::BACKGROUND, TOOLBAR_IMAGE );
241     mDeleteButton.SetSize( Vector2( stageSize.width * 0.15f, stageSize.width * 0.15f ) );
242     mDeleteButton.ClickedSignal().Connect( this, &ItemViewExample::OnDeleteButtonClicked);
243     mDeleteButton.SetLeaveRequired( true );
244     mDeleteButton.SetVisible( false );
245     stage.Add( mDeleteButton );
246
247     // Create an insert button (bottom right of screen)
248     mInsertButton = Toolkit::PushButton::New();
249     mInsertButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
250     mInsertButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
251     mInsertButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
252     mInsertButton.SetDrawMode( DrawMode::OVERLAY_2D );
253     mInsertButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, INSERT_IMAGE );
254     mInsertButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, INSERT_IMAGE_SELECTED );
255     mInsertButton.SetProperty( Toolkit::Control::Property::BACKGROUND, TOOLBAR_IMAGE );
256     mInsertButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
257     mInsertButton.ClickedSignal().Connect( this, &ItemViewExample::OnInsertButtonClicked);
258     mInsertButton.SetLeaveRequired( true );
259     mInsertButton.SetVisible( false );
260     stage.Add( mInsertButton );
261
262     // Create an replace button (bottom right of screen)
263     mReplaceButton = Toolkit::PushButton::New();
264     mReplaceButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
265     mReplaceButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
266     mReplaceButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
267     mReplaceButton.SetDrawMode( DrawMode::OVERLAY_2D );
268     mReplaceButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, REPLACE_IMAGE );
269     mReplaceButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, REPLACE_IMAGE_SELECTED );
270     mReplaceButton.SetProperty( Toolkit::Control::Property::BACKGROUND, TOOLBAR_IMAGE );
271     mReplaceButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
272     mReplaceButton.ClickedSignal().Connect( this, &ItemViewExample::OnReplaceButtonClicked);
273     mReplaceButton.SetLeaveRequired( true );
274     mReplaceButton.SetVisible( false );
275     stage.Add( mReplaceButton );
276
277     // Create the item view actor
278     mItemView = ItemView::New(*this);
279     mItemView.SetParentOrigin(ParentOrigin::CENTER);
280     mItemView.SetAnchorPoint(AnchorPoint::CENTER);
281
282     // Display item view on the stage
283     stage.Add( mItemView );
284     stage.GetRootLayer().SetBehavior( Layer::LAYER_3D );
285
286     // Create the layouts
287     mSpiralLayout = DefaultItemLayout::New( DefaultItemLayout::SPIRAL );
288     mDepthLayout = DefaultItemLayout::New( DefaultItemLayout::DEPTH );
289     mGridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
290
291     // Add the layouts to item view
292     mItemView.AddLayout(*mSpiralLayout);
293     mItemView.AddLayout(*mDepthLayout);
294     mItemView.AddLayout(*mGridLayout);
295
296     mItemView.SetMinimumSwipeDistance(MIN_SWIPE_DISTANCE);
297     mItemView.SetMinimumSwipeSpeed(MIN_SWIPE_SPEED);
298
299     // Activate the spiral layout
300     SetLayout( mCurrentLayout );
301     mItemView.SetKeyboardFocusable( true );
302     KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &ItemViewExample::OnKeyboardPreFocusChange );
303
304     // Set the title and icon to the current layout
305     SetLayoutTitle();
306     SetLayoutImage();
307
308     mLongPressDetector = LongPressGestureDetector::New();
309     mLongPressDetector.Attach( mItemView );
310     mLongPressDetector.DetectedSignal().Connect( this, &ItemViewExample::OnLongPress );
311   }
312
313   Actor OnKeyboardPreFocusChange( Actor current, Actor proposed, Control::KeyboardFocus::Direction direction )
314   {
315     if ( !current && !proposed  )
316     {
317       return mItemView;
318     }
319
320     return proposed;
321   }
322
323   /**
324    * Animate to a different layout
325    */
326   void ChangeLayout()
327   {
328     Animation animation = Animation::New( mDurationSeconds );
329     animation.FinishedSignal().Connect( this, &ItemViewExample::AnimationFinished );
330     animation.AnimateTo( Property( mItemView, Actor::Property::COLOR_ALPHA ), 0.0f );
331     animation.Play();
332   }
333
334   void AnimationFinished( Animation& )
335   {
336     SetLayout( mCurrentLayout );
337
338     Animation animation = Animation::New( mDurationSeconds );
339     animation.AnimateTo( Property( mItemView, Actor::Property::COLOR_ALPHA ), 1.0f );
340     animation.Play();
341   }
342
343   /**
344    * Switch to a different item view layout
345    */
346   void SetLayout( int layoutId )
347   {
348     Stage stage = Dali::Stage::GetCurrent();
349     switch( mCurrentLayout )
350     {
351       case SPIRAL_LAYOUT:
352       case DEPTH_LAYOUT:
353       {
354         stage.GetRootLayer().SetBehavior(Layer::LAYER_3D);
355         break;
356       }
357       case GRID_LAYOUT:
358       {
359         stage.GetRootLayer().SetBehavior(Layer::LAYER_2D);
360         break;
361       }
362     }
363
364     // Set the new orientation to the layout
365     mItemView.GetLayout(layoutId)->SetOrientation(static_cast<ControlOrientation::Type>(mOrientation / 90));
366
367     Vector2 stageSize = Stage::GetCurrent().GetSize();
368
369     if(layoutId == DEPTH_LAYOUT)
370     {
371       // Set up the depth layout according to the new orientation
372       if(Toolkit::IsVertical(mDepthLayout->GetOrientation()))
373       {
374         mDepthLayout->SetItemSize( DepthLayoutItemSizeFunctionPortrait( stageSize.width ) );
375       }
376       else
377       {
378         mDepthLayout->SetItemSize( DepthLayoutItemSizeFunctionLandscape( stageSize.height ) );
379       }
380     }
381
382     // Enable anchoring for depth layout only
383     mItemView.SetAnchoring(layoutId == DEPTH_LAYOUT);
384
385     // Activate the layout
386     mItemView.ActivateLayout( layoutId, Vector3(stageSize.x, stageSize.y, stageSize.x), 0.0f );
387   }
388
389   bool OnLayoutButtonClicked( Toolkit::Button button )
390   {
391     // Switch to the next layout
392     mCurrentLayout = (mCurrentLayout + 1) % mItemView.GetLayoutCount();
393
394     ChangeLayout();
395
396     SetLayoutTitle();
397     SetLayoutImage();
398
399     return true;
400   }
401
402   bool OnModeButtonClicked( Toolkit::Button button )
403   {
404     SwitchToNextMode();
405
406     return true;
407   }
408
409   void SwitchToNextMode()
410   {
411     switch( mMode )
412     {
413       case MODE_REMOVE:
414       {
415         ExitRemoveMode();
416         mMode = MODE_REMOVE_MANY;
417         EnterRemoveManyMode();
418         break;
419       }
420
421       case MODE_REMOVE_MANY:
422       {
423         ExitRemoveManyMode();
424         mMode = MODE_INSERT;
425         EnterInsertMode();
426         break;
427       }
428
429       case MODE_INSERT:
430       {
431         ExitInsertMode();
432         mMode = MODE_INSERT_MANY;
433         EnterInsertManyMode();
434         break;
435       }
436
437       case MODE_INSERT_MANY:
438       {
439         ExitInsertManyMode();
440         mMode = MODE_REPLACE;
441         EnterReplaceMode();
442         break;
443       }
444
445       case MODE_REPLACE:
446       {
447         ExitReplaceMode();
448         mMode = MODE_REPLACE_MANY;
449         EnterReplaceManyMode();
450         break;
451       }
452
453       case MODE_REPLACE_MANY:
454       {
455         ExitReplaceManyMode();
456         mMode = MODE_NORMAL;
457         SetLayoutTitle();
458         break;
459       }
460
461       case MODE_NORMAL:
462       default:
463       {
464         mMode = MODE_REMOVE;
465         EnterRemoveMode();
466         break;
467       }
468     }
469   }
470
471   void EnterRemoveMode()
472   {
473     SetTitle("Edit: Remove");
474
475     mTapDetector = TapGestureDetector::New();
476
477     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
478     {
479       mTapDetector.Attach(mItemView.GetChildAt(i));
480     }
481
482     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::RemoveOnTap );
483   }
484
485   void ExitRemoveMode()
486   {
487     mTapDetector.Reset();
488   }
489
490   void RemoveOnTap( Actor actor, const TapGesture& tap )
491   {
492     mItemView.RemoveItem( mItemView.GetItemId(actor), 0.5f );
493   }
494
495   void EnterRemoveManyMode()
496   {
497     SetTitle("Edit: Remove Many");
498
499     mDeleteButton.SetVisible( true );
500
501     mTapDetector = TapGestureDetector::New();
502
503     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
504     {
505       Actor child = mItemView.GetChildAt( i );
506       Actor box = child.FindChildByName( "CheckBox" );
507
508       if( box )
509       {
510         mTapDetector.Attach( child );
511         box.SetVisible( true );
512       }
513     }
514
515     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
516   }
517
518   void ExitRemoveManyMode()
519   {
520     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
521     {
522       Actor child = mItemView.GetChildAt( i );
523       Actor box = child.FindChildByName( "CheckBox" );
524
525       if( box )
526       {
527         box.SetVisible( false );
528
529         Actor tick = box.FindChildByName( "Tick" );
530         if( tick )
531         {
532           tick.SetVisible( false );
533         }
534       }
535     }
536
537     mTapDetector.Reset();
538
539     mDeleteButton.SetVisible( false );
540   }
541
542   void SelectOnTap( Actor actor, const TapGesture& tap )
543   {
544     Actor tick = actor.FindChildByName( "Tick" );
545     if( tick )
546     {
547       tick.SetVisible( !tick.IsVisible() );
548     }
549   }
550
551   void OnLongPress( Actor actor, const LongPressGesture& gesture )
552   {
553     switch( gesture.state )
554     {
555       case Gesture::Started:
556       {
557         const Size& size = Stage::GetCurrent().GetSize();
558
559         ItemRange range( 0u, 0u );
560         mItemView.GetItemsRange( range );
561
562         const unsigned int item = ( gesture.screenPoint.y < 0.5f * size.height ) ? range.begin : range.end;
563         mItemView.ScrollToItem( item, SCROLL_TO_ITEM_ANIMATION_TIME );
564
565         break;
566       }
567       case Gesture::Finished:
568       {
569         Property::Map attributes;
570         mItemView.DoAction( "stopScrolling", attributes );
571         break;
572       }
573       default:
574       {
575         break;
576       }
577     }
578   }
579
580   bool OnDeleteButtonClicked( Toolkit::Button button )
581   {
582     ItemIdContainer removeList;
583
584     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
585     {
586       Actor child = mItemView.GetChildAt( i );
587       Actor tick = child.FindChildByName( "Tick" );
588
589       if( tick && tick.IsVisible() )
590       {
591         removeList.push_back( mItemView.GetItemId(child) );
592       }
593     }
594
595     if( ! removeList.empty() )
596     {
597       mItemView.RemoveItems( removeList, 0.5f );
598     }
599
600     return true;
601   }
602
603   void EnterInsertMode()
604   {
605     SetTitle("Edit: Insert");
606
607     mTapDetector = TapGestureDetector::New();
608
609     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
610     {
611       mTapDetector.Attach( mItemView.GetChildAt(i) );
612     }
613
614     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::InsertOnTap );
615   }
616
617   void ExitInsertMode()
618   {
619     mTapDetector.Reset();
620   }
621
622   void InsertOnTap( Actor actor, const TapGesture& tap )
623   {
624     ItemId id = mItemView.GetItemId( actor );
625
626     Actor newActor = NewItem( rand() );
627
628     mItemView.InsertItem( Item(id,newActor), 0.5f );
629   }
630
631   void EnterInsertManyMode()
632   {
633     SetTitle("Edit: Insert Many");
634
635     mInsertButton.SetVisible( true );
636
637     mTapDetector = TapGestureDetector::New();
638
639     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
640     {
641       Actor child = mItemView.GetChildAt( i );
642       Actor box = child.FindChildByName( "CheckBox" );
643
644       if( box )
645       {
646         mTapDetector.Attach( child );
647         box.SetVisible( true );
648       }
649     }
650
651     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
652   }
653
654   void ExitInsertManyMode()
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         box.SetVisible( false );
664
665         Actor tick = box.FindChildByName( "Tick" );
666         if( tick )
667         {
668           tick.SetVisible( false );
669         }
670       }
671     }
672
673     mTapDetector.Reset();
674
675     mInsertButton.SetVisible( false );
676   }
677
678   bool OnInsertButtonClicked( Toolkit::Button button )
679   {
680     ItemContainer insertList;
681
682     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
683     {
684       Actor child = mItemView.GetChildAt( i );
685       Actor tick = child.FindChildByName( "Tick" );
686
687       if( tick && tick.IsVisible() )
688       {
689         insertList.push_back( Item( mItemView.GetItemId(child), NewItem(rand()) ) );
690       }
691     }
692
693     if( ! insertList.empty() )
694     {
695       mItemView.InsertItems( insertList, 0.5f );
696     }
697
698     return true;
699   }
700
701   void EnterReplaceMode()
702   {
703     SetTitle("Edit: Replace");
704
705     mTapDetector = TapGestureDetector::New();
706
707     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
708     {
709       mTapDetector.Attach(mItemView.GetChildAt(i));
710     }
711
712     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::ReplaceOnTap );
713   }
714
715   void ReplaceOnTap( Actor actor, const TapGesture& tap )
716   {
717     mItemView.ReplaceItem( Item( mItemView.GetItemId(actor), NewItem(rand()) ), 0.5f );
718   }
719
720   void ExitReplaceMode()
721   {
722     mTapDetector.Reset();
723   }
724
725   void EnterReplaceManyMode()
726   {
727     SetTitle("Edit: Replace Many");
728
729     mReplaceButton.SetVisible( true );
730
731     mTapDetector = TapGestureDetector::New();
732
733     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
734     {
735       Actor child = mItemView.GetChildAt( i );
736       Actor box = child.FindChildByName( "CheckBox" );
737
738       if( box )
739       {
740         mTapDetector.Attach( child );
741         box.SetVisible( true );
742       }
743     }
744
745     mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
746   }
747
748   void ExitReplaceManyMode()
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         box.SetVisible( false );
758
759         Actor tick = box.FindChildByName( "Tick" );
760         if( tick )
761         {
762           tick.SetVisible( false );
763         }
764       }
765     }
766
767     mTapDetector.Reset();
768
769     mReplaceButton.SetVisible( false );
770   }
771
772   bool OnReplaceButtonClicked( Toolkit::Button button )
773   {
774     ItemContainer replaceList;
775
776     for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
777     {
778       Actor child = mItemView.GetChildAt( i );
779       Actor tick = child.FindChildByName( "Tick" );
780
781       if( tick && tick.IsVisible() )
782       {
783         replaceList.push_back( Item( mItemView.GetItemId(child), NewItem(rand()) ) );
784       }
785     }
786
787     if( ! replaceList.empty() )
788     {
789       mItemView.ReplaceItems( replaceList, 0.5f );
790     }
791
792     return true;
793   }
794
795   void SetLayoutTitle()
796   {
797     if( MODE_NORMAL == mMode )
798     {
799       std::stringstream ss(APPLICATION_TITLE);
800       switch(mCurrentLayout)
801       {
802       case SPIRAL_LAYOUT:
803         ss << APPLICATION_TITLE << ": " << SPIRAL_LABEL;
804         break;
805       case GRID_LAYOUT:
806         ss << APPLICATION_TITLE << ": " << GRID_LABEL;
807         break;
808       case DEPTH_LAYOUT:
809         ss << APPLICATION_TITLE << ": " << DEPTH_LABEL;
810         break;
811       default:
812         break;
813       }
814       SetTitle(ss.str());
815     }
816   }
817
818   void SetLayoutImage()
819   {
820     if( mLayoutButton )
821     {
822       switch( mCurrentLayout )
823       {
824         case SPIRAL_LAYOUT:
825         {
826           mLayoutButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, SPIRAL_LAYOUT_IMAGE );
827           mLayoutButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, SPIRAL_LAYOUT_IMAGE_SELECTED );
828           break;
829         }
830
831         case GRID_LAYOUT:
832         {
833           mLayoutButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, GRID_LAYOUT_IMAGE );
834           mLayoutButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, GRID_LAYOUT_IMAGE_SELECTED );
835           break;
836         }
837
838         case DEPTH_LAYOUT:
839         {
840           mLayoutButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, DEPTH_LAYOUT_IMAGE );
841           mLayoutButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, DEPTH_LAYOUT_IMAGE_SELECTED );
842           break;
843         }
844
845         default:
846           break;
847       }
848     }
849   }
850
851 public: // From ItemFactory
852
853   /**
854    * Query the number of items available from the factory.
855    * The maximum available item has an ID of GetNumberOfItems() - 1.
856    */
857   virtual unsigned int GetNumberOfItems()
858   {
859     return NUM_IMAGES * 10;
860   }
861
862   /**
863    * Create an Actor to represent a visible item.
864    * @param itemId
865    * @return the created actor.
866    */
867   virtual Actor NewItem(unsigned int itemId)
868   {
869     // Create an image view for this item
870     Property::Map propertyMap;
871     propertyMap.Insert(Toolkit::Visual::Property::TYPE,  Visual::IMAGE);
872     propertyMap.Insert(ImageVisual::Property::URL, IMAGE_PATHS[ itemId % NUM_IMAGES ] );
873     propertyMap.Insert(DevelVisual::Property::FITTING_MODE, DevelVisual::FILL);
874     ImageView actor = ImageView::New();
875     actor.SetProperty( Toolkit::ImageView::Property::IMAGE, propertyMap );
876     actor.SetZ( 0.0f );
877     actor.SetPosition( INITIAL_OFFSCREEN_POSITION );
878
879     // Add a border image child actor
880     ImageView borderActor = ImageView::New();
881     borderActor.SetParentOrigin( ParentOrigin::CENTER );
882     borderActor.SetAnchorPoint( AnchorPoint::CENTER );
883     borderActor.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::ALL_DIMENSIONS );
884     borderActor.SetSizeModeFactor( Vector3( 2.0f * ITEM_BORDER_SIZE, 2.0f * ITEM_BORDER_SIZE, 0.0f ) );
885     borderActor.SetColorMode( USE_PARENT_COLOR );
886
887     Property::Map borderProperty;
888     borderProperty.Insert( Toolkit::Visual::Property::TYPE, Visual::BORDER );
889     borderProperty.Insert( BorderVisual::Property::COLOR, Color::WHITE );
890     borderProperty.Insert( BorderVisual::Property::SIZE, ITEM_BORDER_SIZE );
891     borderProperty.Insert( BorderVisual::Property::ANTI_ALIASING, true );
892     borderActor.SetProperty( ImageView::Property::IMAGE, borderProperty );
893
894     actor.Add(borderActor);
895
896     actor.SetKeyboardFocusable( true );
897
898     Vector3 spiralItemSize;
899     static_cast<ItemLayout&>(*mSpiralLayout).GetItemSize( 0u, Vector3( Stage::GetCurrent().GetSize() ), spiralItemSize );
900
901     // Add a checkbox child actor; invisible until edit-mode is enabled
902     ImageView checkbox = ImageView::New();
903     checkbox.SetName( "CheckBox" );
904     checkbox.SetColorMode( USE_PARENT_COLOR );
905     checkbox.SetParentOrigin( ParentOrigin::TOP_RIGHT );
906     checkbox.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
907     checkbox.SetSize( spiralItemSize.width * 0.2f, spiralItemSize.width * 0.2f );
908     checkbox.SetPosition( -SELECTION_BORDER_WIDTH, SELECTION_BORDER_WIDTH );
909     checkbox.SetZ( 0.1f );
910
911     Property::Map solidColorProperty;
912     solidColorProperty.Insert( Toolkit::Visual::Property::TYPE, Visual::COLOR );
913     solidColorProperty.Insert( ColorVisual::Property::MIX_COLOR, Vector4(0.f, 0.f, 0.f, 0.6f) );
914     checkbox.SetProperty( ImageView::Property::IMAGE, solidColorProperty );
915
916     if( MODE_REMOVE_MANY  != mMode &&
917         MODE_INSERT_MANY  != mMode &&
918         MODE_REPLACE_MANY != mMode )
919     {
920       checkbox.SetVisible( false );
921     }
922     borderActor.Add( checkbox );
923
924     ImageView tick = ImageView::New( SELECTED_IMAGE );
925     tick.SetName( "Tick" );
926     tick.SetColorMode( USE_PARENT_COLOR );
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( 0.2f );
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 = DemoHelper::CreateToolBarLabel( "" );
954       // Add title to the tool bar.
955       mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HorizontalCenter );
956     }
957
958     mTitleActor.SetProperty( TextLabel::Property::TEXT, title );
959   }
960
961   /**
962    * Main key event handler
963    */
964   void OnKeyEvent(const KeyEvent& event)
965   {
966     if(event.state == KeyEvent::Down)
967     {
968       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
969       {
970         mApplication.Quit();
971       }
972     }
973   }
974
975 private:
976
977   Application& mApplication;
978   Mode mMode;
979
980   Toolkit::Control mView;
981   unsigned int mOrientation;
982
983   Toolkit::ToolBar mToolBar;
984   TextLabel mTitleActor;             ///< The Toolbar's Title.
985
986   ItemView mItemView;
987   unsigned int mCurrentLayout;
988   float mDurationSeconds;
989
990   ItemLayoutPtr mSpiralLayout;
991   ItemLayoutPtr mDepthLayout;
992   ItemLayoutPtr mGridLayout;
993
994   TapGestureDetector mTapDetector;
995   Toolkit::PushButton mLayoutButton;
996   Toolkit::PushButton mDeleteButton;
997   Toolkit::PushButton mInsertButton;
998   Toolkit::PushButton mReplaceButton;
999
1000   LongPressGestureDetector mLongPressDetector;
1001 };
1002
1003 int DALI_EXPORT_API main(int argc, char **argv)
1004 {
1005   Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
1006   ItemViewExample test(app);
1007   app.MainLoop();
1008   return 0;
1009 }