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 LOGO_MARGIN_RATIO = 0.5f / 0.9f;
57 const float BOTTOM_PADDING_RATIO = 0.4f / 0.9f;
58 const Vector3 SCROLLVIEW_RELATIVE_SIZE(0.9f, 1.0f, 0.8f ); ///< ScrollView's relative size to its parent
59 const Vector3 TABLE_RELATIVE_SIZE(0.9f, 0.9f, 0.8f ); ///< TableView's relative size to the entire stage. The Y value means sum of the logo and table relative heights.
60 const float STENCIL_RELATIVE_SIZE = 1.0f;
62 const float EFFECT_SNAP_DURATION = 0.66f; ///< Scroll Snap Duration for Effects
63 const float EFFECT_FLICK_DURATION = 0.5f; ///< Scroll Flick Duration for Effects
64 const Vector3 ANGLE_CUBE_PAGE_ROTATE(Math::PI * 0.5f, Math::PI * 0.5f, 0.0f);
66 const int NUM_BACKGROUND_IMAGES = 20;
67 const float BACKGROUND_SWIPE_SCALE = 0.025f;
68 const float BACKGROUND_SPREAD_SCALE = 1.5f;
69 const float SCALE_MOD = 1000.0f * Math::PI * 2.0f;
70 const float SCALE_SPEED = 10.0f;
71 const float SCALE_SPEED_SIN = 0.1f;
73 const unsigned int BACKGROUND_ANIMATION_DURATION = 15000; // 15 secs
75 const float BACKGROUND_Z = -1.0f;
76 const float BACKGROUND_SIZE_SCALE = 1.0f;
77 const Vector4 BACKGROUND_COLOR( 1.0f, 1.0f, 1.0f, 1.0f );
79 const float BUBBLE_MIN_Z = -1.0;
80 const float BUBBLE_MAX_Z = 0.0f;
82 // 3D Effect constants
83 const Vector2 ANGLE_SWING_3DEFFECT( Math::PI_2 * 0.75, Math::PI_2 * 0.75f ); ///< Angle Swing in radians
84 const Vector2 POSITION_SWING_3DEFFECT( 0.55f, 0.4f ); ///< Position Swing relative to stage size.
85 const Vector3 ANCHOR_3DEFFECT_STYLE0( -105.0f, 30.0f, -240.0f ); ///< Rotation Anchor position for 3D Effect (Style 0)
86 const Vector3 ANCHOR_3DEFFECT_STYLE1( 65.0f, -70.0f, -500.0f ); ///< Rotation Anchor position for 3D Effect (Style 1)
88 //const std::string DEFAULT_TEXT_STYLE_FONT_FAMILY("HelveticaNeue");
89 //const std::string DEFAULT_TEXT_STYLE_FONT_STYLE("Regular");
90 //const Dali::Vector4 DEFAULT_TEXT_STYLE_COLOR(0.7f, 0.7f, 0.7f, 1.0f);
92 //const std::string TABLE_TEXT_STYLE_FONT_FAMILY("HelveticaNeue");
93 //const std::string TABLE_TEXT_STYLE_FONT_STYLE("Regular");
94 //const Dali::PointSize TABLE_TEXT_STYLE_POINT_SIZE( 8.0f );
95 //const Dali::TextStyle::Weight TABLE_TEXT_STYLE_WEIGHT(Dali::TextStyle::LIGHT);
96 //const Dali::Vector4 TABLE_TEXT_STYLE_COLOR(0.0f, 0.0f, 0.0f, 1.0f);
98 Vector3 ScalePointSize(const Vector3& vec)
100 return Vector3( DemoHelper::ScalePointSize( vec.x ), DemoHelper::ScalePointSize( vec.y ), DemoHelper::ScalePointSize( vec.z ) );
103 #define DP(x) DemoHelper::ScalePointSize(x)
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 const 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 Stage::GetCurrent().Add( mRootActor );
267 Dali::Toolkit::ToolBar toolbar;
268 Dali::Layer toolBarLayer = DemoHelper::CreateToolbar(toolbar,
269 DEFAULT_TOOLBAR_IMAGE_PATH,
270 DEFAULT_TOOLBAR_TEXT,
271 DemoHelper::DEFAULT_VIEW_STYLE);
273 mRootActor.AddChild( toolBarLayer, TableView::CellPosition( 0, 0 ) );
274 const float toolbarHeight = DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight;
275 mRootActor.SetFixedHeight( 0, toolbarHeight );
278 mLogo = CreateLogo( LOGO_PATH );
279 const float paddingHeight = ( ( 1.f-TABLE_RELATIVE_SIZE.y ) * stageSize.y );
280 const float logoMargin = paddingHeight * LOGO_MARGIN_RATIO;
281 const float logoHeight = mLogo.GetImage().GetHeight() + logoMargin;
282 mRootActor.SetFixedHeight( 1, logoHeight );
284 const float bottomMargin = paddingHeight * BOTTOM_PADDING_RATIO;
285 mButtonsPageRelativeSize = Vector3( TABLE_RELATIVE_SIZE.x, 1.f - ( toolbarHeight + logoHeight + bottomMargin) / stageSize.height, TABLE_RELATIVE_SIZE.z );
286 mRootActor.SetFixedHeight( 2, mButtonsPageRelativeSize.y * stageSize.height );
288 Alignment alignment = Alignment::New();
289 alignment.Add(mLogo);
290 mRootActor.AddChild( alignment, TableView::CellPosition( 1, 0 ) );
292 // scrollview occupying the majority of the screen
293 mScrollView = ScrollView::New();
295 mScrollView.SetAnchorPoint( AnchorPoint::CENTER );
296 mScrollView.SetParentOrigin( ParentOrigin::CENTER );
297 mScrollView.ApplyConstraint( Dali::Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ),
298 Dali::RelativeToConstraint( SCROLLVIEW_RELATIVE_SIZE ) ) );
299 mScrollView.SetAxisAutoLock( true );
300 mScrollView.ScrollCompletedSignal().Connect( this, &DaliTableView::OnScrollComplete );
301 mScrollView.ScrollStartedSignal().Connect( this, &DaliTableView::OnScrollStart );
302 mScrollView.TouchedSignal().Connect( this, &DaliTableView::OnScrollTouched );
304 mScrollViewLayer = Layer::New();
305 mScrollViewLayer.SetAnchorPoint( AnchorPoint::CENTER );
306 mScrollViewLayer.SetParentOrigin( ParentOrigin::CENTER );
307 mScrollViewLayer.SetDrawMode( DrawMode::OVERLAY );
309 // Populate background and bubbles - needs to be scrollViewLayer so scroll ends show
310 SetupBackground( mScrollView, mScrollViewLayer, stageSize );
312 mScrollViewLayer.Add( mScrollView );
313 mRootActor.AddChild( mScrollViewLayer, TableView::CellPosition( 2, 0 ) );
315 // Add scroll view effect and setup constraints on pages
316 ApplyScrollViewEffect();
318 // Add pages and tiles
321 // Remove constraints for inner cube effect
322 ApplyCubeEffectToActors();
324 // Set initial orientation
325 unsigned int degrees = application.GetOrientation().GetDegrees();
328 Dali::Window winHandle = application.GetWindow();
329 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
330 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE );
331 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
332 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
334 Dali::Orientation orientation = winHandle.GetOrientation();
335 orientation.ChangedSignal().Connect( this, &DaliTableView::OrientationChanged );
337 winHandle.ShowIndicator( Dali::Window::INVISIBLE );
340 mAnimationTimer = Timer::New( BACKGROUND_ANIMATION_DURATION );
341 mAnimationTimer.TickSignal().Connect( this, &DaliTableView::PauseBackgroundAnimation );
342 mAnimationTimer.Start();
343 mBackgroundAnimsPlaying = true;
345 KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &DaliTableView::OnKeyboardPreFocusChange );
346 KeyboardFocusManager::Get().FocusedActorActivatedSignal().Connect( this, &DaliTableView::OnFocusedActorActivated );
349 void DaliTableView::ApplyCubeEffectToActors()
351 for( ActorIter pageIter = mPages.begin(); pageIter != mPages.end(); ++pageIter )
353 Actor page = *pageIter;
355 unsigned int numChildren = page.GetChildCount();
356 Actor pageActor = page;
357 for( unsigned int i=0; i<numChildren; ++i)
359 // Remove old effect's manual constraints.
360 Actor child = pageActor.GetChildAt(i);
363 ApplyCubeEffectToActor( child );
368 void DaliTableView::Populate()
370 const Vector2 stageSize = Stage::GetCurrent().GetSize();
372 mTotalPages = ( mExampleList.size() + EXAMPLES_PER_PAGE - 1 ) / EXAMPLES_PER_PAGE;
374 // Populate ScrollView.
375 if( mExampleList.size() > 0 )
377 if( mSortAlphabetically )
379 sort( mExampleList.begin(), mExampleList.end(), CompareByTitle );
382 unsigned int exampleCount = 0;
383 ExampleListConstIter iter = mExampleList.begin();
385 for( int t = 0; t < mTotalPages; t++ )
387 // Create Table. (contains up to 9 Examples)
388 Actor page = Actor::New();
390 // Add tableView to container.
391 mScrollView.Add( page );
393 page.SetAnchorPoint( AnchorPoint::CENTER );
394 page.SetParentOrigin( ParentOrigin::CENTER );
395 page.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
397 // add cells to table
398 const float margin = 4.0f;
400 // Calculate the number of images going across (columns) within a page, according to the screen resolution and dpi.
401 const Size tileSize((stageSize.x * mButtonsPageRelativeSize.x / EXAMPLES_PER_ROW) - margin, (stageSize.y * mButtonsPageRelativeSize.y / ROWS_PER_PAGE) - margin );
403 for(int row = 0; row < ROWS_PER_PAGE; row++)
405 for(int column = 0; column < EXAMPLES_PER_ROW; column++)
407 const Example& example = ( *iter );
409 Actor tile = CreateTile( example.name, example.title, tileSize, 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 Vector3 position( margin * 0.5f + (tileSize.x + margin) * column - stageSize.width * mButtonsPageRelativeSize.x * 0.5f,
419 margin * 0.5f + (tileSize.y + margin) * row - stageSize.height * mButtonsPageRelativeSize.y * 0.5f,
421 tile.SetPosition( position + Vector3( tileSize.x, tileSize.y, 0.0f ) * 0.5f );
422 tile.SetSize( tileSize );
427 if( iter == mExampleList.end() )
433 if( iter == mExampleList.end() )
439 // Set tableview position
440 Vector3 pagePos( stageSize.x * mButtonsPageRelativeSize.x * t, 0.0f, 0.0f );
441 page.SetPosition( pagePos );
443 mPages.push_back( page );
445 if( iter == mExampleList.end() )
452 // Update Ruler info.
453 mScrollRulerX = new FixedRuler( stageSize.width * mButtonsPageRelativeSize.x );
454 mScrollRulerY = new DefaultRuler();
455 mScrollRulerX->SetDomain( RulerDomain( 0.0f, mTotalPages * stageSize.width * mButtonsPageRelativeSize.x, true ) );
456 mScrollRulerY->Disable();
457 mScrollView.SetRulerX( mScrollRulerX );
458 mScrollView.SetRulerY( mScrollRulerY );
461 void DaliTableView::OrientationChanged( Orientation orientation )
463 // TODO: Implement if orientation change required
466 void DaliTableView::Rotate( unsigned int degrees )
468 // Resize the root actor
469 Vector2 stageSize = Stage::GetCurrent().GetSize();
470 Vector3 targetSize( stageSize.x, stageSize.y, 1.0f );
472 if( degrees == 90 || degrees == 270 )
474 targetSize = Vector3( stageSize.y, stageSize.x, 1.0f );
477 if( mRotateAnimation )
479 mRotateAnimation.Stop();
480 mRotateAnimation.Clear();
483 mRotateAnimation = Animation::New( ROTATE_ANIMATION_TIME );
484 mRotateAnimation.RotateTo( mRootActor, Degree( 360 - degrees ), Vector3::ZAXIS, AlphaFunctions::EaseOut );
485 mRotateAnimation.Resize( mRootActor, targetSize, AlphaFunctions::EaseOut );
486 mRotateAnimation.Play();
489 Actor DaliTableView::CreateTile( const std::string& name, const std::string& title, const Size& parentSize, bool addBackground )
491 Actor tile = Actor::New();
492 tile.SetName( name );
493 tile.SetAnchorPoint( AnchorPoint::CENTER );
494 tile.SetParentOrigin( ParentOrigin::CENTER );
496 Actor content = Actor::New();
497 content.SetAnchorPoint( AnchorPoint::CENTER );
498 content.SetParentOrigin( ParentOrigin::CENTER );
499 content.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
502 // create background image
505 Image bg = Image::New( TILE_BACKGROUND );
506 ImageActor image = ImageActor::New( bg );
507 image.SetAnchorPoint( AnchorPoint::CENTER );
508 image.SetParentOrigin( ParentOrigin::CENTER );
509 // make the image 100% of tile
510 image.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
511 // move image back to get text appear in front
513 image.SetStyle( ImageActor::STYLE_NINE_PATCH );
514 image.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
516 content.Add( image );
519 ImageActor stencil = NewStencilImage();
520 stencil.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
521 image.Add( stencil );
524 TextLabel label = TextLabel::New();
525 label.SetParentOrigin( ParentOrigin::TOP_LEFT );
526 label.SetAnchorPoint( AnchorPoint::TOP_LEFT );
527 label.SetProperty( TextLabel::PROPERTY_MULTI_LINE, true );
528 label.SetProperty( TextLabel::PROPERTY_TEXT, title );
529 label.SetColor( Color::WHITE );
530 content.Add( label );
532 // Set the tile to be keyboard focusable
533 tile.SetKeyboardFocusable(true);
535 // connect to the touch events
536 tile.TouchedSignal().Connect( this, &DaliTableView::OnTilePressed );
537 tile.HoveredSignal().Connect( this, &DaliTableView::OnTileHovered );
542 ImageActor DaliTableView::NewStencilImage()
544 Image alpha = Image::New( TILE_BACKGROUND_ALPHA );
546 ImageActor stencilActor = ImageActor::New( alpha );
547 stencilActor.SetStyle( ImageActor::STYLE_NINE_PATCH );
548 stencilActor.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
550 stencilActor.SetParentOrigin( ParentOrigin::CENTER );
551 stencilActor.SetAnchorPoint( AnchorPoint::CENTER );
552 stencilActor.SetDrawMode( DrawMode::STENCIL );
554 Dali::ShaderEffect shaderEffect = AlphaDiscardEffect::New();
555 stencilActor.SetShaderEffect( shaderEffect );
560 bool DaliTableView::OnTilePressed( Actor actor, const TouchEvent& event )
562 bool consumed = false;
564 const TouchPoint& point = event.GetPoint( 0 );
565 if( TouchPoint::Down == point.state )
567 mPressedActor = actor;
571 // A button press is only valid if the Down & Up events
572 // both occurred within the button.
573 if( ( TouchPoint::Up == point.state ) &&
574 ( mPressedActor == actor ) )
576 std::string name = actor.GetName();
577 ExampleMapConstIter iter = mExampleMap.find( name );
579 FocusManager focusManager = FocusManager::Get();
581 if( iter != mExampleMap.end() )
583 // ignore Example button presses when scrolling or button animating.
584 if( ( !mScrolling ) && ( !mPressedAnimation ) )
586 // do nothing, until pressed animation finished.
593 mPressedAnimation = Animation::New( BUTTON_PRESS_ANIMATION_TIME );
594 mPressedAnimation.SetEndAction( Animation::Discard );
596 // scale the content actor within the Tile, as to not affect the placement within the Table.
597 Actor content = actor.GetChildAt(0);
598 mPressedAnimation.ScaleTo( content, Vector3( 0.9f, 0.9f, 1.0f ), AlphaFunctions::EaseInOut, 0.0f,
599 BUTTON_PRESS_ANIMATION_TIME * 0.5f );
600 mPressedAnimation.ScaleTo( content, Vector3::ONE, AlphaFunctions::EaseInOut, BUTTON_PRESS_ANIMATION_TIME * 0.5f,
601 BUTTON_PRESS_ANIMATION_TIME * 0.5f );
602 mPressedAnimation.Play();
603 mPressedAnimation.FinishedSignal().Connect( this, &DaliTableView::OnPressedAnimationFinished );
609 void DaliTableView::OnPressedAnimationFinished( Dali::Animation& source )
611 mPressedAnimation.Reset();
614 std::string name = mPressedActor.GetName();
615 ExampleMapConstIter iter = mExampleMap.find( name );
617 if( iter == mExampleMap.end() )
619 if( name == BUTTON_QUIT )
621 // Move focus to the OK button
622 FocusManager focusManager = FocusManager::Get();
624 // Enable the group mode and wrap mode
625 focusManager.SetGroupMode( true );
626 focusManager.SetWrapMode( true );
631 const Example& example( iter->second );
633 std::stringstream stream;
634 stream << DALI_EXAMPLE_BIN << example.name.c_str();
638 execlp( stream.str().c_str(), example.name.c_str(), NULL );
639 DALI_ASSERT_ALWAYS(false && "exec failed!");
642 mPressedActor.Reset();
646 void DaliTableView::OnScrollStart( const Dali::Vector3& position )
653 void DaliTableView::OnScrollComplete( const Dali::Vector3& position )
657 // move focus to 1st item of new page
658 FocusManager focusManager = FocusManager::Get();
659 focusManager.SetCurrentFocusActor(mPages[mScrollView.GetCurrentPage()].GetChildAt(0) );
661 ApplyCubeEffectToActors();
664 bool DaliTableView::OnScrollTouched( Actor actor, const TouchEvent& event )
666 const TouchPoint& point = event.GetPoint( 0 );
667 if( TouchPoint::Down == point.state )
669 mPressedActor = actor;
675 void DaliTableView::ApplyScrollViewEffect()
677 // Remove old effect if exists.
679 if( mScrollViewEffect )
681 mScrollView.RemoveEffect( mScrollViewEffect );
684 // Just one effect for now
685 SetupInnerPageCubeEffect();
687 mScrollView.ApplyEffect( mScrollViewEffect );
690 void DaliTableView::SetupInnerPageCubeEffect()
692 mScrollViewEffect = ScrollViewCubeEffect::New();
693 mScrollView.SetScrollSnapDuration( EFFECT_SNAP_DURATION );
694 mScrollView.SetScrollFlickDuration( EFFECT_FLICK_DURATION );
695 mScrollView.RemoveConstraintsFromChildren();
698 void DaliTableView::ApplyCubeEffectToActor( Actor actor )
700 actor.RemoveConstraints();
702 ScrollViewCubeEffect cubeEffect = ScrollViewCubeEffect::DownCast(mScrollViewEffect);
703 cubeEffect.ApplyToActor( actor,
704 ScalePointSize( ( rand() & 1 ) ? ANCHOR_3DEFFECT_STYLE0 : ANCHOR_3DEFFECT_STYLE1 ),
705 ANGLE_SWING_3DEFFECT,
706 POSITION_SWING_3DEFFECT * Vector2(Stage::GetCurrent().GetSize()));
709 void DaliTableView::OnKeyEvent( const KeyEvent& event )
711 if( event.state == KeyEvent::Down )
713 if ( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
720 Actor CreateBackgroundActor( const Vector2& size )
722 Actor layer = Actor::New();
723 layer.SetAnchorPoint( AnchorPoint::CENTER );
724 layer.SetParentOrigin( ParentOrigin::CENTER );
725 layer.SetSize( size );
729 void DaliTableView::SetupBackground( Actor bubbleLayer, Actor backgroundLayer, const Vector2& size )
731 // Create distance field shape
732 BitmapImage distanceField;
733 Size imageSize( 512, 512 );
734 CreateShapeImage( CIRCLE, imageSize, distanceField );
737 Actor backgroundAnimLayer0 = CreateBackgroundActor( size );
738 Actor backgroundAnimLayer1 = CreateBackgroundActor( size );
739 Actor backgroundAnimLayer2 = CreateBackgroundActor( size );
742 Constraint animConstraint0 = Constraint::New < Vector3 > ( Actor::POSITION,
743 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
744 AnimScrollConstraint( backgroundAnimLayer0.GetCurrentPosition(), 0.75f ) );
745 backgroundAnimLayer0.ApplyConstraint( animConstraint0 );
747 Constraint animConstraint1 = Constraint::New < Vector3 > ( Actor::POSITION,
748 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
749 AnimScrollConstraint( backgroundAnimLayer1.GetCurrentPosition(), 0.5f ) );
750 backgroundAnimLayer1.ApplyConstraint( animConstraint1 );
752 Constraint animConstraint2 = Constraint::New < Vector3 > ( Actor::POSITION,
753 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
754 AnimScrollConstraint( backgroundAnimLayer2.GetCurrentPosition(), 0.25f ) );
755 backgroundAnimLayer2.ApplyConstraint( animConstraint2 );
758 ImageActor layer = Dali::Toolkit::CreateSolidColorActor( BACKGROUND_COLOR );
759 layer.SetAnchorPoint( AnchorPoint::CENTER );
760 layer.SetParentOrigin( ParentOrigin::CENTER );
761 layer.SetSize( size * BACKGROUND_SIZE_SCALE );
762 layer.SetZ( BACKGROUND_Z );
763 layer.SetPositionInheritanceMode( DONT_INHERIT_POSITION );
764 backgroundLayer.Add( layer );
767 bubbleLayer.Add( backgroundAnimLayer0 );
768 bubbleLayer.Add( backgroundAnimLayer1 );
769 bubbleLayer.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( BUBBLE_MIN_Z, BUBBLE_MAX_Z ) );
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 = mPages[mScrollView.GetCurrentPage()].GetChildAt(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 = mPages[newPage].GetChildAt(colPos * EXAMPLES_PER_ROW + rowPos);
989 // Move the focus to the first tile in the new page.
990 nextFocusActor = mPages[newPage].GetChildAt(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 );