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