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>
30 using namespace Dali::Toolkit;
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",
100 const unsigned int NUM_IMAGES = sizeof(IMAGE_PATHS) / sizeof(char*);
102 const unsigned int IMAGE_WIDTH = 256;
103 const unsigned int IMAGE_HEIGHT = 256;
104 const unsigned int NUM_IMAGE_PER_ROW_IN_ATLAS = 8;
106 AlphaFunction ALPHA_FUNCTIONS[] = { AlphaFunction(AlphaFunction::LINEAR),
107 AlphaFunction(AlphaFunction::EASE_IN),
108 AlphaFunction(AlphaFunction::EASE_OUT) };
110 const unsigned int NUM_ALPHA_FUNCTIONS = sizeof(ALPHA_FUNCTIONS) / sizeof(AlphaFunction);
112 const char* ALPHA_FUNCTIONS_TEXT[] = { "Linear",
116 const char* BACKGROUND_IMAGE( "" );
117 const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
118 const char* EDIT_IMAGE( DALI_IMAGE_DIR "icon-edit.png" );
119 const char* SPIRAL_LAYOUT_IMAGE( DALI_IMAGE_DIR "icon-item-view-layout-spiral.png" );
120 const char* GRID_LAYOUT_IMAGE( DALI_IMAGE_DIR "icon-item-view-layout-grid.png" );
121 const char* DEPTH_LAYOUT_IMAGE( DALI_IMAGE_DIR "icon-item-view-layout-depth.png" );
122 const char* DELETE_IMAGE( DALI_IMAGE_DIR "icon-delete.png" );
123 const char* REPLACE_IMAGE( DALI_IMAGE_DIR "icon-replace.png" );
124 const char* INSERT_IMAGE( DALI_IMAGE_DIR "icon-insert.png" );
125 const char* SELECTED_IMAGE( DALI_IMAGE_DIR "item-select-check.png" );
126 const char* APPLICATION_TITLE( "ItemView" );
128 const char* SPIRAL_LABEL("Spiral");
129 const char* GRID_LABEL("Grid");
130 const char* DEPTH_LABEL("Depth");
132 const char* ITEM_BORDER_IMAGE_PATH( DALI_IMAGE_DIR "frame-128x128.png" );
133 const Vector3 ITEM_BORDER_MARGIN_SIZE(24, 24, 0);
135 // These values depend on the border image
136 const float ITEM_IMAGE_BORDER_LEFT = 13.0f;
137 const float ITEM_IMAGE_BORDER_RIGHT = 13.0f;
138 const float ITEM_IMAGE_BORDER_TOP = 13.0f;
139 const float ITEM_IMAGE_BORDER_BOTTOM = 13.0f;
141 const unsigned int DEPTH_LAYOUT_ROWS_PORTRAIT = 26;
142 const unsigned int DEPTH_LAYOUT_ROWS_LANDSCAPE = 16;
143 const float DEPTH_LAYOUT_TILT_ANGLE_PORTRAIT = 25.0f;
144 const float DEPTH_LAYOUT_TILT_ANGLE_LANDSCAPE = 21.0f;
145 const float DEPTH_LAYOUT_ROW_SPACING_FACTOR = 0.1f;
146 const float DEPTH_LAYOUT_ITEM_SIZE_FACTOR_PORTRAIT = 1.0f;
147 const float DEPTH_LAYOUT_ITEM_SIZE_FACTOR_LANDSCAPE = 0.8f;
148 const float DEPTH_LAYOUT_BOTTOM_MARGIN_FACTOR_PORTRAIT = 0.2f;
149 const float DEPTH_LAYOUT_BOTTOM_MARGIN_FACTOR_LANDSCAPE = 0.1f;
151 const float SPIRAL_LAYOUT_REVOLUTION_NUMBER_PORTRAIT = 4.5f;
152 const float SPIRAL_LAYOUT_REVOLUTION_NUMBER_LANDSCAPE = 2.5f;
154 const float DEPTH_LAYOUT_HEIGHT_SCALE = 20.0f;
155 const float MIN_SWIPE_DISTANCE = 15.0f;
156 const float MIN_SWIPE_SPEED = 5.0f;
158 const float SELECTION_BORDER_WIDTH = 3.0f;
159 const float BUTTON_BORDER = -10.0f;
160 const float MENU_OPTION_HEIGHT(140.0f);
161 const float LABEL_TEXT_SIZE_Y = 20.0f;
163 const Vector3 INITIAL_OFFSCREEN_POSITION( 1000.0f, 0, -1000.0f );
165 static Vector3 DepthLayoutItemSizeFunctionPortrait(unsigned int numberOfColumns, float layoutWidth)
167 float width = (layoutWidth / static_cast<float>(numberOfColumns + 1)) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_PORTRAIT;
170 return Vector3(width, width, width);
173 static Vector3 DepthLayoutItemSizeFunctionLandscape(unsigned int numberOfColumns, float layoutWidth)
175 float width = (layoutWidth / static_cast<float>(numberOfColumns + 1)) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_LANDSCAPE;
178 return Vector3(width, width, width);
181 static float DepthLayoutBottomMarginFunctionPortrait(float layoutHeight)
183 return layoutHeight * DEPTH_LAYOUT_BOTTOM_MARGIN_FACTOR_PORTRAIT;
186 static float DepthLayoutBottomMarginFunctionLandscape(float layoutHeight)
188 return layoutHeight * DEPTH_LAYOUT_BOTTOM_MARGIN_FACTOR_LANDSCAPE;
194 * This example shows how to use ItemView UI control.
195 * There are three layouts created for ItemView, i.e., Spiral, Depth and Grid.
196 * There is one button in the upper-left corner for quitting the application and
197 * another button in the upper-right corner for switching between different layouts.
199 class ItemViewExample : public ConnectionTracker, public ItemFactory
217 * @param application class, stored as reference
219 ItemViewExample( Application& application )
220 : mApplication( application ),
221 mMode( MODE_NORMAL ),
224 mCurrentLayout( SPIRAL_LAYOUT ),
225 mDurationSeconds( 1.0f ),
226 mAlphaFuncIndex( 0u )
228 // Connect to the Application's Init signal
229 mApplication.InitSignal().Connect(this, &ItemViewExample::OnInit);
233 * This method gets called once the main loop of application is up and running
235 void OnInit(Application& app)
237 DemoHelper::RequestThemeChange();
239 Stage stage = Dali::Stage::GetCurrent();
240 stage.KeyEventSignal().Connect(this, &ItemViewExample::OnKeyEvent);
242 Vector2 stageSize = Stage::GetCurrent().GetSize();
244 // Create a border image shared by all the item actors
245 mBorderImage = ResourceImage::New(ITEM_BORDER_IMAGE_PATH);
247 // Creates a default view with a default tool bar.
248 // The view is added to the stage.
249 Layer contents = DemoHelper::CreateView( mApplication,
256 app.GetWindow().GetOrientation().ChangedSignal().Connect( this, &ItemViewExample::OnOrientationChanged );
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 );
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 );
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.SetButtonImage( ResourceImage::New( DELETE_IMAGE ) );
279 mDeleteButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_IMAGE ) );
280 mDeleteButton.SetSize( Vector2( 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 );
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.SetButtonImage( ResourceImage::New( INSERT_IMAGE ) );
293 mInsertButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_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 );
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.SetButtonImage( ResourceImage::New( REPLACE_IMAGE ) );
307 mReplaceButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_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 );
314 // Create the item view actor
315 mImageAtlas = CreateImageAtlas();
316 mItemView = ItemView::New(*this);
317 mItemView.SetParentOrigin(ParentOrigin::CENTER);
318 mItemView.SetAnchorPoint(AnchorPoint::CENTER);
320 // Display item view on the stage
321 stage.Add( mItemView );
323 // Create the layouts
324 mSpiralLayout = SpiralLayout::New();
325 mDepthLayout = DepthLayout::New();
326 mGridLayout = GridLayout::New();
328 // Add the layouts to item view
329 mItemView.AddLayout(*mSpiralLayout);
330 mItemView.AddLayout(*mDepthLayout);
331 mItemView.AddLayout(*mGridLayout);
333 mItemView.SetMinimumSwipeDistance(MIN_SWIPE_DISTANCE);
334 mItemView.SetMinimumSwipeSpeed(MIN_SWIPE_SPEED);
336 // Activate the spiral layout
337 UseLayout(mCurrentLayout, 0.0f);
338 mItemView.SetKeyboardFocusable( true );
339 KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &ItemViewExample::OnKeyboardPreFocusChange );
341 // Set the title and icon to the current layout
345 // Store one 1x1 white image for multiple items to share for backgrounds:
346 mWhiteImage = BufferImage::WHITE();
349 Actor OnKeyboardPreFocusChange( Actor current, Actor proposed, Control::KeyboardFocusNavigationDirection direction )
351 if ( !current && !proposed )
360 * Switch to a different item view layout
362 void UseLayout(int layoutId, float duration)
364 // Set the new orientation to the layout
365 mItemView.GetLayout(layoutId)->SetOrientation(static_cast<ControlOrientation::Type>(mOrientation / 90));
367 Vector2 stageSize = Stage::GetCurrent().GetSize();
369 if(layoutId == SPIRAL_LAYOUT)
371 mSpiralLayout->SetRevolutionDistance(stageSize.height / Stage::GetCurrent().GetDpi().y * 45.0f);
374 if(layoutId == GRID_LAYOUT)
376 // Set up the grid layout according to the new orientation
377 float layoutWidth = Toolkit::IsHorizontal(mGridLayout->GetOrientation()) ? stageSize.height : stageSize.width;
378 float gridItemSize = (layoutWidth / mGridLayout->GetNumberOfColumns()) * 0.5f;
379 mGridLayout->SetScrollSpeedFactor(mGridLayout->GetNumberOfColumns() / gridItemSize);
381 float toolbarHeight = mToolBar.GetCurrentSize().y;
382 mGridLayout->SetTopMargin(toolbarHeight + mGridLayout->GetRowSpacing());
385 if(layoutId == DEPTH_LAYOUT)
387 // Set up the depth layout according to the new orientation
388 if(Toolkit::IsVertical(mDepthLayout->GetOrientation()))
390 mDepthLayout->SetRowSpacing(stageSize.height * DEPTH_LAYOUT_ROW_SPACING_FACTOR);
391 mDepthLayout->SetNumberOfRows(DEPTH_LAYOUT_ROWS_PORTRAIT);
392 mDepthLayout->SetTiltAngle( Degree( DEPTH_LAYOUT_TILT_ANGLE_PORTRAIT - mDepthLayout->GetNumberOfColumns() ) );
393 mDepthLayout->SetItemSizeFunction(DepthLayoutItemSizeFunctionPortrait);
394 mDepthLayout->SetBottomMarginFunction(DepthLayoutBottomMarginFunctionPortrait);
398 mDepthLayout->SetRowSpacing(stageSize.width * DEPTH_LAYOUT_ROW_SPACING_FACTOR);
399 mDepthLayout->SetNumberOfRows(DEPTH_LAYOUT_ROWS_LANDSCAPE);
400 mDepthLayout->SetTiltAngle( Degree( DEPTH_LAYOUT_TILT_ANGLE_LANDSCAPE - mDepthLayout->GetNumberOfColumns() ) );
401 mDepthLayout->SetItemSizeFunction(DepthLayoutItemSizeFunctionLandscape);
402 mDepthLayout->SetBottomMarginFunction(DepthLayoutBottomMarginFunctionLandscape);
406 // Enable anchoring for depth layout only
407 mItemView.SetAnchoring(layoutId == DEPTH_LAYOUT);
409 // Activate the layout
410 mItemView.ActivateLayout(layoutId, Vector3(stageSize.x, stageSize.y, stageSize.x), duration);
414 * Orientation changed signal callback
417 void OnOrientationChanged( Orientation orientation )
419 const unsigned int angle = orientation.GetDegrees();
421 // If orientation really changed
422 if( mOrientation != angle )
424 // Remember orientation
425 mOrientation = angle;
427 UseLayout(mCurrentLayout, mDurationSeconds);
431 bool OnLayoutButtonClicked( Toolkit::Button button )
433 // Switch to the next layout
434 mCurrentLayout = (mCurrentLayout + 1) % mItemView.GetLayoutCount();
436 UseLayout(mCurrentLayout, mDurationSeconds);
444 bool OnModeButtonClicked( Toolkit::Button button )
451 void SwitchToNextMode()
458 mMode = MODE_REMOVE_MANY;
459 EnterRemoveManyMode();
463 case MODE_REMOVE_MANY:
465 ExitRemoveManyMode();
474 mMode = MODE_INSERT_MANY;
475 EnterInsertManyMode();
479 case MODE_INSERT_MANY:
481 ExitInsertManyMode();
482 mMode = MODE_REPLACE;
490 mMode = MODE_REPLACE_MANY;
491 EnterReplaceManyMode();
495 case MODE_REPLACE_MANY:
497 ExitReplaceManyMode();
513 void EnterRemoveMode()
515 SetTitle("Edit: Remove");
517 mTapDetector = TapGestureDetector::New();
519 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
521 mTapDetector.Attach(mItemView.GetChildAt(i));
524 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::RemoveOnTap );
527 void ExitRemoveMode()
529 mTapDetector.Reset();
532 void RemoveOnTap( Actor actor, const TapGesture& tap )
534 mItemView.RemoveItem( mItemView.GetItemId(actor), 0.5f );
537 void EnterRemoveManyMode()
539 SetTitle("Edit: Remove Many");
541 mDeleteButton.SetVisible( true );
543 mTapDetector = TapGestureDetector::New();
545 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
547 Actor child = mItemView.GetChildAt( i );
548 Actor box = child.FindChildByName( "CheckBox" );
552 mTapDetector.Attach( child );
553 box.SetVisible( true );
557 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
560 void ExitRemoveManyMode()
562 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
564 Actor child = mItemView.GetChildAt( i );
565 Actor box = child.FindChildByName( "CheckBox" );
569 box.SetVisible( false );
571 Actor tick = box.FindChildByName( "Tick" );
574 tick.SetVisible( false );
579 mTapDetector.Reset();
581 mDeleteButton.SetVisible( false );
584 void SelectOnTap( Actor actor, const TapGesture& tap )
586 Actor tick = actor.FindChildByName( "Tick" );
589 tick.SetVisible( !tick.IsVisible() );
593 bool OnDeleteButtonClicked( Toolkit::Button button )
595 ItemIdContainer removeList;
597 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
599 Actor child = mItemView.GetChildAt( i );
600 Actor tick = child.FindChildByName( "Tick" );
602 if( tick && tick.IsVisible() )
604 removeList.push_back( mItemView.GetItemId(child) );
608 if( ! removeList.empty() )
610 mItemView.RemoveItems( removeList, 0.5f );
616 void EnterInsertMode()
618 SetTitle("Edit: Insert");
620 mTapDetector = TapGestureDetector::New();
622 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
624 mTapDetector.Attach( mItemView.GetChildAt(i) );
627 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::InsertOnTap );
630 void ExitInsertMode()
632 mTapDetector.Reset();
635 void InsertOnTap( Actor actor, const TapGesture& tap )
637 ItemId id = mItemView.GetItemId( actor );
639 Actor newActor = NewItem( rand() );
641 mItemView.InsertItem( Item(id,newActor), 0.5f );
644 void EnterInsertManyMode()
646 SetTitle("Edit: Insert Many");
648 mInsertButton.SetVisible( true );
650 mTapDetector = TapGestureDetector::New();
652 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
654 Actor child = mItemView.GetChildAt( i );
655 Actor box = child.FindChildByName( "CheckBox" );
659 mTapDetector.Attach( child );
660 box.SetVisible( true );
664 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
667 void ExitInsertManyMode()
669 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
671 Actor child = mItemView.GetChildAt( i );
672 Actor box = child.FindChildByName( "CheckBox" );
676 box.SetVisible( false );
678 Actor tick = box.FindChildByName( "Tick" );
681 tick.SetVisible( false );
686 mTapDetector.Reset();
688 mInsertButton.SetVisible( false );
691 bool OnInsertButtonClicked( Toolkit::Button button )
693 ItemContainer insertList;
695 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
697 Actor child = mItemView.GetChildAt( i );
698 Actor tick = child.FindChildByName( "Tick" );
700 if( tick && tick.IsVisible() )
702 insertList.push_back( Item( mItemView.GetItemId(child), NewItem(rand()) ) );
706 if( ! insertList.empty() )
708 mItemView.InsertItems( insertList, 0.5f );
714 void EnterReplaceMode()
716 SetTitle("Edit: Replace");
718 mTapDetector = TapGestureDetector::New();
720 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
722 mTapDetector.Attach(mItemView.GetChildAt(i));
725 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::ReplaceOnTap );
728 void ReplaceOnTap( Actor actor, const TapGesture& tap )
730 mItemView.ReplaceItem( Item( mItemView.GetItemId(actor), NewItem(rand()) ), 0.5f );
733 void ExitReplaceMode()
735 mTapDetector.Reset();
738 void EnterReplaceManyMode()
740 SetTitle("Edit: Replace Many");
742 mReplaceButton.SetVisible( true );
744 mTapDetector = TapGestureDetector::New();
746 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
748 Actor child = mItemView.GetChildAt( i );
749 Actor box = child.FindChildByName( "CheckBox" );
753 mTapDetector.Attach( child );
754 box.SetVisible( true );
758 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
761 void ExitReplaceManyMode()
763 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
765 Actor child = mItemView.GetChildAt( i );
766 Actor box = child.FindChildByName( "CheckBox" );
770 box.SetVisible( false );
772 Actor tick = box.FindChildByName( "Tick" );
775 tick.SetVisible( false );
780 mTapDetector.Reset();
782 mReplaceButton.SetVisible( false );
785 bool OnReplaceButtonClicked( Toolkit::Button button )
787 ItemContainer replaceList;
789 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
791 Actor child = mItemView.GetChildAt( i );
792 Actor tick = child.FindChildByName( "Tick" );
794 if( tick && tick.IsVisible() )
796 replaceList.push_back( Item( mItemView.GetItemId(child), NewItem(rand()) ) );
800 if( ! replaceList.empty() )
802 mItemView.ReplaceItems( replaceList, 0.5f );
808 void SetLayoutTitle()
810 if( MODE_NORMAL == mMode )
812 std::stringstream ss(APPLICATION_TITLE);
813 switch(mCurrentLayout)
816 ss << APPLICATION_TITLE << ": " << SPIRAL_LABEL;
819 ss << APPLICATION_TITLE << ": " << GRID_LABEL;
822 ss << APPLICATION_TITLE << ": " << DEPTH_LABEL;
831 void SetLayoutImage()
835 switch( mCurrentLayout )
839 mLayoutButton.SetBackgroundImage( ResourceImage::New( SPIRAL_LAYOUT_IMAGE ) );
845 mLayoutButton.SetBackgroundImage( ResourceImage::New( GRID_LAYOUT_IMAGE ) );
851 mLayoutButton.SetBackgroundImage( ResourceImage::New( DEPTH_LAYOUT_IMAGE ) );
861 public: // From ItemFactory
864 * Query the number of items available from the factory.
865 * The maximum available item has an ID of GetNumberOfItems() - 1.
867 virtual unsigned int GetNumberOfItems()
869 return NUM_IMAGES * 10;
873 * Create an Actor to represent a visible item.
875 * @return the created actor.
877 virtual Actor NewItem(unsigned int itemId)
879 // Create an image actor for this item
880 unsigned int imageId = itemId % NUM_IMAGES;
881 ImageActor::PixelArea pixelArea( (imageId%NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_WIDTH,
882 (imageId/NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_HEIGHT,
885 Actor actor = ImageActor::New(mImageAtlas, pixelArea);
886 actor.SetPosition( INITIAL_OFFSCREEN_POSITION );
888 // Add a border image child actor
889 ImageActor borderActor = ImageActor::New(mBorderImage);
890 borderActor.SetParentOrigin( ParentOrigin::CENTER );
891 borderActor.SetAnchorPoint( AnchorPoint::CENTER );
892 borderActor.SetPosition( 0.f, 0.f, 1.f );
893 borderActor.SetStyle( ImageActor::STYLE_NINE_PATCH );
894 borderActor.SetNinePatchBorder( Vector4( ITEM_IMAGE_BORDER_LEFT, ITEM_IMAGE_BORDER_TOP, ITEM_IMAGE_BORDER_RIGHT, ITEM_IMAGE_BORDER_BOTTOM ) );
895 borderActor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR ); // darken with parent image-actor
896 borderActor.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::ALL_DIMENSIONS );
897 borderActor.SetSizeModeFactor( ITEM_BORDER_MARGIN_SIZE );
898 actor.Add(borderActor);
899 actor.SetKeyboardFocusable( true );
901 Vector3 spiralItemSize;
902 static_cast<ItemLayout&>(*mSpiralLayout).GetItemSize( 0u, Vector3( Stage::GetCurrent().GetSize() ), spiralItemSize );
904 // Add a checkbox child actor; invisible until edit-mode is enabled
906 ImageActor checkbox = ImageActor::New( mWhiteImage );
907 checkbox.SetName( "CheckBox" );
908 checkbox.SetColor( Vector4(0.0f,0.0f,0.0f,0.6f) );
909 checkbox.SetParentOrigin( ParentOrigin::TOP_RIGHT );
910 checkbox.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
911 checkbox.SetSize( spiralItemSize.width * 0.2f, spiralItemSize.width * 0.2f );
912 checkbox.SetPosition( -SELECTION_BORDER_WIDTH, SELECTION_BORDER_WIDTH );
913 checkbox.SetZ( 1.0f );
914 if( MODE_REMOVE_MANY != mMode &&
915 MODE_INSERT_MANY != mMode &&
916 MODE_REPLACE_MANY != mMode )
918 checkbox.SetVisible( false );
920 actor.Add( checkbox );
922 ImageActor tick = ImageActor::New( ResourceImage::New(SELECTED_IMAGE) );
923 tick.SetColorMode( USE_OWN_COLOR );
924 tick.SetName( "Tick" );
925 tick.SetParentOrigin( ParentOrigin::TOP_RIGHT );
926 tick.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
927 tick.SetSize( spiralItemSize.width * 0.2f, spiralItemSize.width * 0.2f );
929 tick.SetVisible( false );
930 checkbox.Add( tick );
932 // Connect new items for various editing modes
935 mTapDetector.Attach( actor );
944 * Create an Atlas to tile the images inside.
946 Atlas CreateImageAtlas()
948 const unsigned int atlas_width = IMAGE_WIDTH*NUM_IMAGE_PER_ROW_IN_ATLAS;
949 const unsigned int atlas_height = IMAGE_HEIGHT*ceil( static_cast<float>(NUM_IMAGES)/ static_cast<float>(NUM_IMAGE_PER_ROW_IN_ATLAS));
950 Atlas atlas = Atlas::New(atlas_width, atlas_height, Pixel::RGB888);
952 for( unsigned int i = 0; i < NUM_IMAGES; i++ )
954 atlas.Upload( IMAGE_PATHS[i], (i%NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_WIDTH, (i/NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_HEIGHT );
961 * Sets/Updates the title of the View
962 * @param[in] title The new title for the view.
964 void SetTitle(const std::string& title)
968 mTitleActor = DemoHelper::CreateToolBarLabel( "" );
969 // Add title to the tool bar.
970 mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HorizontalCenter );
973 mTitleActor.SetProperty( TextLabel::Property::TEXT, title );
978 Stage stage = Stage::GetCurrent();
979 const float popupWidth = stage.GetSize().x * 0.75f;
981 mMenu = Toolkit::Popup::New();
982 mMenu.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
983 mMenu.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
984 mMenu.SetSize( popupWidth, MENU_OPTION_HEIGHT * 2 );
985 mMenu.OutsideTouchedSignal().Connect( this, &ItemViewExample::HideMenu );
987 TableView tableView = TableView::New( 0, 0 );
988 tableView.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
989 mMenu.Add( tableView );
991 Slider slider = Slider::New();
992 slider.SetProperty( Slider::Property::LOWER_BOUND, 0.0f );
993 slider.SetProperty( Slider::Property::UPPER_BOUND, 3.0f );
994 slider.SetProperty( Slider::Property::VALUE, mDurationSeconds );
995 slider.SetProperty( Slider::Property::VALUE_PRECISION, 2 );
996 slider.SetProperty( Slider::Property::SHOW_POPUP, true );
997 slider.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
998 slider.ValueChangedSignal().Connect( this, &ItemViewExample::SliderValueChange );
999 tableView.AddChild( slider, TableView::CellPosition( 0, 0 ) );
1001 TextLabel text = TextLabel::New( "Duration" );
1002 text.SetAnchorPoint( ParentOrigin::TOP_LEFT );
1003 text.SetParentOrigin( ParentOrigin::TOP_LEFT );
1004 text.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
1005 text.SetResizePolicy( ResizePolicy::FIXED, Dimension::HEIGHT );
1006 text.SetSize( Vector2( 0.0f, LABEL_TEXT_SIZE_Y ) );
1009 Actor textContainer = Actor::New();
1010 textContainer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
1011 mAlphaFunctionText = TextLabel::New( ALPHA_FUNCTIONS_TEXT[mAlphaFuncIndex] );
1012 mAlphaFunctionText.SetAnchorPoint( ParentOrigin::CENTER );
1013 mAlphaFunctionText.SetParentOrigin( ParentOrigin::CENTER );
1014 textContainer.Add( mAlphaFunctionText );
1015 tableView.AddChild( textContainer, TableView::CellPosition( 1, 0 ) );
1017 mTapDetector = TapGestureDetector::New();
1018 mTapDetector.Attach(mAlphaFunctionText);
1019 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::ChangeAlphaFunctionOnTap );
1021 text = TextLabel::New( "Alpha Function" );
1022 text.SetAnchorPoint( ParentOrigin::TOP_LEFT );
1023 text.SetParentOrigin( ParentOrigin::TOP_LEFT );
1024 text.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
1025 text.SetSize( 0.0f, LABEL_TEXT_SIZE_Y );
1026 textContainer.Add( text );
1032 bool SliderValueChange( Toolkit::Slider slider, float value )
1034 mDurationSeconds = value;
1039 void ChangeAlphaFunctionOnTap( Actor actor, const TapGesture& tap )
1041 if( NUM_ALPHA_FUNCTIONS <= ++mAlphaFuncIndex )
1043 mAlphaFuncIndex = 0;
1046 if( mAlphaFunctionText )
1048 mAlphaFunctionText.SetProperty( TextLabel::Property::TEXT, std::string(ALPHA_FUNCTIONS_TEXT[mAlphaFuncIndex]) );
1053 mItemView.GetActiveLayout()->SetAlphaFunction( ALPHA_FUNCTIONS[mAlphaFuncIndex] );
1059 mTapDetector.Reset();
1071 * Main key event handler
1073 void OnKeyEvent(const KeyEvent& event)
1075 if(event.state == KeyEvent::Down)
1077 if( IsKey( event, DALI_KEY_MENU ) )
1088 else if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
1096 mApplication.Quit();
1104 Application& mApplication;
1108 Toolkit::Control mView;
1109 unsigned int mOrientation;
1111 Toolkit::ToolBar mToolBar;
1112 TextLabel mTitleActor; ///< The Toolbar's Title.
1117 unsigned int mCurrentLayout;
1118 float mDurationSeconds;
1120 SpiralLayoutPtr mSpiralLayout;
1121 DepthLayoutPtr mDepthLayout;
1122 GridLayoutPtr mGridLayout;
1124 Toolkit::Popup mMenu;
1126 TapGestureDetector mTapDetector;
1127 Toolkit::PushButton mLayoutButton;
1128 Toolkit::PushButton mDeleteButton;
1129 Toolkit::PushButton mInsertButton;
1130 Toolkit::PushButton mReplaceButton;
1132 unsigned int mAlphaFuncIndex;
1133 TextLabel mAlphaFunctionText;
1134 BufferImage mWhiteImage;
1137 void RunTest(Application& app)
1139 ItemViewExample test(app);
1144 int main(int argc, char **argv)
1146 Application app = Application::New(&argc, &argv);