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