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[] = { AlphaFunctions::Linear,
107 AlphaFunctions::EaseIn,
108 AlphaFunctions::EaseOut };
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 char* DEFAULT_TEXT_STYLE_FONT_FAMILY("HelveticaNue");
164 const char* DEFAULT_TEXT_STYLE_FONT_STYLE("Regular");
165 const PointSize DEFAULT_TEXT_STYLE_POINT_SIZE( 5.0f );
166 const TextStyle::Weight DEFAULT_TEXT_STYLE_WEIGHT(Dali::TextStyle::MEDIUM);
167 const Vector4 DEFAULT_TEXT_STYLE_COLOR(1.0f, 1.0f, 1.0f, 1.0f);
169 const Vector3 INITIAL_OFFSCREEN_POSITION( 1000.0f, 0, -1000.0f );
171 static Vector3 DepthLayoutItemSizeFunctionPortrait(unsigned int numberOfColumns, float layoutWidth)
173 float width = (layoutWidth / static_cast<float>(numberOfColumns + 1)) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_PORTRAIT;
176 return Vector3(width, width, width);
179 static Vector3 DepthLayoutItemSizeFunctionLandscape(unsigned int numberOfColumns, float layoutWidth)
181 float width = (layoutWidth / static_cast<float>(numberOfColumns + 1)) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_LANDSCAPE;
184 return Vector3(width, width, width);
187 static float DepthLayoutBottomMarginFunctionPortrait(float layoutHeight)
189 return layoutHeight * DEPTH_LAYOUT_BOTTOM_MARGIN_FACTOR_PORTRAIT;
192 static float DepthLayoutBottomMarginFunctionLandscape(float layoutHeight)
194 return layoutHeight * DEPTH_LAYOUT_BOTTOM_MARGIN_FACTOR_LANDSCAPE;
200 * This example shows how to use ItemView UI control.
201 * There are three layouts created for ItemView, i.e., Spiral, Depth and Grid.
202 * There is one button in the upper-left corner for quitting the application and
203 * another button in the upper-right corner for switching between different layouts.
205 class ItemViewExample : public ConnectionTracker, public ItemFactory
223 * @param application class, stored as reference
225 ItemViewExample( Application& application )
226 : mApplication( application ),
227 mMode( MODE_NORMAL ),
230 mCurrentLayout( SPIRAL_LAYOUT ),
231 mDurationSeconds( 1.0f ),
232 mAlphaFuncIndex( 0u )
234 // Connect to the Application's Init signal
235 mApplication.InitSignal().Connect(this, &ItemViewExample::OnInit);
239 * This method gets called once the main loop of application is up and running
241 void OnInit(Application& app)
243 Stage stage = Dali::Stage::GetCurrent();
244 stage.KeyEventSignal().Connect(this, &ItemViewExample::OnKeyEvent);
246 Vector2 stageSize = Stage::GetCurrent().GetSize();
248 // Create a border image shared by all the item actors
249 mBorderImage = ResourceImage::New(ITEM_BORDER_IMAGE_PATH);
251 // Creates a default view with a default tool bar.
252 // The view is added to the stage.
253 Layer contents = DemoHelper::CreateView( mApplication,
260 mView.OrientationAnimationStartedSignal().Connect( this, &ItemViewExample::OnOrientationChanged );
262 // Create an edit mode button. (left of toolbar)
263 Toolkit::PushButton editButton = Toolkit::PushButton::New();
264 editButton.SetBackgroundImage( ResourceImage::New( EDIT_IMAGE ) );
265 editButton.ClickedSignal().Connect( this, &ItemViewExample::OnModeButtonClicked);
266 editButton.SetLeaveRequired( true );
267 mToolBar.AddControl( editButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalLeft, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
269 // Create a layout toggle button. (right of toolbar)
270 mLayoutButton = Toolkit::PushButton::New();
271 mLayoutButton.SetBackgroundImage( ResourceImage::New( SPIRAL_LAYOUT_IMAGE ) );
272 mLayoutButton.ClickedSignal().Connect( this, &ItemViewExample::OnLayoutButtonClicked);
273 mLayoutButton.SetLeaveRequired( true );
274 mToolBar.AddControl( mLayoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
276 // Create a delete button (bottom right of screen)
277 mDeleteButton = Toolkit::PushButton::New();
278 mDeleteButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
279 mDeleteButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
280 mDeleteButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
281 mDeleteButton.SetDrawMode( DrawMode::OVERLAY );
282 mDeleteButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_IMAGE ) );
283 mDeleteButton.SetButtonImage( ResourceImage::New( DELETE_IMAGE ) );
284 mDeleteButton.SetResizePolicy( FIXED, ALL_DIMENSIONS );
285 mDeleteButton.SetPreferredSize( Vector2( stageSize.width * 0.15f, stageSize.width * 0.15f ) );
286 mDeleteButton.ClickedSignal().Connect( this, &ItemViewExample::OnDeleteButtonClicked);
287 mDeleteButton.SetLeaveRequired( true );
288 mDeleteButton.SetVisible( false );
289 stage.Add( mDeleteButton );
291 // Create an insert button (bottom right of screen)
292 mInsertButton = Toolkit::PushButton::New();
293 mInsertButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
294 mInsertButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
295 mInsertButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
296 mInsertButton.SetDrawMode( DrawMode::OVERLAY );
297 mInsertButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_IMAGE ) );
298 mInsertButton.SetButtonImage( ResourceImage::New( INSERT_IMAGE ) );
299 mInsertButton.SetResizePolicy( FIXED, ALL_DIMENSIONS );
300 mInsertButton.SetPreferredSize( Vector2( stageSize.width * 0.15f, stageSize.width * 0.15f ) );
301 mInsertButton.ClickedSignal().Connect( this, &ItemViewExample::OnInsertButtonClicked);
302 mInsertButton.SetLeaveRequired( true );
303 mInsertButton.SetVisible( false );
304 stage.Add( mInsertButton );
306 // Create an replace button (bottom right of screen)
307 mReplaceButton = Toolkit::PushButton::New();
308 mReplaceButton.SetParentOrigin(ParentOrigin::BOTTOM_RIGHT);
309 mReplaceButton.SetAnchorPoint(AnchorPoint::BOTTOM_RIGHT);
310 mReplaceButton.SetPosition( BUTTON_BORDER, BUTTON_BORDER );
311 mReplaceButton.SetDrawMode( DrawMode::OVERLAY );
312 mReplaceButton.SetBackgroundImage( ResourceImage::New( TOOLBAR_IMAGE ) );
313 mReplaceButton.SetButtonImage( ResourceImage::New( REPLACE_IMAGE ) );
314 mReplaceButton.SetResizePolicy( FIXED, ALL_DIMENSIONS );
315 mReplaceButton.SetPreferredSize( Vector2( stageSize.width * 0.15f, stageSize.width * 0.15f ) );
316 mReplaceButton.ClickedSignal().Connect( this, &ItemViewExample::OnReplaceButtonClicked);
317 mReplaceButton.SetLeaveRequired( true );
318 mReplaceButton.SetVisible( false );
319 stage.Add( mReplaceButton );
321 // Create the item view actor
322 mImageAtlas = CreateImageAtlas();
323 mItemView = ItemView::New(*this);
324 mItemView.SetRelayoutEnabled( false );
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 = BufferImage::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 unsigned int imageId = itemId % NUM_IMAGES;
889 ImageActor::PixelArea pixelArea( (imageId%NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_WIDTH,
890 (imageId/NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_HEIGHT,
893 Actor actor = ImageActor::New(mImageAtlas, pixelArea);
894 actor.SetPosition( INITIAL_OFFSCREEN_POSITION );
896 // Add a border image child actor
897 ImageActor borderActor = ImageActor::New(mBorderImage);
898 borderActor.SetParentOrigin( ParentOrigin::CENTER );
899 borderActor.SetAnchorPoint( AnchorPoint::CENTER );
900 borderActor.SetPosition( 0.f, 0.f, 1.f );
901 borderActor.SetStyle( ImageActor::STYLE_NINE_PATCH );
902 borderActor.SetNinePatchBorder( Vector4( ITEM_IMAGE_BORDER_LEFT, ITEM_IMAGE_BORDER_TOP, ITEM_IMAGE_BORDER_RIGHT, ITEM_IMAGE_BORDER_BOTTOM ) );
903 borderActor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR ); // darken with parent image-actor
904 borderActor.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
905 borderActor.SetSizeMode( SIZE_FIXED_OFFSET_FROM_PARENT );
906 borderActor.SetSizeModeFactor( ITEM_BORDER_MARGIN_SIZE );
907 actor.Add(borderActor);
908 actor.SetKeyboardFocusable( true );
910 Vector3 spiralItemSize;
911 static_cast<ItemLayout&>(*mSpiralLayout).GetItemSize( 0u, Vector3( Stage::GetCurrent().GetSize() ), spiralItemSize );
913 // Add a checkbox child actor; invisible until edit-mode is enabled
915 ImageActor checkbox = ImageActor::New( mWhiteImage );
916 checkbox.SetRelayoutEnabled( false );
917 checkbox.SetName( "CheckBox" );
918 checkbox.SetColor( Vector4(0.0f,0.0f,0.0f,0.6f) );
919 checkbox.SetParentOrigin( ParentOrigin::TOP_RIGHT );
920 checkbox.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
921 checkbox.SetSize( spiralItemSize.width * 0.2f, spiralItemSize.width * 0.2f );
922 checkbox.SetPosition( -SELECTION_BORDER_WIDTH, SELECTION_BORDER_WIDTH );
923 checkbox.SetZ( 1.0f );
924 if( MODE_REMOVE_MANY != mMode &&
925 MODE_INSERT_MANY != mMode &&
926 MODE_REPLACE_MANY != mMode )
928 checkbox.SetVisible( false );
930 actor.Add( checkbox );
932 ImageActor tick = ImageActor::New( ResourceImage::New(SELECTED_IMAGE) );
933 tick.SetRelayoutEnabled( false );
934 tick.SetColorMode( USE_OWN_COLOR );
935 tick.SetName( "Tick" );
936 tick.SetParentOrigin( ParentOrigin::TOP_RIGHT );
937 tick.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
938 tick.SetSize( spiralItemSize.width * 0.2f, spiralItemSize.width * 0.2f );
940 tick.SetVisible( false );
941 checkbox.Add( tick );
943 // Connect new items for various editing modes
946 mTapDetector.Attach( actor );
955 * Create an Atlas to tile the images inside.
957 Atlas CreateImageAtlas()
959 const unsigned int atlas_width = IMAGE_WIDTH*NUM_IMAGE_PER_ROW_IN_ATLAS;
960 const unsigned int atlas_height = IMAGE_HEIGHT*ceil( static_cast<float>(NUM_IMAGES)/ static_cast<float>(NUM_IMAGE_PER_ROW_IN_ATLAS));
961 Atlas atlas = Atlas::New(atlas_width, atlas_height, Pixel::RGB888);
963 for( unsigned int i = 0; i < NUM_IMAGES; i++ )
965 atlas.Upload( IMAGE_PATHS[i], (i%NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_WIDTH, (i/NUM_IMAGE_PER_ROW_IN_ATLAS)*IMAGE_HEIGHT );
972 * Sets/Updates the title of the View
973 * @param[in] title The new title for the view.
975 void SetTitle(const std::string& title)
979 mTitleActor = TextView::New();
980 // Add title to the tool bar.
981 mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HorizontalCenter );
984 Font font = Font::New();
985 mTitleActor.SetText( title );
986 mTitleActor.SetSize( font.MeasureText( title ) );
987 mTitleActor.SetStyleToCurrentText(DemoHelper::GetDefaultTextStyle());
992 Stage stage = Stage::GetCurrent();
993 const float popupWidth = stage.GetSize().x * 0.75f;
995 mMenu = Toolkit::Popup::New();
996 mMenu.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
997 mMenu.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
998 mMenu.SetResizePolicy( FIXED, ALL_DIMENSIONS );
999 mMenu.SetPreferredSize( Vector2( popupWidth, MENU_OPTION_HEIGHT * 2 ) );
1000 mMenu.OutsideTouchedSignal().Connect( this, &ItemViewExample::HideMenu );
1002 TableView tableView = TableView::New( 0, 0 );
1003 tableView.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
1004 mMenu.Add( tableView );
1006 Slider slider = Slider::New();
1007 slider.SetProperty( Slider::Property::LOWER_BOUND, 0.0f );
1008 slider.SetProperty( Slider::Property::UPPER_BOUND, 3.0f );
1009 slider.SetProperty( Slider::Property::VALUE, mDurationSeconds );
1010 slider.SetProperty( Slider::Property::VALUE_PRECISION, 2 );
1011 slider.SetProperty( Slider::Property::SHOW_POPUP, true );
1012 slider.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
1013 slider.ValueChangedSignal().Connect( this, &ItemViewExample::SliderValueChange );
1014 tableView.AddChild( slider, TableView::CellPosition( 0, 0 ) );
1016 TextStyle defaultTextStyle;
1017 defaultTextStyle.SetFontName(DEFAULT_TEXT_STYLE_FONT_FAMILY);
1018 defaultTextStyle.SetFontStyle(DEFAULT_TEXT_STYLE_FONT_STYLE);
1019 defaultTextStyle.SetFontPointSize(DEFAULT_TEXT_STYLE_POINT_SIZE);
1020 defaultTextStyle.SetWeight(DEFAULT_TEXT_STYLE_WEIGHT);
1021 defaultTextStyle.SetTextColor(DEFAULT_TEXT_STYLE_COLOR);
1023 TextView text = TextView::New( "Duration" );
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.SetResizePolicy( FILL_TO_PARENT, WIDTH );
1029 text.SetResizePolicy( FIXED, HEIGHT );
1030 text.SetPreferredSize( Vector2( 0.0f, LABEL_TEXT_SIZE_Y ) );
1034 Actor textContainer = Actor::New();
1035 textContainer.SetRelayoutEnabled( true );
1036 textContainer.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
1037 mAlphaFunctionText = TextView::New( ALPHA_FUNCTIONS_TEXT[mAlphaFuncIndex] );
1038 mAlphaFunctionText.SetAnchorPoint( ParentOrigin::CENTER );
1039 mAlphaFunctionText.SetParentOrigin( ParentOrigin::CENTER );
1040 mAlphaFunctionText.SetTextAlignment( Toolkit::Alignment::VerticalCenter );
1041 textContainer.Add( mAlphaFunctionText );
1042 tableView.AddChild( textContainer, TableView::CellPosition( 1, 0 ) );
1044 mTapDetector = TapGestureDetector::New();
1045 mTapDetector.Attach(mAlphaFunctionText);
1046 mTapDetector.DetectedSignal().Connect( this, &ItemViewExample::ChangeAlphaFunctionOnTap );
1048 text = TextView::New( "Alpha Function" );
1049 text.SetAnchorPoint( ParentOrigin::TOP_LEFT );
1050 text.SetParentOrigin( ParentOrigin::TOP_LEFT );
1051 text.SetTextAlignment( Dali::Toolkit::Alignment::HorizontalLeft );
1052 text.SetStyleToCurrentText( defaultTextStyle );
1053 text.SetResizePolicy( FILL_TO_PARENT, WIDTH );
1054 text.SetResizePolicy( FIXED, HEIGHT );
1055 text.SetPreferredSize( Vector2( 0.0f, LABEL_TEXT_SIZE_Y ) );
1056 textContainer.Add( text );
1058 mMenu.MarkDirtyForRelayout();
1063 bool SliderValueChange( Toolkit::Slider slider, float value )
1065 mDurationSeconds = value;
1070 void ChangeAlphaFunctionOnTap( Actor actor, const TapGesture& tap )
1072 if( NUM_ALPHA_FUNCTIONS <= ++mAlphaFuncIndex )
1074 mAlphaFuncIndex = 0;
1077 if( mAlphaFunctionText )
1079 mAlphaFunctionText.SetText( ALPHA_FUNCTIONS_TEXT[mAlphaFuncIndex] );
1084 mItemView.GetActiveLayout()->SetAlphaFunction( ALPHA_FUNCTIONS[mAlphaFuncIndex] );
1090 mTapDetector.Reset();
1102 * Main key event handler
1104 void OnKeyEvent(const KeyEvent& event)
1106 if(event.state == KeyEvent::Down)
1108 if( IsKey( event, DALI_KEY_MENU ) )
1119 else if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
1127 mApplication.Quit();
1135 Application& mApplication;
1139 Toolkit::View mView;
1140 unsigned int mOrientation;
1142 Toolkit::ToolBar mToolBar;
1143 TextView mTitleActor; ///< The Toolbar's Title.
1148 unsigned int mCurrentLayout;
1149 float mDurationSeconds;
1151 SpiralLayoutPtr mSpiralLayout;
1152 DepthLayoutPtr mDepthLayout;
1153 GridLayoutPtr mGridLayout;
1155 Toolkit::Popup mMenu;
1157 TapGestureDetector mTapDetector;
1158 Toolkit::PushButton mLayoutButton;
1159 Toolkit::PushButton mDeleteButton;
1160 Toolkit::PushButton mInsertButton;
1161 Toolkit::PushButton mReplaceButton;
1163 unsigned int mAlphaFuncIndex;
1164 TextView mAlphaFunctionText;
1165 BufferImage mWhiteImage;
1168 void RunTest(Application& app)
1170 ItemViewExample test(app);
1175 int main(int argc, char **argv)
1177 Application app = Application::New(&argc, &argv);