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