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