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::Vector4 DEFAULT_TEXT_STYLE_COLOR(0.7f, 0.7f, 0.7f, 1.0f);
85 //const std::string TABLE_TEXT_STYLE_FONT_FAMILY("HelveticaNeue");
86 //const std::string TABLE_TEXT_STYLE_FONT_STYLE("Regular");
87 //const Dali::Vector4 TABLE_TEXT_STYLE_COLOR(0.0f, 0.0f, 0.0f, 1.0f);
90 * Creates the background image
92 ImageActor CreateBackground( std::string imagePath )
94 Image image = Image::New( imagePath );
95 ImageActor background = ImageActor::New( image );
97 background.SetAnchorPoint( AnchorPoint::CENTER );
98 background.SetParentOrigin( ParentOrigin::CENTER );
99 background.SetZ( -1.0f );
104 // These values depend on the tile image
105 const float IMAGE_BORDER_LEFT = 11.0f;
106 const float IMAGE_BORDER_RIGHT = IMAGE_BORDER_LEFT;
107 const float IMAGE_BORDER_TOP = IMAGE_BORDER_LEFT;
108 const float IMAGE_BORDER_BOTTOM = IMAGE_BORDER_LEFT;
111 * TableViewVisibilityConstraint
113 struct TableViewVisibilityConstraint
115 bool operator()( const bool& current,
116 const PropertyInput& pagePositionProperty,
117 const PropertyInput& pageSizeProperty )
119 // Only the tableview in the current page should be visible.
120 const Vector3& pagePosition = pagePositionProperty.GetVector3();
121 const Vector3& pageSize = pageSizeProperty.GetVector3();
122 return fabsf( pagePosition.x ) < pageSize.x;
127 * Constraint to wrap an actor in y that is moving vertically
129 Vector3 ShapeMovementConstraint( const Vector3& current,
130 const PropertyInput& shapeSizeProperty,
131 const PropertyInput& parentSizeProperty )
133 const Vector3& shapeSize = shapeSizeProperty.GetVector3();
134 const Vector3& parentSize = parentSizeProperty.GetVector3();
136 Vector3 pos( current );
137 if( pos.y + shapeSize.y * 0.5f < -parentSize.y * 0.5f )
139 pos.y += parentSize.y + shapeSize.y;
146 * Constraint to return a position for the background based on the scroll value
148 struct AnimScrollConstraint
152 AnimScrollConstraint( const Vector3& initialPos, float scale )
153 : mInitialPos( initialPos ),
159 Vector3 operator()( const Vector3& current, const PropertyInput& scrollProperty )
161 float scrollPos = scrollProperty.GetVector3().x;
163 return mInitialPos + Vector3( -scrollPos * mScale, 0.0f, 0.0f );
172 * Constraint to return a tracked world position added to the constant local position
174 struct TranslateLocalConstraint
178 TranslateLocalConstraint( const Vector3& localPos )
179 : mLocalPos( localPos )
183 Vector3 operator()( const Vector3& current, const PropertyInput& pagePosProperty )
185 Vector3 worldPos = pagePosProperty.GetVector3();
187 return ( worldPos + mLocalPos );
195 bool CompareByTitle( const Example& lhs, const Example& rhs )
197 return lhs.title < rhs.title;
202 DaliTableView::DaliTableView( Application& application )
203 : mApplication( application ),
205 mBackgroundImagePath( DEFAULT_BACKGROUND_IMAGE_PATH ),
206 mSortAlphabetically( false ),
207 mBackgroundAnimsPlaying( false )
209 application.InitSignal().Connect( this, &DaliTableView::Initialize );
212 DaliTableView::~DaliTableView()
216 void DaliTableView::AddExample( Example example )
218 mExampleList.push_back( example );
219 mExampleMap[ example.name ] = example;
222 void DaliTableView::SetBackgroundPath( std::string imagePath )
224 mBackgroundImagePath = imagePath;
227 void DaliTableView::SortAlphabetically( bool sortAlphabetically )
229 mSortAlphabetically = sortAlphabetically;
232 void DaliTableView::Initialize( Application& application )
234 Stage::GetCurrent().KeyEventSignal().Connect( this, &DaliTableView::OnKeyEvent );
236 Vector2 stageSize = Stage::GetCurrent().GetSize();
239 mBackground = CreateBackground( mBackgroundImagePath );
240 // set same size as parent actor
241 mBackground.SetSize( stageSize );
242 Stage::GetCurrent().Add( mBackground );
244 // Render entire content as overlays, as is all on same 2D plane.
245 mRootActor = TableView::New( 4, 1 );
246 mRootActor.SetAnchorPoint( AnchorPoint::CENTER );
247 mRootActor.SetParentOrigin( ParentOrigin::CENTER );
248 mRootActor.SetFixedHeight( 3, BOTTOM_PADDING_HEIGHT );
249 Stage::GetCurrent().Add( mRootActor );
252 Dali::Toolkit::ToolBar toolbar;
253 Dali::Layer toolBarLayer = DemoHelper::CreateToolbar(toolbar,
254 DEFAULT_TOOLBAR_IMAGE_PATH,
255 DEFAULT_TOOLBAR_TEXT,
256 DemoHelper::DEFAULT_VIEW_STYLE);
258 mRootActor.AddChild( toolBarLayer, TableView::CellPosition( 0, 0 ) );
259 mRootActor.SetFixedHeight( 0, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight );
262 mLogo = CreateLogo( LOGO_PATH );
263 Alignment alignment = Alignment::New();
264 alignment.Add(mLogo);
265 mRootActor.AddChild( alignment, TableView::CellPosition( 1, 0 ) );
267 // scrollview occupying the majority of the screen
268 mScrollView = ScrollView::New();
270 mScrollView.SetAnchorPoint( AnchorPoint::CENTER );
271 mScrollView.SetParentOrigin( ParentOrigin::CENTER );
272 mScrollView.ApplyConstraint( Dali::Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ), Dali::RelativeToConstraint( TABLE_RELATIVE_SIZE ) ) );
273 mScrollView.SetAxisAutoLock( true );
274 mScrollView.ScrollCompletedSignal().Connect( this, &DaliTableView::OnScrollComplete );
275 mScrollView.ScrollStartedSignal().Connect( this, &DaliTableView::OnScrollStart );
276 mScrollView.TouchedSignal().Connect( this, &DaliTableView::OnScrollTouched );
278 mScrollViewLayer = Layer::New();
279 mScrollViewLayer.SetAnchorPoint( AnchorPoint::CENTER );
280 mScrollViewLayer.SetParentOrigin( ParentOrigin::CENTER );
281 mScrollViewLayer.SetSize( stageSize );
282 mScrollViewLayer.Add( mScrollView );
283 mRootActor.AddChild( mScrollViewLayer, TableView::CellPosition( 2, 0 ) );
285 // Setup the scenegraph
286 // 1) Add scroll view effect and setup constraints on pages
287 ApplyScrollViewEffect();
289 // 2) Add pages and tiles
292 // 3) Populate scrollview with background so constraints on background layers can work with scrollview
293 SetupBackground( mScrollView, stageSize );
295 // 4) Remove constraints for inner cube effect
296 for( TableViewListIter pageIter = mTableViewList.begin(); pageIter != mTableViewList.end(); ++pageIter )
298 TableView page = *pageIter;
300 unsigned int numChildren = page.GetChildCount();
301 Actor pageActor = page;
302 for( unsigned int i=0; i<numChildren; ++i)
304 // Remove old effect's manual constraints.
305 Actor child = pageActor.GetChildAt(i);
308 child.RemoveConstraints();
313 // Set initial orientation
314 unsigned int degrees = application.GetOrientation().GetDegrees();
317 Dali::Window winHandle = application.GetWindow();
318 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
319 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE );
320 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
321 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
323 Dali::Orientation orientation = winHandle.GetOrientation();
324 orientation.ChangedSignal().Connect( this, &DaliTableView::OrientationChanged );
326 winHandle.ShowIndicator( Dali::Window::INVISIBLE );
329 mAnimationTimer = Timer::New( BACKGROUND_ANIMATION_DURATION );
330 mAnimationTimer.TickSignal().Connect( this, &DaliTableView::PauseBackgroundAnimation );
331 mAnimationTimer.Start();
332 mBackgroundAnimsPlaying = true;
334 KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &DaliTableView::OnKeyboardPreFocusChange );
335 KeyboardFocusManager::Get().FocusedActorActivatedSignal().Connect( this, &DaliTableView::OnFocusedActorActivated );
338 void DaliTableView::Populate()
340 const Vector2 stageSize = Stage::GetCurrent().GetSize();
342 const Size demoTileSize( 0.25f * stageSize.width, 0.25f * stageSize.height );
344 mTotalPages = ( mExampleList.size() + EXAMPLES_PER_PAGE - 1 ) / EXAMPLES_PER_PAGE;
346 // Populate ScrollView.
347 if( mExampleList.size() > 0 )
349 if( mSortAlphabetically )
351 sort( mExampleList.begin(), mExampleList.end(), CompareByTitle );
354 unsigned int exampleCount = 0;
355 ExampleListConstIter iter = mExampleList.begin();
356 for( int t = 0; t < mTotalPages; t++ )
358 // Create Table. (contains up to 9 Examples)
359 TableView tableView = TableView::New( 4, 3 );
360 // Add tableView to container.
361 mScrollView.Add( tableView );
362 ApplyEffectToPage( tableView, TABLE_RELATIVE_SIZE );
364 tableView.SetAnchorPoint( AnchorPoint::CENTER );
365 tableView.SetParentOrigin( ParentOrigin::CENTER );
366 // 2 pixels of padding
367 tableView.SetCellPadding( Size( 2.0f, 2.0f ) );
369 Constraint constraint = Constraint::New<Vector3>( Actor::SCALE,
370 LocalSource( Actor::SIZE ),
371 ParentSource( Actor::SIZE ),
372 ScaleToFitConstraint() );
373 tableView.ApplyConstraint(constraint);
375 // Apply visibility constraint to table view
376 Constraint visibleConstraint = Constraint::New< bool >( Actor::VISIBLE,
377 LocalSource( Actor::POSITION ),
378 ParentSource( Actor::SIZE ),
379 TableViewVisibilityConstraint() );
380 visibleConstraint.SetRemoveAction( Constraint::Discard );
381 tableView.ApplyConstraint( visibleConstraint );
383 // add cells to table
384 for( int y = 0; y < ROWS_PER_PAGE; y++ )
386 for( int x = 0; x < EXAMPLES_PER_ROW; x++ )
388 const Example& example = ( *iter );
390 Actor tile = CreateTile( example.name, example.title, demoTileSize, true );
391 FocusManager focusManager = FocusManager::Get();
392 focusManager.SetFocusOrder( tile, ++exampleCount );
393 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_LABEL,
395 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_TRAIT, "Tile" );
396 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_HINT,
397 "You can run this example" );
399 tableView.AddChild( tile, TableView::CellPosition( y, x ) );
402 if( iter == mExampleList.end() )
407 if( iter == mExampleList.end() )
414 tableView.SetFixedHeight( 3, BOTTOM_ROW_HEIGHT );
416 std::stringstream out;
417 out << ( t + 1 ) << " of " << mTotalPages;
418 Actor pageNumberText = CreateTile( "", out.str(), Size( 0.8f * stageSize.width, BOTTOM_ROW_HEIGHT ), false );
420 pageNumberText.ApplyConstraint( Constraint::New< Vector3 >( Actor::POSITION, Source( tableView, Actor::WORLD_POSITION),
421 TranslateLocalConstraint( Vector3( 0.0f, stageSize.y * 0.4f, 0.0f ) ) ) );
422 pageNumberText.ApplyConstraint( Constraint::New< Quaternion >( Actor::ROTATION, Source( tableView, Actor::WORLD_ROTATION ), EqualToConstraint() ) );
423 pageNumberText.ApplyConstraint( Constraint::New< Vector4 >( Actor::COLOR, Source( tableView, Actor::COLOR ), EqualToConstraint() ) );
425 //Stage::GetCurrent().Add( pageNumberText );
427 // Set tableview position
428 Vector3 tableViewPos( stageSize.x * TABLE_RELATIVE_SIZE.x * t, 0.0f, 0.0f );
429 tableView.SetPosition( tableViewPos );
431 mTableViewList.push_back( tableView );
433 if( iter == mExampleList.end() )
440 // Update Ruler info.
441 mScrollRulerX = new FixedRuler( stageSize.width * TABLE_RELATIVE_SIZE.x );
442 mScrollRulerY = new DefaultRuler();
443 mScrollRulerX->SetDomain( RulerDomain( 0.0f, mTotalPages * stageSize.width * TABLE_RELATIVE_SIZE.x, true ) );
444 mScrollRulerY->Disable();
445 mScrollView.SetRulerX( mScrollRulerX );
446 mScrollView.SetRulerY( mScrollRulerY );
449 void DaliTableView::OrientationChanged( Orientation orientation )
451 // TODO: Implement if orientation change required
454 void DaliTableView::Rotate( unsigned int degrees )
456 // Resize the root actor
457 Vector2 stageSize = Stage::GetCurrent().GetSize();
458 Vector3 targetSize( stageSize.x, stageSize.y, 1.0f );
460 if( degrees == 90 || degrees == 270 )
462 targetSize = Vector3( stageSize.y, stageSize.x, 1.0f );
465 if( mRotateAnimation )
467 mRotateAnimation.Stop();
468 mRotateAnimation.Clear();
471 mRotateAnimation = Animation::New( ROTATE_ANIMATION_TIME );
472 mRotateAnimation.RotateTo( mRootActor, Degree( 360 - degrees ), Vector3::ZAXIS, AlphaFunctions::EaseOut );
473 mRotateAnimation.Resize( mRootActor, targetSize, AlphaFunctions::EaseOut );
474 mRotateAnimation.Play();
477 Actor DaliTableView::CreateTile( const std::string& name, const std::string& title, const Size& parentSize, bool addBackground )
479 Actor tile = Actor::New();
480 tile.SetName( name );
481 tile.SetAnchorPoint( AnchorPoint::CENTER );
482 tile.SetParentOrigin( ParentOrigin::CENTER );
484 // make the tile 100% of parent
485 tile.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
487 Actor content = Actor::New();
488 content.SetAnchorPoint( AnchorPoint::CENTER );
489 content.SetParentOrigin( ParentOrigin::CENTER );
490 content.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
493 // create background image
496 Image bg = Image::New( TILE_BACKGROUND );
497 ImageActor image = ImageActor::New( bg );
498 image.SetAnchorPoint( AnchorPoint::CENTER );
499 image.SetParentOrigin( ParentOrigin::CENTER );
500 // make the image 100% of tile
501 image.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
502 // move image back to get text appear in front
504 image.SetStyle( ImageActor::STYLE_NINE_PATCH );
505 image.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
507 content.Add( image );
510 ImageActor stencil = NewStencilImage();
511 stencil.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
512 image.Add( stencil );
515 // Set the tile to be keyboard focusable
516 tile.SetKeyboardFocusable(true);
518 // connect to the touch events
519 tile.TouchedSignal().Connect( this, &DaliTableView::OnTilePressed );
520 tile.HoveredSignal().Connect( this, &DaliTableView::OnTileHovered );
525 ImageActor DaliTableView::NewStencilImage()
527 Image alpha = Image::New( TILE_BACKGROUND_ALPHA );
529 ImageActor stencilActor = ImageActor::New( alpha );
530 stencilActor.SetStyle( ImageActor::STYLE_NINE_PATCH );
531 stencilActor.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
533 stencilActor.SetParentOrigin( ParentOrigin::CENTER );
534 stencilActor.SetAnchorPoint( AnchorPoint::CENTER );
535 stencilActor.SetDrawMode( DrawMode::STENCIL );
537 Dali::ShaderEffect shaderEffect = AlphaDiscardEffect::New();
538 stencilActor.SetShaderEffect( shaderEffect );
543 bool DaliTableView::OnTilePressed( Actor actor, const TouchEvent& event )
545 bool consumed = false;
547 const TouchPoint& point = event.GetPoint( 0 );
548 if( TouchPoint::Down == point.state )
550 mPressedActor = actor;
554 // A button press is only valid if the Down & Up events
555 // both occurred within the button.
556 if( ( TouchPoint::Up == point.state ) &&
557 ( mPressedActor == actor ) )
559 std::string name = actor.GetName();
560 ExampleMapConstIter iter = mExampleMap.find( name );
562 FocusManager focusManager = FocusManager::Get();
564 if( iter != mExampleMap.end() )
566 // ignore Example button presses when scrolling or button animating.
567 if( ( !mScrolling ) && ( !mPressedAnimation ) )
569 // do nothing, until pressed animation finished.
576 mPressedAnimation = Animation::New( BUTTON_PRESS_ANIMATION_TIME );
577 mPressedAnimation.SetEndAction( Animation::Discard );
579 // scale the content actor within the Tile, as to not affect the placement within the Table.
580 Actor content = actor.GetChildAt(0);
581 mPressedAnimation.ScaleTo( content, Vector3( 0.9f, 0.9f, 1.0f ), AlphaFunctions::EaseInOut, 0.0f,
582 BUTTON_PRESS_ANIMATION_TIME * 0.5f );
583 mPressedAnimation.ScaleTo( content, Vector3::ONE, AlphaFunctions::EaseInOut, BUTTON_PRESS_ANIMATION_TIME * 0.5f,
584 BUTTON_PRESS_ANIMATION_TIME * 0.5f );
585 mPressedAnimation.Play();
586 mPressedAnimation.FinishedSignal().Connect( this, &DaliTableView::OnPressedAnimationFinished );
592 void DaliTableView::OnPressedAnimationFinished( Dali::Animation& source )
594 mPressedAnimation.Reset();
597 std::string name = mPressedActor.GetName();
598 ExampleMapConstIter iter = mExampleMap.find( name );
600 if( iter == mExampleMap.end() )
602 if( name == BUTTON_QUIT )
604 // Move focus to the OK button
605 FocusManager focusManager = FocusManager::Get();
607 // Enable the group mode and wrap mode
608 focusManager.SetGroupMode( true );
609 focusManager.SetWrapMode( true );
614 const Example& example( iter->second );
616 std::stringstream stream;
617 stream << DALI_EXAMPLE_BIN << example.name.c_str();
621 execlp( stream.str().c_str(), example.name.c_str(), NULL );
622 DALI_ASSERT_ALWAYS(false && "exec failed!");
625 mPressedActor.Reset();
629 void DaliTableView::OnScrollStart( const Dali::Vector3& position )
636 void DaliTableView::OnScrollComplete( const Dali::Vector3& position )
640 // move focus to 1st item of new page
641 FocusManager focusManager = FocusManager::Get();
642 focusManager.SetCurrentFocusActor(mTableViewList[mScrollView.GetCurrentPage()].GetChildAt(TableView::CellPosition(1, 0)) );
646 bool DaliTableView::OnScrollTouched( Actor actor, const TouchEvent& event )
648 const TouchPoint& point = event.GetPoint( 0 );
649 if( TouchPoint::Down == point.state )
651 mPressedActor = actor;
657 void DaliTableView::ApplyScrollViewEffect()
659 // Remove old effect if exists.
661 if( mScrollViewEffect )
663 mScrollView.RemoveEffect( mScrollViewEffect );
666 // Just one effect for now
667 SetupInnerPageCubeEffect();
669 mScrollView.ApplyEffect( mScrollViewEffect );
672 void DaliTableView::SetupInnerPageCubeEffect()
674 ScrollViewCustomEffect customEffect;
675 mScrollViewEffect = customEffect = ScrollViewCustomEffect::New();
676 mScrollView.SetScrollSnapDuration( EFFECT_SNAP_DURATION );
677 mScrollView.SetScrollFlickDuration( EFFECT_FLICK_DURATION );
678 mScrollView.SetScrollSnapAlphaFunction( AlphaFunctions::EaseOutBack );
679 mScrollView.SetScrollFlickAlphaFunction( AlphaFunctions::EaseOutBack );
680 mScrollView.RemoveConstraintsFromChildren();
682 customEffect.SetPageSpacing( Vector2( 30.0f, 30.0f ) );
683 customEffect.SetAngledOriginPageRotation( ANGLE_CUBE_PAGE_ROTATE );
684 customEffect.SetSwingAngle( ANGLE_CUBE_PAGE_ROTATE.x, Vector3( 0, -1, 0 ) );
685 customEffect.SetOpacityThreshold( 0.5f ); // Make fade out on edges
688 void DaliTableView::ApplyEffectToPage( Actor page, const Vector3& tableRelativeSize )
690 page.RemoveConstraints();
692 Constraint constraint = Constraint::New<Vector3>( Actor::SCALE,
693 LocalSource( Actor::SIZE ),
694 ParentSource( Actor::SIZE ),
695 ScaleToFitConstraint() );
696 page.ApplyConstraint(constraint);
698 ApplyCustomEffectToPage( page );
701 void DaliTableView::ApplyCustomEffectToPage( Actor page )
703 ScrollViewCustomEffect customEffect = ScrollViewCustomEffect::DownCast( mScrollViewEffect );
704 Vector2 vStageSize( Stage::GetCurrent().GetSize() );
705 customEffect.ApplyToPage( page, Vector3( vStageSize.x, vStageSize.y, 1.0f ) );
708 void DaliTableView::OnKeyEvent( const KeyEvent& event )
710 if( event.state == KeyEvent::Down )
712 if ( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
719 Actor CreateBackgroundActor( const Vector2& size )
721 Actor layer = Actor::New();
722 layer.SetAnchorPoint( AnchorPoint::CENTER );
723 layer.SetParentOrigin( ParentOrigin::CENTER );
724 layer.SetSize( size );
728 void DaliTableView::SetupBackground( Actor addToLayer, const Vector2& size )
730 // Create distance field shape
731 BitmapImage distanceField;
732 Size imageSize( 512, 512 );
733 CreateShapeImage( CIRCLE, imageSize, distanceField );
736 Actor backgroundAnimLayer0 = CreateBackgroundActor( size );
737 Actor backgroundAnimLayer1 = CreateBackgroundActor( size );
738 Actor backgroundAnimLayer2 = CreateBackgroundActor( size );
741 Constraint animConstraint0 = Constraint::New < Vector3 > ( Actor::POSITION,
742 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
743 AnimScrollConstraint( backgroundAnimLayer0.GetCurrentPosition(), 0.75f ) );
744 backgroundAnimLayer0.ApplyConstraint( animConstraint0 );
746 Constraint animConstraint1 = Constraint::New < Vector3 > ( Actor::POSITION,
747 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
748 AnimScrollConstraint( backgroundAnimLayer1.GetCurrentPosition(), 0.5f ) );
749 backgroundAnimLayer1.ApplyConstraint( animConstraint1 );
751 Constraint animConstraint2 = Constraint::New < Vector3 > ( Actor::POSITION,
752 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
753 AnimScrollConstraint( backgroundAnimLayer2.GetCurrentPosition(), 0.25f ) );
754 backgroundAnimLayer2.ApplyConstraint( animConstraint2 );
757 ImageActor layer = Dali::Toolkit::CreateSolidColorActor( BACKGROUND_COLOR );
758 layer.SetAnchorPoint( AnchorPoint::CENTER );
759 layer.SetParentOrigin( ParentOrigin::CENTER );
760 layer.SetSize( size * BACKGROUND_SIZE_SCALE );
761 layer.SetZ( BACKGROUND_Z );
762 layer.SetPositionInheritanceMode( DONT_INHERIT_POSITION );
764 addToLayer.Add( layer );
767 addToLayer.Add( backgroundAnimLayer0 );
768 addToLayer.Add( backgroundAnimLayer1 );
769 addToLayer.Add( backgroundAnimLayer2 );
771 // Add all the children
772 AddBackgroundActors( backgroundAnimLayer0, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
773 AddBackgroundActors( backgroundAnimLayer1, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
774 AddBackgroundActors( backgroundAnimLayer2, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
777 void DaliTableView::AddBackgroundActors( Actor layer, int count, BitmapImage distanceField, const Dali::Vector2& size )
779 for( int i = 0; i < count; ++i )
781 float randSize = Random::Range( 10.0f, 400.0f );
782 float hue = Random::Range( 0.3f, 1.0f );
783 Vector4 randColour( hue, hue*0.5, 0.0f, Random::Range( 0.3f, 0.6f ));
785 ImageActor dfActor = ImageActor::New( distanceField );
786 mBackgroundActors.push_back( dfActor );
787 dfActor.SetSize( Vector2( randSize, randSize ) );
788 dfActor.SetParentOrigin( ParentOrigin::CENTER );
790 Toolkit::DistanceFieldEffect effect = Toolkit::DistanceFieldEffect::New();
791 dfActor.SetShaderEffect( effect );
792 dfActor.SetColor( randColour );
793 effect.SetOutlineParams( Vector2( 0.55f, 0.00f ) );
794 effect.SetSmoothingEdge( 0.5f );
795 layer.Add( dfActor );
799 Random::Range( -size.x * 0.5f * BACKGROUND_SPREAD_SCALE, size.x * 0.5f * BACKGROUND_SPREAD_SCALE ),
800 Random::Range( -size.y * 0.5f - randSize, size.y * 0.5f + randSize ),
801 Random::Range(-1.0f, 0.0f) );
802 dfActor.SetPosition( actorPos );
804 Constraint movementConstraint = Constraint::New < Vector3 > ( Actor::POSITION,
805 LocalSource( Actor::SIZE ),
806 ParentSource( Actor::SIZE ),
807 ShapeMovementConstraint );
808 dfActor.ApplyConstraint( movementConstraint );
811 Animation animation = Animation::New( Random::Range( 40.0f, 200.0f ) );
812 KeyFrames keyframes = KeyFrames::New();
813 keyframes.Add( 0.0f, actorPos );
814 Vector3 toPos( actorPos );
815 toPos.y -= ( size.y + randSize );
816 keyframes.Add( 1.0f, toPos );
817 animation.AnimateBetween( Property( dfActor, Actor::POSITION ), keyframes );
818 animation.SetLooping( true );
820 mBackgroundAnimations.push_back( animation );
824 void DaliTableView::CreateShapeImage( ShapeType shapeType, const Size& size, BitmapImage& distanceFieldOut )
826 // this bitmap will hold the alpha map for the distance field shader
827 distanceFieldOut = BitmapImage::New( size.width, size.height, Pixel::A8 );
829 // Generate bit pattern
830 std::vector< unsigned char > imageDataA8;
831 imageDataA8.reserve( size.width * size.height ); // A8
836 GenerateCircle( size, imageDataA8 );
839 GenerateSquare( size, imageDataA8 );
845 PixelBuffer* buffer = distanceFieldOut.GetBuffer();
848 GenerateDistanceFieldMap( &imageDataA8[ 0 ], size, buffer, size, 8.0f, size );
849 distanceFieldOut.Update();
853 void DaliTableView::GenerateSquare( const Size& size, std::vector< unsigned char >& distanceFieldOut )
855 for( int h = 0; h < size.height; ++h )
857 for( int w = 0; w < size.width; ++w )
859 distanceFieldOut.push_back( 0xFF );
864 void DaliTableView::GenerateCircle( const Size& size, std::vector< unsigned char >& distanceFieldOut )
866 const float radius = size.width * 0.5f * size.width * 0.5f;
867 Vector2 center( size.width / 2, size.height / 2 );
869 for( int h = 0; h < size.height; ++h )
871 for( int w = 0; w < size.width; ++w )
874 Vector2 dist = pos - center;
876 if( dist.x * dist.x + dist.y * dist.y > radius )
878 distanceFieldOut.push_back( 0x00 );
882 distanceFieldOut.push_back( 0xFF );
888 ImageActor DaliTableView::CreateLogo( std::string imagePath )
890 Image image = Image::New( imagePath );
891 image.LoadingFinishedSignal().Connect( this, &DaliTableView::OnLogoLoaded );
893 ImageActor logo = ImageActor::New( image );
895 logo.SetAnchorPoint( AnchorPoint::CENTER );
896 logo.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
901 void DaliTableView::OnLogoLoaded( Dali::Image image )
903 mRootActor.SetFixedHeight( 1, image.GetHeight() + LOGO_BOTTOM_PADDING_HEIGHT );
906 bool DaliTableView::PauseBackgroundAnimation()
913 void DaliTableView::PauseAnimation()
915 if( mBackgroundAnimsPlaying )
917 for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
919 Animation anim = *animIter;
924 mBackgroundAnimsPlaying = false;
928 void DaliTableView::PlayAnimation()
930 if ( !mBackgroundAnimsPlaying )
932 for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
934 Animation anim = *animIter;
939 mBackgroundAnimsPlaying = true;
942 mAnimationTimer.SetInterval( BACKGROUND_ANIMATION_DURATION );
945 Dali::Actor DaliTableView::OnKeyboardPreFocusChange( Dali::Actor current, Dali::Actor proposed, Dali::Toolkit::Control::KeyboardFocusNavigationDirection direction )
947 Actor nextFocusActor = proposed;
949 if ( !current && !proposed )
951 // Set the initial focus to the first tile in the current page should be focused.
952 nextFocusActor = mTableViewList[mScrollView.GetCurrentPage()].GetChildAt(TableView::CellPosition(0, 0));
954 else if( !proposed || (proposed && proposed == mScrollViewLayer) )
956 // ScrollView is being focused but nothing in the current page can be focused further
957 // in the given direction. We should work out which page to scroll to next.
958 int currentPage = mScrollView.GetCurrentPage();
959 int newPage = currentPage;
960 if( direction == Dali::Toolkit::Control::Left )
964 else if( direction == Dali::Toolkit::Control::Right )
969 newPage = std::max(0, std::min(static_cast<int>(mScrollRulerX->GetTotalPages() - 1), newPage));
970 if( newPage == currentPage )
972 if( direction == Dali::Toolkit::Control::Left )
974 newPage = mScrollRulerX->GetTotalPages() - 1;
975 } else if( direction == Dali::Toolkit::Control::Right )
981 // Scroll to the page in the given direction
982 mScrollView.ScrollTo(newPage);
984 if( direction == Dali::Toolkit::Control::Left )
986 // Work out the cell position for the last tile
987 int remainingExamples = mExampleList.size() - newPage * EXAMPLES_PER_PAGE;
988 int rowPos = (remainingExamples >= EXAMPLES_PER_PAGE) ? ROWS_PER_PAGE - 1 : ( (remainingExamples % EXAMPLES_PER_PAGE + EXAMPLES_PER_ROW) / EXAMPLES_PER_ROW - 1 );
989 int colPos = remainingExamples >= EXAMPLES_PER_PAGE ? EXAMPLES_PER_ROW - 1 : ( remainingExamples % EXAMPLES_PER_PAGE - rowPos * EXAMPLES_PER_ROW - 1 );
991 // Move the focus to the last tile in the new page.
992 nextFocusActor = mTableViewList[newPage].GetChildAt(TableView::CellPosition(rowPos, colPos));
996 // Move the focus to the first tile in the new page.
997 nextFocusActor = mTableViewList[newPage].GetChildAt(TableView::CellPosition(0, 0));
1001 return nextFocusActor;
1004 void DaliTableView::OnFocusedActorActivated( Dali::Actor activatedActor )
1008 mPressedActor = activatedActor;
1010 // Activate the current focused actor;
1011 TouchEvent touchEventUp;
1012 touchEventUp.points.push_back( TouchPoint ( 0, TouchPoint::Up, 0.0f, 0.0f ) );
1013 OnTilePressed(mPressedActor, touchEventUp);
1017 bool DaliTableView::OnTileHovered( Actor actor, const HoverEvent& event )
1019 KeyboardFocusManager::Get().SetCurrentFocusActor( actor );