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>
29 #include <dali/devel-api/rendering/cull-face.h>
32 using namespace Dali::Toolkit;
46 const char* IMAGE_PATHS[] = {
47 DALI_IMAGE_DIR "gallery-medium-1.jpg",
48 DALI_IMAGE_DIR "gallery-medium-2.jpg",
49 DALI_IMAGE_DIR "gallery-medium-3.jpg",
50 DALI_IMAGE_DIR "gallery-medium-4.jpg",
51 DALI_IMAGE_DIR "gallery-medium-5.jpg",
52 DALI_IMAGE_DIR "gallery-medium-6.jpg",
53 DALI_IMAGE_DIR "gallery-medium-7.jpg",
54 DALI_IMAGE_DIR "gallery-medium-8.jpg",
55 DALI_IMAGE_DIR "gallery-medium-9.jpg",
56 DALI_IMAGE_DIR "gallery-medium-10.jpg",
57 DALI_IMAGE_DIR "gallery-medium-11.jpg",
58 DALI_IMAGE_DIR "gallery-medium-12.jpg",
59 DALI_IMAGE_DIR "gallery-medium-13.jpg",
60 DALI_IMAGE_DIR "gallery-medium-14.jpg",
61 DALI_IMAGE_DIR "gallery-medium-15.jpg",
62 DALI_IMAGE_DIR "gallery-medium-16.jpg",
63 DALI_IMAGE_DIR "gallery-medium-17.jpg",
64 DALI_IMAGE_DIR "gallery-medium-18.jpg",
65 DALI_IMAGE_DIR "gallery-medium-19.jpg",
66 DALI_IMAGE_DIR "gallery-medium-20.jpg",
67 DALI_IMAGE_DIR "gallery-medium-21.jpg",
68 DALI_IMAGE_DIR "gallery-medium-22.jpg",
69 DALI_IMAGE_DIR "gallery-medium-23.jpg",
70 DALI_IMAGE_DIR "gallery-medium-24.jpg",
71 DALI_IMAGE_DIR "gallery-medium-25.jpg",
72 DALI_IMAGE_DIR "gallery-medium-26.jpg",
73 DALI_IMAGE_DIR "gallery-medium-27.jpg",
74 DALI_IMAGE_DIR "gallery-medium-28.jpg",
75 DALI_IMAGE_DIR "gallery-medium-29.jpg",
76 DALI_IMAGE_DIR "gallery-medium-30.jpg",
77 DALI_IMAGE_DIR "gallery-medium-31.jpg",
78 DALI_IMAGE_DIR "gallery-medium-32.jpg",
79 DALI_IMAGE_DIR "gallery-medium-33.jpg",
80 DALI_IMAGE_DIR "gallery-medium-34.jpg",
81 DALI_IMAGE_DIR "gallery-medium-35.jpg",
82 DALI_IMAGE_DIR "gallery-medium-36.jpg",
83 DALI_IMAGE_DIR "gallery-medium-37.jpg",
84 DALI_IMAGE_DIR "gallery-medium-38.jpg",
85 DALI_IMAGE_DIR "gallery-medium-39.jpg",
86 DALI_IMAGE_DIR "gallery-medium-40.jpg",
87 DALI_IMAGE_DIR "gallery-medium-41.jpg",
88 DALI_IMAGE_DIR "gallery-medium-42.jpg",
89 DALI_IMAGE_DIR "gallery-medium-43.jpg",
90 DALI_IMAGE_DIR "gallery-medium-44.jpg",
91 DALI_IMAGE_DIR "gallery-medium-45.jpg",
92 DALI_IMAGE_DIR "gallery-medium-46.jpg",
93 DALI_IMAGE_DIR "gallery-medium-47.jpg",
94 DALI_IMAGE_DIR "gallery-medium-48.jpg",
95 DALI_IMAGE_DIR "gallery-medium-49.jpg",
96 DALI_IMAGE_DIR "gallery-medium-50.jpg",
97 DALI_IMAGE_DIR "gallery-medium-51.jpg",
98 DALI_IMAGE_DIR "gallery-medium-52.jpg",
99 DALI_IMAGE_DIR "gallery-medium-53.jpg",
102 const unsigned int NUM_IMAGES = sizeof(IMAGE_PATHS) / sizeof(char*);
104 const unsigned int IMAGE_WIDTH = 256;
105 const unsigned int IMAGE_HEIGHT = 256;
106 const unsigned int NUM_IMAGE_PER_ROW_IN_ATLAS = 8;
108 const char* BACKGROUND_IMAGE( "" );
109 const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
110 const char* EDIT_IMAGE( DALI_IMAGE_DIR "icon-edit.png" );
111 const char* EDIT_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-edit-selected.png" );
112 const char* SPIRAL_LAYOUT_IMAGE( DALI_IMAGE_DIR "icon-item-view-layout-spiral.png" );
113 const char* SPIRAL_LAYOUT_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-item-view-layout-spiral-selected.png" );
114 const char* GRID_LAYOUT_IMAGE( DALI_IMAGE_DIR "icon-item-view-layout-grid.png" );
115 const char* GRID_LAYOUT_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-item-view-layout-grid-selected.png" );
116 const char* DEPTH_LAYOUT_IMAGE( DALI_IMAGE_DIR "icon-item-view-layout-depth.png" );
117 const char* DEPTH_LAYOUT_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-item-view-layout-depth-selected.png" );
118 const char* DELETE_IMAGE( DALI_IMAGE_DIR "icon-delete.png" );
119 const char* DELETE_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-delete-selected.png" );
120 const char* REPLACE_IMAGE( DALI_IMAGE_DIR "icon-replace.png" );
121 const char* REPLACE_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-replace-selected.png" );
122 const char* INSERT_IMAGE( DALI_IMAGE_DIR "icon-insert.png" );
123 const char* INSERT_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-insert-selected.png" );
124 const char* SELECTED_IMAGE( DALI_IMAGE_DIR "item-select-check.png" );
125 const char* APPLICATION_TITLE( "ItemView" );
127 const char* SPIRAL_LABEL("Spiral");
128 const char* GRID_LABEL("Grid");
129 const char* DEPTH_LABEL("Depth");
131 const char* ITEM_BORDER_IMAGE_PATH( DALI_IMAGE_DIR "frame-128x128.png" );
132 const Vector3 ITEM_BORDER_MARGIN_SIZE(24, 24, 0);
134 // These values depend on the border image
135 const float ITEM_IMAGE_BORDER_LEFT = 13.0f;
136 const float ITEM_IMAGE_BORDER_RIGHT = 13.0f;
137 const float ITEM_IMAGE_BORDER_TOP = 13.0f;
138 const float ITEM_IMAGE_BORDER_BOTTOM = 13.0f;
140 const float DEPTH_LAYOUT_ITEM_SIZE_FACTOR_PORTRAIT = 1.0f;
141 const float DEPTH_LAYOUT_ITEM_SIZE_FACTOR_LANDSCAPE = 0.8f;
142 const float DEPTH_LAYOUT_COLUMNS = 3.0f;
144 const float MIN_SWIPE_DISTANCE = 15.0f;
145 const float MIN_SWIPE_SPEED = 5.0f;
147 const float SELECTION_BORDER_WIDTH = 3.0f;
148 const float BUTTON_BORDER = -10.0f;
149 const float MENU_OPTION_HEIGHT(140.0f);
150 const float LABEL_TEXT_SIZE_Y = 20.0f;
152 const Vector3 INITIAL_OFFSCREEN_POSITION( 1000.0f, 0, -1000.0f );
154 static Vector3 DepthLayoutItemSizeFunctionPortrait( float layoutWidth )
156 float width = ( layoutWidth / ( DEPTH_LAYOUT_COLUMNS + 1.0f ) ) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_PORTRAIT;
159 return Vector3(width, width, width);
162 static Vector3 DepthLayoutItemSizeFunctionLandscape( float layoutWidth )
164 float width = ( layoutWidth / ( DEPTH_LAYOUT_COLUMNS + 1.0f ) ) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_LANDSCAPE;
167 return Vector3(width, width, width);
170 } // unnamed namespace
173 * This example shows how to use ItemView UI control.
174 * There are three layouts created for ItemView, i.e., Spiral, Depth and Grid.
175 * There is one button in the upper-left corner for quitting the application and
176 * another button in the upper-right corner for switching between different layouts.
178 class ItemViewExample : public ConnectionTracker, public ItemFactory
196 * @param application class, stored as reference
198 ItemViewExample( Application& application )
199 : mApplication( application ),
200 mMode( MODE_NORMAL ),
202 mCurrentLayout( SPIRAL_LAYOUT ),
203 mDurationSeconds( 0.25f )
205 // Connect to the Application's Init signal
206 mApplication.InitSignal().Connect(this, &ItemViewExample::OnInit);
210 * This method gets called once the main loop of application is up and running
212 void OnInit(Application& app)
214 Stage stage = Dali::Stage::GetCurrent();
215 stage.KeyEventSignal().Connect(this, &ItemViewExample::OnKeyEvent);
216 stage.GetRootLayer().SetBehavior(Layer::LAYER_3D);
218 Vector2 stageSize = Stage::GetCurrent().GetSize();
220 // Create a border image shared by all the item actors
221 mBorderImage = ResourceImage::New(ITEM_BORDER_IMAGE_PATH);
223 // Creates a default view with a default tool bar.
224 // The view is added to the stage.
225 Layer contents = DemoHelper::CreateView( mApplication,
232 //app.GetWindow().GetOrientation().ChangedSignal().Connect( this, &ItemViewExample::OnOrientationChanged );
234 // Create an edit mode button. (left of toolbar)
235 Toolkit::PushButton editButton = Toolkit::PushButton::New();
236 editButton.SetUnselectedImage( EDIT_IMAGE );
237 editButton.SetSelectedImage( EDIT_IMAGE_SELECTED );
238 editButton.ClickedSignal().Connect( this, &ItemViewExample::OnModeButtonClicked);
239 editButton.SetLeaveRequired( true );
240 mToolBar.AddControl( editButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalLeft, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
242 // Create a layout toggle button. (right of toolbar)
243 mLayoutButton = Toolkit::PushButton::New();
244 mLayoutButton.SetUnselectedImage( SPIRAL_LAYOUT_IMAGE );
245 mLayoutButton.SetSelectedImage(SPIRAL_LAYOUT_IMAGE_SELECTED );
246 mLayoutButton.ClickedSignal().Connect( this, &ItemViewExample::OnLayoutButtonClicked);
247 mLayoutButton.SetLeaveRequired( true );
248 mToolBar.AddControl( mLayoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
250 // Create a delete button (bottom right of screen)
251 mDeleteButton = Toolkit::PushButton::New();
252 mDeleteButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
253 mDeleteButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
254 mDeleteButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
255 mDeleteButton.SetDrawMode( DrawMode::OVERLAY_2D );
256 mDeleteButton.SetUnselectedImage( DELETE_IMAGE );
257 mDeleteButton.SetSelectedImage( DELETE_IMAGE_SELECTED );
258 mDeleteButton.SetBackgroundImage( TOOLBAR_IMAGE );
259 mDeleteButton.SetSize( Vector2( stageSize.width * 0.15f, stageSize.width * 0.15f ) );
260 mDeleteButton.ClickedSignal().Connect( this, &ItemViewExample::OnDeleteButtonClicked);
261 mDeleteButton.SetLeaveRequired( true );
262 mDeleteButton.SetVisible( false );
263 stage.Add( mDeleteButton );
265 // Create an insert button (bottom right of screen)
266 mInsertButton = Toolkit::PushButton::New();
267 mInsertButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
268 mInsertButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
269 mInsertButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
270 mInsertButton.SetDrawMode( DrawMode::OVERLAY_2D );
271 mInsertButton.SetUnselectedImage( INSERT_IMAGE );
272 mInsertButton.SetSelectedImage( INSERT_IMAGE_SELECTED );
273 mInsertButton.SetBackgroundImage( TOOLBAR_IMAGE );
274 mInsertButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
275 mInsertButton.ClickedSignal().Connect( this, &ItemViewExample::OnInsertButtonClicked);
276 mInsertButton.SetLeaveRequired( true );
277 mInsertButton.SetVisible( false );
278 stage.Add( mInsertButton );
280 // Create an replace button (bottom right of screen)
281 mReplaceButton = Toolkit::PushButton::New();
282 mReplaceButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
283 mReplaceButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
284 mReplaceButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
285 mReplaceButton.SetDrawMode( DrawMode::OVERLAY_2D );
286 mReplaceButton.SetUnselectedImage( REPLACE_IMAGE );
287 mReplaceButton.SetSelectedImage( REPLACE_IMAGE_SELECTED );
288 mReplaceButton.SetBackgroundImage( TOOLBAR_IMAGE );
289 mReplaceButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
290 mReplaceButton.ClickedSignal().Connect( this, &ItemViewExample::OnReplaceButtonClicked);
291 mReplaceButton.SetLeaveRequired( true );
292 mReplaceButton.SetVisible( false );
293 stage.Add( mReplaceButton );
295 // Store one 1x1 white image for multiple items to share for backgrounds:
296 mWhiteImage = BufferImage::WHITE();
298 // Create the item view actor
299 mImageAtlas = CreateImageAtlas();
300 mItemView = ItemView::New(*this);
301 mItemView.SetParentOrigin(ParentOrigin::CENTER);
302 mItemView.SetAnchorPoint(AnchorPoint::CENTER);
304 // Display item view on the stage
305 stage.Add( mItemView );
306 stage.GetRootLayer().SetBehavior( Layer::LAYER_3D );
308 // Create the layouts
309 mSpiralLayout = DefaultItemLayout::New( DefaultItemLayout::SPIRAL );
310 mDepthLayout = DefaultItemLayout::New( DefaultItemLayout::DEPTH );
311 mGridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
313 // Add the layouts to item view
314 mItemView.AddLayout(*mSpiralLayout);
315 mItemView.AddLayout(*mDepthLayout);
316 mItemView.AddLayout(*mGridLayout);
318 mItemView.SetMinimumSwipeDistance(MIN_SWIPE_DISTANCE);
319 mItemView.SetMinimumSwipeSpeed(MIN_SWIPE_SPEED);
321 // Activate the spiral layout
322 SetLayout( mCurrentLayout );
323 mItemView.SetKeyboardFocusable( true );
324 KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &ItemViewExample::OnKeyboardPreFocusChange );
326 // Set the title and icon to the current layout
331 Actor OnKeyboardPreFocusChange( Actor current, Actor proposed, Control::KeyboardFocus::Direction direction )
333 if ( !current && !proposed )
342 * Animate to a different layout
346 Animation animation = Animation::New( mDurationSeconds );
347 animation.FinishedSignal().Connect( this, &ItemViewExample::AnimationFinished );
348 animation.AnimateTo( Property( mItemView, Actor::Property::COLOR_ALPHA ), 0.0f );
352 void AnimationFinished( Animation& )
354 SetLayout( mCurrentLayout );
356 Animation animation = Animation::New( mDurationSeconds );
357 animation.AnimateTo( Property( mItemView, Actor::Property::COLOR_ALPHA ), 1.0f );
362 * Switch to a different item view layout
364 void SetLayout( int layoutId )
366 // Set the new orientation to the layout
367 mItemView.GetLayout(layoutId)->SetOrientation(static_cast<ControlOrientation::Type>(mOrientation / 90));
369 Vector2 stageSize = Stage::GetCurrent().GetSize();
371 if(layoutId == DEPTH_LAYOUT)
373 // Set up the depth layout according to the new orientation
374 if(Toolkit::IsVertical(mDepthLayout->GetOrientation()))
376 mDepthLayout->SetItemSize( DepthLayoutItemSizeFunctionPortrait( stageSize.width ) );
380 mDepthLayout->SetItemSize( DepthLayoutItemSizeFunctionLandscape( stageSize.height ) );
384 // Enable anchoring for depth layout only
385 mItemView.SetAnchoring(layoutId == DEPTH_LAYOUT);
387 // Activate the layout
388 mItemView.ActivateLayout( layoutId, Vector3(stageSize.x, stageSize.y, stageSize.x), 0.0f );
392 * Orientation changed signal callback
395 void OnOrientationChanged( Orientation orientation )
397 const unsigned int angle = orientation.GetDegrees();
399 // If orientation really changed
400 if( mOrientation != angle )
402 // Remember orientation
403 mOrientation = angle;
405 SetLayout( mCurrentLayout );
409 bool OnLayoutButtonClicked( Toolkit::Button button )
411 // Switch to the next layout
412 mCurrentLayout = (mCurrentLayout + 1) % mItemView.GetLayoutCount();
422 bool OnModeButtonClicked( Toolkit::Button button )
429 void SwitchToNextMode()
436 mMode = MODE_REMOVE_MANY;
437 EnterRemoveManyMode();
441 case MODE_REMOVE_MANY:
443 ExitRemoveManyMode();
452 mMode = MODE_INSERT_MANY;
453 EnterInsertManyMode();
457 case MODE_INSERT_MANY:
459 ExitInsertManyMode();
460 mMode = MODE_REPLACE;
468 mMode = MODE_REPLACE_MANY;
469 EnterReplaceManyMode();
473 case MODE_REPLACE_MANY:
475 ExitReplaceManyMode();
491 void EnterRemoveMode()
493 SetTitle("Edit: Remove");
495 mTapDetector = TapGestureDetector::New();
497 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
499 mTapDetector.Attach(mItemView.GetChildAt(i));
502 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::RemoveOnTap );
505 void ExitRemoveMode()
507 mTapDetector.Reset();
510 void RemoveOnTap( Actor actor, const TapGesture& tap )
512 mItemView.RemoveItem( mItemView.GetItemId(actor), 0.5f );
515 void EnterRemoveManyMode()
517 SetTitle("Edit: Remove Many");
519 mDeleteButton.SetVisible( true );
521 mTapDetector = TapGestureDetector::New();
523 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
525 Actor child = mItemView.GetChildAt( i );
526 Actor box = child.FindChildByName( "CheckBox" );
530 mTapDetector.Attach( child );
531 box.SetVisible( true );
535 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
538 void ExitRemoveManyMode()
540 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
542 Actor child = mItemView.GetChildAt( i );
543 Actor box = child.FindChildByName( "CheckBox" );
547 box.SetVisible( false );
549 Actor tick = box.FindChildByName( "Tick" );
552 tick.SetVisible( false );
557 mTapDetector.Reset();
559 mDeleteButton.SetVisible( false );
562 void SelectOnTap( Actor actor, const TapGesture& tap )
564 Actor tick = actor.FindChildByName( "Tick" );
567 tick.SetVisible( !tick.IsVisible() );
571 bool OnDeleteButtonClicked( Toolkit::Button button )
573 ItemIdContainer removeList;
575 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
577 Actor child = mItemView.GetChildAt( i );
578 Actor tick = child.FindChildByName( "Tick" );
580 if( tick && tick.IsVisible() )
582 removeList.push_back( mItemView.GetItemId(child) );
586 if( ! removeList.empty() )
588 mItemView.RemoveItems( removeList, 0.5f );
594 void EnterInsertMode()
596 SetTitle("Edit: Insert");
598 mTapDetector = TapGestureDetector::New();
600 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
602 mTapDetector.Attach( mItemView.GetChildAt(i) );
605 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::InsertOnTap );
608 void ExitInsertMode()
610 mTapDetector.Reset();
613 void InsertOnTap( Actor actor, const TapGesture& tap )
615 ItemId id = mItemView.GetItemId( actor );
617 Actor newActor = NewItem( rand() );
619 mItemView.InsertItem( Item(id,newActor), 0.5f );
622 void EnterInsertManyMode()
624 SetTitle("Edit: Insert Many");
626 mInsertButton.SetVisible( true );
628 mTapDetector = TapGestureDetector::New();
630 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
632 Actor child = mItemView.GetChildAt( i );
633 Actor box = child.FindChildByName( "CheckBox" );
637 mTapDetector.Attach( child );
638 box.SetVisible( true );
642 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
645 void ExitInsertManyMode()
647 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
649 Actor child = mItemView.GetChildAt( i );
650 Actor box = child.FindChildByName( "CheckBox" );
654 box.SetVisible( false );
656 Actor tick = box.FindChildByName( "Tick" );
659 tick.SetVisible( false );
664 mTapDetector.Reset();
666 mInsertButton.SetVisible( false );
669 bool OnInsertButtonClicked( Toolkit::Button button )
671 ItemContainer insertList;
673 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
675 Actor child = mItemView.GetChildAt( i );
676 Actor tick = child.FindChildByName( "Tick" );
678 if( tick && tick.IsVisible() )
680 insertList.push_back( Item( mItemView.GetItemId(child), NewItem(rand()) ) );
684 if( ! insertList.empty() )
686 mItemView.InsertItems( insertList, 0.5f );
692 void EnterReplaceMode()
694 SetTitle("Edit: Replace");
696 mTapDetector = TapGestureDetector::New();
698 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
700 mTapDetector.Attach(mItemView.GetChildAt(i));
703 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::ReplaceOnTap );
706 void ReplaceOnTap( Actor actor, const TapGesture& tap )
708 mItemView.ReplaceItem( Item( mItemView.GetItemId(actor), NewItem(rand()) ), 0.5f );
711 void ExitReplaceMode()
713 mTapDetector.Reset();
716 void EnterReplaceManyMode()
718 SetTitle("Edit: Replace Many");
720 mReplaceButton.SetVisible( true );
722 mTapDetector = TapGestureDetector::New();
724 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
726 Actor child = mItemView.GetChildAt( i );
727 Actor box = child.FindChildByName( "CheckBox" );
731 mTapDetector.Attach( child );
732 box.SetVisible( true );
736 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
739 void ExitReplaceManyMode()
741 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
743 Actor child = mItemView.GetChildAt( i );
744 Actor box = child.FindChildByName( "CheckBox" );
748 box.SetVisible( false );
750 Actor tick = box.FindChildByName( "Tick" );
753 tick.SetVisible( false );
758 mTapDetector.Reset();
760 mReplaceButton.SetVisible( false );
763 bool OnReplaceButtonClicked( Toolkit::Button button )
765 ItemContainer replaceList;
767 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
769 Actor child = mItemView.GetChildAt( i );
770 Actor tick = child.FindChildByName( "Tick" );
772 if( tick && tick.IsVisible() )
774 replaceList.push_back( Item( mItemView.GetItemId(child), NewItem(rand()) ) );
778 if( ! replaceList.empty() )
780 mItemView.ReplaceItems( replaceList, 0.5f );
786 void SetLayoutTitle()
788 if( MODE_NORMAL == mMode )
790 std::stringstream ss(APPLICATION_TITLE);
791 switch(mCurrentLayout)
794 ss << APPLICATION_TITLE << ": " << SPIRAL_LABEL;
797 ss << APPLICATION_TITLE << ": " << GRID_LABEL;
800 ss << APPLICATION_TITLE << ": " << DEPTH_LABEL;
809 void SetLayoutImage()
813 switch( mCurrentLayout )
817 mLayoutButton.SetUnselectedImage( SPIRAL_LAYOUT_IMAGE );
818 mLayoutButton.SetSelectedImage( SPIRAL_LAYOUT_IMAGE_SELECTED );
824 mLayoutButton.SetUnselectedImage( GRID_LAYOUT_IMAGE );
825 mLayoutButton.SetSelectedImage( GRID_LAYOUT_IMAGE_SELECTED );
831 mLayoutButton.SetUnselectedImage( DEPTH_LAYOUT_IMAGE );
832 mLayoutButton.SetSelectedImage( DEPTH_LAYOUT_IMAGE_SELECTED );
842 public: // From ItemFactory
845 * Query the number of items available from the factory.
846 * The maximum available item has an ID of GetNumberOfItems() - 1.
848 virtual unsigned int GetNumberOfItems()
850 return NUM_IMAGES * 10;
854 * Create an Actor to represent a visible item.
856 * @return the created actor.
858 virtual Actor NewItem(unsigned int itemId)
860 // Create an image actor for this item
861 unsigned int imageId = itemId % NUM_IMAGES;
862 ImageActor::PixelArea pixelArea( (imageId%NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_WIDTH,
863 (imageId/NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_HEIGHT,
866 Actor actor = ImageActor::New(mImageAtlas, pixelArea);
867 actor.SetPosition( INITIAL_OFFSCREEN_POSITION );
869 // Add a border image child actor
870 ImageActor borderActor = ImageActor::New(mBorderImage);
871 borderActor.SetParentOrigin( ParentOrigin::CENTER );
872 borderActor.SetAnchorPoint( AnchorPoint::CENTER );
873 borderActor.SetPosition( 0.f, 0.f, 1.f );
874 borderActor.SetStyle( ImageActor::STYLE_NINE_PATCH );
875 borderActor.SetNinePatchBorder( Vector4( ITEM_IMAGE_BORDER_LEFT, ITEM_IMAGE_BORDER_TOP, ITEM_IMAGE_BORDER_RIGHT, ITEM_IMAGE_BORDER_BOTTOM ) );
876 borderActor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR ); // darken with parent image-actor
877 borderActor.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::ALL_DIMENSIONS );
878 borderActor.SetSizeModeFactor( ITEM_BORDER_MARGIN_SIZE );
879 actor.Add(borderActor);
880 actor.SetKeyboardFocusable( true );
882 Vector3 spiralItemSize;
883 static_cast<ItemLayout&>(*mSpiralLayout).GetItemSize( 0u, Vector3( Stage::GetCurrent().GetSize() ), spiralItemSize );
885 // Add a checkbox child actor; invisible until edit-mode is enabled
887 ImageActor checkbox = ImageActor::New( mWhiteImage );
888 checkbox.SetName( "CheckBox" );
889 checkbox.SetColor( Vector4(0.0f,0.0f,0.0f,0.6f) );
890 checkbox.SetParentOrigin( ParentOrigin::TOP_RIGHT );
891 checkbox.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
892 checkbox.SetSize( spiralItemSize.width * 0.2f, spiralItemSize.width * 0.2f );
893 checkbox.SetPosition( -SELECTION_BORDER_WIDTH, SELECTION_BORDER_WIDTH );
894 checkbox.SetZ( 1.0f );
895 SetCullFace(checkbox, Dali::CullBack);
896 checkbox.SetSortModifier( 150.0f );
897 if( MODE_REMOVE_MANY != mMode &&
898 MODE_INSERT_MANY != mMode &&
899 MODE_REPLACE_MANY != mMode )
901 checkbox.SetVisible( false );
903 actor.Add( checkbox );
905 ImageActor tick = ImageActor::New( ResourceImage::New(SELECTED_IMAGE) );
906 tick.SetColorMode( USE_OWN_COLOR );
907 tick.SetName( "Tick" );
908 tick.SetParentOrigin( ParentOrigin::TOP_RIGHT );
909 tick.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
910 tick.SetSize( spiralItemSize.width * 0.2f, spiralItemSize.width * 0.2f );
912 tick.SetSortModifier( 150.0f );
913 tick.SetVisible( false );
914 SetCullFace(tick, Dali::CullBack);
915 checkbox.Add( tick );
917 // Connect new items for various editing modes
920 mTapDetector.Attach( actor );
929 * Create an Atlas to tile the images inside.
931 Atlas CreateImageAtlas()
933 const unsigned int atlas_width = IMAGE_WIDTH*NUM_IMAGE_PER_ROW_IN_ATLAS;
934 const unsigned int atlas_height = IMAGE_HEIGHT*ceil( static_cast<float>(NUM_IMAGES)/ static_cast<float>(NUM_IMAGE_PER_ROW_IN_ATLAS));
935 Atlas atlas = Atlas::New(atlas_width, atlas_height, Pixel::RGB888);
937 for( unsigned int i = 0; i < NUM_IMAGES; i++ )
939 atlas.Upload( IMAGE_PATHS[i], (i%NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_WIDTH, (i/NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_HEIGHT );
946 * Sets/Updates the title of the View
947 * @param[in] title The new title for the view.
949 void SetTitle(const std::string& title)
953 mTitleActor = DemoHelper::CreateToolBarLabel( "" );
954 // Add title to the tool bar.
955 mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HorizontalCenter );
958 mTitleActor.SetProperty( TextLabel::Property::TEXT, title );
962 * Main key event handler
964 void OnKeyEvent(const KeyEvent& event)
966 if(event.state == KeyEvent::Down)
968 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
977 Application& mApplication;
980 Toolkit::Control mView;
981 unsigned int mOrientation;
983 Toolkit::ToolBar mToolBar;
984 TextLabel mTitleActor; ///< The Toolbar's Title.
989 unsigned int mCurrentLayout;
990 float mDurationSeconds;
992 ItemLayoutPtr mSpiralLayout;
993 ItemLayoutPtr mDepthLayout;
994 ItemLayoutPtr mGridLayout;
996 TapGestureDetector mTapDetector;
997 Toolkit::PushButton mLayoutButton;
998 Toolkit::PushButton mDeleteButton;
999 Toolkit::PushButton mInsertButton;
1000 Toolkit::PushButton mReplaceButton;
1002 BufferImage mWhiteImage;
1005 void RunTest(Application& app)
1007 ItemViewExample test(app);
1012 int main(int argc, char **argv)
1014 Application app = Application::New(&argc, &argv, DALI_DEMO_THEME_PATH);