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 AlphaFunction ALPHA_FUNCTIONS[] = { AlphaFunctions::Linear,
103 AlphaFunctions::EaseIn,
104 AlphaFunctions::EaseOut };
106 const unsigned int NUM_ALPHA_FUNCTIONS = sizeof(ALPHA_FUNCTIONS) / sizeof(AlphaFunction);
108 const char* ALPHA_FUNCTIONS_TEXT[] = { "Linear",
112 const char* BACKGROUND_IMAGE( "" );
113 const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
114 const char* EDIT_IMAGE( DALI_IMAGE_DIR "icon-edit.png" );
115 const char* SPIRAL_LAYOUT_IMAGE( DALI_IMAGE_DIR "icon-item-view-layout-spiral.png" );
116 const char* GRID_LAYOUT_IMAGE( DALI_IMAGE_DIR "icon-item-view-layout-grid.png" );
117 const char* DEPTH_LAYOUT_IMAGE( DALI_IMAGE_DIR "icon-item-view-layout-depth.png" );
118 const char* DELETE_IMAGE( DALI_IMAGE_DIR "icon-delete.png" );
119 const char* REPLACE_IMAGE( DALI_IMAGE_DIR "icon-replace.png" );
120 const char* INSERT_IMAGE( DALI_IMAGE_DIR "icon-insert.png" );
121 const char* SELECTED_IMAGE( DALI_IMAGE_DIR "item-select-check.png" );
122 const char* APPLICATION_TITLE( "ItemView" );
124 const char* SPIRAL_LABEL("Spiral");
125 const char* GRID_LABEL("Grid");
126 const char* DEPTH_LABEL("Depth");
128 const char* ITEM_BORDER_IMAGE_PATH( DALI_IMAGE_DIR "frame-128x128.png" );
129 const Vector3 ITEM_BORDER_MARGIN_SIZE(24, 24, 0);
131 // These values depend on the border image
132 const float ITEM_IMAGE_BORDER_LEFT = 13.0f;
133 const float ITEM_IMAGE_BORDER_RIGHT = 13.0f;
134 const float ITEM_IMAGE_BORDER_TOP = 13.0f;
135 const float ITEM_IMAGE_BORDER_BOTTOM = 13.0f;
137 const unsigned int DEPTH_LAYOUT_ROWS_PORTRAIT = 26;
138 const unsigned int DEPTH_LAYOUT_ROWS_LANDSCAPE = 16;
139 const float DEPTH_LAYOUT_TILT_ANGLE_PORTRAIT = 25.0f;
140 const float DEPTH_LAYOUT_TILT_ANGLE_LANDSCAPE = 21.0f;
141 const float DEPTH_LAYOUT_ROW_SPACING_FACTOR = 0.1f;
142 const float DEPTH_LAYOUT_ITEM_SIZE_FACTOR_PORTRAIT = 1.0f;
143 const float DEPTH_LAYOUT_ITEM_SIZE_FACTOR_LANDSCAPE = 0.8f;
144 const float DEPTH_LAYOUT_BOTTOM_MARGIN_FACTOR_PORTRAIT = 0.2f;
145 const float DEPTH_LAYOUT_BOTTOM_MARGIN_FACTOR_LANDSCAPE = 0.1f;
147 const float SPIRAL_LAYOUT_REVOLUTION_NUMBER_PORTRAIT = 4.5f;
148 const float SPIRAL_LAYOUT_REVOLUTION_NUMBER_LANDSCAPE = 2.5f;
150 const float DEPTH_LAYOUT_HEIGHT_SCALE = 20.0f;
151 const float MIN_SWIPE_DISTANCE = 15.0f;
152 const float MIN_SWIPE_SPEED = 5.0f;
154 const float SELECTION_BORDER_WIDTH = 3.0f;
155 const float BUTTON_BORDER = -10.0f;
156 const float MENU_OPTION_HEIGHT(140.0f);
157 const float LABEL_TEXT_SIZE_Y = 20.0f;
159 const char* DEFAULT_TEXT_STYLE_FONT_FAMILY("HelveticaNue");
160 const char* DEFAULT_TEXT_STYLE_FONT_STYLE("Regular");
161 const PointSize DEFAULT_TEXT_STYLE_POINT_SIZE( 5.0f );
162 const TextStyle::Weight DEFAULT_TEXT_STYLE_WEIGHT(Dali::TextStyle::MEDIUM);
163 const Vector4 DEFAULT_TEXT_STYLE_COLOR(1.0f, 1.0f, 1.0f, 1.0f);
165 const Vector3 INITIAL_OFFSCREEN_POSITION( 1000.0f, 0, -1000.0f );
167 struct BorderSizeConstraintFunction
169 Vector3 operator()(const Vector3& current,
170 const PropertyInput& parentSize)
172 return parentSize.GetVector3() + ITEM_BORDER_MARGIN_SIZE;
176 static Vector3 DepthLayoutItemSizeFunctionPortrait(unsigned int numberOfColumns, float layoutWidth)
178 float width = (layoutWidth / static_cast<float>(numberOfColumns + 1)) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_PORTRAIT;
181 return Vector3(width, width, width);
184 static Vector3 DepthLayoutItemSizeFunctionLandscape(unsigned int numberOfColumns, float layoutWidth)
186 float width = (layoutWidth / static_cast<float>(numberOfColumns + 1)) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_LANDSCAPE;
189 return Vector3(width, width, width);
192 static float DepthLayoutBottomMarginFunctionPortrait(float layoutHeight)
194 return layoutHeight * DEPTH_LAYOUT_BOTTOM_MARGIN_FACTOR_PORTRAIT;
197 static float DepthLayoutBottomMarginFunctionLandscape(float layoutHeight)
199 return layoutHeight * DEPTH_LAYOUT_BOTTOM_MARGIN_FACTOR_LANDSCAPE;
205 * This example shows how to use ItemView UI control.
206 * There are three layouts created for ItemView, i.e., Spiral, Depth and Grid.
207 * There is one button in the upper-left corner for quitting the application and
208 * another button in the upper-right corner for switching between different layouts.
210 class ItemViewExample : public ConnectionTracker, public ItemFactory
228 * @param application class, stored as reference
230 ItemViewExample( Application& application )
231 : mApplication( application ),
232 mMode( MODE_NORMAL ),
235 mCurrentLayout( SPIRAL_LAYOUT ),
236 mDurationSeconds( 1.0f ),
237 mAlphaFuncIndex( 0u )
239 // Connect to the Application's Init signal
240 mApplication.InitSignal().Connect(this, &ItemViewExample::OnInit);
244 * This method gets called once the main loop of application is up and running
246 void OnInit(Application& app)
248 Stage stage = Dali::Stage::GetCurrent();
249 stage.KeyEventSignal().Connect(this, &ItemViewExample::OnKeyEvent);
251 Vector2 stageSize = Stage::GetCurrent().GetSize();
253 // Create a border image shared by all the item actors
254 mBorderImage = ResourceImage::New(ITEM_BORDER_IMAGE_PATH);
256 // Creates a default view with a default tool bar.
257 // The view is added to the stage.
258 Layer contents = DemoHelper::CreateView( mApplication,
265 mView.OrientationAnimationStartedSignal().Connect( this, &ItemViewExample::OnOrientationChanged );
267 // Create an edit mode button. (left of toolbar)
268 Toolkit::PushButton editButton = Toolkit::PushButton::New();
269 editButton.SetBackgroundImage( ResourceImage::New( EDIT_IMAGE ) );
270 editButton.ClickedSignal().Connect( this, &ItemViewExample::OnModeButtonClicked);
271 editButton.SetLeaveRequired( true );
272 mToolBar.AddControl( editButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalLeft, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
274 // Create a layout toggle button. (right of toolbar)
275 mLayoutButton = Toolkit::PushButton::New();
276 mLayoutButton.SetBackgroundImage( ResourceImage::New( SPIRAL_LAYOUT_IMAGE ) );
277 mLayoutButton.ClickedSignal().Connect( this, &ItemViewExample::OnLayoutButtonClicked);
278 mLayoutButton.SetLeaveRequired( true );
279 mToolBar.AddControl( mLayoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
281 // Create a delete button (bottom right of screen)
282 mDeleteButton = Toolkit::PushButton::New();
283 mDeleteButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
284 mDeleteButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
285 mDeleteButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
286 mDeleteButton.SetDrawMode( DrawMode::OVERLAY );
287 mDeleteButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_IMAGE ) );
288 mDeleteButton.SetButtonImage( ResourceImage::New( DELETE_IMAGE ) );
289 mDeleteButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
290 mDeleteButton.ClickedSignal().Connect( this, &ItemViewExample::OnDeleteButtonClicked);
291 mDeleteButton.SetLeaveRequired( true );
292 mDeleteButton.SetVisible( false );
293 stage.Add( mDeleteButton );
295 // Create an insert button (bottom right of screen)
296 mInsertButton = Toolkit::PushButton::New();
297 mInsertButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
298 mInsertButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
299 mInsertButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
300 mInsertButton.SetDrawMode( DrawMode::OVERLAY );
301 mInsertButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_IMAGE ) );
302 mInsertButton.SetButtonImage( ResourceImage::New( INSERT_IMAGE ) );
303 mInsertButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
304 mInsertButton.ClickedSignal().Connect( this, &ItemViewExample::OnInsertButtonClicked);
305 mInsertButton.SetLeaveRequired( true );
306 mInsertButton.SetVisible( false );
307 stage.Add( mInsertButton );
309 // Create an replace button (bottom right of screen)
310 mReplaceButton = Toolkit::PushButton::New();
311 mReplaceButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
312 mReplaceButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
313 mReplaceButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
314 mReplaceButton.SetDrawMode( DrawMode::OVERLAY );
315 mReplaceButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_IMAGE ) );
316 mReplaceButton.SetButtonImage( ResourceImage::New( REPLACE_IMAGE ) );
317 mReplaceButton.SetSize( stageSize.width * 0.15f, stageSize.width * 0.15f );
318 mReplaceButton.ClickedSignal().Connect( this, &ItemViewExample::OnReplaceButtonClicked);
319 mReplaceButton.SetLeaveRequired( true );
320 mReplaceButton.SetVisible( false );
321 stage.Add( mReplaceButton );
323 // Create the item view actor
324 mItemView = ItemView::New(*this);
325 mItemView.SetParentOrigin(ParentOrigin::CENTER);
326 mItemView.SetAnchorPoint(AnchorPoint::CENTER);
328 // Display item view on the stage
329 stage.Add( mItemView );
331 // Create the layouts
332 mSpiralLayout = SpiralLayout::New();
333 mDepthLayout = DepthLayout::New();
334 mGridLayout = GridLayout::New();
336 // Add the layouts to item view
337 mItemView.AddLayout(*mSpiralLayout);
338 mItemView.AddLayout(*mDepthLayout);
339 mItemView.AddLayout(*mGridLayout);
341 mItemView.SetMinimumSwipeDistance(MIN_SWIPE_DISTANCE);
342 mItemView.SetMinimumSwipeSpeed(MIN_SWIPE_SPEED);
344 // Activate the spiral layout
345 UseLayout(mCurrentLayout, 0.0f);
346 mItemView.SetKeyboardFocusable( true );
347 KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &ItemViewExample::OnKeyboardPreFocusChange );
349 // Set the title and icon to the current layout
353 // Store one 1x1 white image for multiple items to share for backgrounds:
354 mWhiteImage = BitmapImage::WHITE();
357 Actor OnKeyboardPreFocusChange( Actor current, Actor proposed, Control::KeyboardFocusNavigationDirection direction )
359 if ( !current && !proposed )
368 * Switch to a different item view layout
370 void UseLayout(int layoutId, float duration)
372 // Set the new orientation to the layout
373 mItemView.GetLayout(layoutId)->SetOrientation(static_cast<ControlOrientation::Type>(mOrientation / 90));
375 Vector2 stageSize = Stage::GetCurrent().GetSize();
377 if(layoutId == SPIRAL_LAYOUT)
379 mSpiralLayout->SetRevolutionDistance(stageSize.height / Stage::GetCurrent().GetDpi().y * 45.0f);
382 if(layoutId == GRID_LAYOUT)
384 // Set up the grid layout according to the new orientation
385 float layoutWidth = Toolkit::IsHorizontal(mGridLayout->GetOrientation()) ? stageSize.height : stageSize.width;
386 float gridItemSize = (layoutWidth / mGridLayout->GetNumberOfColumns()) * 0.5f;
387 mGridLayout->SetScrollSpeedFactor(mGridLayout->GetNumberOfColumns() / gridItemSize);
389 float toolbarHeight = mToolBar.GetCurrentSize().y;
390 mGridLayout->SetTopMargin(toolbarHeight + mGridLayout->GetRowSpacing());
393 if(layoutId == DEPTH_LAYOUT)
395 // Set up the depth layout according to the new orientation
396 if(Toolkit::IsVertical(mDepthLayout->GetOrientation()))
398 mDepthLayout->SetRowSpacing(stageSize.height * DEPTH_LAYOUT_ROW_SPACING_FACTOR);
399 mDepthLayout->SetNumberOfRows(DEPTH_LAYOUT_ROWS_PORTRAIT);
400 mDepthLayout->SetTiltAngle( Degree( DEPTH_LAYOUT_TILT_ANGLE_PORTRAIT - mDepthLayout->GetNumberOfColumns() ) );
401 mDepthLayout->SetItemSizeFunction(DepthLayoutItemSizeFunctionPortrait);
402 mDepthLayout->SetBottomMarginFunction(DepthLayoutBottomMarginFunctionPortrait);
406 mDepthLayout->SetRowSpacing(stageSize.width * DEPTH_LAYOUT_ROW_SPACING_FACTOR);
407 mDepthLayout->SetNumberOfRows(DEPTH_LAYOUT_ROWS_LANDSCAPE);
408 mDepthLayout->SetTiltAngle( Degree( DEPTH_LAYOUT_TILT_ANGLE_LANDSCAPE - mDepthLayout->GetNumberOfColumns() ) );
409 mDepthLayout->SetItemSizeFunction(DepthLayoutItemSizeFunctionLandscape);
410 mDepthLayout->SetBottomMarginFunction(DepthLayoutBottomMarginFunctionLandscape);
414 // Enable anchoring for depth layout only
415 mItemView.SetAnchoring(layoutId == DEPTH_LAYOUT);
417 // Activate the layout
418 mItemView.ActivateLayout(layoutId, Vector3(stageSize.x, stageSize.y, stageSize.x), duration);
422 * Orientation changed signal callback
425 void OnOrientationChanged( View view, Animation& animation, const Orientation& orientation )
427 const unsigned int angle = orientation.GetDegrees();
429 // If orientation really changed
430 if( mOrientation != angle )
432 // Remember orientation
433 mOrientation = angle;
435 UseLayout(mCurrentLayout, mDurationSeconds);
439 bool OnLayoutButtonClicked( Toolkit::Button button )
441 // Switch to the next layout
442 mCurrentLayout = (mCurrentLayout + 1) % mItemView.GetLayoutCount();
444 UseLayout(mCurrentLayout, mDurationSeconds);
452 bool OnModeButtonClicked( Toolkit::Button button )
459 void SwitchToNextMode()
466 mMode = MODE_REMOVE_MANY;
467 EnterRemoveManyMode();
471 case MODE_REMOVE_MANY:
473 ExitRemoveManyMode();
482 mMode = MODE_INSERT_MANY;
483 EnterInsertManyMode();
487 case MODE_INSERT_MANY:
489 ExitInsertManyMode();
490 mMode = MODE_REPLACE;
498 mMode = MODE_REPLACE_MANY;
499 EnterReplaceManyMode();
503 case MODE_REPLACE_MANY:
505 ExitReplaceManyMode();
521 void EnterRemoveMode()
523 SetTitle("Edit: Remove");
525 mTapDetector = TapGestureDetector::New();
527 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
529 mTapDetector.Attach(mItemView.GetChildAt(i));
532 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::RemoveOnTap );
535 void ExitRemoveMode()
537 mTapDetector.Reset();
540 void RemoveOnTap( Actor actor, const TapGesture& tap )
542 mItemView.RemoveItem( mItemView.GetItemId(actor), 0.5f );
545 void EnterRemoveManyMode()
547 SetTitle("Edit: Remove Many");
549 mDeleteButton.SetVisible( true );
551 mTapDetector = TapGestureDetector::New();
553 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
555 Actor child = mItemView.GetChildAt( i );
556 Actor box = child.FindChildByName( "CheckBox" );
560 mTapDetector.Attach( child );
561 box.SetVisible( true );
565 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
568 void ExitRemoveManyMode()
570 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
572 Actor child = mItemView.GetChildAt( i );
573 Actor box = child.FindChildByName( "CheckBox" );
577 box.SetVisible( false );
579 Actor tick = box.FindChildByName( "Tick" );
582 tick.SetVisible( false );
587 mTapDetector.Reset();
589 mDeleteButton.SetVisible( false );
592 void SelectOnTap( Actor actor, const TapGesture& tap )
594 Actor tick = actor.FindChildByName( "Tick" );
597 tick.SetVisible( !tick.IsVisible() );
601 bool OnDeleteButtonClicked( Toolkit::Button button )
603 ItemIdContainer removeList;
605 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
607 Actor child = mItemView.GetChildAt( i );
608 Actor tick = child.FindChildByName( "Tick" );
610 if( tick && tick.IsVisible() )
612 removeList.push_back( mItemView.GetItemId(child) );
616 if( ! removeList.empty() )
618 mItemView.RemoveItems( removeList, 0.5f );
624 void EnterInsertMode()
626 SetTitle("Edit: Insert");
628 mTapDetector = TapGestureDetector::New();
630 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
632 mTapDetector.Attach( mItemView.GetChildAt(i) );
635 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::InsertOnTap );
638 void ExitInsertMode()
640 mTapDetector.Reset();
643 void InsertOnTap( Actor actor, const TapGesture& tap )
645 ItemId id = mItemView.GetItemId( actor );
647 Actor newActor = NewItem( rand() );
649 mItemView.InsertItem( Item(id,newActor), 0.5f );
652 void EnterInsertManyMode()
654 SetTitle("Edit: Insert Many");
656 mInsertButton.SetVisible( true );
658 mTapDetector = TapGestureDetector::New();
660 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
662 Actor child = mItemView.GetChildAt( i );
663 Actor box = child.FindChildByName( "CheckBox" );
667 mTapDetector.Attach( child );
668 box.SetVisible( true );
672 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
675 void ExitInsertManyMode()
677 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
679 Actor child = mItemView.GetChildAt( i );
680 Actor box = child.FindChildByName( "CheckBox" );
684 box.SetVisible( false );
686 Actor tick = box.FindChildByName( "Tick" );
689 tick.SetVisible( false );
694 mTapDetector.Reset();
696 mInsertButton.SetVisible( false );
699 bool OnInsertButtonClicked( Toolkit::Button button )
701 ItemContainer insertList;
703 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
705 Actor child = mItemView.GetChildAt( i );
706 Actor tick = child.FindChildByName( "Tick" );
708 if( tick && tick.IsVisible() )
710 insertList.push_back( Item( mItemView.GetItemId(child), NewItem(rand()) ) );
714 if( ! insertList.empty() )
716 mItemView.InsertItems( insertList, 0.5f );
722 void EnterReplaceMode()
724 SetTitle("Edit: Replace");
726 mTapDetector = TapGestureDetector::New();
728 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
730 mTapDetector.Attach(mItemView.GetChildAt(i));
733 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::ReplaceOnTap );
736 void ReplaceOnTap( Actor actor, const TapGesture& tap )
738 mItemView.ReplaceItem( Item( mItemView.GetItemId(actor), NewItem(rand()) ), 0.5f );
741 void ExitReplaceMode()
743 mTapDetector.Reset();
746 void EnterReplaceManyMode()
748 SetTitle("Edit: Replace Many");
750 mReplaceButton.SetVisible( true );
752 mTapDetector = TapGestureDetector::New();
754 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
756 Actor child = mItemView.GetChildAt( i );
757 Actor box = child.FindChildByName( "CheckBox" );
761 mTapDetector.Attach( child );
762 box.SetVisible( true );
766 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::SelectOnTap );
769 void ExitReplaceManyMode()
771 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
773 Actor child = mItemView.GetChildAt( i );
774 Actor box = child.FindChildByName( "CheckBox" );
778 box.SetVisible( false );
780 Actor tick = box.FindChildByName( "Tick" );
783 tick.SetVisible( false );
788 mTapDetector.Reset();
790 mReplaceButton.SetVisible( false );
793 bool OnReplaceButtonClicked( Toolkit::Button button )
795 ItemContainer replaceList;
797 for( unsigned int i = 0u; i < mItemView.GetChildCount(); ++i )
799 Actor child = mItemView.GetChildAt( i );
800 Actor tick = child.FindChildByName( "Tick" );
802 if( tick && tick.IsVisible() )
804 replaceList.push_back( Item( mItemView.GetItemId(child), NewItem(rand()) ) );
808 if( ! replaceList.empty() )
810 mItemView.ReplaceItems( replaceList, 0.5f );
816 void SetLayoutTitle()
818 if( MODE_NORMAL == mMode )
820 std::stringstream ss(APPLICATION_TITLE);
821 switch(mCurrentLayout)
824 ss << APPLICATION_TITLE << ": " << SPIRAL_LABEL;
827 ss << APPLICATION_TITLE << ": " << GRID_LABEL;
830 ss << APPLICATION_TITLE << ": " << DEPTH_LABEL;
839 void SetLayoutImage()
843 switch( mCurrentLayout )
847 mLayoutButton.SetBackgroundImage( ResourceImage::New( SPIRAL_LAYOUT_IMAGE ) );
853 mLayoutButton.SetBackgroundImage( ResourceImage::New( GRID_LAYOUT_IMAGE ) );
859 mLayoutButton.SetBackgroundImage( ResourceImage::New( DEPTH_LAYOUT_IMAGE ) );
869 public: // From ItemFactory
872 * Query the number of items available from the factory.
873 * The maximum available item has an ID of GetNumberOfItems() - 1.
875 virtual unsigned int GetNumberOfItems()
877 return NUM_IMAGES * 10;
881 * Create an Actor to represent a visible item.
883 * @return the created actor.
885 virtual Actor NewItem(unsigned int itemId)
887 // Create an image actor for this item
888 Image image = ResourceImage::New( IMAGE_PATHS[itemId % NUM_IMAGES] );
889 Actor actor = ImageActor::New(image);
890 actor.SetPosition( INITIAL_OFFSCREEN_POSITION );
892 // Add a border image child actor
893 ImageActor borderActor = ImageActor::New(mBorderImage);
894 borderActor.SetParentOrigin( ParentOrigin::CENTER );
895 borderActor.SetAnchorPoint( AnchorPoint::CENTER );
896 borderActor.SetPosition( 0.f, 0.f, 1.f );
897 borderActor.SetStyle( ImageActor::STYLE_NINE_PATCH );
898 borderActor.SetNinePatchBorder( Vector4( ITEM_IMAGE_BORDER_LEFT, ITEM_IMAGE_BORDER_TOP, ITEM_IMAGE_BORDER_RIGHT, ITEM_IMAGE_BORDER_BOTTOM ) );
899 borderActor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR ); // darken with parent image-actor
901 Constraint constraint = Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), BorderSizeConstraintFunction() );
902 borderActor.ApplyConstraint(constraint);
903 actor.Add(borderActor);
904 actor.SetKeyboardFocusable( true );
906 Vector3 spiralItemSize;
907 static_cast<ItemLayout&>(*mSpiralLayout).GetItemSize( 0u, Vector3( Stage::GetCurrent().GetSize() ), spiralItemSize );
909 // Add a checkbox child actor; invisible until edit-mode is enabled
911 ImageActor checkbox = ImageActor::New( mWhiteImage );
912 checkbox.SetName( "CheckBox" );
913 checkbox.SetColor( Vector4(0.0f,0.0f,0.0f,0.6f) );
914 checkbox.SetParentOrigin( ParentOrigin::TOP_RIGHT );
915 checkbox.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
916 checkbox.SetSize( spiralItemSize.width * 0.2f, spiralItemSize.width * 0.2f );
917 checkbox.SetPosition( -SELECTION_BORDER_WIDTH, SELECTION_BORDER_WIDTH );
918 checkbox.SetZ( 1.0f );
919 if( MODE_REMOVE_MANY != mMode &&
920 MODE_INSERT_MANY != mMode &&
921 MODE_REPLACE_MANY != mMode )
923 checkbox.SetVisible( false );
925 actor.Add( checkbox );
927 ImageActor tick = ImageActor::New( ResourceImage::New(SELECTED_IMAGE) );
928 tick.SetColorMode( USE_OWN_COLOR );
929 tick.SetName( "Tick" );
930 tick.SetParentOrigin( ParentOrigin::TOP_RIGHT );
931 tick.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
932 tick.SetSize( spiralItemSize.width * 0.2f, spiralItemSize.width * 0.2f );
934 tick.SetVisible( false );
935 checkbox.Add( tick );
937 // Connect new items for various editing modes
940 mTapDetector.Attach( actor );
949 * Sets/Updates the title of the View
950 * @param[in] title The new title for the view.
952 void SetTitle(const std::string& title)
956 mTitleActor = TextView::New();
957 // Add title to the tool bar.
958 mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HorizontalCenter );
961 Font font = Font::New();
962 mTitleActor.SetText( title );
963 mTitleActor.SetSize( font.MeasureText( title ) );
964 mTitleActor.SetStyleToCurrentText(DemoHelper::GetDefaultTextStyle());
969 Stage stage = Stage::GetCurrent();
970 const float popupWidth = stage.GetSize().x * 0.75f;
972 mMenu = Toolkit::Popup::New();
973 mMenu.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
974 mMenu.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
975 mMenu.OutsideTouchedSignal().Connect( this, &ItemViewExample::HideMenu );
978 TableView tableView = TableView::New( 0, 0 );
979 Vector2 tableSize = Vector2( popupWidth, MENU_OPTION_HEIGHT * 2 );
980 tableView.SetSize( tableSize );
981 mMenu.Add( tableView );
983 Slider slider = Slider::New();
984 slider.SetProperty( Slider::LOWER_BOUND_PROPERTY, 0.0f );
985 slider.SetProperty( Slider::UPPER_BOUND_PROPERTY, 3.0f );
986 slider.SetProperty( Slider::VALUE_PROPERTY, mDurationSeconds );
987 slider.SetProperty( Slider::VALUE_PRECISION_PROPERTY, 2 );
988 slider.SetProperty( Slider::SHOW_POPUP_PROPERTY, true );
989 slider.ValueChangedSignal().Connect( this, &ItemViewExample::SliderValueChange );
990 tableView.AddChild( slider, TableView::CellPosition( 0, 0 ) );
991 tableView.SetRelativeHeight( 0, 0.5f );
993 TextStyle defaultTextStyle;
994 defaultTextStyle.SetFontName(DEFAULT_TEXT_STYLE_FONT_FAMILY);
995 defaultTextStyle.SetFontStyle(DEFAULT_TEXT_STYLE_FONT_STYLE);
996 defaultTextStyle.SetFontPointSize(DEFAULT_TEXT_STYLE_POINT_SIZE);
997 defaultTextStyle.SetWeight(DEFAULT_TEXT_STYLE_WEIGHT);
998 defaultTextStyle.SetTextColor(DEFAULT_TEXT_STYLE_COLOR);
1000 TextView text = TextView::New( "Duration" );
1001 text.SetAnchorPoint( ParentOrigin::TOP_LEFT );
1002 text.SetParentOrigin( ParentOrigin::TOP_LEFT );
1003 text.SetTextAlignment( Dali::Toolkit::Alignment::HorizontalLeft );
1004 text.SetStyleToCurrentText( defaultTextStyle );
1005 text.SetSize( 0.0f, LABEL_TEXT_SIZE_Y );
1006 text.ApplyConstraint( Dali::Constraint::New<float>( Dali::Actor::SIZE_WIDTH, Dali::ParentSource( Dali::Actor::SIZE_WIDTH ), Dali::EqualToConstraint() ) );
1010 Actor textContainer = Actor::New();
1011 mAlphaFunctionText = TextView::New( ALPHA_FUNCTIONS_TEXT[mAlphaFuncIndex] );
1012 mAlphaFunctionText.SetAnchorPoint( ParentOrigin::CENTER );
1013 mAlphaFunctionText.SetParentOrigin( ParentOrigin::CENTER );
1014 mAlphaFunctionText.SetTextAlignment( Toolkit::Alignment::VerticalCenter );
1015 textContainer.Add( mAlphaFunctionText );
1016 tableView.AddChild( textContainer, TableView::CellPosition( 1, 0 ) );
1017 tableView.SetRelativeHeight( 0, 0.5f );
1019 mTapDetector = TapGestureDetector::New();
1020 mTapDetector.Attach(mAlphaFunctionText);
1021 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::ChangeAlphaFunctionOnTap );
1023 text = TextView::New( "Alpha Function" );
1024 text.SetAnchorPoint( ParentOrigin::TOP_LEFT );
1025 text.SetParentOrigin( ParentOrigin::TOP_LEFT );
1026 text.SetTextAlignment( Dali::Toolkit::Alignment::HorizontalLeft );
1027 text.SetStyleToCurrentText( defaultTextStyle );
1028 text.SetSize( 0.0f, LABEL_TEXT_SIZE_Y );
1029 text.ApplyConstraint( Dali::Constraint::New<float>( Dali::Actor::SIZE_WIDTH, Dali::ParentSource( Dali::Actor::SIZE_WIDTH ), Dali::EqualToConstraint() ) );
1030 textContainer.Add( text );
1036 bool SliderValueChange( Toolkit::Slider slider, float value )
1038 mDurationSeconds = value;
1043 void ChangeAlphaFunctionOnTap( Actor actor, const TapGesture& tap )
1045 if( NUM_ALPHA_FUNCTIONS <= ++mAlphaFuncIndex )
1047 mAlphaFuncIndex = 0;
1050 if( mAlphaFunctionText )
1052 mAlphaFunctionText.SetText( ALPHA_FUNCTIONS_TEXT[mAlphaFuncIndex] );
1057 mItemView.GetActiveLayout()->SetAlphaFunction( ALPHA_FUNCTIONS[mAlphaFuncIndex] );
1063 mTapDetector.Reset();
1075 * Main key event handler
1077 void OnKeyEvent(const KeyEvent& event)
1079 if(event.state == KeyEvent::Down)
1081 if( IsKey( event, DALI_KEY_MENU ) )
1092 else if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
1100 mApplication.Quit();
1108 Application& mApplication;
1112 Toolkit::View mView;
1113 unsigned int mOrientation;
1115 Toolkit::ToolBar mToolBar;
1116 TextView mTitleActor; ///< The Toolbar's Title.
1120 unsigned int mCurrentLayout;
1121 float mDurationSeconds;
1123 SpiralLayoutPtr mSpiralLayout;
1124 DepthLayoutPtr mDepthLayout;
1125 GridLayoutPtr mGridLayout;
1127 Toolkit::Popup mMenu;
1129 TapGestureDetector mTapDetector;
1130 Toolkit::PushButton mLayoutButton;
1131 Toolkit::PushButton mDeleteButton;
1132 Toolkit::PushButton mInsertButton;
1133 Toolkit::PushButton mReplaceButton;
1135 unsigned int mAlphaFuncIndex;
1136 TextView mAlphaFunctionText;
1137 BitmapImage mWhiteImage;
1140 void RunTest(Application& app)
1142 ItemViewExample test(app);
1147 int main(int argc, char **argv)
1149 Application app = Application::New(&argc, &argv);