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.
19 #include "dali-table-view.h"
20 #include "examples/shared/view.h"
28 using namespace Dali::Toolkit;
30 ///////////////////////////////////////////////////////////////////////////////
35 const std::string BUTTON_BACKWARD( "Backward" );
36 const std::string BUTTON_FORWARD( "Forward" );
37 const std::string BUTTON_QUIT( "Quit" );
38 const std::string BUTTON_OK( "Ok" );
39 const std::string BUTTON_CANCEL( "Cancel" );
41 const std::string DEFAULT_BACKGROUND_IMAGE_PATH( DALI_IMAGE_DIR "background-gradient.jpg" );
42 const std::string LOGO_PATH( DALI_IMAGE_DIR "dali-logo.png" );
43 const std::string DEFAULT_TOOLBAR_IMAGE_PATH( DALI_IMAGE_DIR "top-bar.png" );
44 const std::string BUTTON_BACKGROUND(DALI_IMAGE_DIR "button-background.png");
45 const std::string TILE_BACKGROUND(DALI_IMAGE_DIR "item-background.png");
46 const std::string TILE_BACKGROUND_ALPHA(DALI_IMAGE_DIR "item-background-alpha.png");
48 const char * const DEFAULT_TOOLBAR_TEXT( "TOUCH TO LAUNCH EXAMPLE" );
50 const float BUTTON_PRESS_ANIMATION_TIME = 0.25f; ///< Time to perform button scale effect.
51 const float ROTATE_ANIMATION_TIME = 0.5f; ///< Time to perform rotate effect.
52 const int MAX_PAGES = 256; ///< Maximum pages (arbitrary safety limit)
53 const int EXAMPLES_PER_ROW = 3;
54 const int ROWS_PER_PAGE = 3;
55 const int EXAMPLES_PER_PAGE = EXAMPLES_PER_ROW * ROWS_PER_PAGE;
56 const float TOP_ROW_HEIGHT = 35.0f;
57 const float BOTTOM_ROW_HEIGHT = 35.0f;
58 const int BOTTOM_PADDING_HEIGHT = 40;
59 const int LOGO_BOTTOM_PADDING_HEIGHT = 30;
60 const Vector3 TABLE_RELATIVE_SIZE(0.9f, 1.0f, 0.8f ); ///< TableView's relative size to the entire stage.
61 const float STENCIL_RELATIVE_SIZE = 1.0f;
63 const float EFFECT_SNAP_DURATION = 0.66f; ///< Scroll Snap Duration for Effects
64 const float EFFECT_FLICK_DURATION = 0.5f; ///< Scroll Flick Duration for Effects
65 const Vector3 ANGLE_CUBE_PAGE_ROTATE(Math::PI * 0.5f, Math::PI * 0.5f, 0.0f);
67 const int NUM_BACKGROUND_IMAGES = 20;
68 const float BACKGROUND_SWIPE_SCALE = 0.025f;
69 const float BACKGROUND_SPREAD_SCALE = 1.5f;
70 const float SCALE_MOD = 1000.0f * Math::PI * 2.0f;
71 const float SCALE_SPEED = 10.0f;
72 const float SCALE_SPEED_SIN = 0.1f;
74 const unsigned int BACKGROUND_ANIMATION_DURATION = 15000; // 15 secs
76 const float BACKGROUND_Z = -1000.0f;
77 const float BACKGROUND_SIZE_SCALE = 2.0f;
78 const Vector4 BACKGROUND_COLOR( 1.0f, 1.0f, 1.0f, 1.0f );
81 const std::string DEFAULT_TEXT_STYLE_FONT_FAMILY("HelveticaNeue");
82 const std::string DEFAULT_TEXT_STYLE_FONT_STYLE("Regular");
83 const Dali::PointSize DEFAULT_TEXT_STYLE_POINT_SIZE( 8.0f );
84 const Dali::TextStyle::Weight DEFAULT_TEXT_STYLE_WEIGHT(Dali::TextStyle::REGULAR);
85 const Dali::Vector4 DEFAULT_TEXT_STYLE_COLOR(0.7f, 0.7f, 0.7f, 1.0f);
87 const std::string TABLE_TEXT_STYLE_FONT_FAMILY("HelveticaNeue");
88 const std::string TABLE_TEXT_STYLE_FONT_STYLE("Regular");
89 const Dali::PointSize TABLE_TEXT_STYLE_POINT_SIZE( 8.0f );
90 const Dali::TextStyle::Weight TABLE_TEXT_STYLE_WEIGHT(Dali::TextStyle::LIGHT);
91 const Dali::Vector4 TABLE_TEXT_STYLE_COLOR(0.0f, 0.0f, 0.0f, 1.0f);
94 TextStyle GetTableTextStyle()
97 textStyle.SetFontName(TABLE_TEXT_STYLE_FONT_FAMILY);
98 textStyle.SetFontStyle(TABLE_TEXT_STYLE_FONT_STYLE);
99 textStyle.SetFontPointSize( Dali::PointSize(DemoHelper::ScalePointSize(TABLE_TEXT_STYLE_POINT_SIZE)));
100 textStyle.SetWeight(TABLE_TEXT_STYLE_WEIGHT);
101 textStyle.SetTextColor(TABLE_TEXT_STYLE_COLOR);
106 * Creates the background image
108 ImageActor CreateBackground( std::string imagePath )
110 Image image = Image::New( imagePath );
111 ImageActor background = ImageActor::New( image );
113 background.SetAnchorPoint( AnchorPoint::CENTER );
114 background.SetParentOrigin( ParentOrigin::CENTER );
115 background.SetZ( -1.0f );
120 // These values depend on the tile image
121 const float IMAGE_BORDER_LEFT = 11.0f;
122 const float IMAGE_BORDER_RIGHT = IMAGE_BORDER_LEFT;
123 const float IMAGE_BORDER_TOP = IMAGE_BORDER_LEFT;
124 const float IMAGE_BORDER_BOTTOM = IMAGE_BORDER_LEFT;
127 * TableViewVisibilityConstraint
129 struct TableViewVisibilityConstraint
131 bool operator()( const bool& current,
132 const PropertyInput& pagePositionProperty,
133 const PropertyInput& pageSizeProperty )
135 // Only the tableview in the current page should be visible.
136 const Vector3& pagePosition = pagePositionProperty.GetVector3();
137 const Vector3& pageSize = pageSizeProperty.GetVector3();
138 return fabsf( pagePosition.x ) < pageSize.x;
143 * Constraint to wrap an actor in y that is moving vertically
145 Vector3 ShapeMovementConstraint( const Vector3& current,
146 const PropertyInput& shapeSizeProperty,
147 const PropertyInput& parentSizeProperty )
149 const Vector3& shapeSize = shapeSizeProperty.GetVector3();
150 const Vector3& parentSize = parentSizeProperty.GetVector3();
152 Vector3 pos( current );
153 if( pos.y + shapeSize.y * 0.5f < -parentSize.y * 0.5f )
155 pos.y += parentSize.y + shapeSize.y;
162 * Constraint to return a position for the background based on the scroll value
164 struct AnimScrollConstraint
168 AnimScrollConstraint( const Vector3& initialPos, float scale )
169 : mInitialPos( initialPos ),
175 Vector3 operator()( const Vector3& current, const PropertyInput& scrollProperty )
177 float scrollPos = scrollProperty.GetVector3().x;
179 return mInitialPos + Vector3( -scrollPos * mScale, 0.0f, 0.0f );
188 * Constraint to return a tracked world position added to the constant local position
190 struct TranslateLocalConstraint
194 TranslateLocalConstraint( const Vector3& localPos )
195 : mLocalPos( localPos )
199 Vector3 operator()( const Vector3& current, const PropertyInput& pagePosProperty )
201 Vector3 worldPos = pagePosProperty.GetVector3();
203 return ( worldPos + mLocalPos );
211 bool CompareByTitle( const Example& lhs, const Example& rhs )
213 return lhs.title < rhs.title;
218 DaliTableView::DaliTableView( Application& application )
219 : mApplication( application ),
221 mBackgroundImagePath( DEFAULT_BACKGROUND_IMAGE_PATH ),
222 mSortAlphabetically( false ),
223 mBackgroundAnimsPlaying( false )
225 application.InitSignal().Connect( this, &DaliTableView::Initialize );
228 DaliTableView::~DaliTableView()
232 void DaliTableView::AddExample( Example example )
234 mExampleList.push_back( example );
235 mExampleMap[ example.name ] = example;
238 void DaliTableView::SetBackgroundPath( std::string imagePath )
240 mBackgroundImagePath = imagePath;
243 void DaliTableView::SortAlphabetically( bool sortAlphabetically )
245 mSortAlphabetically = sortAlphabetically;
248 void DaliTableView::Initialize( Application& application )
250 Stage::GetCurrent().KeyEventSignal().Connect( this, &DaliTableView::OnKeyEvent );
252 Vector2 stageSize = Stage::GetCurrent().GetSize();
255 mBackground = CreateBackground( mBackgroundImagePath );
256 // set same size as parent actor
257 mBackground.SetSize( stageSize );
258 Stage::GetCurrent().Add( mBackground );
260 // Render entire content as overlays, as is all on same 2D plane.
261 mRootActor = TableView::New( 4, 1 );
262 mRootActor.SetAnchorPoint( AnchorPoint::CENTER );
263 mRootActor.SetParentOrigin( ParentOrigin::CENTER );
264 mRootActor.SetFixedHeight( 3, BOTTOM_PADDING_HEIGHT );
265 Stage::GetCurrent().Add( mRootActor );
268 Dali::Toolkit::ToolBar toolbar;
269 Dali::Layer toolBarLayer = DemoHelper::CreateToolbar(toolbar,
270 DEFAULT_TOOLBAR_IMAGE_PATH,
271 DEFAULT_TOOLBAR_TEXT,
272 DemoHelper::DEFAULT_VIEW_STYLE,
273 DemoHelper::GetDefaultTextStyle());
275 mRootActor.AddChild( toolBarLayer, TableView::CellPosition( 0, 0 ) );
276 mRootActor.SetFixedHeight( 0, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight );
279 mLogo = CreateLogo( LOGO_PATH );
280 mRootActor.SetFixedHeight( 1, mLogo.GetImage().GetHeight() + LOGO_BOTTOM_PADDING_HEIGHT );
282 Alignment alignment = Alignment::New();
283 alignment.Add(mLogo);
284 mRootActor.AddChild( alignment, TableView::CellPosition( 1, 0 ) );
286 // scrollview occupying the majority of the screen
287 mScrollView = ScrollView::New();
289 mScrollView.SetAnchorPoint( AnchorPoint::CENTER );
290 mScrollView.SetParentOrigin( ParentOrigin::CENTER );
291 mScrollView.ApplyConstraint( Dali::Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ), Dali::RelativeToConstraint( TABLE_RELATIVE_SIZE ) ) );
292 mScrollView.SetAxisAutoLock( true );
293 mScrollView.ScrollCompletedSignal().Connect( this, &DaliTableView::OnScrollComplete );
294 mScrollView.ScrollStartedSignal().Connect( this, &DaliTableView::OnScrollStart );
295 mScrollView.TouchedSignal().Connect( this, &DaliTableView::OnScrollTouched );
297 mScrollViewLayer = Layer::New();
298 mScrollViewLayer.SetAnchorPoint( AnchorPoint::CENTER );
299 mScrollViewLayer.SetParentOrigin( ParentOrigin::CENTER );
300 mScrollViewLayer.SetSize( stageSize );
301 mScrollViewLayer.Add( mScrollView );
302 mRootActor.AddChild( mScrollViewLayer, TableView::CellPosition( 2, 0 ) );
304 // Setup the scenegraph
305 // 1) Add scroll view effect and setup constraints on pages
306 ApplyScrollViewEffect();
308 // 2) Add pages and tiles
311 // 3) Populate scrollview with background so constraints on background layers can work with scrollview
312 SetupBackground( mScrollView, stageSize );
314 // 4) Remove constraints for inner cube effect
315 for( TableViewListIter pageIter = mTableViewList.begin(); pageIter != mTableViewList.end(); ++pageIter )
317 TableView page = *pageIter;
319 unsigned int numChildren = page.GetChildCount();
320 Actor pageActor = page;
321 for( unsigned int i=0; i<numChildren; ++i)
323 // Remove old effect's manual constraints.
324 Actor child = pageActor.GetChildAt(i);
327 child.RemoveConstraints();
332 // Set initial orientation
333 unsigned int degrees = application.GetOrientation().GetDegrees();
336 Dali::Window winHandle = application.GetWindow();
337 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
338 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE );
339 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
340 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
342 Dali::Orientation orientation = winHandle.GetOrientation();
343 orientation.ChangedSignal().Connect( this, &DaliTableView::OrientationChanged );
345 winHandle.ShowIndicator( Dali::Window::INVISIBLE );
348 mAnimationTimer = Timer::New( BACKGROUND_ANIMATION_DURATION );
349 mAnimationTimer.TickSignal().Connect( this, &DaliTableView::PauseBackgroundAnimation );
350 mAnimationTimer.Start();
351 mBackgroundAnimsPlaying = true;
353 KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &DaliTableView::OnKeyboardPreFocusChange );
354 KeyboardFocusManager::Get().FocusedActorActivatedSignal().Connect( this, &DaliTableView::OnFocusedActorActivated );
357 void DaliTableView::Populate()
359 const Vector2 stageSize = Stage::GetCurrent().GetSize();
361 const Size demoTileSize( 0.25f * stageSize.width, 0.25f * stageSize.height );
363 mTotalPages = ( mExampleList.size() + EXAMPLES_PER_PAGE - 1 ) / EXAMPLES_PER_PAGE;
365 // Populate ScrollView.
366 if( mExampleList.size() > 0 )
368 if( mSortAlphabetically )
370 sort( mExampleList.begin(), mExampleList.end(), CompareByTitle );
373 unsigned int exampleCount = 0;
374 ExampleListConstIter iter = mExampleList.begin();
375 for( int t = 0; t < mTotalPages; t++ )
377 // Create Table. (contains up to 9 Examples)
378 TableView tableView = TableView::New( 4, 3 );
379 // Add tableView to container.
380 mScrollView.Add( tableView );
381 ApplyEffectToPage( tableView, TABLE_RELATIVE_SIZE );
383 tableView.SetAnchorPoint( AnchorPoint::CENTER );
384 tableView.SetParentOrigin( ParentOrigin::CENTER );
385 // 2 pixels of padding
386 tableView.SetCellPadding( Size( 2.0f, 2.0f ) );
388 Constraint constraint = Constraint::New<Vector3>( Actor::SCALE,
389 LocalSource( Actor::SIZE ),
390 ParentSource( Actor::SIZE ),
391 ScaleToFitConstraint() );
392 tableView.ApplyConstraint(constraint);
394 // Apply visibility constraint to table view
395 Constraint visibleConstraint = Constraint::New< bool >( Actor::VISIBLE,
396 LocalSource( Actor::POSITION ),
397 ParentSource( Actor::SIZE ),
398 TableViewVisibilityConstraint() );
399 visibleConstraint.SetRemoveAction( Constraint::Discard );
400 tableView.ApplyConstraint( visibleConstraint );
402 // add cells to table
403 for( int y = 0; y < ROWS_PER_PAGE; y++ )
405 for( int x = 0; x < EXAMPLES_PER_ROW; x++ )
407 const Example& example = ( *iter );
409 Actor tile = CreateTile( example.name, example.title, demoTileSize, true );
410 FocusManager focusManager = FocusManager::Get();
411 focusManager.SetFocusOrder( tile, ++exampleCount );
412 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_LABEL,
414 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_TRAIT, "Tile" );
415 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_HINT,
416 "You can run this example" );
418 tableView.AddChild( tile, TableView::CellPosition( y, x ) );
421 if( iter == mExampleList.end() )
426 if( iter == mExampleList.end() )
433 tableView.SetFixedHeight( 3, BOTTOM_ROW_HEIGHT );
435 std::stringstream out;
436 out << ( t + 1 ) << " of " << mTotalPages;
437 Actor pageNumberText = CreateTile( "", out.str(), Size( 0.8f * stageSize.width, BOTTOM_ROW_HEIGHT ), false );
439 pageNumberText.ApplyConstraint( Constraint::New< Vector3 >( Actor::POSITION, Source( tableView, Actor::WORLD_POSITION),
440 TranslateLocalConstraint( Vector3( 0.0f, stageSize.y * 0.4f, 0.0f ) ) ) );
441 pageNumberText.ApplyConstraint( Constraint::New< Quaternion >( Actor::ROTATION, Source( tableView, Actor::WORLD_ROTATION ), EqualToConstraint() ) );
442 pageNumberText.ApplyConstraint( Constraint::New< Vector4 >( Actor::COLOR, Source( tableView, Actor::COLOR ), EqualToConstraint() ) );
444 //Stage::GetCurrent().Add( pageNumberText );
446 // Set tableview position
447 Vector3 tableViewPos( stageSize.x * TABLE_RELATIVE_SIZE.x * t, 0.0f, 0.0f );
448 tableView.SetPosition( tableViewPos );
450 mTableViewList.push_back( tableView );
452 if( iter == mExampleList.end() )
459 // Update Ruler info.
460 mScrollRulerX = new FixedRuler( stageSize.width * TABLE_RELATIVE_SIZE.x );
461 mScrollRulerY = new DefaultRuler();
462 mScrollRulerX->SetDomain( RulerDomain( 0.0f, mTotalPages * stageSize.width * TABLE_RELATIVE_SIZE.x, true ) );
463 mScrollRulerY->Disable();
464 mScrollView.SetRulerX( mScrollRulerX );
465 mScrollView.SetRulerY( mScrollRulerY );
468 void DaliTableView::OrientationChanged( Orientation orientation )
470 // TODO: Implement if orientation change required
473 void DaliTableView::Rotate( unsigned int degrees )
475 // Resize the root actor
476 Vector2 stageSize = Stage::GetCurrent().GetSize();
477 Vector3 targetSize( stageSize.x, stageSize.y, 1.0f );
479 if( degrees == 90 || degrees == 270 )
481 targetSize = Vector3( stageSize.y, stageSize.x, 1.0f );
484 if( mRotateAnimation )
486 mRotateAnimation.Stop();
487 mRotateAnimation.Clear();
490 mRotateAnimation = Animation::New( ROTATE_ANIMATION_TIME );
491 mRotateAnimation.RotateTo( mRootActor, Degree( 360 - degrees ), Vector3::ZAXIS, AlphaFunctions::EaseOut );
492 mRotateAnimation.Resize( mRootActor, targetSize, AlphaFunctions::EaseOut );
493 mRotateAnimation.Play();
496 Actor DaliTableView::CreateTile( const std::string& name, const std::string& title, const Size& parentSize, bool addBackground )
498 Actor tile = Actor::New();
499 tile.SetName( name );
500 tile.SetAnchorPoint( AnchorPoint::CENTER );
501 tile.SetParentOrigin( ParentOrigin::CENTER );
503 // make the tile 100% of parent
504 tile.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
506 Actor content = Actor::New();
507 content.SetAnchorPoint( AnchorPoint::CENTER );
508 content.SetParentOrigin( ParentOrigin::CENTER );
509 content.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
512 // create background image
515 Image bg = Image::New( TILE_BACKGROUND );
516 ImageActor image = ImageActor::New( bg );
517 image.SetAnchorPoint( AnchorPoint::CENTER );
518 image.SetParentOrigin( ParentOrigin::CENTER );
519 // make the image 100% of tile
520 image.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
521 // move image back to get text appear in front
523 image.SetStyle( ImageActor::STYLE_NINE_PATCH );
524 image.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
526 content.Add( image );
529 ImageActor stencil = NewStencilImage();
530 stencil.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
531 image.Add( stencil );
534 TextView text = TextView::New( title );
535 text.SetAnchorPoint( AnchorPoint::CENTER );
536 text.SetParentOrigin( ParentOrigin::CENTER );
537 text.SetWidthExceedPolicy( Toolkit::TextView::ShrinkToFit );
538 text.SetMultilinePolicy( Toolkit::TextView::SplitByWord );
539 text.SetLineJustification( Toolkit::TextView::Center );
540 text.SetTextAlignment( Toolkit::Alignment::Type( Alignment::HorizontalCenter | Alignment::VerticalCenter ) );
541 text.SetColor( Color::WHITE );
543 // make the text 90% of tile
544 text.SetSize( 0.9f * parentSize.width, 0.9f * parentSize.height );
545 text.SetStyleToCurrentText( GetTableTextStyle() );
546 text.SetSnapshotModeEnabled( false );
549 // Set the tile to be keyboard focusable
550 tile.SetKeyboardFocusable(true);
552 // connect to the touch events
553 tile.TouchedSignal().Connect( this, &DaliTableView::OnTilePressed );
554 tile.HoveredSignal().Connect( this, &DaliTableView::OnTileHovered );
559 ImageActor DaliTableView::NewStencilImage()
561 Image alpha = Image::New( TILE_BACKGROUND_ALPHA );
563 ImageActor stencilActor = ImageActor::New( alpha );
564 stencilActor.SetStyle( ImageActor::STYLE_NINE_PATCH );
565 stencilActor.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
567 stencilActor.SetParentOrigin( ParentOrigin::CENTER );
568 stencilActor.SetAnchorPoint( AnchorPoint::CENTER );
569 stencilActor.SetDrawMode( DrawMode::STENCIL );
571 Dali::ShaderEffect shaderEffect = AlphaDiscardEffect::New();
572 stencilActor.SetShaderEffect( shaderEffect );
577 bool DaliTableView::OnTilePressed( Actor actor, const TouchEvent& event )
579 bool consumed = false;
581 const TouchPoint& point = event.GetPoint( 0 );
582 if( TouchPoint::Down == point.state )
584 mPressedActor = actor;
588 // A button press is only valid if the Down & Up events
589 // both occurred within the button.
590 if( ( TouchPoint::Up == point.state ) &&
591 ( mPressedActor == actor ) )
593 std::string name = actor.GetName();
594 ExampleMapConstIter iter = mExampleMap.find( name );
596 FocusManager focusManager = FocusManager::Get();
598 if( iter != mExampleMap.end() )
600 // ignore Example button presses when scrolling or button animating.
601 if( ( !mScrolling ) && ( !mPressedAnimation ) )
603 // do nothing, until pressed animation finished.
610 mPressedAnimation = Animation::New( BUTTON_PRESS_ANIMATION_TIME );
611 mPressedAnimation.SetEndAction( Animation::Discard );
613 // scale the content actor within the Tile, as to not affect the placement within the Table.
614 Actor content = actor.GetChildAt(0);
615 mPressedAnimation.ScaleTo( content, Vector3( 0.9f, 0.9f, 1.0f ), AlphaFunctions::EaseInOut, 0.0f,
616 BUTTON_PRESS_ANIMATION_TIME * 0.5f );
617 mPressedAnimation.ScaleTo( content, Vector3::ONE, AlphaFunctions::EaseInOut, BUTTON_PRESS_ANIMATION_TIME * 0.5f,
618 BUTTON_PRESS_ANIMATION_TIME * 0.5f );
619 mPressedAnimation.Play();
620 mPressedAnimation.FinishedSignal().Connect( this, &DaliTableView::OnPressedAnimationFinished );
626 void DaliTableView::OnPressedAnimationFinished( Dali::Animation& source )
628 mPressedAnimation.Reset();
631 std::string name = mPressedActor.GetName();
632 ExampleMapConstIter iter = mExampleMap.find( name );
634 if( iter == mExampleMap.end() )
636 if( name == BUTTON_QUIT )
638 // Move focus to the OK button
639 FocusManager focusManager = FocusManager::Get();
641 // Enable the group mode and wrap mode
642 focusManager.SetGroupMode( true );
643 focusManager.SetWrapMode( true );
648 const Example& example( iter->second );
650 std::stringstream stream;
651 stream << DALI_EXAMPLE_BIN << example.name.c_str();
655 execlp( stream.str().c_str(), example.name.c_str(), NULL );
656 DALI_ASSERT_ALWAYS(false && "exec failed!");
659 mPressedActor.Reset();
663 void DaliTableView::OnScrollStart( const Dali::Vector3& position )
670 void DaliTableView::OnScrollComplete( const Dali::Vector3& position )
674 // move focus to 1st item of new page
675 FocusManager focusManager = FocusManager::Get();
676 focusManager.SetCurrentFocusActor(mTableViewList[mScrollView.GetCurrentPage()].GetChildAt(TableView::CellPosition(1, 0)) );
680 bool DaliTableView::OnScrollTouched( Actor actor, const TouchEvent& event )
682 const TouchPoint& point = event.GetPoint( 0 );
683 if( TouchPoint::Down == point.state )
685 mPressedActor = actor;
691 void DaliTableView::ApplyScrollViewEffect()
693 // Remove old effect if exists.
695 if( mScrollViewEffect )
697 mScrollView.RemoveEffect( mScrollViewEffect );
700 // Just one effect for now
701 SetupInnerPageCubeEffect();
703 mScrollView.ApplyEffect( mScrollViewEffect );
706 void DaliTableView::SetupInnerPageCubeEffect()
708 ScrollViewCustomEffect customEffect;
709 mScrollViewEffect = customEffect = ScrollViewCustomEffect::New();
710 mScrollView.SetScrollSnapDuration( EFFECT_SNAP_DURATION );
711 mScrollView.SetScrollFlickDuration( EFFECT_FLICK_DURATION );
712 mScrollView.RemoveConstraintsFromChildren();
714 customEffect.SetPageSpacing( Vector2( 30.0f, 30.0f ) );
715 customEffect.SetAngledOriginPageRotation( ANGLE_CUBE_PAGE_ROTATE );
716 customEffect.SetSwingAngle( ANGLE_CUBE_PAGE_ROTATE.x, Vector3( 0, -1, 0 ) );
717 customEffect.SetOpacityThreshold( 0.5f ); // Make fade out on edges
720 void DaliTableView::ApplyEffectToPage( Actor page, const Vector3& tableRelativeSize )
722 page.RemoveConstraints();
724 Constraint constraint = Constraint::New<Vector3>( Actor::SCALE,
725 LocalSource( Actor::SIZE ),
726 ParentSource( Actor::SIZE ),
727 ScaleToFitConstraint() );
728 page.ApplyConstraint(constraint);
730 ApplyCustomEffectToPage( page );
733 void DaliTableView::ApplyCustomEffectToPage( Actor page )
735 ScrollViewCustomEffect customEffect = ScrollViewCustomEffect::DownCast( mScrollViewEffect );
736 Vector2 vStageSize( Stage::GetCurrent().GetSize() );
737 customEffect.ApplyToPage( page, Vector3( vStageSize.x, vStageSize.y, 1.0f ) );
740 void DaliTableView::OnKeyEvent( const KeyEvent& event )
742 if( event.state == KeyEvent::Down )
744 if ( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
751 Actor CreateBackgroundActor( const Vector2& size )
753 Actor layer = Actor::New();
754 layer.SetAnchorPoint( AnchorPoint::CENTER );
755 layer.SetParentOrigin( ParentOrigin::CENTER );
756 layer.SetSize( size );
760 void DaliTableView::SetupBackground( Actor addToLayer, const Vector2& size )
762 // Create distance field shape
763 BitmapImage distanceField;
764 Size imageSize( 512, 512 );
765 CreateShapeImage( CIRCLE, imageSize, distanceField );
768 Actor backgroundAnimLayer0 = CreateBackgroundActor( size );
769 Actor backgroundAnimLayer1 = CreateBackgroundActor( size );
770 Actor backgroundAnimLayer2 = CreateBackgroundActor( size );
773 Constraint animConstraint0 = Constraint::New < Vector3 > ( Actor::POSITION,
774 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
775 AnimScrollConstraint( backgroundAnimLayer0.GetCurrentPosition(), 0.75f ) );
776 backgroundAnimLayer0.ApplyConstraint( animConstraint0 );
778 Constraint animConstraint1 = Constraint::New < Vector3 > ( Actor::POSITION,
779 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
780 AnimScrollConstraint( backgroundAnimLayer1.GetCurrentPosition(), 0.5f ) );
781 backgroundAnimLayer1.ApplyConstraint( animConstraint1 );
783 Constraint animConstraint2 = Constraint::New < Vector3 > ( Actor::POSITION,
784 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
785 AnimScrollConstraint( backgroundAnimLayer2.GetCurrentPosition(), 0.25f ) );
786 backgroundAnimLayer2.ApplyConstraint( animConstraint2 );
789 ImageActor layer = Dali::Toolkit::CreateSolidColorActor( BACKGROUND_COLOR );
790 layer.SetAnchorPoint( AnchorPoint::CENTER );
791 layer.SetParentOrigin( ParentOrigin::CENTER );
792 layer.SetSize( size * BACKGROUND_SIZE_SCALE );
793 layer.SetZ( BACKGROUND_Z );
794 layer.SetPositionInheritanceMode( DONT_INHERIT_POSITION );
796 addToLayer.Add( layer );
799 addToLayer.Add( backgroundAnimLayer0 );
800 addToLayer.Add( backgroundAnimLayer1 );
801 addToLayer.Add( backgroundAnimLayer2 );
803 // Add all the children
804 AddBackgroundActors( backgroundAnimLayer0, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
805 AddBackgroundActors( backgroundAnimLayer1, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
806 AddBackgroundActors( backgroundAnimLayer2, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
809 void DaliTableView::AddBackgroundActors( Actor layer, int count, BitmapImage distanceField, const Dali::Vector2& size )
811 for( int i = 0; i < count; ++i )
813 float randSize = Random::Range( 10.0f, 400.0f );
814 float hue = Random::Range( 0.3f, 1.0f );
815 Vector4 randColour( hue, hue*0.5, 0.0f, Random::Range( 0.3f, 0.6f ));
817 ImageActor dfActor = ImageActor::New( distanceField );
818 mBackgroundActors.push_back( dfActor );
819 dfActor.SetSize( Vector2( randSize, randSize ) );
820 dfActor.SetParentOrigin( ParentOrigin::CENTER );
822 Toolkit::DistanceFieldEffect effect = Toolkit::DistanceFieldEffect::New();
823 dfActor.SetShaderEffect( effect );
824 dfActor.SetColor( randColour );
825 effect.SetOutlineParams( Vector2( 0.55f, 0.00f ) );
826 effect.SetSmoothingEdge( 0.5f );
827 layer.Add( dfActor );
831 Random::Range( -size.x * 0.5f * BACKGROUND_SPREAD_SCALE, size.x * 0.5f * BACKGROUND_SPREAD_SCALE ),
832 Random::Range( -size.y * 0.5f - randSize, size.y * 0.5f + randSize ),
833 Random::Range(-1.0f, 0.0f) );
834 dfActor.SetPosition( actorPos );
836 Constraint movementConstraint = Constraint::New < Vector3 > ( Actor::POSITION,
837 LocalSource( Actor::SIZE ),
838 ParentSource( Actor::SIZE ),
839 ShapeMovementConstraint );
840 dfActor.ApplyConstraint( movementConstraint );
843 Animation animation = Animation::New( Random::Range( 40.0f, 200.0f ) );
844 KeyFrames keyframes = KeyFrames::New();
845 keyframes.Add( 0.0f, actorPos );
846 Vector3 toPos( actorPos );
847 toPos.y -= ( size.y + randSize );
848 keyframes.Add( 1.0f, toPos );
849 animation.AnimateBetween( Property( dfActor, Actor::POSITION ), keyframes );
850 animation.SetLooping( true );
852 mBackgroundAnimations.push_back( animation );
856 void DaliTableView::CreateShapeImage( ShapeType shapeType, const Size& size, BitmapImage& distanceFieldOut )
858 // this bitmap will hold the alpha map for the distance field shader
859 distanceFieldOut = BitmapImage::New( size.width, size.height, Pixel::A8 );
861 // Generate bit pattern
862 std::vector< unsigned char > imageDataA8;
863 imageDataA8.reserve( size.width * size.height ); // A8
868 GenerateCircle( size, imageDataA8 );
871 GenerateSquare( size, imageDataA8 );
877 PixelBuffer* buffer = distanceFieldOut.GetBuffer();
880 GenerateDistanceFieldMap( &imageDataA8[ 0 ], size, buffer, size, 8.0f, size );
881 distanceFieldOut.Update();
885 void DaliTableView::GenerateSquare( const Size& size, std::vector< unsigned char >& distanceFieldOut )
887 for( int h = 0; h < size.height; ++h )
889 for( int w = 0; w < size.width; ++w )
891 distanceFieldOut.push_back( 0xFF );
896 void DaliTableView::GenerateCircle( const Size& size, std::vector< unsigned char >& distanceFieldOut )
898 const float radius = size.width * 0.5f * size.width * 0.5f;
899 Vector2 center( size.width / 2, size.height / 2 );
901 for( int h = 0; h < size.height; ++h )
903 for( int w = 0; w < size.width; ++w )
906 Vector2 dist = pos - center;
908 if( dist.x * dist.x + dist.y * dist.y > radius )
910 distanceFieldOut.push_back( 0x00 );
914 distanceFieldOut.push_back( 0xFF );
920 ImageActor DaliTableView::CreateLogo( std::string imagePath )
922 Image image = Image::New( imagePath );
923 ImageActor logo = ImageActor::New( image );
925 logo.SetAnchorPoint( AnchorPoint::CENTER );
926 logo.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
931 bool DaliTableView::PauseBackgroundAnimation()
938 void DaliTableView::PauseAnimation()
940 if( mBackgroundAnimsPlaying )
942 for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
944 Animation anim = *animIter;
949 mBackgroundAnimsPlaying = false;
953 void DaliTableView::PlayAnimation()
955 if ( !mBackgroundAnimsPlaying )
957 for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
959 Animation anim = *animIter;
964 mBackgroundAnimsPlaying = true;
967 mAnimationTimer.SetInterval( BACKGROUND_ANIMATION_DURATION );
970 Dali::Actor DaliTableView::OnKeyboardPreFocusChange( Dali::Actor current, Dali::Actor proposed, Dali::Toolkit::Control::KeyboardFocusNavigationDirection direction )
972 Actor nextFocusActor = proposed;
974 if ( !current && !proposed )
976 // Set the initial focus to the first tile in the current page should be focused.
977 nextFocusActor = mTableViewList[mScrollView.GetCurrentPage()].GetChildAt(TableView::CellPosition(0, 0));
979 else if( !proposed || (proposed && proposed == mScrollViewLayer) )
981 // ScrollView is being focused but nothing in the current page can be focused further
982 // in the given direction. We should work out which page to scroll to next.
983 int currentPage = mScrollView.GetCurrentPage();
984 int newPage = currentPage;
985 if( direction == Dali::Toolkit::Control::Left )
989 else if( direction == Dali::Toolkit::Control::Right )
994 newPage = std::max(0, std::min(static_cast<int>(mScrollRulerX->GetTotalPages() - 1), newPage));
995 if( newPage == currentPage )
997 if( direction == Dali::Toolkit::Control::Left )
999 newPage = mScrollRulerX->GetTotalPages() - 1;
1000 } else if( direction == Dali::Toolkit::Control::Right )
1006 // Scroll to the page in the given direction
1007 mScrollView.ScrollTo(newPage);
1009 if( direction == Dali::Toolkit::Control::Left )
1011 // Work out the cell position for the last tile
1012 int remainingExamples = mExampleList.size() - newPage * EXAMPLES_PER_PAGE;
1013 int rowPos = (remainingExamples >= EXAMPLES_PER_PAGE) ? ROWS_PER_PAGE - 1 : ( (remainingExamples % EXAMPLES_PER_PAGE + EXAMPLES_PER_ROW) / EXAMPLES_PER_ROW - 1 );
1014 int colPos = remainingExamples >= EXAMPLES_PER_PAGE ? EXAMPLES_PER_ROW - 1 : ( remainingExamples % EXAMPLES_PER_PAGE - rowPos * EXAMPLES_PER_ROW - 1 );
1016 // Move the focus to the last tile in the new page.
1017 nextFocusActor = mTableViewList[newPage].GetChildAt(TableView::CellPosition(rowPos, colPos));
1021 // Move the focus to the first tile in the new page.
1022 nextFocusActor = mTableViewList[newPage].GetChildAt(TableView::CellPosition(0, 0));
1026 return nextFocusActor;
1029 void DaliTableView::OnFocusedActorActivated( Dali::Actor activatedActor )
1033 mPressedActor = activatedActor;
1035 // Activate the current focused actor;
1036 TouchEvent touchEventUp;
1037 touchEventUp.points.push_back( TouchPoint ( 0, TouchPoint::Up, 0.0f, 0.0f ) );
1038 OnTilePressed(mPressedActor, touchEventUp);
1042 bool DaliTableView::OnTileHovered( Actor actor, const HoverEvent& event )
1044 KeyboardFocusManager::Get().SetCurrentFocusActor( actor );