2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
18 #include "dali-table-view.h"
26 using namespace Dali::Toolkit;
29 ///////////////////////////////////////////////////////////////////////////////
34 const std::string BUTTON_BACKWARD( "Backward" );
35 const std::string BUTTON_FORWARD( "Forward" );
36 const std::string BUTTON_QUIT( "Quit" );
37 const std::string BUTTON_OK( "Ok" );
38 const std::string BUTTON_CANCEL( "Cancel" );
40 const std::string DEFAULT_BACKGROUND_IMAGE_PATH( DALI_IMAGE_DIR "background-default.png" );
41 const std::string LOGO_PATH( DALI_IMAGE_DIR "dali-logo-small.png" );
42 const std::string DEFAULT_TOOLBAR_IMAGE_PATH( DALI_IMAGE_DIR "top-bar-demo.png" );
43 const std::string BUTTON_BACKGROUND(DALI_IMAGE_DIR "button-background.png");
44 const std::string TILE_BACKGROUND(DALI_IMAGE_DIR "item-background.png");
45 const std::string TILE_BACKGROUND_ALPHA(DALI_IMAGE_DIR "item-background-alpha.png");
47 const float BUTTON_PRESS_ANIMATION_TIME = 0.25f; ///< Time to perform button scale effect.
48 const float ROTATE_ANIMATION_TIME = 0.5f; ///< Time to perform rotate effect.
49 const int MAX_PAGES = 256; ///< Maximum pages (arbitrary safety limit)
50 const int EXAMPLES_PER_ROW = 3;
51 const int ROWS_PER_PAGE = 3;
52 const int EXAMPLES_PER_PAGE = EXAMPLES_PER_ROW * ROWS_PER_PAGE;
53 const float TOP_ROW_HEIGHT = 35.0f;
54 const float BOTTOM_ROW_HEIGHT = 35.0f;
55 const int BOTTOM_PADDING_HEIGHT = 40;
56 const int LOGO_BOTTOM_PADDING_HEIGHT = 30;
57 const Vector3 TABLE_RELATIVE_SIZE(0.9f, 1.0f, 0.8f ); ///< TableView's relative size to the entire stage.
58 const float STENCIL_RELATIVE_SIZE = 1.0f;
60 const float EFFECT_SNAP_DURATION = 0.66f; ///< Scroll Snap Duration for Effects
61 const float EFFECT_FLICK_DURATION = 0.5f; ///< Scroll Flick Duration for Effects
62 const Vector3 ANGLE_CUBE_PAGE_ROTATE(Math::PI * 0.5f, Math::PI * 0.5f, 0.0f);
64 const int NUM_BACKGROUND_IMAGES = 20;
65 const float BACKGROUND_SWIPE_SCALE = 0.025f;
66 const float BACKGROUND_SPREAD_SCALE = 1.5f;
67 const float SCALE_MOD = 1000.0f * Math::PI * 2.0f;
68 const float SCALE_SPEED = 10.0f;
69 const float SCALE_SPEED_SIN = 0.1f;
71 const unsigned int BACKGROUND_ANIMATION_DURATION = 15000; // 15 secs
73 const float BACKGROUND_Z = -1000.0f;
74 const float BACKGROUND_SIZE_SCALE = 2.0f;
75 const Vector4 BACKGROUND_COLOR( 1.0f, 1.0f, 1.0f, 1.0f );
78 const std::string DEFAULT_TEXT_STYLE_FONT_FAMILY("HelveticaNeue");
79 const std::string DEFAULT_TEXT_STYLE_FONT_STYLE("Regular");
80 const Dali::PointSize DEFAULT_TEXT_STYLE_POINT_SIZE( 8.0f );
81 const Dali::TextStyle::Weight DEFAULT_TEXT_STYLE_WEIGHT(Dali::TextStyle::LIGHT);
82 const Dali::Vector4 DEFAULT_TEXT_STYLE_COLOR(0.0f, 0.75f, 1.0f, 1.0f);
84 const std::string TABLE_TEXT_STYLE_FONT_FAMILY("HelveticaNeue");
85 const std::string TABLE_TEXT_STYLE_FONT_STYLE("Regular");
86 const Dali::PointSize TABLE_TEXT_STYLE_POINT_SIZE( 8.0f );
87 const Dali::TextStyle::Weight TABLE_TEXT_STYLE_WEIGHT(Dali::TextStyle::LIGHT);
88 const Dali::Vector4 TABLE_TEXT_STYLE_COLOR(0.0f, 0.0f, 0.0f, 1.0f);
90 TextStyle GetDefaultTextStyle()
93 textStyle.SetFontName(DEFAULT_TEXT_STYLE_FONT_FAMILY);
94 textStyle.SetFontStyle(DEFAULT_TEXT_STYLE_FONT_STYLE);
95 textStyle.SetFontPointSize(DEFAULT_TEXT_STYLE_POINT_SIZE);
96 textStyle.SetWeight(DEFAULT_TEXT_STYLE_WEIGHT);
97 textStyle.SetTextColor(DEFAULT_TEXT_STYLE_COLOR);
101 TextStyle GetTableTextStyle()
104 textStyle.SetFontName(TABLE_TEXT_STYLE_FONT_FAMILY);
105 textStyle.SetFontStyle(TABLE_TEXT_STYLE_FONT_STYLE);
106 textStyle.SetFontPointSize(TABLE_TEXT_STYLE_POINT_SIZE);
107 textStyle.SetWeight(TABLE_TEXT_STYLE_WEIGHT);
108 textStyle.SetTextColor(TABLE_TEXT_STYLE_COLOR);
113 * Creates the background image
115 ImageActor CreateBackground( std::string imagePath )
117 Image image = Image::New( imagePath );
118 ImageActor background = ImageActor::New( image );
120 background.SetAnchorPoint( AnchorPoint::CENTER );
121 background.SetParentOrigin( ParentOrigin::CENTER );
122 background.SetZ( -1.0f );
127 // These values depend on the tile image
128 const float IMAGE_BORDER_LEFT = 11.0f;
129 const float IMAGE_BORDER_RIGHT = IMAGE_BORDER_LEFT;
130 const float IMAGE_BORDER_TOP = IMAGE_BORDER_LEFT;
131 const float IMAGE_BORDER_BOTTOM = IMAGE_BORDER_LEFT;
134 * TableViewVisibilityConstraint
136 struct TableViewVisibilityConstraint
138 bool operator()( const bool& current,
139 const PropertyInput& pagePositionProperty,
140 const PropertyInput& pageSizeProperty )
142 // Only the tableview in the current page should be visible.
143 const Vector3& pagePosition = pagePositionProperty.GetVector3();
144 const Vector3& pageSize = pageSizeProperty.GetVector3();
145 return fabsf( pagePosition.x ) < pageSize.x;
150 * Constraint to wrap an actor in y that is moving vertically
152 Vector3 ShapeMovementConstraint( const Vector3& current,
153 const PropertyInput& shapeSizeProperty,
154 const PropertyInput& parentSizeProperty )
156 const Vector3& shapeSize = shapeSizeProperty.GetVector3();
157 const Vector3& parentSize = parentSizeProperty.GetVector3();
159 Vector3 pos( current );
160 if( pos.y + shapeSize.y * 0.5f < -parentSize.y * 0.5f )
162 pos.y += parentSize.y + shapeSize.y;
169 * Constraint to return a bool value based on the alpha channel value
171 bool AlphaVisibleConstraint( bool current, const PropertyInput& alphaProperty )
173 Vector4 colour = alphaProperty.GetVector4();
174 return ( colour.a > Math::MACHINE_EPSILON_10000 );
178 * Constraint to return a position for the background based on the scroll value
180 struct AnimScrollConstraint
184 AnimScrollConstraint( const Vector3& initialPos, float scale )
185 : mInitialPos( initialPos ),
191 Vector3 operator()( const Vector3& current, const PropertyInput& scrollProperty )
193 float scrollPos = scrollProperty.GetFloat();
195 return mInitialPos + Vector3( -scrollPos * mScale, 0.0f, 0.0f );
204 * Constraint to return a tracked world position added to the constant local position
206 struct TranslateLocalConstraint
210 TranslateLocalConstraint( const Vector3& localPos )
211 : mLocalPos( localPos )
215 Vector3 operator()( const Vector3& current, const PropertyInput& pagePosProperty )
217 Vector3 worldPos = pagePosProperty.GetVector3();
219 return ( worldPos + mLocalPos );
227 bool CompareByTitle( const Example& lhs, const Example& rhs )
229 return lhs.title < rhs.title;
234 DaliTableView::DaliTableView( Application& application )
235 : mApplication( application ),
237 mBackgroundImagePath( DEFAULT_BACKGROUND_IMAGE_PATH ),
238 mSortAlphabetically( false ),
239 mBackgroundAnimsPlaying( false )
241 application.InitSignal().Connect( this, &DaliTableView::Initialize );
244 DaliTableView::~DaliTableView()
248 void DaliTableView::AddExample( Example example )
250 mExampleList.push_back( example );
251 mExampleMap[ example.name ] = example;
254 void DaliTableView::SetBackgroundPath( std::string imagePath )
256 mBackgroundImagePath = imagePath;
259 void DaliTableView::SortAlphabetically( bool sortAlphabetically )
261 mSortAlphabetically = sortAlphabetically;
264 void DaliTableView::Initialize( Application& application )
266 Stage::GetCurrent().KeyEventSignal().Connect( this, &DaliTableView::OnKeyEvent );
268 Vector2 stageSize = Stage::GetCurrent().GetSize();
271 mBackground = CreateBackground( mBackgroundImagePath );
272 // set same size as parent actor
273 mBackground.SetSize( stageSize );
274 Stage::GetCurrent().Add( mBackground );
276 // Render entire content as overlays, as is all on same 2D plane.
277 mRootActor = TableView::New( 4, 1 );
278 mRootActor.SetAnchorPoint( AnchorPoint::CENTER );
279 mRootActor.SetParentOrigin( ParentOrigin::CENTER );
280 mRootActor.SetFixedHeight( 3, BOTTOM_PADDING_HEIGHT );
281 Stage::GetCurrent().Add( mRootActor );
284 CreateToolbar( mRootActor, std::string(), DEFAULT_TOOLBAR_IMAGE_PATH );
287 mLogo = CreateLogo( LOGO_PATH );
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 ), Dali::RelativeToConstraint( TABLE_RELATIVE_SIZE ) ) );
298 mScrollView.SetAxisAutoLock( true );
299 mScrollView.ScrollCompletedSignal().Connect( this, &DaliTableView::OnScrollComplete );
300 mScrollView.ScrollStartedSignal().Connect( this, &DaliTableView::OnScrollStart );
301 mScrollView.TouchedSignal().Connect( this, &DaliTableView::OnScrollTouched );
303 mScrollViewLayer = Layer::New();
304 mScrollViewLayer.SetAnchorPoint( AnchorPoint::CENTER );
305 mScrollViewLayer.SetParentOrigin( ParentOrigin::CENTER );
306 mScrollViewLayer.SetSize( stageSize );
307 mScrollViewLayer.Add( mScrollView );
308 mRootActor.AddChild( mScrollViewLayer, TableView::CellPosition( 2, 0 ) );
310 // Setup the scenegraph
311 // 1) Add scroll view effect and setup constraints on pages
312 ApplyScrollViewEffect();
314 // 2) Add pages and tiles
317 // 3) Populate scrollview with background so constraints on background layers can work with scrollview
318 SetupBackground( mScrollView, stageSize );
320 // 4) Remove constraints for inner cube effect
321 for( TableViewListIter pageIter = mTableViewList.begin(); pageIter != mTableViewList.end(); ++pageIter )
323 TableView page = *pageIter;
325 unsigned int numChildren = page.GetChildCount();
326 Actor pageActor = page;
327 for( unsigned int i=0; i<numChildren; ++i)
329 // Remove old effect's manual constraints.
330 Actor child = pageActor.GetChildAt(i);
333 child.RemoveConstraints();
338 // Set initial orientation
339 unsigned int degrees = application.GetOrientation().GetDegrees();
342 Dali::Window winHandle = application.GetWindow();
343 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
344 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE );
345 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
346 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
348 Dali::Orientation orientation = winHandle.GetOrientation();
349 orientation.ChangedSignal().Connect( this, &DaliTableView::OrientationChanged );
351 winHandle.ShowIndicator( false );
354 mAnimationTimer = Timer::New( BACKGROUND_ANIMATION_DURATION );
355 mAnimationTimer.TickSignal().Connect( this, &DaliTableView::PauseBackgroundAnimation );
356 mAnimationTimer.Start();
357 mBackgroundAnimsPlaying = true;
359 KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &DaliTableView::OnKeyboardPreFocusChange );
360 KeyboardFocusManager::Get().FocusedActorActivatedSignal().Connect( this, &DaliTableView::OnFocusedActorActivated );
363 void DaliTableView::CreateToolbar( TableView root, const std::string& title, const std::string& toolbarImagePath,
364 const ViewStyle& style )
366 // Create default ToolBar
367 Dali::Stage stage = Dali::Stage::GetCurrent();
368 Dali::Vector2 dpi = stage.GetDpi();
370 // Create toolbar layer.
371 Dali::Layer toolBarLayer = Dali::Layer::New();
372 toolBarLayer.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
373 toolBarLayer.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
374 toolBarLayer.ApplyConstraint( Dali::Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ), Dali::SourceWidthFixedHeight( style.mToolBarHeight * dpi.y / style.mDpi ) ) );
375 float toolBarLayerHeight = style.mToolBarHeight * dpi.y / style.mDpi;
376 toolBarLayer.SetSize( 0.0f, toolBarLayerHeight );
378 // Add tool bar layer to the view.
379 root.AddChild( toolBarLayer, TableView::CellPosition( 0, 0 ) );
380 root.SetFixedHeight( 0, toolBarLayerHeight );
382 // Raise tool bar layer to the top.
383 toolBarLayer.RaiseToTop();
386 Dali::Image image = Dali::Image::New( toolbarImagePath );
387 Dali::ImageActor toolBarBackground = Dali::ImageActor::New( image );
388 Dali::Toolkit::ToolBar toolBar = Dali::Toolkit::ToolBar::New();
389 toolBar.SetBackground( toolBarBackground );
390 toolBar.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
391 toolBar.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
392 toolBar.ApplyConstraint( Dali::Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ), Dali::EqualToConstraint() ) );
393 toolBar.SetSize( 0.0f, style.mToolBarHeight * dpi.y / style.mDpi );
394 toolBarBackground.SetZ( -1.0f );
396 // Add the tool bar to the too bar layer.
397 toolBarLayer.Add( toolBar );
399 Dali::Font font = Dali::Font::New();
404 Dali::Toolkit::TextView titleActor = Dali::Toolkit::TextView::New();
405 titleActor.SetName( "ToolbarTitle" );
406 titleActor.SetText( title );
407 titleActor.SetSize( font.MeasureText( title ) );
408 titleActor.SetStyleToCurrentText( GetDefaultTextStyle() );
409 titleActor.SetZ( 1.0f );
411 // Add title to the tool bar.
412 const float padding( style.mToolBarPadding * dpi.x / style.mDpi );
413 toolBar.AddControl( titleActor, style.mToolBarTitlePercentage, Dali::Toolkit::Alignment::HorizontalCenter,
414 Dali::Toolkit::Alignment::Padding( padding, padding, padding, padding ) );
418 void DaliTableView::Populate()
420 const Vector2 stageSize = Stage::GetCurrent().GetSize();
422 const Size demoTileSize( 0.25f * stageSize.width, 0.25f * stageSize.height );
424 mTotalPages = ( mExampleList.size() + EXAMPLES_PER_PAGE - 1 ) / EXAMPLES_PER_PAGE;
426 // Populate ScrollView.
427 if( mExampleList.size() > 0 )
429 if( mSortAlphabetically )
431 sort( mExampleList.begin(), mExampleList.end(), CompareByTitle );
434 unsigned int exampleCount = 0;
435 ExampleListConstIter iter = mExampleList.begin();
436 for( int t = 0; t < mTotalPages; t++ )
438 // Create Table. (contains up to 9 Examples)
439 TableView tableView = TableView::New( 4, 3 );
440 // Add tableView to container.
441 mScrollView.Add( tableView );
442 ApplyEffectToPage( tableView, TABLE_RELATIVE_SIZE );
444 tableView.SetAnchorPoint( AnchorPoint::CENTER );
445 tableView.SetParentOrigin( ParentOrigin::CENTER );
446 // 2 pixels of padding
447 tableView.SetCellPadding( Size( 2.0f, 2.0f ) );
449 Constraint constraint = Constraint::New<Vector3>( Actor::SCALE,
450 LocalSource( Actor::SIZE ),
451 ParentSource( Actor::SIZE ),
452 ScaleToFitConstraint() );
453 tableView.ApplyConstraint(constraint);
455 // Apply visibility constraint to table view
456 Constraint visibleConstraint = Constraint::New< bool >( Actor::VISIBLE,
457 LocalSource( Actor::POSITION ),
458 ParentSource( Actor::SIZE ),
459 TableViewVisibilityConstraint() );
460 visibleConstraint.SetRemoveAction( Constraint::Discard );
461 tableView.ApplyConstraint( visibleConstraint );
463 // add cells to table
464 for( int y = 0; y < ROWS_PER_PAGE; y++ )
466 for( int x = 0; x < EXAMPLES_PER_ROW; x++ )
468 const Example& example = ( *iter );
470 Actor tile = CreateTile( example.name, example.title, demoTileSize, true );
471 FocusManager focusManager = FocusManager::Get();
472 focusManager.SetFocusOrder( tile, ++exampleCount );
473 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_LABEL,
475 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_TRAIT, "Tile" );
476 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_HINT,
477 "You can run this example" );
479 tableView.AddChild( tile, TableView::CellPosition( y, x ) );
482 if( iter == mExampleList.end() )
487 if( iter == mExampleList.end() )
494 tableView.SetFixedHeight( 3, BOTTOM_ROW_HEIGHT );
496 std::stringstream out;
497 out << ( t + 1 ) << " of " << mTotalPages;
498 Actor pageNumberText = CreateTile( "", out.str(), Size( 0.8f * stageSize.width, BOTTOM_ROW_HEIGHT ), false );
500 pageNumberText.ApplyConstraint( Constraint::New< Vector3 >( Actor::POSITION, Source( tableView, Actor::WORLD_POSITION),
501 TranslateLocalConstraint( Vector3( 0.0f, stageSize.y * 0.4f, 0.0f ) ) ) );
502 pageNumberText.ApplyConstraint( Constraint::New< Quaternion >( Actor::ROTATION, Source( tableView, Actor::WORLD_ROTATION ), EqualToConstraint() ) );
503 pageNumberText.ApplyConstraint( Constraint::New< Vector4 >( Actor::COLOR, Source( tableView, Actor::COLOR ), EqualToConstraint() ) );
505 Stage::GetCurrent().Add( pageNumberText );
507 // Set tableview position
508 Vector3 tableViewPos( stageSize.x * TABLE_RELATIVE_SIZE.x * t, 0.0f, 0.0f );
509 tableView.SetPosition( tableViewPos );
511 mTableViewList.push_back( tableView );
513 if( iter == mExampleList.end() )
520 // Update Ruler info.
521 mScrollRulerX = new FixedRuler( stageSize.width * TABLE_RELATIVE_SIZE.x );
522 mScrollRulerY = new DefaultRuler();
523 mScrollRulerX->SetDomain( RulerDomain( 0.0f, mTotalPages * stageSize.width * TABLE_RELATIVE_SIZE.x, true ) );
524 mScrollRulerY->Disable();
525 mScrollView.SetRulerX( mScrollRulerX );
526 mScrollView.SetRulerY( mScrollRulerY );
529 void DaliTableView::OrientationChanged( Orientation orientation )
531 // TODO: Implement if orientation change required
534 void DaliTableView::Rotate( unsigned int degrees )
536 // Resize the root actor
537 Vector2 stageSize = Stage::GetCurrent().GetSize();
538 Vector3 targetSize( stageSize.x, stageSize.y, 1.0f );
540 if( degrees == 90 || degrees == 270 )
542 targetSize = Vector3( stageSize.y, stageSize.x, 1.0f );
545 if( mRotateAnimation )
547 mRotateAnimation.Stop();
548 mRotateAnimation.Clear();
551 mRotateAnimation = Animation::New( ROTATE_ANIMATION_TIME );
552 mRotateAnimation.RotateTo( mRootActor, Degree( 360 - degrees ), Vector3::ZAXIS, AlphaFunctions::EaseOut );
553 mRotateAnimation.Resize( mRootActor, targetSize, AlphaFunctions::EaseOut );
554 mRotateAnimation.Play();
557 Actor DaliTableView::CreateTile( const string& name, const string& title, const Size& parentSize, bool addBackground )
559 Actor tile = Actor::New();
560 tile.SetName( name );
561 tile.SetAnchorPoint( AnchorPoint::CENTER );
562 tile.SetParentOrigin( ParentOrigin::CENTER );
564 // make the tile 100% of parent
565 tile.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
567 Actor content = Actor::New();
568 content.SetAnchorPoint( AnchorPoint::CENTER );
569 content.SetParentOrigin( ParentOrigin::CENTER );
570 content.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
573 // create background image
576 Image bg = Image::New( TILE_BACKGROUND );
577 ImageActor image = ImageActor::New( bg );
578 image.SetAnchorPoint( AnchorPoint::CENTER );
579 image.SetParentOrigin( ParentOrigin::CENTER );
580 // make the image 100% of tile
581 image.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
582 // move image back to get text appear in front
584 image.SetStyle( ImageActor::STYLE_NINE_PATCH );
585 image.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
587 content.Add( image );
590 ImageActor stencil = NewStencilImage();
591 stencil.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
592 image.Add( stencil );
595 TextView text = TextView::New( title );
596 text.SetAnchorPoint( AnchorPoint::CENTER );
597 text.SetParentOrigin( ParentOrigin::CENTER );
598 text.SetWidthExceedPolicy( Toolkit::TextView::ShrinkToFit );
599 text.SetMultilinePolicy( Toolkit::TextView::SplitByWord );
600 text.SetLineJustification( Toolkit::TextView::Center );
601 text.SetTextAlignment( Toolkit::Alignment::Type( Alignment::HorizontalCenter | Alignment::VerticalCenter ) );
602 text.SetColor( Color::WHITE );
604 // make the text 90% of tile
605 text.SetSize( 0.9f * parentSize.width, 0.9f * parentSize.height );
606 text.SetStyleToCurrentText( GetTableTextStyle() );
607 text.SetSnapshotModeEnabled( false );
610 // Set the tile to be keyboard focusable
611 tile.SetKeyboardFocusable(true);
613 // connect to the touch events
614 tile.TouchedSignal().Connect( this, &DaliTableView::OnTilePressed );
619 ImageActor DaliTableView::NewStencilImage()
621 Image alpha = Image::New( TILE_BACKGROUND_ALPHA );
623 ImageActor stencilActor = ImageActor::New( alpha );
624 stencilActor.SetStyle( ImageActor::STYLE_NINE_PATCH );
625 stencilActor.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
627 stencilActor.SetParentOrigin( ParentOrigin::CENTER );
628 stencilActor.SetAnchorPoint( AnchorPoint::CENTER );
629 stencilActor.SetDrawMode( DrawMode::STENCIL );
631 Dali::ShaderEffect shaderEffect = AlphaDiscardEffect::New();
632 stencilActor.SetShaderEffect( shaderEffect );
637 bool DaliTableView::OnTilePressed( Actor actor, const TouchEvent& event )
639 bool consumed = false;
641 const TouchPoint& point = event.GetPoint( 0 );
642 if( TouchPoint::Down == point.state )
644 mPressedActor = actor;
648 // A button press is only valid if the Down & Up events
649 // both occurred within the button.
650 if( ( TouchPoint::Up == point.state ) &&
651 ( mPressedActor == actor ) )
653 std::string name = actor.GetName();
654 ExampleMapConstIter iter = mExampleMap.find( name );
656 FocusManager focusManager = FocusManager::Get();
658 if( iter != mExampleMap.end() )
660 // ignore Example button presses when scrolling or button animating.
661 if( ( !mScrolling ) && ( !mPressedAnimation ) )
663 // do nothing, until pressed animation finished.
670 mPressedAnimation = Animation::New( BUTTON_PRESS_ANIMATION_TIME );
671 mPressedAnimation.SetDestroyAction( Animation::Discard );
673 // scale the content actor within the Tile, as to not affect the placement within the Table.
674 Actor content = actor.GetChildAt(0);
675 mPressedAnimation.ScaleTo( content, Vector3( 0.9f, 0.9f, 1.0f ), AlphaFunctions::EaseInOut, 0.0f,
676 BUTTON_PRESS_ANIMATION_TIME * 0.5f );
677 mPressedAnimation.ScaleTo( content, Vector3::ONE, AlphaFunctions::EaseInOut, BUTTON_PRESS_ANIMATION_TIME * 0.5f,
678 BUTTON_PRESS_ANIMATION_TIME * 0.5f );
679 mPressedAnimation.Play();
680 mPressedAnimation.FinishedSignal().Connect( this, &DaliTableView::OnPressedAnimationFinished );
686 void DaliTableView::OnPressedAnimationFinished( Dali::Animation& source )
688 mPressedAnimation.Reset();
691 std::string name = mPressedActor.GetName();
692 ExampleMapConstIter iter = mExampleMap.find( name );
694 if( iter == mExampleMap.end() )
696 if( name == BUTTON_QUIT )
698 // Move focus to the OK button
699 FocusManager focusManager = FocusManager::Get();
701 // Enable the group mode and wrap mode
702 focusManager.SetGroupMode( true );
703 focusManager.SetWrapMode( true );
708 const Example& example( iter->second );
709 aul_open_app( example.name.c_str() );
711 mPressedActor.Reset();
715 void DaliTableView::OnScrollStart( const Dali::Vector3& position )
722 void DaliTableView::OnScrollComplete( const Dali::Vector3& position )
726 // move focus to 1st item of new page
727 FocusManager focusManager = FocusManager::Get();
728 focusManager.SetCurrentFocusActor(mTableViewList[mScrollView.GetCurrentPage()].GetChildAt(TableView::CellPosition(1, 0)) );
732 bool DaliTableView::OnScrollTouched( Actor actor, const TouchEvent& event )
734 const TouchPoint& point = event.GetPoint( 0 );
735 if( TouchPoint::Down == point.state )
737 mPressedActor = actor;
743 void DaliTableView::ApplyScrollViewEffect()
745 // Remove old effect if exists.
747 if( mScrollViewEffect )
749 mScrollView.RemoveEffect( mScrollViewEffect );
752 // Just one effect for now
753 SetupInnerPageCubeEffect();
755 mScrollView.ApplyEffect( mScrollViewEffect );
758 void DaliTableView::SetupInnerPageCubeEffect()
760 ScrollViewCustomEffect customEffect;
761 mScrollViewEffect = customEffect = ScrollViewCustomEffect::New();
762 mScrollView.SetScrollSnapDuration( EFFECT_SNAP_DURATION );
763 mScrollView.SetScrollFlickDuration( EFFECT_FLICK_DURATION );
764 mScrollView.SetScrollSnapAlphaFunction( AlphaFunctions::EaseOutBack );
765 mScrollView.SetScrollFlickAlphaFunction( AlphaFunctions::EaseOutBack );
766 mScrollView.RemoveConstraintsFromChildren();
768 customEffect.SetPageSpacing( Vector2( 30.0f, 30.0f ) );
769 customEffect.SetAngledOriginPageRotation( ANGLE_CUBE_PAGE_ROTATE );
770 customEffect.SetSwingAngle( ANGLE_CUBE_PAGE_ROTATE.x, Vector3( 0, -1, 0 ) );
771 customEffect.SetOpacityThreshold( 0.5f ); // Make fade out on edges
774 void DaliTableView::ApplyEffectToPage( Actor page, const Vector3& tableRelativeSize )
776 page.RemoveConstraints();
778 Constraint constraint = Constraint::New<Vector3>( Actor::SCALE,
779 LocalSource( Actor::SIZE ),
780 ParentSource( Actor::SIZE ),
781 ScaleToFitConstraint() );
782 page.ApplyConstraint(constraint);
784 ApplyCustomEffectToPage( page );
787 void DaliTableView::ApplyCustomEffectToPage( Actor page )
789 ScrollViewCustomEffect customEffect = ScrollViewCustomEffect::DownCast( mScrollViewEffect );
790 Vector2 vStageSize( Stage::GetCurrent().GetSize() );
791 customEffect.ApplyToPage( page, Vector3( vStageSize.x, vStageSize.y, 1.0f ) );
794 void DaliTableView::OnKeyEvent( const KeyEvent& event )
796 if( event.state == KeyEvent::Down )
798 if ( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
805 Actor CreateBackgroundActor( const Vector2& size )
807 Actor layer = Actor::New();
808 layer.SetAnchorPoint( AnchorPoint::CENTER );
809 layer.SetParentOrigin( ParentOrigin::CENTER );
810 layer.SetSize( size );
814 void DaliTableView::SetupBackground( Actor addToLayer, const Vector2& size )
816 // Create distance field shape
817 BitmapImage distanceField;
818 Size imageSize( 512, 512 );
819 CreateShapeImage( CIRCLE, imageSize, distanceField );
822 Actor backgroundAnimLayer0 = CreateBackgroundActor( size );
823 Actor backgroundAnimLayer1 = CreateBackgroundActor( size );
824 Actor backgroundAnimLayer2 = CreateBackgroundActor( size );
827 Constraint animConstraint0 = Constraint::New < Vector3 > ( Actor::POSITION,
828 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_X_PROPERTY_NAME ) ),
829 AnimScrollConstraint( backgroundAnimLayer0.GetCurrentPosition(), 0.75f ) );
830 backgroundAnimLayer0.ApplyConstraint( animConstraint0 );
832 Constraint animConstraint1 = Constraint::New < Vector3 > ( Actor::POSITION,
833 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_X_PROPERTY_NAME ) ),
834 AnimScrollConstraint( backgroundAnimLayer1.GetCurrentPosition(), 0.5f ) );
835 backgroundAnimLayer1.ApplyConstraint( animConstraint1 );
837 Constraint animConstraint2 = Constraint::New < Vector3 > ( Actor::POSITION,
838 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_X_PROPERTY_NAME ) ),
839 AnimScrollConstraint( backgroundAnimLayer2.GetCurrentPosition(), 0.25f ) );
840 backgroundAnimLayer2.ApplyConstraint( animConstraint2 );
843 ImageActor layer = Dali::Toolkit::CreateSolidColorActor( BACKGROUND_COLOR );
844 layer.SetAnchorPoint( AnchorPoint::CENTER );
845 layer.SetParentOrigin( ParentOrigin::CENTER );
846 layer.SetSize( size * BACKGROUND_SIZE_SCALE );
847 layer.SetZ( BACKGROUND_Z );
848 layer.SetPositionInheritanceMode( DONT_INHERIT_POSITION );
850 addToLayer.Add( layer );
853 addToLayer.Add( backgroundAnimLayer0 );
854 addToLayer.Add( backgroundAnimLayer1 );
855 addToLayer.Add( backgroundAnimLayer2 );
857 // Add all the children
858 AddBackgroundActors( backgroundAnimLayer0, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
859 AddBackgroundActors( backgroundAnimLayer1, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
860 AddBackgroundActors( backgroundAnimLayer2, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
863 void DaliTableView::AddBackgroundActors( Actor layer, int count, BitmapImage distanceField, const Dali::Vector2& size )
865 for( int i = 0; i < count; ++i )
867 float randSize = Random::Range( 10.0f, 400.0f );
868 Vector4 randColour( 0.0f, Random::Range( 0.0f, 0.75f ), 1.0f, Random::Range( 0.3f, 0.6f ));
870 ImageActor dfActor = ImageActor::New( distanceField );
871 mBackgroundActors.push_back( dfActor );
872 dfActor.SetSize( Vector2( randSize, randSize ) );
873 dfActor.SetParentOrigin( ParentOrigin::CENTER );
875 Toolkit::DistanceFieldEffect effect = Toolkit::DistanceFieldEffect::New();
876 dfActor.SetShaderEffect( effect );
877 dfActor.SetColor( randColour );
878 effect.SetOutlineParams( Vector2( 0.55f, 0.00f ) );
879 effect.SetSmoothingEdge( 0.5f );
880 layer.Add( dfActor );
884 Random::Range( -size.x * 0.5f * BACKGROUND_SPREAD_SCALE, size.x * 0.5f * BACKGROUND_SPREAD_SCALE ),
885 Random::Range( -size.y * 0.5f - randSize, size.y * 0.5f + randSize ),
886 Random::Range(-1.0f, 0.0f) );
887 dfActor.SetPosition( actorPos );
888 dfActor.SetSortModifier(size.x * 0.7f);
890 Constraint movementConstraint = Constraint::New < Vector3 > ( Actor::POSITION,
891 LocalSource( Actor::SIZE ),
892 ParentSource( Actor::SIZE ),
893 ShapeMovementConstraint );
894 dfActor.ApplyConstraint( movementConstraint );
897 Animation animation = Animation::New( Random::Range( 40.0f, 200.0f ) );
898 KeyFrames keyframes = KeyFrames::New();
899 keyframes.Add( 0.0f, actorPos );
900 Vector3 toPos( actorPos );
901 toPos.y -= ( size.y + randSize );
902 keyframes.Add( 1.0f, toPos );
903 animation.AnimateBetween( Property( dfActor, Actor::POSITION ), keyframes );
904 animation.SetLooping( true );
906 mBackgroundAnimations.push_back( animation );
910 void DaliTableView::CreateShapeImage( ShapeType shapeType, const Size& size, BitmapImage& distanceFieldOut )
912 // this bitmap will hold the alpha map for the distance field shader
913 distanceFieldOut = BitmapImage::New( size.width, size.height, Pixel::A8 );
915 // Generate bit pattern
916 std::vector< unsigned char > imageDataA8;
917 imageDataA8.reserve( size.width * size.height ); // A8
922 GenerateCircle( size, imageDataA8 );
925 GenerateSquare( size, imageDataA8 );
931 GenerateDistanceFieldMap( &imageDataA8[ 0 ], size, distanceFieldOut.GetBuffer(), size, 8.0f, size );
932 distanceFieldOut.Update();
935 void DaliTableView::GenerateSquare( const Size& size, std::vector< unsigned char >& distanceFieldOut )
937 for( int h = 0; h < size.height; ++h )
939 for( int w = 0; w < size.width; ++w )
941 distanceFieldOut.push_back( 0xFF );
946 void DaliTableView::GenerateCircle( const Size& size, std::vector< unsigned char >& distanceFieldOut )
948 const float radius = size.width * 0.5f * size.width * 0.5f;
949 Vector2 center( size.width / 2, size.height / 2 );
951 for( int h = 0; h < size.height; ++h )
953 for( int w = 0; w < size.width; ++w )
956 Vector2 dist = pos - center;
958 if( dist.x * dist.x + dist.y * dist.y > radius )
960 distanceFieldOut.push_back( 0x00 );
964 distanceFieldOut.push_back( 0xFF );
970 ImageActor DaliTableView::CreateLogo( std::string imagePath )
972 Image image = Image::New( imagePath );
973 image.LoadingFinishedSignal().Connect( this, &DaliTableView::OnLogoLoaded );
975 ImageActor logo = ImageActor::New( image );
977 logo.SetAnchorPoint( AnchorPoint::CENTER );
978 logo.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
983 void DaliTableView::OnLogoLoaded( Dali::Image image )
985 mRootActor.SetFixedHeight( 1, image.GetHeight() + LOGO_BOTTOM_PADDING_HEIGHT );
988 bool DaliTableView::PauseBackgroundAnimation()
995 void DaliTableView::PauseAnimation()
997 if( mBackgroundAnimsPlaying )
999 for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
1001 Animation anim = *animIter;
1006 mBackgroundAnimsPlaying = false;
1010 void DaliTableView::PlayAnimation()
1012 if ( !mBackgroundAnimsPlaying )
1014 for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
1016 Animation anim = *animIter;
1021 mBackgroundAnimsPlaying = true;
1024 mAnimationTimer.SetInterval( BACKGROUND_ANIMATION_DURATION );
1027 Dali::Actor DaliTableView::OnKeyboardPreFocusChange( Dali::Actor current, Dali::Actor proposed, Dali::Toolkit::Control::KeyboardFocusNavigationDirection direction )
1029 Actor nextFocusActor = proposed;
1031 if ( !current && !proposed )
1033 // Set the initial focus to the first tile in the current page should be focused.
1034 nextFocusActor = mTableViewList[mScrollView.GetCurrentPage()].GetChildAt(TableView::CellPosition(0, 0));
1036 else if( !proposed || (proposed && proposed == mScrollViewLayer) )
1038 // ScrollView is being focused but nothing in the current page can be focused further
1039 // in the given direction. We should work out which page to scroll to next.
1040 int currentPage = mScrollView.GetCurrentPage();
1041 int newPage = currentPage;
1042 if( direction == Dali::Toolkit::Control::Left )
1046 else if( direction == Dali::Toolkit::Control::Right )
1051 newPage = std::max(0, std::min(static_cast<int>(mScrollRulerX->GetTotalPages() - 1), newPage));
1052 if( newPage == currentPage )
1054 if( direction == Dali::Toolkit::Control::Left )
1056 newPage = mScrollRulerX->GetTotalPages() - 1;
1057 } else if( direction == Dali::Toolkit::Control::Right )
1063 // Scroll to the page in the given direction
1064 mScrollView.ScrollTo(newPage);
1066 if( direction == Dali::Toolkit::Control::Left )
1068 // Work out the cell position for the last tile
1069 int remainingExamples = mExampleList.size() - newPage * EXAMPLES_PER_PAGE;
1070 int rowPos = (remainingExamples >= EXAMPLES_PER_PAGE) ? ROWS_PER_PAGE - 1 : ( (remainingExamples % EXAMPLES_PER_PAGE + EXAMPLES_PER_ROW) / EXAMPLES_PER_ROW - 1 );
1071 int colPos = remainingExamples >= EXAMPLES_PER_PAGE ? EXAMPLES_PER_ROW - 1 : ( remainingExamples % EXAMPLES_PER_PAGE - rowPos * EXAMPLES_PER_ROW - 1 );
1073 // Move the focus to the last tile in the new page.
1074 nextFocusActor = mTableViewList[newPage].GetChildAt(TableView::CellPosition(rowPos, colPos));
1078 // Move the focus to the first tile in the new page.
1079 nextFocusActor = mTableViewList[newPage].GetChildAt(TableView::CellPosition(0, 0));
1083 return nextFocusActor;
1086 void DaliTableView::OnFocusedActorActivated( Dali::Actor activatedActor )
1090 mPressedActor = activatedActor;
1092 // Activate the current focused actor;
1093 TouchEvent touchEventUp;
1094 touchEventUp.points.push_back( TouchPoint ( 0, TouchPoint::Up, 0.0f, 0.0f ) );
1095 OnTilePressed(mPressedActor, touchEventUp);