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