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;
31 ///////////////////////////////////////////////////////////////////////////////
36 const std::string BUTTON_BACKWARD( "Backward" );
37 const std::string BUTTON_FORWARD( "Forward" );
38 const std::string BUTTON_QUIT( "Quit" );
39 const std::string BUTTON_OK( "Ok" );
40 const std::string BUTTON_CANCEL( "Cancel" );
42 const std::string DEFAULT_BACKGROUND_IMAGE_PATH( DALI_IMAGE_DIR "background-gradient.jpg" );
43 const std::string LOGO_PATH( DALI_IMAGE_DIR "dali-logo.png" );
44 const std::string DEFAULT_TOOLBAR_IMAGE_PATH( DALI_IMAGE_DIR "top-bar.png" );
45 const std::string BUTTON_BACKGROUND(DALI_IMAGE_DIR "button-background.png");
46 const std::string TILE_BACKGROUND(DALI_IMAGE_DIR "item-background.png");
47 const std::string TILE_BACKGROUND_ALPHA(DALI_IMAGE_DIR "item-background-alpha.png");
49 const char * const DEFAULT_TOOLBAR_TEXT( "TOUCH TO LAUNCH EXAMPLE" );
51 const float BUTTON_PRESS_ANIMATION_TIME = 0.25f; ///< Time to perform button scale effect.
52 const float ROTATE_ANIMATION_TIME = 0.5f; ///< Time to perform rotate effect.
53 const int MAX_PAGES = 256; ///< Maximum pages (arbitrary safety limit)
54 const int EXAMPLES_PER_ROW = 3;
55 const int ROWS_PER_PAGE = 3;
56 const int EXAMPLES_PER_PAGE = EXAMPLES_PER_ROW * ROWS_PER_PAGE;
57 const float TOP_ROW_HEIGHT = 35.0f;
58 const float BOTTOM_ROW_HEIGHT = 35.0f;
59 const int BOTTOM_PADDING_HEIGHT = 40;
60 const int LOGO_BOTTOM_PADDING_HEIGHT = 30;
61 const Vector3 TABLE_RELATIVE_SIZE(0.9f, 1.0f, 0.8f ); ///< TableView's relative size to the entire stage.
62 const float STENCIL_RELATIVE_SIZE = 1.0f;
64 const float EFFECT_SNAP_DURATION = 0.66f; ///< Scroll Snap Duration for Effects
65 const float EFFECT_FLICK_DURATION = 0.5f; ///< Scroll Flick Duration for Effects
66 const Vector3 ANGLE_CUBE_PAGE_ROTATE(Math::PI * 0.5f, Math::PI * 0.5f, 0.0f);
68 const int NUM_BACKGROUND_IMAGES = 20;
69 const float BACKGROUND_SWIPE_SCALE = 0.025f;
70 const float BACKGROUND_SPREAD_SCALE = 1.5f;
71 const float SCALE_MOD = 1000.0f * Math::PI * 2.0f;
72 const float SCALE_SPEED = 10.0f;
73 const float SCALE_SPEED_SIN = 0.1f;
75 const unsigned int BACKGROUND_ANIMATION_DURATION = 15000; // 15 secs
77 const float BACKGROUND_Z = -1000.0f;
78 const float BACKGROUND_SIZE_SCALE = 2.0f;
79 const Vector4 BACKGROUND_COLOR( 1.0f, 1.0f, 1.0f, 1.0f );
82 const std::string DEFAULT_TEXT_STYLE_FONT_FAMILY("HelveticaNeue");
83 const std::string DEFAULT_TEXT_STYLE_FONT_STYLE("Regular");
84 const Dali::PointSize DEFAULT_TEXT_STYLE_POINT_SIZE( 8.0f );
85 const Dali::TextStyle::Weight DEFAULT_TEXT_STYLE_WEIGHT(Dali::TextStyle::REGULAR);
86 const Dali::Vector4 DEFAULT_TEXT_STYLE_COLOR(0.7f, 0.7f, 0.7f, 1.0f);
88 const std::string TABLE_TEXT_STYLE_FONT_FAMILY("HelveticaNeue");
89 const std::string TABLE_TEXT_STYLE_FONT_STYLE("Regular");
90 const Dali::PointSize TABLE_TEXT_STYLE_POINT_SIZE( 8.0f );
91 const Dali::TextStyle::Weight TABLE_TEXT_STYLE_WEIGHT(Dali::TextStyle::LIGHT);
92 const Dali::Vector4 TABLE_TEXT_STYLE_COLOR(0.0f, 0.0f, 0.0f, 1.0f);
95 TextStyle GetDefaultTextStyle()
98 textStyle.SetFontName(DEFAULT_TEXT_STYLE_FONT_FAMILY);
99 textStyle.SetFontStyle(DEFAULT_TEXT_STYLE_FONT_STYLE);
100 textStyle.SetFontPointSize( Dali::PointSize(DemoHelper::ScalePointSize(DEFAULT_TEXT_STYLE_POINT_SIZE)));
101 textStyle.SetWeight(DEFAULT_TEXT_STYLE_WEIGHT);
102 textStyle.SetTextColor(DEFAULT_TEXT_STYLE_COLOR);
103 textStyle.SetShadow( true );
107 TextStyle GetTableTextStyle()
110 textStyle.SetFontName(TABLE_TEXT_STYLE_FONT_FAMILY);
111 textStyle.SetFontStyle(TABLE_TEXT_STYLE_FONT_STYLE);
112 textStyle.SetFontPointSize( Dali::PointSize(DemoHelper::ScalePointSize(TABLE_TEXT_STYLE_POINT_SIZE)));
113 textStyle.SetWeight(TABLE_TEXT_STYLE_WEIGHT);
114 textStyle.SetTextColor(TABLE_TEXT_STYLE_COLOR);
119 * Creates the background image
121 ImageActor CreateBackground( std::string imagePath )
123 Image image = Image::New( imagePath );
124 ImageActor background = ImageActor::New( image );
126 background.SetAnchorPoint( AnchorPoint::CENTER );
127 background.SetParentOrigin( ParentOrigin::CENTER );
128 background.SetZ( -1.0f );
133 // These values depend on the tile image
134 const float IMAGE_BORDER_LEFT = 11.0f;
135 const float IMAGE_BORDER_RIGHT = IMAGE_BORDER_LEFT;
136 const float IMAGE_BORDER_TOP = IMAGE_BORDER_LEFT;
137 const float IMAGE_BORDER_BOTTOM = IMAGE_BORDER_LEFT;
140 * TableViewVisibilityConstraint
142 struct TableViewVisibilityConstraint
144 bool operator()( const bool& current,
145 const PropertyInput& pagePositionProperty,
146 const PropertyInput& pageSizeProperty )
148 // Only the tableview in the current page should be visible.
149 const Vector3& pagePosition = pagePositionProperty.GetVector3();
150 const Vector3& pageSize = pageSizeProperty.GetVector3();
151 return fabsf( pagePosition.x ) < pageSize.x;
156 * Constraint to wrap an actor in y that is moving vertically
158 Vector3 ShapeMovementConstraint( const Vector3& current,
159 const PropertyInput& shapeSizeProperty,
160 const PropertyInput& parentSizeProperty )
162 const Vector3& shapeSize = shapeSizeProperty.GetVector3();
163 const Vector3& parentSize = parentSizeProperty.GetVector3();
165 Vector3 pos( current );
166 if( pos.y + shapeSize.y * 0.5f < -parentSize.y * 0.5f )
168 pos.y += parentSize.y + shapeSize.y;
175 * Constraint to return a bool value based on the alpha channel value
177 bool AlphaVisibleConstraint( bool current, const PropertyInput& alphaProperty )
179 Vector4 colour = alphaProperty.GetVector4();
180 return ( colour.a > Math::MACHINE_EPSILON_10000 );
184 * Constraint to return a position for the background based on the scroll value
186 struct AnimScrollConstraint
190 AnimScrollConstraint( const Vector3& initialPos, float scale )
191 : mInitialPos( initialPos ),
197 Vector3 operator()( const Vector3& current, const PropertyInput& scrollProperty )
199 float scrollPos = scrollProperty.GetVector3().x;
201 return mInitialPos + Vector3( -scrollPos * mScale, 0.0f, 0.0f );
210 * Constraint to return a tracked world position added to the constant local position
212 struct TranslateLocalConstraint
216 TranslateLocalConstraint( const Vector3& localPos )
217 : mLocalPos( localPos )
221 Vector3 operator()( const Vector3& current, const PropertyInput& pagePosProperty )
223 Vector3 worldPos = pagePosProperty.GetVector3();
225 return ( worldPos + mLocalPos );
233 bool CompareByTitle( const Example& lhs, const Example& rhs )
235 return lhs.title < rhs.title;
240 DaliTableView::DaliTableView( Application& application )
241 : mApplication( application ),
243 mBackgroundImagePath( DEFAULT_BACKGROUND_IMAGE_PATH ),
244 mSortAlphabetically( false ),
245 mBackgroundAnimsPlaying( false )
247 application.InitSignal().Connect( this, &DaliTableView::Initialize );
250 DaliTableView::~DaliTableView()
254 void DaliTableView::AddExample( Example example )
256 mExampleList.push_back( example );
257 mExampleMap[ example.name ] = example;
260 void DaliTableView::SetBackgroundPath( std::string imagePath )
262 mBackgroundImagePath = imagePath;
265 void DaliTableView::SortAlphabetically( bool sortAlphabetically )
267 mSortAlphabetically = sortAlphabetically;
270 void DaliTableView::Initialize( Application& application )
272 Stage::GetCurrent().KeyEventSignal().Connect( this, &DaliTableView::OnKeyEvent );
274 Vector2 stageSize = Stage::GetCurrent().GetSize();
277 mBackground = CreateBackground( mBackgroundImagePath );
278 // set same size as parent actor
279 mBackground.SetSize( stageSize );
280 Stage::GetCurrent().Add( mBackground );
282 // Render entire content as overlays, as is all on same 2D plane.
283 mRootActor = TableView::New( 4, 1 );
284 mRootActor.SetAnchorPoint( AnchorPoint::CENTER );
285 mRootActor.SetParentOrigin( ParentOrigin::CENTER );
286 mRootActor.SetFixedHeight( 3, BOTTOM_PADDING_HEIGHT );
287 Stage::GetCurrent().Add( mRootActor );
290 Dali::Toolkit::ToolBar toolbar;
291 Dali::Layer toolBarLayer = DemoHelper::CreateToolbar(toolbar,
292 DEFAULT_TOOLBAR_IMAGE_PATH,
293 DEFAULT_TOOLBAR_TEXT,
294 DemoHelper::DEFAULT_VIEW_STYLE,
295 DemoHelper::GetDefaultTextStyle());
297 mRootActor.AddChild( toolBarLayer, TableView::CellPosition( 0, 0 ) );
298 mRootActor.SetFixedHeight( 0, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight );
301 mLogo = CreateLogo( LOGO_PATH );
302 Alignment alignment = Alignment::New();
303 alignment.Add(mLogo);
304 mRootActor.AddChild( alignment, TableView::CellPosition( 1, 0 ) );
306 // scrollview occupying the majority of the screen
307 mScrollView = ScrollView::New();
309 mScrollView.SetAnchorPoint( AnchorPoint::CENTER );
310 mScrollView.SetParentOrigin( ParentOrigin::CENTER );
311 mScrollView.ApplyConstraint( Dali::Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ), Dali::RelativeToConstraint( TABLE_RELATIVE_SIZE ) ) );
312 mScrollView.SetAxisAutoLock( true );
313 mScrollView.ScrollCompletedSignal().Connect( this, &DaliTableView::OnScrollComplete );
314 mScrollView.ScrollStartedSignal().Connect( this, &DaliTableView::OnScrollStart );
315 mScrollView.TouchedSignal().Connect( this, &DaliTableView::OnScrollTouched );
317 mScrollViewLayer = Layer::New();
318 mScrollViewLayer.SetAnchorPoint( AnchorPoint::CENTER );
319 mScrollViewLayer.SetParentOrigin( ParentOrigin::CENTER );
320 mScrollViewLayer.SetSize( stageSize );
321 mScrollViewLayer.Add( mScrollView );
322 mRootActor.AddChild( mScrollViewLayer, TableView::CellPosition( 2, 0 ) );
324 // Setup the scenegraph
325 // 1) Add scroll view effect and setup constraints on pages
326 ApplyScrollViewEffect();
328 // 2) Add pages and tiles
331 // 3) Populate scrollview with background so constraints on background layers can work with scrollview
332 SetupBackground( mScrollView, stageSize );
334 // 4) Remove constraints for inner cube effect
335 for( TableViewListIter pageIter = mTableViewList.begin(); pageIter != mTableViewList.end(); ++pageIter )
337 TableView page = *pageIter;
339 unsigned int numChildren = page.GetChildCount();
340 Actor pageActor = page;
341 for( unsigned int i=0; i<numChildren; ++i)
343 // Remove old effect's manual constraints.
344 Actor child = pageActor.GetChildAt(i);
347 child.RemoveConstraints();
352 // Set initial orientation
353 unsigned int degrees = application.GetOrientation().GetDegrees();
356 Dali::Window winHandle = application.GetWindow();
357 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
358 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE );
359 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
360 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
362 Dali::Orientation orientation = winHandle.GetOrientation();
363 orientation.ChangedSignal().Connect( this, &DaliTableView::OrientationChanged );
365 winHandle.ShowIndicator( Dali::Window::INVISIBLE );
368 mAnimationTimer = Timer::New( BACKGROUND_ANIMATION_DURATION );
369 mAnimationTimer.TickSignal().Connect( this, &DaliTableView::PauseBackgroundAnimation );
370 mAnimationTimer.Start();
371 mBackgroundAnimsPlaying = true;
373 KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &DaliTableView::OnKeyboardPreFocusChange );
374 KeyboardFocusManager::Get().FocusedActorActivatedSignal().Connect( this, &DaliTableView::OnFocusedActorActivated );
377 void DaliTableView::Populate()
379 const Vector2 stageSize = Stage::GetCurrent().GetSize();
381 const Size demoTileSize( 0.25f * stageSize.width, 0.25f * stageSize.height );
383 mTotalPages = ( mExampleList.size() + EXAMPLES_PER_PAGE - 1 ) / EXAMPLES_PER_PAGE;
385 // Populate ScrollView.
386 if( mExampleList.size() > 0 )
388 if( mSortAlphabetically )
390 sort( mExampleList.begin(), mExampleList.end(), CompareByTitle );
393 unsigned int exampleCount = 0;
394 ExampleListConstIter iter = mExampleList.begin();
395 for( int t = 0; t < mTotalPages; t++ )
397 // Create Table. (contains up to 9 Examples)
398 TableView tableView = TableView::New( 4, 3 );
399 // Add tableView to container.
400 mScrollView.Add( tableView );
401 ApplyEffectToPage( tableView, TABLE_RELATIVE_SIZE );
403 tableView.SetAnchorPoint( AnchorPoint::CENTER );
404 tableView.SetParentOrigin( ParentOrigin::CENTER );
405 // 2 pixels of padding
406 tableView.SetCellPadding( Size( 2.0f, 2.0f ) );
408 Constraint constraint = Constraint::New<Vector3>( Actor::SCALE,
409 LocalSource( Actor::SIZE ),
410 ParentSource( Actor::SIZE ),
411 ScaleToFitConstraint() );
412 tableView.ApplyConstraint(constraint);
414 // Apply visibility constraint to table view
415 Constraint visibleConstraint = Constraint::New< bool >( Actor::VISIBLE,
416 LocalSource( Actor::POSITION ),
417 ParentSource( Actor::SIZE ),
418 TableViewVisibilityConstraint() );
419 visibleConstraint.SetRemoveAction( Constraint::Discard );
420 tableView.ApplyConstraint( visibleConstraint );
422 // add cells to table
423 for( int y = 0; y < ROWS_PER_PAGE; y++ )
425 for( int x = 0; x < EXAMPLES_PER_ROW; x++ )
427 const Example& example = ( *iter );
429 Actor tile = CreateTile( example.name, example.title, demoTileSize, true );
430 FocusManager focusManager = FocusManager::Get();
431 focusManager.SetFocusOrder( tile, ++exampleCount );
432 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_LABEL,
434 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_TRAIT, "Tile" );
435 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_HINT,
436 "You can run this example" );
438 tableView.AddChild( tile, TableView::CellPosition( y, x ) );
441 if( iter == mExampleList.end() )
446 if( iter == mExampleList.end() )
453 tableView.SetFixedHeight( 3, BOTTOM_ROW_HEIGHT );
455 std::stringstream out;
456 out << ( t + 1 ) << " of " << mTotalPages;
457 Actor pageNumberText = CreateTile( "", out.str(), Size( 0.8f * stageSize.width, BOTTOM_ROW_HEIGHT ), false );
459 pageNumberText.ApplyConstraint( Constraint::New< Vector3 >( Actor::POSITION, Source( tableView, Actor::WORLD_POSITION),
460 TranslateLocalConstraint( Vector3( 0.0f, stageSize.y * 0.4f, 0.0f ) ) ) );
461 pageNumberText.ApplyConstraint( Constraint::New< Quaternion >( Actor::ROTATION, Source( tableView, Actor::WORLD_ROTATION ), EqualToConstraint() ) );
462 pageNumberText.ApplyConstraint( Constraint::New< Vector4 >( Actor::COLOR, Source( tableView, Actor::COLOR ), EqualToConstraint() ) );
464 //Stage::GetCurrent().Add( pageNumberText );
466 // Set tableview position
467 Vector3 tableViewPos( stageSize.x * TABLE_RELATIVE_SIZE.x * t, 0.0f, 0.0f );
468 tableView.SetPosition( tableViewPos );
470 mTableViewList.push_back( tableView );
472 if( iter == mExampleList.end() )
479 // Update Ruler info.
480 mScrollRulerX = new FixedRuler( stageSize.width * TABLE_RELATIVE_SIZE.x );
481 mScrollRulerY = new DefaultRuler();
482 mScrollRulerX->SetDomain( RulerDomain( 0.0f, mTotalPages * stageSize.width * TABLE_RELATIVE_SIZE.x, true ) );
483 mScrollRulerY->Disable();
484 mScrollView.SetRulerX( mScrollRulerX );
485 mScrollView.SetRulerY( mScrollRulerY );
488 void DaliTableView::OrientationChanged( Orientation orientation )
490 // TODO: Implement if orientation change required
493 void DaliTableView::Rotate( unsigned int degrees )
495 // Resize the root actor
496 Vector2 stageSize = Stage::GetCurrent().GetSize();
497 Vector3 targetSize( stageSize.x, stageSize.y, 1.0f );
499 if( degrees == 90 || degrees == 270 )
501 targetSize = Vector3( stageSize.y, stageSize.x, 1.0f );
504 if( mRotateAnimation )
506 mRotateAnimation.Stop();
507 mRotateAnimation.Clear();
510 mRotateAnimation = Animation::New( ROTATE_ANIMATION_TIME );
511 mRotateAnimation.RotateTo( mRootActor, Degree( 360 - degrees ), Vector3::ZAXIS, AlphaFunctions::EaseOut );
512 mRotateAnimation.Resize( mRootActor, targetSize, AlphaFunctions::EaseOut );
513 mRotateAnimation.Play();
516 Actor DaliTableView::CreateTile( const string& name, const string& title, const Size& parentSize, bool addBackground )
518 Actor tile = Actor::New();
519 tile.SetName( name );
520 tile.SetAnchorPoint( AnchorPoint::CENTER );
521 tile.SetParentOrigin( ParentOrigin::CENTER );
523 // make the tile 100% of parent
524 tile.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
526 Actor content = Actor::New();
527 content.SetAnchorPoint( AnchorPoint::CENTER );
528 content.SetParentOrigin( ParentOrigin::CENTER );
529 content.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
532 // create background image
535 Image bg = Image::New( TILE_BACKGROUND );
536 ImageActor image = ImageActor::New( bg );
537 image.SetAnchorPoint( AnchorPoint::CENTER );
538 image.SetParentOrigin( ParentOrigin::CENTER );
539 // make the image 100% of tile
540 image.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
541 // move image back to get text appear in front
543 image.SetStyle( ImageActor::STYLE_NINE_PATCH );
544 image.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
546 content.Add( image );
549 ImageActor stencil = NewStencilImage();
550 stencil.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
551 image.Add( stencil );
554 TextView text = TextView::New( title );
555 text.SetAnchorPoint( AnchorPoint::CENTER );
556 text.SetParentOrigin( ParentOrigin::CENTER );
557 text.SetWidthExceedPolicy( Toolkit::TextView::ShrinkToFit );
558 text.SetMultilinePolicy( Toolkit::TextView::SplitByWord );
559 text.SetLineJustification( Toolkit::TextView::Center );
560 text.SetTextAlignment( Toolkit::Alignment::Type( Alignment::HorizontalCenter | Alignment::VerticalCenter ) );
561 text.SetColor( Color::WHITE );
563 // make the text 90% of tile
564 text.SetSize( 0.9f * parentSize.width, 0.9f * parentSize.height );
565 text.SetStyleToCurrentText( GetTableTextStyle() );
566 text.SetSnapshotModeEnabled( false );
569 // Set the tile to be keyboard focusable
570 tile.SetKeyboardFocusable(true);
572 // connect to the touch events
573 tile.TouchedSignal().Connect( this, &DaliTableView::OnTilePressed );
578 ImageActor DaliTableView::NewStencilImage()
580 Image alpha = Image::New( TILE_BACKGROUND_ALPHA );
582 ImageActor stencilActor = ImageActor::New( alpha );
583 stencilActor.SetStyle( ImageActor::STYLE_NINE_PATCH );
584 stencilActor.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
586 stencilActor.SetParentOrigin( ParentOrigin::CENTER );
587 stencilActor.SetAnchorPoint( AnchorPoint::CENTER );
588 stencilActor.SetDrawMode( DrawMode::STENCIL );
590 Dali::ShaderEffect shaderEffect = AlphaDiscardEffect::New();
591 stencilActor.SetShaderEffect( shaderEffect );
596 bool DaliTableView::OnTilePressed( Actor actor, const TouchEvent& event )
598 bool consumed = false;
600 const TouchPoint& point = event.GetPoint( 0 );
601 if( TouchPoint::Down == point.state )
603 mPressedActor = actor;
607 // A button press is only valid if the Down & Up events
608 // both occurred within the button.
609 if( ( TouchPoint::Up == point.state ) &&
610 ( mPressedActor == actor ) )
612 std::string name = actor.GetName();
613 ExampleMapConstIter iter = mExampleMap.find( name );
615 FocusManager focusManager = FocusManager::Get();
617 if( iter != mExampleMap.end() )
619 // ignore Example button presses when scrolling or button animating.
620 if( ( !mScrolling ) && ( !mPressedAnimation ) )
622 // do nothing, until pressed animation finished.
629 mPressedAnimation = Animation::New( BUTTON_PRESS_ANIMATION_TIME );
630 mPressedAnimation.SetDestroyAction( Animation::Discard );
632 // scale the content actor within the Tile, as to not affect the placement within the Table.
633 Actor content = actor.GetChildAt(0);
634 mPressedAnimation.ScaleTo( content, Vector3( 0.9f, 0.9f, 1.0f ), AlphaFunctions::EaseInOut, 0.0f,
635 BUTTON_PRESS_ANIMATION_TIME * 0.5f );
636 mPressedAnimation.ScaleTo( content, Vector3::ONE, AlphaFunctions::EaseInOut, BUTTON_PRESS_ANIMATION_TIME * 0.5f,
637 BUTTON_PRESS_ANIMATION_TIME * 0.5f );
638 mPressedAnimation.Play();
639 mPressedAnimation.FinishedSignal().Connect( this, &DaliTableView::OnPressedAnimationFinished );
645 void DaliTableView::OnPressedAnimationFinished( Dali::Animation& source )
647 mPressedAnimation.Reset();
650 std::string name = mPressedActor.GetName();
651 ExampleMapConstIter iter = mExampleMap.find( name );
653 if( iter == mExampleMap.end() )
655 if( name == BUTTON_QUIT )
657 // Move focus to the OK button
658 FocusManager focusManager = FocusManager::Get();
660 // Enable the group mode and wrap mode
661 focusManager.SetGroupMode( true );
662 focusManager.SetWrapMode( true );
667 const Example& example( iter->second );
669 std::stringstream stream;
670 stream << DALI_EXAMPLE_BIN << example.name.c_str();
674 execlp( stream.str().c_str(), example.name.c_str(), NULL );
675 DALI_ASSERT_ALWAYS(false && "exec failed!");
678 mPressedActor.Reset();
682 void DaliTableView::OnScrollStart( const Dali::Vector3& position )
689 void DaliTableView::OnScrollComplete( const Dali::Vector3& position )
693 // move focus to 1st item of new page
694 FocusManager focusManager = FocusManager::Get();
695 focusManager.SetCurrentFocusActor(mTableViewList[mScrollView.GetCurrentPage()].GetChildAt(TableView::CellPosition(1, 0)) );
699 bool DaliTableView::OnScrollTouched( Actor actor, const TouchEvent& event )
701 const TouchPoint& point = event.GetPoint( 0 );
702 if( TouchPoint::Down == point.state )
704 mPressedActor = actor;
710 void DaliTableView::ApplyScrollViewEffect()
712 // Remove old effect if exists.
714 if( mScrollViewEffect )
716 mScrollView.RemoveEffect( mScrollViewEffect );
719 // Just one effect for now
720 SetupInnerPageCubeEffect();
722 mScrollView.ApplyEffect( mScrollViewEffect );
725 void DaliTableView::SetupInnerPageCubeEffect()
727 ScrollViewCustomEffect customEffect;
728 mScrollViewEffect = customEffect = ScrollViewCustomEffect::New();
729 mScrollView.SetScrollSnapDuration( EFFECT_SNAP_DURATION );
730 mScrollView.SetScrollFlickDuration( EFFECT_FLICK_DURATION );
731 mScrollView.SetScrollSnapAlphaFunction( AlphaFunctions::EaseOutBack );
732 mScrollView.SetScrollFlickAlphaFunction( AlphaFunctions::EaseOutBack );
733 mScrollView.RemoveConstraintsFromChildren();
735 customEffect.SetPageSpacing( Vector2( 30.0f, 30.0f ) );
736 customEffect.SetAngledOriginPageRotation( ANGLE_CUBE_PAGE_ROTATE );
737 customEffect.SetSwingAngle( ANGLE_CUBE_PAGE_ROTATE.x, Vector3( 0, -1, 0 ) );
738 customEffect.SetOpacityThreshold( 0.5f ); // Make fade out on edges
741 void DaliTableView::ApplyEffectToPage( Actor page, const Vector3& tableRelativeSize )
743 page.RemoveConstraints();
745 Constraint constraint = Constraint::New<Vector3>( Actor::SCALE,
746 LocalSource( Actor::SIZE ),
747 ParentSource( Actor::SIZE ),
748 ScaleToFitConstraint() );
749 page.ApplyConstraint(constraint);
751 ApplyCustomEffectToPage( page );
754 void DaliTableView::ApplyCustomEffectToPage( Actor page )
756 ScrollViewCustomEffect customEffect = ScrollViewCustomEffect::DownCast( mScrollViewEffect );
757 Vector2 vStageSize( Stage::GetCurrent().GetSize() );
758 customEffect.ApplyToPage( page, Vector3( vStageSize.x, vStageSize.y, 1.0f ) );
761 void DaliTableView::OnKeyEvent( const KeyEvent& event )
763 if( event.state == KeyEvent::Down )
765 if ( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
772 Actor CreateBackgroundActor( const Vector2& size )
774 Actor layer = Actor::New();
775 layer.SetAnchorPoint( AnchorPoint::CENTER );
776 layer.SetParentOrigin( ParentOrigin::CENTER );
777 layer.SetSize( size );
781 void DaliTableView::SetupBackground( Actor addToLayer, const Vector2& size )
783 // Create distance field shape
784 BitmapImage distanceField;
785 Size imageSize( 512, 512 );
786 CreateShapeImage( CIRCLE, imageSize, distanceField );
789 Actor backgroundAnimLayer0 = CreateBackgroundActor( size );
790 Actor backgroundAnimLayer1 = CreateBackgroundActor( size );
791 Actor backgroundAnimLayer2 = CreateBackgroundActor( size );
794 Constraint animConstraint0 = Constraint::New < Vector3 > ( Actor::POSITION,
795 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
796 AnimScrollConstraint( backgroundAnimLayer0.GetCurrentPosition(), 0.75f ) );
797 backgroundAnimLayer0.ApplyConstraint( animConstraint0 );
799 Constraint animConstraint1 = Constraint::New < Vector3 > ( Actor::POSITION,
800 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
801 AnimScrollConstraint( backgroundAnimLayer1.GetCurrentPosition(), 0.5f ) );
802 backgroundAnimLayer1.ApplyConstraint( animConstraint1 );
804 Constraint animConstraint2 = Constraint::New < Vector3 > ( Actor::POSITION,
805 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
806 AnimScrollConstraint( backgroundAnimLayer2.GetCurrentPosition(), 0.25f ) );
807 backgroundAnimLayer2.ApplyConstraint( animConstraint2 );
810 ImageActor layer = Dali::Toolkit::CreateSolidColorActor( BACKGROUND_COLOR );
811 layer.SetAnchorPoint( AnchorPoint::CENTER );
812 layer.SetParentOrigin( ParentOrigin::CENTER );
813 layer.SetSize( size * BACKGROUND_SIZE_SCALE );
814 layer.SetZ( BACKGROUND_Z );
815 layer.SetPositionInheritanceMode( DONT_INHERIT_POSITION );
817 addToLayer.Add( layer );
820 addToLayer.Add( backgroundAnimLayer0 );
821 addToLayer.Add( backgroundAnimLayer1 );
822 addToLayer.Add( backgroundAnimLayer2 );
824 // Add all the children
825 AddBackgroundActors( backgroundAnimLayer0, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
826 AddBackgroundActors( backgroundAnimLayer1, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
827 AddBackgroundActors( backgroundAnimLayer2, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
830 void DaliTableView::AddBackgroundActors( Actor layer, int count, BitmapImage distanceField, const Dali::Vector2& size )
832 for( int i = 0; i < count; ++i )
834 float randSize = Random::Range( 10.0f, 400.0f );
835 float hue = Random::Range( 0.3f, 1.0f );
836 Vector4 randColour( hue, hue*0.5, 0.0f, Random::Range( 0.3f, 0.6f ));
838 ImageActor dfActor = ImageActor::New( distanceField );
839 mBackgroundActors.push_back( dfActor );
840 dfActor.SetSize( Vector2( randSize, randSize ) );
841 dfActor.SetParentOrigin( ParentOrigin::CENTER );
843 Toolkit::DistanceFieldEffect effect = Toolkit::DistanceFieldEffect::New();
844 dfActor.SetShaderEffect( effect );
845 dfActor.SetColor( randColour );
846 effect.SetOutlineParams( Vector2( 0.55f, 0.00f ) );
847 effect.SetSmoothingEdge( 0.5f );
848 layer.Add( dfActor );
852 Random::Range( -size.x * 0.5f * BACKGROUND_SPREAD_SCALE, size.x * 0.5f * BACKGROUND_SPREAD_SCALE ),
853 Random::Range( -size.y * 0.5f - randSize, size.y * 0.5f + randSize ),
854 Random::Range(-1.0f, 0.0f) );
855 dfActor.SetPosition( actorPos );
857 Constraint movementConstraint = Constraint::New < Vector3 > ( Actor::POSITION,
858 LocalSource( Actor::SIZE ),
859 ParentSource( Actor::SIZE ),
860 ShapeMovementConstraint );
861 dfActor.ApplyConstraint( movementConstraint );
864 Animation animation = Animation::New( Random::Range( 40.0f, 200.0f ) );
865 KeyFrames keyframes = KeyFrames::New();
866 keyframes.Add( 0.0f, actorPos );
867 Vector3 toPos( actorPos );
868 toPos.y -= ( size.y + randSize );
869 keyframes.Add( 1.0f, toPos );
870 animation.AnimateBetween( Property( dfActor, Actor::POSITION ), keyframes );
871 animation.SetLooping( true );
873 mBackgroundAnimations.push_back( animation );
877 void DaliTableView::CreateShapeImage( ShapeType shapeType, const Size& size, BitmapImage& distanceFieldOut )
879 // this bitmap will hold the alpha map for the distance field shader
880 distanceFieldOut = BitmapImage::New( size.width, size.height, Pixel::A8 );
882 // Generate bit pattern
883 std::vector< unsigned char > imageDataA8;
884 imageDataA8.reserve( size.width * size.height ); // A8
889 GenerateCircle( size, imageDataA8 );
892 GenerateSquare( size, imageDataA8 );
898 GenerateDistanceFieldMap( &imageDataA8[ 0 ], size, distanceFieldOut.GetBuffer(), size, 8.0f, size );
899 distanceFieldOut.Update();
902 void DaliTableView::GenerateSquare( const Size& size, std::vector< unsigned char >& distanceFieldOut )
904 for( int h = 0; h < size.height; ++h )
906 for( int w = 0; w < size.width; ++w )
908 distanceFieldOut.push_back( 0xFF );
913 void DaliTableView::GenerateCircle( const Size& size, std::vector< unsigned char >& distanceFieldOut )
915 const float radius = size.width * 0.5f * size.width * 0.5f;
916 Vector2 center( size.width / 2, size.height / 2 );
918 for( int h = 0; h < size.height; ++h )
920 for( int w = 0; w < size.width; ++w )
923 Vector2 dist = pos - center;
925 if( dist.x * dist.x + dist.y * dist.y > radius )
927 distanceFieldOut.push_back( 0x00 );
931 distanceFieldOut.push_back( 0xFF );
937 ImageActor DaliTableView::CreateLogo( std::string imagePath )
939 Image image = Image::New( imagePath );
940 image.LoadingFinishedSignal().Connect( this, &DaliTableView::OnLogoLoaded );
942 ImageActor logo = ImageActor::New( image );
944 logo.SetAnchorPoint( AnchorPoint::CENTER );
945 logo.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
950 void DaliTableView::OnLogoLoaded( Dali::Image image )
952 mRootActor.SetFixedHeight( 1, image.GetHeight() + LOGO_BOTTOM_PADDING_HEIGHT );
955 bool DaliTableView::PauseBackgroundAnimation()
962 void DaliTableView::PauseAnimation()
964 if( mBackgroundAnimsPlaying )
966 for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
968 Animation anim = *animIter;
973 mBackgroundAnimsPlaying = false;
977 void DaliTableView::PlayAnimation()
979 if ( !mBackgroundAnimsPlaying )
981 for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
983 Animation anim = *animIter;
988 mBackgroundAnimsPlaying = true;
991 mAnimationTimer.SetInterval( BACKGROUND_ANIMATION_DURATION );
994 Dali::Actor DaliTableView::OnKeyboardPreFocusChange( Dali::Actor current, Dali::Actor proposed, Dali::Toolkit::Control::KeyboardFocusNavigationDirection direction )
996 Actor nextFocusActor = proposed;
998 if ( !current && !proposed )
1000 // Set the initial focus to the first tile in the current page should be focused.
1001 nextFocusActor = mTableViewList[mScrollView.GetCurrentPage()].GetChildAt(TableView::CellPosition(0, 0));
1003 else if( !proposed || (proposed && proposed == mScrollViewLayer) )
1005 // ScrollView is being focused but nothing in the current page can be focused further
1006 // in the given direction. We should work out which page to scroll to next.
1007 int currentPage = mScrollView.GetCurrentPage();
1008 int newPage = currentPage;
1009 if( direction == Dali::Toolkit::Control::Left )
1013 else if( direction == Dali::Toolkit::Control::Right )
1018 newPage = std::max(0, std::min(static_cast<int>(mScrollRulerX->GetTotalPages() - 1), newPage));
1019 if( newPage == currentPage )
1021 if( direction == Dali::Toolkit::Control::Left )
1023 newPage = mScrollRulerX->GetTotalPages() - 1;
1024 } else if( direction == Dali::Toolkit::Control::Right )
1030 // Scroll to the page in the given direction
1031 mScrollView.ScrollTo(newPage);
1033 if( direction == Dali::Toolkit::Control::Left )
1035 // Work out the cell position for the last tile
1036 int remainingExamples = mExampleList.size() - newPage * EXAMPLES_PER_PAGE;
1037 int rowPos = (remainingExamples >= EXAMPLES_PER_PAGE) ? ROWS_PER_PAGE - 1 : ( (remainingExamples % EXAMPLES_PER_PAGE + EXAMPLES_PER_ROW) / EXAMPLES_PER_ROW - 1 );
1038 int colPos = remainingExamples >= EXAMPLES_PER_PAGE ? EXAMPLES_PER_ROW - 1 : ( remainingExamples % EXAMPLES_PER_PAGE - rowPos * EXAMPLES_PER_ROW - 1 );
1040 // Move the focus to the last tile in the new page.
1041 nextFocusActor = mTableViewList[newPage].GetChildAt(TableView::CellPosition(rowPos, colPos));
1045 // Move the focus to the first tile in the new page.
1046 nextFocusActor = mTableViewList[newPage].GetChildAt(TableView::CellPosition(0, 0));
1050 return nextFocusActor;
1053 void DaliTableView::OnFocusedActorActivated( Dali::Actor activatedActor )
1057 mPressedActor = activatedActor;
1059 // Activate the current focused actor;
1060 TouchEvent touchEventUp;
1061 touchEventUp.points.push_back( TouchPoint ( 0, TouchPoint::Up, 0.0f, 0.0f ) );
1062 OnTilePressed(mPressedActor, touchEventUp);