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 mRootActor.SetFixedHeight( 1, mLogo.GetImage().GetHeight() + LOGO_BOTTOM_PADDING_HEIGHT );
265 Alignment alignment = Alignment::New();
266 alignment.Add(mLogo);
267 mRootActor.AddChild( alignment, TableView::CellPosition( 1, 0 ) );
269 // scrollview occupying the majority of the screen
270 mScrollView = ScrollView::New();
272 mScrollView.SetAnchorPoint( AnchorPoint::CENTER );
273 mScrollView.SetParentOrigin( ParentOrigin::CENTER );
274 mScrollView.ApplyConstraint( Dali::Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ), Dali::RelativeToConstraint( TABLE_RELATIVE_SIZE ) ) );
275 mScrollView.SetAxisAutoLock( true );
276 mScrollView.ScrollCompletedSignal().Connect( this, &DaliTableView::OnScrollComplete );
277 mScrollView.ScrollStartedSignal().Connect( this, &DaliTableView::OnScrollStart );
278 mScrollView.TouchedSignal().Connect( this, &DaliTableView::OnScrollTouched );
280 mScrollViewLayer = Layer::New();
281 mScrollViewLayer.SetAnchorPoint( AnchorPoint::CENTER );
282 mScrollViewLayer.SetParentOrigin( ParentOrigin::CENTER );
283 mScrollViewLayer.SetSize( stageSize );
284 mScrollViewLayer.Add( mScrollView );
285 mRootActor.AddChild( mScrollViewLayer, TableView::CellPosition( 2, 0 ) );
287 // Setup the scenegraph
288 // 1) Add scroll view effect and setup constraints on pages
289 ApplyScrollViewEffect();
291 // 2) Add pages and tiles
294 // 3) Populate scrollview with background so constraints on background layers can work with scrollview
295 SetupBackground( mScrollView, stageSize );
297 // 4) Remove constraints for inner cube effect
298 for( TableViewListIter pageIter = mTableViewList.begin(); pageIter != mTableViewList.end(); ++pageIter )
300 TableView page = *pageIter;
302 unsigned int numChildren = page.GetChildCount();
303 Actor pageActor = page;
304 for( unsigned int i=0; i<numChildren; ++i)
306 // Remove old effect's manual constraints.
307 Actor child = pageActor.GetChildAt(i);
310 child.RemoveConstraints();
315 // Set initial orientation
316 unsigned int degrees = application.GetOrientation().GetDegrees();
319 Dali::Window winHandle = application.GetWindow();
320 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
321 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE );
322 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
323 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
325 Dali::Orientation orientation = winHandle.GetOrientation();
326 orientation.ChangedSignal().Connect( this, &DaliTableView::OrientationChanged );
328 winHandle.ShowIndicator( Dali::Window::INVISIBLE );
331 mAnimationTimer = Timer::New( BACKGROUND_ANIMATION_DURATION );
332 mAnimationTimer.TickSignal().Connect( this, &DaliTableView::PauseBackgroundAnimation );
333 mAnimationTimer.Start();
334 mBackgroundAnimsPlaying = true;
336 KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &DaliTableView::OnKeyboardPreFocusChange );
337 KeyboardFocusManager::Get().FocusedActorActivatedSignal().Connect( this, &DaliTableView::OnFocusedActorActivated );
340 void DaliTableView::Populate()
342 const Vector2 stageSize = Stage::GetCurrent().GetSize();
344 const Size demoTileSize( 0.25f * stageSize.width, 0.25f * stageSize.height );
346 mTotalPages = ( mExampleList.size() + EXAMPLES_PER_PAGE - 1 ) / EXAMPLES_PER_PAGE;
348 // Populate ScrollView.
349 if( mExampleList.size() > 0 )
351 if( mSortAlphabetically )
353 sort( mExampleList.begin(), mExampleList.end(), CompareByTitle );
356 unsigned int exampleCount = 0;
357 ExampleListConstIter iter = mExampleList.begin();
358 for( int t = 0; t < mTotalPages; t++ )
360 // Create Table. (contains up to 9 Examples)
361 TableView tableView = TableView::New( 4, 3 );
362 // Add tableView to container.
363 mScrollView.Add( tableView );
364 ApplyEffectToPage( tableView, TABLE_RELATIVE_SIZE );
366 tableView.SetAnchorPoint( AnchorPoint::CENTER );
367 tableView.SetParentOrigin( ParentOrigin::CENTER );
368 // 2 pixels of padding
369 tableView.SetCellPadding( Size( 2.0f, 2.0f ) );
371 Constraint constraint = Constraint::New<Vector3>( Actor::SCALE,
372 LocalSource( Actor::SIZE ),
373 ParentSource( Actor::SIZE ),
374 ScaleToFitConstraint() );
375 tableView.ApplyConstraint(constraint);
377 // Apply visibility constraint to table view
378 Constraint visibleConstraint = Constraint::New< bool >( Actor::VISIBLE,
379 LocalSource( Actor::POSITION ),
380 ParentSource( Actor::SIZE ),
381 TableViewVisibilityConstraint() );
382 visibleConstraint.SetRemoveAction( Constraint::Discard );
383 tableView.ApplyConstraint( visibleConstraint );
385 // add cells to table
386 for( int y = 0; y < ROWS_PER_PAGE; y++ )
388 for( int x = 0; x < EXAMPLES_PER_ROW; x++ )
390 const Example& example = ( *iter );
392 Actor tile = CreateTile( example.name, example.title, demoTileSize, true );
393 FocusManager focusManager = FocusManager::Get();
394 focusManager.SetFocusOrder( tile, ++exampleCount );
395 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_LABEL,
397 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_TRAIT, "Tile" );
398 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_HINT,
399 "You can run this example" );
401 tableView.AddChild( tile, TableView::CellPosition( y, x ) );
404 if( iter == mExampleList.end() )
409 if( iter == mExampleList.end() )
416 tableView.SetFixedHeight( 3, BOTTOM_ROW_HEIGHT );
418 std::stringstream out;
419 out << ( t + 1 ) << " of " << mTotalPages;
420 Actor pageNumberText = CreateTile( "", out.str(), Size( 0.8f * stageSize.width, BOTTOM_ROW_HEIGHT ), false );
422 pageNumberText.ApplyConstraint( Constraint::New< Vector3 >( Actor::POSITION, Source( tableView, Actor::WORLD_POSITION),
423 TranslateLocalConstraint( Vector3( 0.0f, stageSize.y * 0.4f, 0.0f ) ) ) );
424 pageNumberText.ApplyConstraint( Constraint::New< Quaternion >( Actor::ROTATION, Source( tableView, Actor::WORLD_ROTATION ), EqualToConstraint() ) );
425 pageNumberText.ApplyConstraint( Constraint::New< Vector4 >( Actor::COLOR, Source( tableView, Actor::COLOR ), EqualToConstraint() ) );
427 //Stage::GetCurrent().Add( pageNumberText );
429 // Set tableview position
430 Vector3 tableViewPos( stageSize.x * TABLE_RELATIVE_SIZE.x * t, 0.0f, 0.0f );
431 tableView.SetPosition( tableViewPos );
433 mTableViewList.push_back( tableView );
435 if( iter == mExampleList.end() )
442 // Update Ruler info.
443 mScrollRulerX = new FixedRuler( stageSize.width * TABLE_RELATIVE_SIZE.x );
444 mScrollRulerY = new DefaultRuler();
445 mScrollRulerX->SetDomain( RulerDomain( 0.0f, mTotalPages * stageSize.width * TABLE_RELATIVE_SIZE.x, true ) );
446 mScrollRulerY->Disable();
447 mScrollView.SetRulerX( mScrollRulerX );
448 mScrollView.SetRulerY( mScrollRulerY );
451 void DaliTableView::OrientationChanged( Orientation orientation )
453 // TODO: Implement if orientation change required
456 void DaliTableView::Rotate( unsigned int degrees )
458 // Resize the root actor
459 Vector2 stageSize = Stage::GetCurrent().GetSize();
460 Vector3 targetSize( stageSize.x, stageSize.y, 1.0f );
462 if( degrees == 90 || degrees == 270 )
464 targetSize = Vector3( stageSize.y, stageSize.x, 1.0f );
467 if( mRotateAnimation )
469 mRotateAnimation.Stop();
470 mRotateAnimation.Clear();
473 mRotateAnimation = Animation::New( ROTATE_ANIMATION_TIME );
474 mRotateAnimation.RotateTo( mRootActor, Degree( 360 - degrees ), Vector3::ZAXIS, AlphaFunctions::EaseOut );
475 mRotateAnimation.Resize( mRootActor, targetSize, AlphaFunctions::EaseOut );
476 mRotateAnimation.Play();
479 Actor DaliTableView::CreateTile( const std::string& name, const std::string& title, const Size& parentSize, bool addBackground )
481 Actor tile = Actor::New();
482 tile.SetName( name );
483 tile.SetAnchorPoint( AnchorPoint::CENTER );
484 tile.SetParentOrigin( ParentOrigin::CENTER );
486 // make the tile 100% of parent
487 tile.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
489 Actor content = Actor::New();
490 content.SetAnchorPoint( AnchorPoint::CENTER );
491 content.SetParentOrigin( ParentOrigin::CENTER );
492 content.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
495 // create background image
498 Image bg = Image::New( TILE_BACKGROUND );
499 ImageActor image = ImageActor::New( bg );
500 image.SetAnchorPoint( AnchorPoint::CENTER );
501 image.SetParentOrigin( ParentOrigin::CENTER );
502 // make the image 100% of tile
503 image.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
504 // move image back to get text appear in front
506 image.SetStyle( ImageActor::STYLE_NINE_PATCH );
507 image.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
509 content.Add( image );
512 ImageActor stencil = NewStencilImage();
513 stencil.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
514 image.Add( stencil );
517 // Set the tile to be keyboard focusable
518 tile.SetKeyboardFocusable(true);
520 // connect to the touch events
521 tile.TouchedSignal().Connect( this, &DaliTableView::OnTilePressed );
522 tile.HoveredSignal().Connect( this, &DaliTableView::OnTileHovered );
527 ImageActor DaliTableView::NewStencilImage()
529 Image alpha = Image::New( TILE_BACKGROUND_ALPHA );
531 ImageActor stencilActor = ImageActor::New( alpha );
532 stencilActor.SetStyle( ImageActor::STYLE_NINE_PATCH );
533 stencilActor.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
535 stencilActor.SetParentOrigin( ParentOrigin::CENTER );
536 stencilActor.SetAnchorPoint( AnchorPoint::CENTER );
537 stencilActor.SetDrawMode( DrawMode::STENCIL );
539 Dali::ShaderEffect shaderEffect = AlphaDiscardEffect::New();
540 stencilActor.SetShaderEffect( shaderEffect );
545 bool DaliTableView::OnTilePressed( Actor actor, const TouchEvent& event )
547 bool consumed = false;
549 const TouchPoint& point = event.GetPoint( 0 );
550 if( TouchPoint::Down == point.state )
552 mPressedActor = actor;
556 // A button press is only valid if the Down & Up events
557 // both occurred within the button.
558 if( ( TouchPoint::Up == point.state ) &&
559 ( mPressedActor == actor ) )
561 std::string name = actor.GetName();
562 ExampleMapConstIter iter = mExampleMap.find( name );
564 FocusManager focusManager = FocusManager::Get();
566 if( iter != mExampleMap.end() )
568 // ignore Example button presses when scrolling or button animating.
569 if( ( !mScrolling ) && ( !mPressedAnimation ) )
571 // do nothing, until pressed animation finished.
578 mPressedAnimation = Animation::New( BUTTON_PRESS_ANIMATION_TIME );
579 mPressedAnimation.SetEndAction( Animation::Discard );
581 // scale the content actor within the Tile, as to not affect the placement within the Table.
582 Actor content = actor.GetChildAt(0);
583 mPressedAnimation.ScaleTo( content, Vector3( 0.9f, 0.9f, 1.0f ), AlphaFunctions::EaseInOut, 0.0f,
584 BUTTON_PRESS_ANIMATION_TIME * 0.5f );
585 mPressedAnimation.ScaleTo( content, Vector3::ONE, AlphaFunctions::EaseInOut, BUTTON_PRESS_ANIMATION_TIME * 0.5f,
586 BUTTON_PRESS_ANIMATION_TIME * 0.5f );
587 mPressedAnimation.Play();
588 mPressedAnimation.FinishedSignal().Connect( this, &DaliTableView::OnPressedAnimationFinished );
594 void DaliTableView::OnPressedAnimationFinished( Dali::Animation& source )
596 mPressedAnimation.Reset();
599 std::string name = mPressedActor.GetName();
600 ExampleMapConstIter iter = mExampleMap.find( name );
602 if( iter == mExampleMap.end() )
604 if( name == BUTTON_QUIT )
606 // Move focus to the OK button
607 FocusManager focusManager = FocusManager::Get();
609 // Enable the group mode and wrap mode
610 focusManager.SetGroupMode( true );
611 focusManager.SetWrapMode( true );
616 const Example& example( iter->second );
618 std::stringstream stream;
619 stream << DALI_EXAMPLE_BIN << example.name.c_str();
623 execlp( stream.str().c_str(), example.name.c_str(), NULL );
624 DALI_ASSERT_ALWAYS(false && "exec failed!");
627 mPressedActor.Reset();
631 void DaliTableView::OnScrollStart( const Dali::Vector3& position )
638 void DaliTableView::OnScrollComplete( const Dali::Vector3& position )
642 // move focus to 1st item of new page
643 FocusManager focusManager = FocusManager::Get();
644 focusManager.SetCurrentFocusActor(mTableViewList[mScrollView.GetCurrentPage()].GetChildAt(TableView::CellPosition(1, 0)) );
648 bool DaliTableView::OnScrollTouched( Actor actor, const TouchEvent& event )
650 const TouchPoint& point = event.GetPoint( 0 );
651 if( TouchPoint::Down == point.state )
653 mPressedActor = actor;
659 void DaliTableView::ApplyScrollViewEffect()
661 // Remove old effect if exists.
663 if( mScrollViewEffect )
665 mScrollView.RemoveEffect( mScrollViewEffect );
668 // Just one effect for now
669 SetupInnerPageCubeEffect();
671 mScrollView.ApplyEffect( mScrollViewEffect );
674 void DaliTableView::SetupInnerPageCubeEffect()
676 ScrollViewCustomEffect customEffect;
677 mScrollViewEffect = customEffect = ScrollViewCustomEffect::New();
678 mScrollView.SetScrollSnapDuration( EFFECT_SNAP_DURATION );
679 mScrollView.SetScrollFlickDuration( EFFECT_FLICK_DURATION );
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 ImageActor logo = ImageActor::New( image );
893 logo.SetAnchorPoint( AnchorPoint::CENTER );
894 logo.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
899 bool DaliTableView::PauseBackgroundAnimation()
906 void DaliTableView::PauseAnimation()
908 if( mBackgroundAnimsPlaying )
910 for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
912 Animation anim = *animIter;
917 mBackgroundAnimsPlaying = false;
921 void DaliTableView::PlayAnimation()
923 if ( !mBackgroundAnimsPlaying )
925 for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
927 Animation anim = *animIter;
932 mBackgroundAnimsPlaying = true;
935 mAnimationTimer.SetInterval( BACKGROUND_ANIMATION_DURATION );
938 Dali::Actor DaliTableView::OnKeyboardPreFocusChange( Dali::Actor current, Dali::Actor proposed, Dali::Toolkit::Control::KeyboardFocusNavigationDirection direction )
940 Actor nextFocusActor = proposed;
942 if ( !current && !proposed )
944 // Set the initial focus to the first tile in the current page should be focused.
945 nextFocusActor = mTableViewList[mScrollView.GetCurrentPage()].GetChildAt(TableView::CellPosition(0, 0));
947 else if( !proposed || (proposed && proposed == mScrollViewLayer) )
949 // ScrollView is being focused but nothing in the current page can be focused further
950 // in the given direction. We should work out which page to scroll to next.
951 int currentPage = mScrollView.GetCurrentPage();
952 int newPage = currentPage;
953 if( direction == Dali::Toolkit::Control::Left )
957 else if( direction == Dali::Toolkit::Control::Right )
962 newPage = std::max(0, std::min(static_cast<int>(mScrollRulerX->GetTotalPages() - 1), newPage));
963 if( newPage == currentPage )
965 if( direction == Dali::Toolkit::Control::Left )
967 newPage = mScrollRulerX->GetTotalPages() - 1;
968 } else if( direction == Dali::Toolkit::Control::Right )
974 // Scroll to the page in the given direction
975 mScrollView.ScrollTo(newPage);
977 if( direction == Dali::Toolkit::Control::Left )
979 // Work out the cell position for the last tile
980 int remainingExamples = mExampleList.size() - newPage * EXAMPLES_PER_PAGE;
981 int rowPos = (remainingExamples >= EXAMPLES_PER_PAGE) ? ROWS_PER_PAGE - 1 : ( (remainingExamples % EXAMPLES_PER_PAGE + EXAMPLES_PER_ROW) / EXAMPLES_PER_ROW - 1 );
982 int colPos = remainingExamples >= EXAMPLES_PER_PAGE ? EXAMPLES_PER_ROW - 1 : ( remainingExamples % EXAMPLES_PER_PAGE - rowPos * EXAMPLES_PER_ROW - 1 );
984 // Move the focus to the last tile in the new page.
985 nextFocusActor = mTableViewList[newPage].GetChildAt(TableView::CellPosition(rowPos, colPos));
989 // Move the focus to the first tile in the new page.
990 nextFocusActor = mTableViewList[newPage].GetChildAt(TableView::CellPosition(0, 0));
994 return nextFocusActor;
997 void DaliTableView::OnFocusedActorActivated( Dali::Actor activatedActor )
1001 mPressedActor = activatedActor;
1003 // Activate the current focused actor;
1004 TouchEvent touchEventUp;
1005 touchEventUp.points.push_back( TouchPoint ( 0, TouchPoint::Up, 0.0f, 0.0f ) );
1006 OnTilePressed(mPressedActor, touchEventUp);
1010 bool DaliTableView::OnTileHovered( Actor actor, const HoverEvent& event )
1012 KeyboardFocusManager::Get().SetCurrentFocusActor( actor );