2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <cstdlib> // rand
24 #include "shared/view.h"
26 #include <dali/dali.h>
27 #include <dali-toolkit/dali-toolkit.h>
28 #include <dali/devel-api/images/atlas.h>
31 using namespace Dali::Toolkit;
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",
101 const unsigned int NUM_IMAGES = sizeof(IMAGE_PATHS) / sizeof(char*);
103 const unsigned int IMAGE_WIDTH = 256;
104 const unsigned int IMAGE_HEIGHT = 256;
105 const unsigned int NUM_IMAGE_PER_ROW_IN_ATLAS = 8;
107 const char* BACKGROUND_IMAGE( "" );
108 const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
109 const char* EDIT_IMAGE( DALI_IMAGE_DIR "icon-edit.png" );
110 const char* EDIT_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-edit-selected.png" );
111 const char* SPIRAL_LAYOUT_IMAGE( DALI_IMAGE_DIR "icon-item-view-layout-spiral.png" );
112 const char* SPIRAL_LAYOUT_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-item-view-layout-spiral-selected.png" );
113 const char* GRID_LAYOUT_IMAGE( DALI_IMAGE_DIR "icon-item-view-layout-grid.png" );
114 const char* GRID_LAYOUT_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-item-view-layout-grid-selected.png" );
115 const char* DEPTH_LAYOUT_IMAGE( DALI_IMAGE_DIR "icon-item-view-layout-depth.png" );
116 const char* DEPTH_LAYOUT_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-item-view-layout-depth-selected.png" );
117 const char* DELETE_IMAGE( DALI_IMAGE_DIR "icon-delete.png" );
118 const char* DELETE_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-delete-selected.png" );
119 const char* REPLACE_IMAGE( DALI_IMAGE_DIR "icon-replace.png" );
120 const char* REPLACE_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-replace-selected.png" );
121 const char* INSERT_IMAGE( DALI_IMAGE_DIR "icon-insert.png" );
122 const char* INSERT_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-insert-selected.png" );
123 const char* SELECTED_IMAGE( DALI_IMAGE_DIR "item-select-check.png" );
124 const char* APPLICATION_TITLE( "ItemView" );
126 const char* SPIRAL_LABEL("Spiral");
127 const char* GRID_LABEL("Grid");
128 const char* DEPTH_LABEL("Depth");
130 const char* ITEM_BORDER_IMAGE_PATH( DALI_IMAGE_DIR "frame-128x128.png" );
131 const Vector3 ITEM_BORDER_MARGIN_SIZE(24, 24, 0);
133 // These values depend on the border image
134 const float ITEM_IMAGE_BORDER_LEFT = 13.0f;
135 const float ITEM_IMAGE_BORDER_RIGHT = 13.0f;
136 const float ITEM_IMAGE_BORDER_TOP = 13.0f;
137 const float ITEM_IMAGE_BORDER_BOTTOM = 13.0f;
139 const float DEPTH_LAYOUT_ITEM_SIZE_FACTOR_PORTRAIT = 1.0f;
140 const float DEPTH_LAYOUT_ITEM_SIZE_FACTOR_LANDSCAPE = 0.8f;
141 const float DEPTH_LAYOUT_COLUMNS = 3.0f;
143 const float MIN_SWIPE_DISTANCE = 15.0f;
144 const float MIN_SWIPE_SPEED = 5.0f;
146 const float SELECTION_BORDER_WIDTH = 3.0f;
147 const float BUTTON_BORDER = -10.0f;
148 const float MENU_OPTION_HEIGHT(140.0f);
149 const float LABEL_TEXT_SIZE_Y = 20.0f;
151 const Vector3 INITIAL_OFFSCREEN_POSITION( 1000.0f, 0, -1000.0f );
153 static Vector3 DepthLayoutItemSizeFunctionPortrait( float layoutWidth )
155 float width = ( layoutWidth / ( DEPTH_LAYOUT_COLUMNS + 1.0f ) ) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_PORTRAIT;
158 return Vector3(width, width, width);
161 static Vector3 DepthLayoutItemSizeFunctionLandscape( float layoutWidth )
163 float width = ( layoutWidth / ( DEPTH_LAYOUT_COLUMNS + 1.0f ) ) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_LANDSCAPE;
166 return Vector3(width, width, width);
169 } // unnamed namespace
172 * This example shows how to use ItemView UI control.
173 * There are three layouts created for ItemView, i.e., Spiral, Depth and Grid.
174 * There is one button in the upper-left corner for quitting the application and
175 * another button in the upper-right corner for switching between different layouts.
177 class ItemViewExample : public ConnectionTracker, public ItemFactory
195 * @param application class, stored as reference
197 ItemViewExample( Application& application )
198 : mApplication( application ),
199 mMode( MODE_NORMAL ),
201 mCurrentLayout( SPIRAL_LAYOUT ),
202 mDurationSeconds( 0.25f )
204 // Connect to the Application's Init signal
205 mApplication.InitSignal().Connect(this, &ItemViewExample::OnInit);
209 * This method gets called once the main loop of application is up and running
211 void OnInit(Application& app)
213 Stage stage = Dali::Stage::GetCurrent();
214 stage.KeyEventSignal().Connect(this, &ItemViewExample::OnKeyEvent);
216 Vector2 stageSize = Stage::GetCurrent().GetSize();
218 // Create a border image shared by all the item actors
219 mBorderImage = ResourceImage::New(ITEM_BORDER_IMAGE_PATH);
221 // Creates a default view with a default tool bar.
222 // The view is added to the stage.
223 Layer contents = DemoHelper::CreateView( mApplication,
230 //app.GetWindow().GetOrientation().ChangedSignal().Connect( this, &ItemViewExample::OnOrientationChanged );
232 // Create an edit mode button. (left of toolbar)
233 Toolkit::PushButton editButton = Toolkit::PushButton::New();
234 editButton.SetButtonImage( ResourceImage::New( EDIT_IMAGE ) );
235 editButton.SetSelectedImage( ResourceImage::New( EDIT_IMAGE_SELECTED ) );
236 editButton.ClickedSignal().Connect( this, &ItemViewExample::OnModeButtonClicked);
237 editButton.SetLeaveRequired( true );
238 mToolBar.AddControl( editButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalLeft, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
240 // Create a layout toggle button. (right of toolbar)
241 mLayoutButton = Toolkit::PushButton::New();
242 mLayoutButton.SetButtonImage( ResourceImage::New( SPIRAL_LAYOUT_IMAGE ) );
243 mLayoutButton.SetSelectedImage( ResourceImage::New( SPIRAL_LAYOUT_IMAGE_SELECTED ) );
244 mLayoutButton.ClickedSignal().Connect( this, &ItemViewExample::OnLayoutButtonClicked);
245 mLayoutButton.SetLeaveRequired( true );
246 mToolBar.AddControl( mLayoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
248 // Create a delete button (bottom right of screen)
249 mDeleteButton = Toolkit::PushButton::New();
250 mDeleteButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
251 mDeleteButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
252 mDeleteButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
253 mDeleteButton.SetDrawMode( DrawMode::OVERLAY );
254 mDeleteButton.SetButtonImage( ResourceImage::New( DELETE_IMAGE ) );
255 mDeleteButton.SetSelectedImage( ResourceImage::New( DELETE_IMAGE_SELECTED ) );
256 mDeleteButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_IMAGE ) );
257 mDeleteButton.SetSize( Vector2( stageSize.width * 0.15f, stageSize.width * 0.15f ) );
258 mDeleteButton.ClickedSignal().Connect( this, &ItemViewExample::OnDeleteButtonClicked);
259 mDeleteButton.SetLeaveRequired( true );
260 mDeleteButton.SetVisible( false );
261 stage.Add( mDeleteButton );
263 // Create an insert button (bottom right of screen)
264 mInsertButton = Toolkit::PushButton::New();
265 mInsertButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
266 mInsertButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
267 mInsertButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
268 mInsertButton.SetDrawMode( DrawMode::OVERLAY );
269 mInsertButton.SetButtonImage( ResourceImage::New( INSERT_IMAGE ) );
270 mInsertButton.SetSelectedImage( ResourceImage::New( INSERT_IMAGE_SELECTED ) );
271 mInsertButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_IMAGE ) );
272 mInsertButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
273 mInsertButton.ClickedSignal().Connect( this, &ItemViewExample::OnInsertButtonClicked);
274 mInsertButton.SetLeaveRequired( true );
275 mInsertButton.SetVisible( false );
276 stage.Add( mInsertButton );
278 // Create an replace button (bottom right of screen)
279 mReplaceButton = Toolkit::PushButton::New();
280 mReplaceButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
281 mReplaceButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
282 mReplaceButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
283 mReplaceButton.SetDrawMode( DrawMode::OVERLAY );
284 mReplaceButton.SetButtonImage( ResourceImage::New( REPLACE_IMAGE ) );
285 mReplaceButton.SetSelectedImage( ResourceImage::New( REPLACE_IMAGE_SELECTED ) );
286 mReplaceButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_IMAGE ) );
287 mReplaceButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
288 mReplaceButton.ClickedSignal().Connect( this, &ItemViewExample::OnReplaceButtonClicked);
289 mReplaceButton.SetLeaveRequired( true );
290 mReplaceButton.SetVisible( false );
291 stage.Add( mReplaceButton );
293 // Create the item view actor
294 mImageAtlas = CreateImageAtlas();
295 mItemView = ItemView::New(*this);
296 mItemView.SetParentOrigin(ParentOrigin::CENTER);
297 mItemView.SetAnchorPoint(AnchorPoint::CENTER);
299 // Display item view on the stage
300 stage.Add( mItemView );
302 // Create the layouts
303 mSpiralLayout = DefaultItemLayout::New( DefaultItemLayout::SPIRAL );
304 mDepthLayout = DefaultItemLayout::New( DefaultItemLayout::DEPTH );
305 mGridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
307 // Add the layouts to item view
308 mItemView.AddLayout(*mSpiralLayout);
309 mItemView.AddLayout(*mDepthLayout);
310 mItemView.AddLayout(*mGridLayout);
312 mItemView.SetMinimumSwipeDistance(MIN_SWIPE_DISTANCE);
313 mItemView.SetMinimumSwipeSpeed(MIN_SWIPE_SPEED);
315 // Activate the spiral layout
316 SetLayout( mCurrentLayout );
317 mItemView.SetKeyboardFocusable( true );
318 KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &ItemViewExample::OnKeyboardPreFocusChange );
320 // Set the title and icon to the current layout
324 // Store one 1x1 white image for multiple items to share for backgrounds:
325 mWhiteImage = BufferImage::WHITE();
328 Actor OnKeyboardPreFocusChange( Actor current, Actor proposed, Control::KeyboardFocus::Direction direction )
330 if ( !current && !proposed )
339 * Animate to a different layout
343 Animation animation = Animation::New( mDurationSeconds );
344 animation.FinishedSignal().Connect( this, &ItemViewExample::AnimationFinished );
345 animation.AnimateTo( Property( mItemView, Actor::Property::COLOR_ALPHA ), 0.0f );
349 void AnimationFinished( Animation& )
351 SetLayout( mCurrentLayout );
353 Animation animation = Animation::New( mDurationSeconds );
354 animation.AnimateTo( Property( mItemView, Actor::Property::COLOR_ALPHA ), 1.0f );
359 * Switch to a different item view layout
361 void SetLayout( int layoutId )
363 // Set the new orientation to the layout
364 mItemView.GetLayout(layoutId)->SetOrientation(static_cast<ControlOrientation::Type>(mOrientation / 90));
366 Vector2 stageSize = Stage::GetCurrent().GetSize();
368 if(layoutId == DEPTH_LAYOUT)
370 // Set up the depth layout according to the new orientation
371 if(Toolkit::IsVertical(mDepthLayout->GetOrientation()))
373 mDepthLayout->SetItemSize( DepthLayoutItemSizeFunctionPortrait( stageSize.width ) );
377 mDepthLayout->SetItemSize( DepthLayoutItemSizeFunctionLandscape( stageSize.height ) );
381 // Enable anchoring for depth layout only
382 mItemView.SetAnchoring(layoutId == DEPTH_LAYOUT);
384 // Activate the layout
385 mItemView.ActivateLayout( layoutId, Vector3(stageSize.x, stageSize.y, stageSize.x), 0.0f );
389 * Orientation changed signal callback
392 void OnOrientationChanged( Orientation orientation )
394 const unsigned int angle = orientation.GetDegrees();
396 // If orientation really changed
397 if( mOrientation != angle )
399 // Remember orientation
400 mOrientation = angle;
402 SetLayout( mCurrentLayout );
406 bool OnLayoutButtonClicked( Toolkit::Button button )
408 // Switch to the next layout
409 mCurrentLayout = (mCurrentLayout + 1) % mItemView.GetLayoutCount();
419 bool OnModeButtonClicked( Toolkit::Button button )
426 void SwitchToNextMode()
433 mMode = MODE_REMOVE_MANY;
434 EnterRemoveManyMode();
438 case MODE_REMOVE_MANY:
440 ExitRemoveManyMode();
449 mMode = MODE_INSERT_MANY;
450 EnterInsertManyMode();
454 case MODE_INSERT_MANY:
456 ExitInsertManyMode();
457 mMode = MODE_REPLACE;
465 mMode = MODE_REPLACE_MANY;
466 EnterReplaceManyMode();
470 case MODE_REPLACE_MANY:
472 ExitReplaceManyMode();
488 void EnterRemoveMode()
490 SetTitle("Edit: Remove");
492 mTapDetector = TapGestureDetector::New();
494 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
496 mTapDetector.Attach(mItemView.GetChildAt(i));
499 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::RemoveOnTap );
502 void ExitRemoveMode()
504 mTapDetector.Reset();
507 void RemoveOnTap( Actor actor, const TapGesture& tap )
509 mItemView.RemoveItem( mItemView.GetItemId(actor), 0.5f );
512 void EnterRemoveManyMode()
514 SetTitle("Edit: Remove Many");
516 mDeleteButton.SetVisible( true );
518 mTapDetector = TapGestureDetector::New();
520 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
522 Actor child = mItemView.GetChildAt( i );
523 Actor box = child.FindChildByName( "CheckBox" );
527 mTapDetector.Attach( child );
528 box.SetVisible( true );
532 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
535 void ExitRemoveManyMode()
537 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
539 Actor child = mItemView.GetChildAt( i );
540 Actor box = child.FindChildByName( "CheckBox" );
544 box.SetVisible( false );
546 Actor tick = box.FindChildByName( "Tick" );
549 tick.SetVisible( false );
554 mTapDetector.Reset();
556 mDeleteButton.SetVisible( false );
559 void SelectOnTap( Actor actor, const TapGesture& tap )
561 Actor tick = actor.FindChildByName( "Tick" );
564 tick.SetVisible( !tick.IsVisible() );
568 bool OnDeleteButtonClicked( Toolkit::Button button )
570 ItemIdContainer removeList;
572 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
574 Actor child = mItemView.GetChildAt( i );
575 Actor tick = child.FindChildByName( "Tick" );
577 if( tick && tick.IsVisible() )
579 removeList.push_back( mItemView.GetItemId(child) );
583 if( ! removeList.empty() )
585 mItemView.RemoveItems( removeList, 0.5f );
591 void EnterInsertMode()
593 SetTitle("Edit: Insert");
595 mTapDetector = TapGestureDetector::New();
597 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
599 mTapDetector.Attach( mItemView.GetChildAt(i) );
602 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::InsertOnTap );
605 void ExitInsertMode()
607 mTapDetector.Reset();
610 void InsertOnTap( Actor actor, const TapGesture& tap )
612 ItemId id = mItemView.GetItemId( actor );
614 Actor newActor = NewItem( rand() );
616 mItemView.InsertItem( Item(id,newActor), 0.5f );
619 void EnterInsertManyMode()
621 SetTitle("Edit: Insert Many");
623 mInsertButton.SetVisible( true );
625 mTapDetector = TapGestureDetector::New();
627 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
629 Actor child = mItemView.GetChildAt( i );
630 Actor box = child.FindChildByName( "CheckBox" );
634 mTapDetector.Attach( child );
635 box.SetVisible( true );
639 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
642 void ExitInsertManyMode()
644 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
646 Actor child = mItemView.GetChildAt( i );
647 Actor box = child.FindChildByName( "CheckBox" );
651 box.SetVisible( false );
653 Actor tick = box.FindChildByName( "Tick" );
656 tick.SetVisible( false );
661 mTapDetector.Reset();
663 mInsertButton.SetVisible( false );
666 bool OnInsertButtonClicked( Toolkit::Button button )
668 ItemContainer insertList;
670 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
672 Actor child = mItemView.GetChildAt( i );
673 Actor tick = child.FindChildByName( "Tick" );
675 if( tick && tick.IsVisible() )
677 insertList.push_back( Item( mItemView.GetItemId(child), NewItem(rand()) ) );
681 if( ! insertList.empty() )
683 mItemView.InsertItems( insertList, 0.5f );
689 void EnterReplaceMode()
691 SetTitle("Edit: Replace");
693 mTapDetector = TapGestureDetector::New();
695 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
697 mTapDetector.Attach(mItemView.GetChildAt(i));
700 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::ReplaceOnTap );
703 void ReplaceOnTap( Actor actor, const TapGesture& tap )
705 mItemView.ReplaceItem( Item( mItemView.GetItemId(actor), NewItem(rand()) ), 0.5f );
708 void ExitReplaceMode()
710 mTapDetector.Reset();
713 void EnterReplaceManyMode()
715 SetTitle("Edit: Replace Many");
717 mReplaceButton.SetVisible( true );
719 mTapDetector = TapGestureDetector::New();
721 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
723 Actor child = mItemView.GetChildAt( i );
724 Actor box = child.FindChildByName( "CheckBox" );
728 mTapDetector.Attach( child );
729 box.SetVisible( true );
733 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
736 void ExitReplaceManyMode()
738 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
740 Actor child = mItemView.GetChildAt( i );
741 Actor box = child.FindChildByName( "CheckBox" );
745 box.SetVisible( false );
747 Actor tick = box.FindChildByName( "Tick" );
750 tick.SetVisible( false );
755 mTapDetector.Reset();
757 mReplaceButton.SetVisible( false );
760 bool OnReplaceButtonClicked( Toolkit::Button button )
762 ItemContainer replaceList;
764 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
766 Actor child = mItemView.GetChildAt( i );
767 Actor tick = child.FindChildByName( "Tick" );
769 if( tick && tick.IsVisible() )
771 replaceList.push_back( Item( mItemView.GetItemId(child), NewItem(rand()) ) );
775 if( ! replaceList.empty() )
777 mItemView.ReplaceItems( replaceList, 0.5f );
783 void SetLayoutTitle()
785 if( MODE_NORMAL == mMode )
787 std::stringstream ss(APPLICATION_TITLE);
788 switch(mCurrentLayout)
791 ss << APPLICATION_TITLE << ": " << SPIRAL_LABEL;
794 ss << APPLICATION_TITLE << ": " << GRID_LABEL;
797 ss << APPLICATION_TITLE << ": " << DEPTH_LABEL;
806 void SetLayoutImage()
810 switch( mCurrentLayout )
814 mLayoutButton.SetButtonImage( ResourceImage::New( SPIRAL_LAYOUT_IMAGE ) );
815 mLayoutButton.SetSelectedImage( ResourceImage::New( SPIRAL_LAYOUT_IMAGE_SELECTED ) );
821 mLayoutButton.SetButtonImage( ResourceImage::New( GRID_LAYOUT_IMAGE ) );
822 mLayoutButton.SetSelectedImage( ResourceImage::New( GRID_LAYOUT_IMAGE_SELECTED ) );
828 mLayoutButton.SetButtonImage( ResourceImage::New( DEPTH_LAYOUT_IMAGE ) );
829 mLayoutButton.SetSelectedImage( ResourceImage::New( DEPTH_LAYOUT_IMAGE_SELECTED ) );
839 public: // From ItemFactory
842 * Query the number of items available from the factory.
843 * The maximum available item has an ID of GetNumberOfItems() - 1.
845 virtual unsigned int GetNumberOfItems()
847 return NUM_IMAGES * 10;
851 * Create an Actor to represent a visible item.
853 * @return the created actor.
855 virtual Actor NewItem(unsigned int itemId)
857 // Create an image actor for this item
858 unsigned int imageId = itemId % NUM_IMAGES;
859 ImageActor::PixelArea pixelArea( (imageId%NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_WIDTH,
860 (imageId/NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_HEIGHT,
863 Actor actor = ImageActor::New(mImageAtlas, pixelArea);
864 actor.SetPosition( INITIAL_OFFSCREEN_POSITION );
866 // Add a border image child actor
867 ImageActor borderActor = ImageActor::New(mBorderImage);
868 borderActor.SetParentOrigin( ParentOrigin::CENTER );
869 borderActor.SetAnchorPoint( AnchorPoint::CENTER );
870 borderActor.SetPosition( 0.f, 0.f, 1.f );
871 borderActor.SetStyle( ImageActor::STYLE_NINE_PATCH );
872 borderActor.SetNinePatchBorder( Vector4( ITEM_IMAGE_BORDER_LEFT, ITEM_IMAGE_BORDER_TOP, ITEM_IMAGE_BORDER_RIGHT, ITEM_IMAGE_BORDER_BOTTOM ) );
873 borderActor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR ); // darken with parent image-actor
874 borderActor.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::ALL_DIMENSIONS );
875 borderActor.SetSizeModeFactor( ITEM_BORDER_MARGIN_SIZE );
876 actor.Add(borderActor);
877 actor.SetKeyboardFocusable( true );
879 Vector3 spiralItemSize;
880 static_cast<ItemLayout&>(*mSpiralLayout).GetItemSize( 0u, Vector3( Stage::GetCurrent().GetSize() ), spiralItemSize );
882 // Add a checkbox child actor; invisible until edit-mode is enabled
884 ImageActor checkbox = ImageActor::New( mWhiteImage );
885 checkbox.SetName( "CheckBox" );
886 checkbox.SetColor( Vector4(0.0f,0.0f,0.0f,0.6f) );
887 checkbox.SetParentOrigin( ParentOrigin::TOP_RIGHT );
888 checkbox.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
889 checkbox.SetSize( spiralItemSize.width * 0.2f, spiralItemSize.width * 0.2f );
890 checkbox.SetPosition( -SELECTION_BORDER_WIDTH, SELECTION_BORDER_WIDTH );
891 checkbox.SetZ( 1.0f );
892 if( MODE_REMOVE_MANY != mMode &&
893 MODE_INSERT_MANY != mMode &&
894 MODE_REPLACE_MANY != mMode )
896 checkbox.SetVisible( false );
898 actor.Add( checkbox );
900 ImageActor tick = ImageActor::New( ResourceImage::New(SELECTED_IMAGE) );
901 tick.SetColorMode( USE_OWN_COLOR );
902 tick.SetName( "Tick" );
903 tick.SetParentOrigin( ParentOrigin::TOP_RIGHT );
904 tick.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
905 tick.SetSize( spiralItemSize.width * 0.2f, spiralItemSize.width * 0.2f );
907 tick.SetVisible( false );
908 checkbox.Add( tick );
910 // Connect new items for various editing modes
913 mTapDetector.Attach( actor );
922 * Create an Atlas to tile the images inside.
924 Atlas CreateImageAtlas()
926 const unsigned int atlas_width = IMAGE_WIDTH*NUM_IMAGE_PER_ROW_IN_ATLAS;
927 const unsigned int atlas_height = IMAGE_HEIGHT*ceil( static_cast<float>(NUM_IMAGES)/ static_cast<float>(NUM_IMAGE_PER_ROW_IN_ATLAS));
928 Atlas atlas = Atlas::New(atlas_width, atlas_height, Pixel::RGB888);
930 for( unsigned int i = 0; i < NUM_IMAGES; i++ )
932 atlas.Upload( IMAGE_PATHS[i], (i%NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_WIDTH, (i/NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_HEIGHT );
939 * Sets/Updates the title of the View
940 * @param[in] title The new title for the view.
942 void SetTitle(const std::string& title)
946 mTitleActor = DemoHelper::CreateToolBarLabel( "" );
947 // Add title to the tool bar.
948 mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HorizontalCenter );
951 mTitleActor.SetProperty( TextLabel::Property::TEXT, title );
955 * Main key event handler
957 void OnKeyEvent(const KeyEvent& event)
959 if(event.state == KeyEvent::Down)
961 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
970 Application& mApplication;
973 Toolkit::Control mView;
974 unsigned int mOrientation;
976 Toolkit::ToolBar mToolBar;
977 TextLabel mTitleActor; ///< The Toolbar's Title.
982 unsigned int mCurrentLayout;
983 float mDurationSeconds;
985 ItemLayoutPtr mSpiralLayout;
986 ItemLayoutPtr mDepthLayout;
987 ItemLayoutPtr mGridLayout;
989 TapGestureDetector mTapDetector;
990 Toolkit::PushButton mLayoutButton;
991 Toolkit::PushButton mDeleteButton;
992 Toolkit::PushButton mInsertButton;
993 Toolkit::PushButton mReplaceButton;
995 BufferImage mWhiteImage;
998 void RunTest(Application& app)
1000 ItemViewExample test(app);
1005 int main(int argc, char **argv)
1007 Application app = Application::New(&argc, &argv, DALI_DEMO_THEME_PATH);