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);
215 stage.GetRootLayer().SetBehavior(Layer::LAYER_3D);
217 Vector2 stageSize = Stage::GetCurrent().GetSize();
219 // Create a border image shared by all the item actors
220 mBorderImage = ResourceImage::New(ITEM_BORDER_IMAGE_PATH);
222 // Creates a default view with a default tool bar.
223 // The view is added to the stage.
224 Layer contents = DemoHelper::CreateView( mApplication,
231 //app.GetWindow().GetOrientation().ChangedSignal().Connect( this, &ItemViewExample::OnOrientationChanged );
233 // Create an edit mode button. (left of toolbar)
234 Toolkit::PushButton editButton = Toolkit::PushButton::New();
235 editButton.SetUnselectedImage( EDIT_IMAGE );
236 editButton.SetSelectedImage( EDIT_IMAGE_SELECTED );
237 editButton.ClickedSignal().Connect( this, &ItemViewExample::OnModeButtonClicked);
238 editButton.SetLeaveRequired( true );
239 mToolBar.AddControl( editButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalLeft, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
241 // Create a layout toggle button. (right of toolbar)
242 mLayoutButton = Toolkit::PushButton::New();
243 mLayoutButton.SetUnselectedImage( SPIRAL_LAYOUT_IMAGE );
244 mLayoutButton.SetSelectedImage(SPIRAL_LAYOUT_IMAGE_SELECTED );
245 mLayoutButton.ClickedSignal().Connect( this, &ItemViewExample::OnLayoutButtonClicked);
246 mLayoutButton.SetLeaveRequired( true );
247 mToolBar.AddControl( mLayoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
249 // Create a delete button (bottom right of screen)
250 mDeleteButton = Toolkit::PushButton::New();
251 mDeleteButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
252 mDeleteButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
253 mDeleteButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
254 mDeleteButton.SetDrawMode( DrawMode::OVERLAY_2D );
255 mDeleteButton.SetUnselectedImage( DELETE_IMAGE );
256 mDeleteButton.SetSelectedImage( DELETE_IMAGE_SELECTED );
257 mDeleteButton.SetBackgroundImage( TOOLBAR_IMAGE );
258 mDeleteButton.SetSize( Vector2( stageSize.width * 0.15f, stageSize.width * 0.15f ) );
259 mDeleteButton.ClickedSignal().Connect( this, &ItemViewExample::OnDeleteButtonClicked);
260 mDeleteButton.SetLeaveRequired( true );
261 mDeleteButton.SetVisible( false );
262 stage.Add( mDeleteButton );
264 // Create an insert button (bottom right of screen)
265 mInsertButton = Toolkit::PushButton::New();
266 mInsertButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
267 mInsertButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
268 mInsertButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
269 mInsertButton.SetDrawMode( DrawMode::OVERLAY_2D );
270 mInsertButton.SetUnselectedImage( INSERT_IMAGE );
271 mInsertButton.SetSelectedImage( INSERT_IMAGE_SELECTED );
272 mInsertButton.SetBackgroundImage( TOOLBAR_IMAGE );
273 mInsertButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
274 mInsertButton.ClickedSignal().Connect( this, &ItemViewExample::OnInsertButtonClicked);
275 mInsertButton.SetLeaveRequired( true );
276 mInsertButton.SetVisible( false );
277 stage.Add( mInsertButton );
279 // Create an replace button (bottom right of screen)
280 mReplaceButton = Toolkit::PushButton::New();
281 mReplaceButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
282 mReplaceButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
283 mReplaceButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
284 mReplaceButton.SetDrawMode( DrawMode::OVERLAY_2D );
285 mReplaceButton.SetUnselectedImage( REPLACE_IMAGE );
286 mReplaceButton.SetSelectedImage( REPLACE_IMAGE_SELECTED );
287 mReplaceButton.SetBackgroundImage( TOOLBAR_IMAGE );
288 mReplaceButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
289 mReplaceButton.ClickedSignal().Connect( this, &ItemViewExample::OnReplaceButtonClicked);
290 mReplaceButton.SetLeaveRequired( true );
291 mReplaceButton.SetVisible( false );
292 stage.Add( mReplaceButton );
294 // Create the item view actor
295 mImageAtlas = CreateImageAtlas();
296 mItemView = ItemView::New(*this);
297 mItemView.SetParentOrigin(ParentOrigin::CENTER);
298 mItemView.SetAnchorPoint(AnchorPoint::CENTER);
300 // Display item view on the stage
301 stage.Add( mItemView );
302 stage.GetRootLayer().SetBehavior( Layer::LAYER_3D );
304 // Create the layouts
305 mSpiralLayout = DefaultItemLayout::New( DefaultItemLayout::SPIRAL );
306 mDepthLayout = DefaultItemLayout::New( DefaultItemLayout::DEPTH );
307 mGridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
309 // Add the layouts to item view
310 mItemView.AddLayout(*mSpiralLayout);
311 mItemView.AddLayout(*mDepthLayout);
312 mItemView.AddLayout(*mGridLayout);
314 mItemView.SetMinimumSwipeDistance(MIN_SWIPE_DISTANCE);
315 mItemView.SetMinimumSwipeSpeed(MIN_SWIPE_SPEED);
317 // Activate the spiral layout
318 SetLayout( mCurrentLayout );
319 mItemView.SetKeyboardFocusable( true );
320 KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &ItemViewExample::OnKeyboardPreFocusChange );
322 // Set the title and icon to the current layout
326 // Store one 1x1 white image for multiple items to share for backgrounds:
327 mWhiteImage = BufferImage::WHITE();
330 Actor OnKeyboardPreFocusChange( Actor current, Actor proposed, Control::KeyboardFocus::Direction direction )
332 if ( !current && !proposed )
341 * Animate to a different layout
345 Animation animation = Animation::New( mDurationSeconds );
346 animation.FinishedSignal().Connect( this, &ItemViewExample::AnimationFinished );
347 animation.AnimateTo( Property( mItemView, Actor::Property::COLOR_ALPHA ), 0.0f );
351 void AnimationFinished( Animation& )
353 SetLayout( mCurrentLayout );
355 Animation animation = Animation::New( mDurationSeconds );
356 animation.AnimateTo( Property( mItemView, Actor::Property::COLOR_ALPHA ), 1.0f );
361 * Switch to a different item view layout
363 void SetLayout( int layoutId )
365 // Set the new orientation to the layout
366 mItemView.GetLayout(layoutId)->SetOrientation(static_cast<ControlOrientation::Type>(mOrientation / 90));
368 Vector2 stageSize = Stage::GetCurrent().GetSize();
370 if(layoutId == DEPTH_LAYOUT)
372 // Set up the depth layout according to the new orientation
373 if(Toolkit::IsVertical(mDepthLayout->GetOrientation()))
375 mDepthLayout->SetItemSize( DepthLayoutItemSizeFunctionPortrait( stageSize.width ) );
379 mDepthLayout->SetItemSize( DepthLayoutItemSizeFunctionLandscape( stageSize.height ) );
383 // Enable anchoring for depth layout only
384 mItemView.SetAnchoring(layoutId == DEPTH_LAYOUT);
386 // Activate the layout
387 mItemView.ActivateLayout( layoutId, Vector3(stageSize.x, stageSize.y, stageSize.x), 0.0f );
391 * Orientation changed signal callback
394 void OnOrientationChanged( Orientation orientation )
396 const unsigned int angle = orientation.GetDegrees();
398 // If orientation really changed
399 if( mOrientation != angle )
401 // Remember orientation
402 mOrientation = angle;
404 SetLayout( mCurrentLayout );
408 bool OnLayoutButtonClicked( Toolkit::Button button )
410 // Switch to the next layout
411 mCurrentLayout = (mCurrentLayout + 1) % mItemView.GetLayoutCount();
421 bool OnModeButtonClicked( Toolkit::Button button )
428 void SwitchToNextMode()
435 mMode = MODE_REMOVE_MANY;
436 EnterRemoveManyMode();
440 case MODE_REMOVE_MANY:
442 ExitRemoveManyMode();
451 mMode = MODE_INSERT_MANY;
452 EnterInsertManyMode();
456 case MODE_INSERT_MANY:
458 ExitInsertManyMode();
459 mMode = MODE_REPLACE;
467 mMode = MODE_REPLACE_MANY;
468 EnterReplaceManyMode();
472 case MODE_REPLACE_MANY:
474 ExitReplaceManyMode();
490 void EnterRemoveMode()
492 SetTitle("Edit: Remove");
494 mTapDetector = TapGestureDetector::New();
496 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
498 mTapDetector.Attach(mItemView.GetChildAt(i));
501 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::RemoveOnTap );
504 void ExitRemoveMode()
506 mTapDetector.Reset();
509 void RemoveOnTap( Actor actor, const TapGesture& tap )
511 mItemView.RemoveItem( mItemView.GetItemId(actor), 0.5f );
514 void EnterRemoveManyMode()
516 SetTitle("Edit: Remove Many");
518 mDeleteButton.SetVisible( true );
520 mTapDetector = TapGestureDetector::New();
522 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
524 Actor child = mItemView.GetChildAt( i );
525 Actor box = child.FindChildByName( "CheckBox" );
529 mTapDetector.Attach( child );
530 box.SetVisible( true );
534 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
537 void ExitRemoveManyMode()
539 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
541 Actor child = mItemView.GetChildAt( i );
542 Actor box = child.FindChildByName( "CheckBox" );
546 box.SetVisible( false );
548 Actor tick = box.FindChildByName( "Tick" );
551 tick.SetVisible( false );
556 mTapDetector.Reset();
558 mDeleteButton.SetVisible( false );
561 void SelectOnTap( Actor actor, const TapGesture& tap )
563 Actor tick = actor.FindChildByName( "Tick" );
566 tick.SetVisible( !tick.IsVisible() );
570 bool OnDeleteButtonClicked( Toolkit::Button button )
572 ItemIdContainer removeList;
574 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
576 Actor child = mItemView.GetChildAt( i );
577 Actor tick = child.FindChildByName( "Tick" );
579 if( tick && tick.IsVisible() )
581 removeList.push_back( mItemView.GetItemId(child) );
585 if( ! removeList.empty() )
587 mItemView.RemoveItems( removeList, 0.5f );
593 void EnterInsertMode()
595 SetTitle("Edit: Insert");
597 mTapDetector = TapGestureDetector::New();
599 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
601 mTapDetector.Attach( mItemView.GetChildAt(i) );
604 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::InsertOnTap );
607 void ExitInsertMode()
609 mTapDetector.Reset();
612 void InsertOnTap( Actor actor, const TapGesture& tap )
614 ItemId id = mItemView.GetItemId( actor );
616 Actor newActor = NewItem( rand() );
618 mItemView.InsertItem( Item(id,newActor), 0.5f );
621 void EnterInsertManyMode()
623 SetTitle("Edit: Insert Many");
625 mInsertButton.SetVisible( true );
627 mTapDetector = TapGestureDetector::New();
629 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
631 Actor child = mItemView.GetChildAt( i );
632 Actor box = child.FindChildByName( "CheckBox" );
636 mTapDetector.Attach( child );
637 box.SetVisible( true );
641 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
644 void ExitInsertManyMode()
646 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
648 Actor child = mItemView.GetChildAt( i );
649 Actor box = child.FindChildByName( "CheckBox" );
653 box.SetVisible( false );
655 Actor tick = box.FindChildByName( "Tick" );
658 tick.SetVisible( false );
663 mTapDetector.Reset();
665 mInsertButton.SetVisible( false );
668 bool OnInsertButtonClicked( Toolkit::Button button )
670 ItemContainer insertList;
672 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
674 Actor child = mItemView.GetChildAt( i );
675 Actor tick = child.FindChildByName( "Tick" );
677 if( tick && tick.IsVisible() )
679 insertList.push_back( Item( mItemView.GetItemId(child), NewItem(rand()) ) );
683 if( ! insertList.empty() )
685 mItemView.InsertItems( insertList, 0.5f );
691 void EnterReplaceMode()
693 SetTitle("Edit: Replace");
695 mTapDetector = TapGestureDetector::New();
697 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
699 mTapDetector.Attach(mItemView.GetChildAt(i));
702 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::ReplaceOnTap );
705 void ReplaceOnTap( Actor actor, const TapGesture& tap )
707 mItemView.ReplaceItem( Item( mItemView.GetItemId(actor), NewItem(rand()) ), 0.5f );
710 void ExitReplaceMode()
712 mTapDetector.Reset();
715 void EnterReplaceManyMode()
717 SetTitle("Edit: Replace Many");
719 mReplaceButton.SetVisible( true );
721 mTapDetector = TapGestureDetector::New();
723 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
725 Actor child = mItemView.GetChildAt( i );
726 Actor box = child.FindChildByName( "CheckBox" );
730 mTapDetector.Attach( child );
731 box.SetVisible( true );
735 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
738 void ExitReplaceManyMode()
740 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
742 Actor child = mItemView.GetChildAt( i );
743 Actor box = child.FindChildByName( "CheckBox" );
747 box.SetVisible( false );
749 Actor tick = box.FindChildByName( "Tick" );
752 tick.SetVisible( false );
757 mTapDetector.Reset();
759 mReplaceButton.SetVisible( false );
762 bool OnReplaceButtonClicked( Toolkit::Button button )
764 ItemContainer replaceList;
766 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
768 Actor child = mItemView.GetChildAt( i );
769 Actor tick = child.FindChildByName( "Tick" );
771 if( tick && tick.IsVisible() )
773 replaceList.push_back( Item( mItemView.GetItemId(child), NewItem(rand()) ) );
777 if( ! replaceList.empty() )
779 mItemView.ReplaceItems( replaceList, 0.5f );
785 void SetLayoutTitle()
787 if( MODE_NORMAL == mMode )
789 std::stringstream ss(APPLICATION_TITLE);
790 switch(mCurrentLayout)
793 ss << APPLICATION_TITLE << ": " << SPIRAL_LABEL;
796 ss << APPLICATION_TITLE << ": " << GRID_LABEL;
799 ss << APPLICATION_TITLE << ": " << DEPTH_LABEL;
808 void SetLayoutImage()
812 switch( mCurrentLayout )
816 mLayoutButton.SetUnselectedImage( SPIRAL_LAYOUT_IMAGE );
817 mLayoutButton.SetSelectedImage( SPIRAL_LAYOUT_IMAGE_SELECTED );
823 mLayoutButton.SetUnselectedImage( GRID_LAYOUT_IMAGE );
824 mLayoutButton.SetSelectedImage( GRID_LAYOUT_IMAGE_SELECTED );
830 mLayoutButton.SetUnselectedImage( DEPTH_LAYOUT_IMAGE );
831 mLayoutButton.SetSelectedImage( DEPTH_LAYOUT_IMAGE_SELECTED );
841 public: // From ItemFactory
844 * Query the number of items available from the factory.
845 * The maximum available item has an ID of GetNumberOfItems() - 1.
847 virtual unsigned int GetNumberOfItems()
849 return NUM_IMAGES * 10;
853 * Create an Actor to represent a visible item.
855 * @return the created actor.
857 virtual Actor NewItem(unsigned int itemId)
859 // Create an image actor for this item
860 unsigned int imageId = itemId % NUM_IMAGES;
861 ImageActor::PixelArea pixelArea( (imageId%NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_WIDTH,
862 (imageId/NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_HEIGHT,
865 Actor actor = ImageActor::New(mImageAtlas, pixelArea);
866 actor.SetPosition( INITIAL_OFFSCREEN_POSITION );
868 // Add a border image child actor
869 ImageActor borderActor = ImageActor::New(mBorderImage);
870 borderActor.SetParentOrigin( ParentOrigin::CENTER );
871 borderActor.SetAnchorPoint( AnchorPoint::CENTER );
872 borderActor.SetPosition( 0.f, 0.f, 1.f );
873 borderActor.SetStyle( ImageActor::STYLE_NINE_PATCH );
874 borderActor.SetNinePatchBorder( Vector4( ITEM_IMAGE_BORDER_LEFT, ITEM_IMAGE_BORDER_TOP, ITEM_IMAGE_BORDER_RIGHT, ITEM_IMAGE_BORDER_BOTTOM ) );
875 borderActor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR ); // darken with parent image-actor
876 borderActor.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::ALL_DIMENSIONS );
877 borderActor.SetSizeModeFactor( ITEM_BORDER_MARGIN_SIZE );
878 actor.Add(borderActor);
879 actor.SetKeyboardFocusable( true );
881 Vector3 spiralItemSize;
882 static_cast<ItemLayout&>(*mSpiralLayout).GetItemSize( 0u, Vector3( Stage::GetCurrent().GetSize() ), spiralItemSize );
884 // Add a checkbox child actor; invisible until edit-mode is enabled
886 ImageActor checkbox = ImageActor::New( mWhiteImage );
887 checkbox.SetName( "CheckBox" );
888 checkbox.SetColor( Vector4(0.0f,0.0f,0.0f,0.6f) );
889 checkbox.SetParentOrigin( ParentOrigin::TOP_RIGHT );
890 checkbox.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
891 checkbox.SetSize( spiralItemSize.width * 0.2f, spiralItemSize.width * 0.2f );
892 checkbox.SetPosition( -SELECTION_BORDER_WIDTH, SELECTION_BORDER_WIDTH );
893 checkbox.SetZ( 1.0f );
894 checkbox.SetSortModifier( -50.0f );
895 if( MODE_REMOVE_MANY != mMode &&
896 MODE_INSERT_MANY != mMode &&
897 MODE_REPLACE_MANY != mMode )
899 checkbox.SetVisible( false );
901 actor.Add( checkbox );
903 ImageActor tick = ImageActor::New( ResourceImage::New(SELECTED_IMAGE) );
904 tick.SetColorMode( USE_OWN_COLOR );
905 tick.SetName( "Tick" );
906 tick.SetParentOrigin( ParentOrigin::TOP_RIGHT );
907 tick.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
908 tick.SetSize( spiralItemSize.width * 0.2f, spiralItemSize.width * 0.2f );
910 tick.SetSortModifier( -50.0f );
911 tick.SetVisible( false );
912 checkbox.Add( tick );
914 // Connect new items for various editing modes
917 mTapDetector.Attach( actor );
926 * Create an Atlas to tile the images inside.
928 Atlas CreateImageAtlas()
930 const unsigned int atlas_width = IMAGE_WIDTH*NUM_IMAGE_PER_ROW_IN_ATLAS;
931 const unsigned int atlas_height = IMAGE_HEIGHT*ceil( static_cast<float>(NUM_IMAGES)/ static_cast<float>(NUM_IMAGE_PER_ROW_IN_ATLAS));
932 Atlas atlas = Atlas::New(atlas_width, atlas_height, Pixel::RGB888);
934 for( unsigned int i = 0; i < NUM_IMAGES; i++ )
936 atlas.Upload( IMAGE_PATHS[i], (i%NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_WIDTH, (i/NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_HEIGHT );
943 * Sets/Updates the title of the View
944 * @param[in] title The new title for the view.
946 void SetTitle(const std::string& title)
950 mTitleActor = DemoHelper::CreateToolBarLabel( "" );
951 // Add title to the tool bar.
952 mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HorizontalCenter );
955 mTitleActor.SetProperty( TextLabel::Property::TEXT, title );
959 * Main key event handler
961 void OnKeyEvent(const KeyEvent& event)
963 if(event.state == KeyEvent::Down)
965 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
974 Application& mApplication;
977 Toolkit::Control mView;
978 unsigned int mOrientation;
980 Toolkit::ToolBar mToolBar;
981 TextLabel mTitleActor; ///< The Toolbar's Title.
986 unsigned int mCurrentLayout;
987 float mDurationSeconds;
989 ItemLayoutPtr mSpiralLayout;
990 ItemLayoutPtr mDepthLayout;
991 ItemLayoutPtr mGridLayout;
993 TapGestureDetector mTapDetector;
994 Toolkit::PushButton mLayoutButton;
995 Toolkit::PushButton mDeleteButton;
996 Toolkit::PushButton mInsertButton;
997 Toolkit::PushButton mReplaceButton;
999 BufferImage mWhiteImage;
1002 void RunTest(Application& app)
1004 ItemViewExample test(app);
1009 int main(int argc, char **argv)
1011 Application app = Application::New(&argc, &argv, DALI_DEMO_THEME_PATH);