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"
32 using namespace Dali::Toolkit;
35 ///////////////////////////////////////////////////////////////////////////////
40 const std::string BUTTON_BACKWARD( "Backward" );
41 const std::string BUTTON_FORWARD( "Forward" );
42 const std::string BUTTON_QUIT( "Quit" );
43 const std::string BUTTON_OK( "Ok" );
44 const std::string BUTTON_CANCEL( "Cancel" );
46 const std::string DEFAULT_BACKGROUND_IMAGE_PATH( DALI_IMAGE_DIR "background-gradient.jpg" );
47 const std::string LOGO_PATH( DALI_IMAGE_DIR "dali-logo.png" );
48 const std::string DEFAULT_TOOLBAR_IMAGE_PATH( DALI_IMAGE_DIR "top-bar.png" );
49 const std::string BUTTON_BACKGROUND(DALI_IMAGE_DIR "button-background.png");
50 const std::string TILE_BACKGROUND(DALI_IMAGE_DIR "item-background.png");
51 const std::string TILE_BACKGROUND_ALPHA(DALI_IMAGE_DIR "item-background-alpha.png");
53 const char * const DEFAULT_TOOLBAR_TEXT( "TOUCH TO LAUNCH EXAMPLE" );
55 const float BUTTON_PRESS_ANIMATION_TIME = 0.25f; ///< Time to perform button scale effect.
56 const float ROTATE_ANIMATION_TIME = 0.5f; ///< Time to perform rotate effect.
57 const int MAX_PAGES = 256; ///< Maximum pages (arbitrary safety limit)
58 const int EXAMPLES_PER_ROW = 3;
59 const int ROWS_PER_PAGE = 3;
60 const int EXAMPLES_PER_PAGE = EXAMPLES_PER_ROW * ROWS_PER_PAGE;
61 const float TOP_ROW_HEIGHT = 35.0f;
62 const float BOTTOM_ROW_HEIGHT = 35.0f;
63 const int BOTTOM_PADDING_HEIGHT = 40;
64 const int LOGO_BOTTOM_PADDING_HEIGHT = 30;
65 const Vector3 TABLE_RELATIVE_SIZE(0.9f, 1.0f, 0.8f ); ///< TableView's relative size to the entire stage.
66 const float STENCIL_RELATIVE_SIZE = 1.0f;
68 const float EFFECT_SNAP_DURATION = 0.66f; ///< Scroll Snap Duration for Effects
69 const float EFFECT_FLICK_DURATION = 0.5f; ///< Scroll Flick Duration for Effects
70 const Vector3 ANGLE_CUBE_PAGE_ROTATE(Math::PI * 0.5f, Math::PI * 0.5f, 0.0f);
72 const int NUM_BACKGROUND_IMAGES = 20;
73 const float BACKGROUND_SWIPE_SCALE = 0.025f;
74 const float BACKGROUND_SPREAD_SCALE = 1.5f;
75 const float SCALE_MOD = 1000.0f * Math::PI * 2.0f;
76 const float SCALE_SPEED = 10.0f;
77 const float SCALE_SPEED_SIN = 0.1f;
79 const unsigned int BACKGROUND_ANIMATION_DURATION = 15000; // 15 secs
81 const float BACKGROUND_Z = -1000.0f;
82 const float BACKGROUND_SIZE_SCALE = 2.0f;
83 const Vector4 BACKGROUND_COLOR( 1.0f, 1.0f, 1.0f, 1.0f );
86 const std::string DEFAULT_TEXT_STYLE_FONT_FAMILY("HelveticaNeue");
87 const std::string DEFAULT_TEXT_STYLE_FONT_STYLE("Regular");
88 const Dali::PointSize DEFAULT_TEXT_STYLE_POINT_SIZE( 8.0f );
89 const Dali::TextStyle::Weight DEFAULT_TEXT_STYLE_WEIGHT(Dali::TextStyle::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 TextStyle GetDefaultTextStyle()
101 textStyle.SetFontName(DEFAULT_TEXT_STYLE_FONT_FAMILY);
102 textStyle.SetFontStyle(DEFAULT_TEXT_STYLE_FONT_STYLE);
103 textStyle.SetFontPointSize(DEFAULT_TEXT_STYLE_POINT_SIZE);
104 textStyle.SetWeight(DEFAULT_TEXT_STYLE_WEIGHT);
105 textStyle.SetTextColor(DEFAULT_TEXT_STYLE_COLOR);
106 textStyle.SetShadow( true );
110 TextStyle GetTableTextStyle()
113 textStyle.SetFontName(TABLE_TEXT_STYLE_FONT_FAMILY);
114 textStyle.SetFontStyle(TABLE_TEXT_STYLE_FONT_STYLE);
115 textStyle.SetFontPointSize(TABLE_TEXT_STYLE_POINT_SIZE);
116 textStyle.SetWeight(TABLE_TEXT_STYLE_WEIGHT);
117 textStyle.SetTextColor(TABLE_TEXT_STYLE_COLOR);
122 * Creates the background image
124 ImageActor CreateBackground( std::string imagePath )
126 Image image = Image::New( imagePath );
127 ImageActor background = ImageActor::New( image );
129 background.SetAnchorPoint( AnchorPoint::CENTER );
130 background.SetParentOrigin( ParentOrigin::CENTER );
131 background.SetZ( -1.0f );
136 // These values depend on the tile image
137 const float IMAGE_BORDER_LEFT = 11.0f;
138 const float IMAGE_BORDER_RIGHT = IMAGE_BORDER_LEFT;
139 const float IMAGE_BORDER_TOP = IMAGE_BORDER_LEFT;
140 const float IMAGE_BORDER_BOTTOM = IMAGE_BORDER_LEFT;
143 * TableViewVisibilityConstraint
145 struct TableViewVisibilityConstraint
147 bool operator()( const bool& current,
148 const PropertyInput& pagePositionProperty,
149 const PropertyInput& pageSizeProperty )
151 // Only the tableview in the current page should be visible.
152 const Vector3& pagePosition = pagePositionProperty.GetVector3();
153 const Vector3& pageSize = pageSizeProperty.GetVector3();
154 return fabsf( pagePosition.x ) < pageSize.x;
159 * Constraint to wrap an actor in y that is moving vertically
161 Vector3 ShapeMovementConstraint( const Vector3& current,
162 const PropertyInput& shapeSizeProperty,
163 const PropertyInput& parentSizeProperty )
165 const Vector3& shapeSize = shapeSizeProperty.GetVector3();
166 const Vector3& parentSize = parentSizeProperty.GetVector3();
168 Vector3 pos( current );
169 if( pos.y + shapeSize.y * 0.5f < -parentSize.y * 0.5f )
171 pos.y += parentSize.y + shapeSize.y;
178 * Constraint to return a bool value based on the alpha channel value
180 bool AlphaVisibleConstraint( bool current, const PropertyInput& alphaProperty )
182 Vector4 colour = alphaProperty.GetVector4();
183 return ( colour.a > Math::MACHINE_EPSILON_10000 );
187 * Constraint to return a position for the background based on the scroll value
189 struct AnimScrollConstraint
193 AnimScrollConstraint( const Vector3& initialPos, float scale )
194 : mInitialPos( initialPos ),
200 Vector3 operator()( const Vector3& current, const PropertyInput& scrollProperty )
202 float scrollPos = scrollProperty.GetVector3().x;
204 return mInitialPos + Vector3( -scrollPos * mScale, 0.0f, 0.0f );
213 * Constraint to return a tracked world position added to the constant local position
215 struct TranslateLocalConstraint
219 TranslateLocalConstraint( const Vector3& localPos )
220 : mLocalPos( localPos )
224 Vector3 operator()( const Vector3& current, const PropertyInput& pagePosProperty )
226 Vector3 worldPos = pagePosProperty.GetVector3();
228 return ( worldPos + mLocalPos );
236 bool CompareByTitle( const Example& lhs, const Example& rhs )
238 return lhs.title < rhs.title;
243 DaliTableView::DaliTableView( Application& application )
244 : mApplication( application ),
246 mBackgroundImagePath( DEFAULT_BACKGROUND_IMAGE_PATH ),
247 mSortAlphabetically( false ),
248 mBackgroundAnimsPlaying( false )
250 application.InitSignal().Connect( this, &DaliTableView::Initialize );
253 DaliTableView::~DaliTableView()
257 void DaliTableView::AddExample( Example example )
259 mExampleList.push_back( example );
260 mExampleMap[ example.name ] = example;
263 void DaliTableView::SetBackgroundPath( std::string imagePath )
265 mBackgroundImagePath = imagePath;
268 void DaliTableView::SortAlphabetically( bool sortAlphabetically )
270 mSortAlphabetically = sortAlphabetically;
273 void DaliTableView::Initialize( Application& application )
275 Stage::GetCurrent().KeyEventSignal().Connect( this, &DaliTableView::OnKeyEvent );
277 Vector2 stageSize = Stage::GetCurrent().GetSize();
280 mBackground = CreateBackground( mBackgroundImagePath );
281 // set same size as parent actor
282 mBackground.SetSize( stageSize );
283 Stage::GetCurrent().Add( mBackground );
285 // Render entire content as overlays, as is all on same 2D plane.
286 mRootActor = TableView::New( 4, 1 );
287 mRootActor.SetAnchorPoint( AnchorPoint::CENTER );
288 mRootActor.SetParentOrigin( ParentOrigin::CENTER );
289 mRootActor.SetFixedHeight( 3, BOTTOM_PADDING_HEIGHT );
290 Stage::GetCurrent().Add( mRootActor );
293 CreateToolbar( mRootActor, DEFAULT_TOOLBAR_TEXT, DEFAULT_TOOLBAR_IMAGE_PATH );
296 mLogo = CreateLogo( LOGO_PATH );
297 Alignment alignment = Alignment::New();
298 alignment.Add(mLogo);
299 mRootActor.AddChild( alignment, TableView::CellPosition( 1, 0 ) );
301 // scrollview occupying the majority of the screen
302 mScrollView = ScrollView::New();
304 mScrollView.SetAnchorPoint( AnchorPoint::CENTER );
305 mScrollView.SetParentOrigin( ParentOrigin::CENTER );
306 mScrollView.ApplyConstraint( Dali::Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ), Dali::RelativeToConstraint( TABLE_RELATIVE_SIZE ) ) );
307 mScrollView.SetAxisAutoLock( true );
308 mScrollView.ScrollCompletedSignal().Connect( this, &DaliTableView::OnScrollComplete );
309 mScrollView.ScrollStartedSignal().Connect( this, &DaliTableView::OnScrollStart );
310 mScrollView.TouchedSignal().Connect( this, &DaliTableView::OnScrollTouched );
312 mScrollViewLayer = Layer::New();
313 mScrollViewLayer.SetAnchorPoint( AnchorPoint::CENTER );
314 mScrollViewLayer.SetParentOrigin( ParentOrigin::CENTER );
315 mScrollViewLayer.SetSize( stageSize );
316 mScrollViewLayer.Add( mScrollView );
317 mRootActor.AddChild( mScrollViewLayer, TableView::CellPosition( 2, 0 ) );
319 // Setup the scenegraph
320 // 1) Add scroll view effect and setup constraints on pages
321 ApplyScrollViewEffect();
323 // 2) Add pages and tiles
326 // 3) Populate scrollview with background so constraints on background layers can work with scrollview
327 SetupBackground( mScrollView, stageSize );
329 // 4) Remove constraints for inner cube effect
330 for( TableViewListIter pageIter = mTableViewList.begin(); pageIter != mTableViewList.end(); ++pageIter )
332 TableView page = *pageIter;
334 unsigned int numChildren = page.GetChildCount();
335 Actor pageActor = page;
336 for( unsigned int i=0; i<numChildren; ++i)
338 // Remove old effect's manual constraints.
339 Actor child = pageActor.GetChildAt(i);
342 child.RemoveConstraints();
347 // Set initial orientation
348 unsigned int degrees = application.GetOrientation().GetDegrees();
351 Dali::Window winHandle = application.GetWindow();
352 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
353 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE );
354 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
355 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
357 Dali::Orientation orientation = winHandle.GetOrientation();
358 orientation.ChangedSignal().Connect( this, &DaliTableView::OrientationChanged );
360 winHandle.ShowIndicator( false );
363 mAnimationTimer = Timer::New( BACKGROUND_ANIMATION_DURATION );
364 mAnimationTimer.TickSignal().Connect( this, &DaliTableView::PauseBackgroundAnimation );
365 mAnimationTimer.Start();
366 mBackgroundAnimsPlaying = true;
368 KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &DaliTableView::OnKeyboardPreFocusChange );
369 KeyboardFocusManager::Get().FocusedActorActivatedSignal().Connect( this, &DaliTableView::OnFocusedActorActivated );
372 void DaliTableView::CreateToolbar( TableView root, const std::string& title, const std::string& toolbarImagePath,
373 const ViewStyle& style )
375 // Create default ToolBar
376 Dali::Stage stage = Dali::Stage::GetCurrent();
377 Dali::Vector2 dpi = stage.GetDpi();
379 // Create toolbar layer.
380 Dali::Layer toolBarLayer = Dali::Layer::New();
381 toolBarLayer.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
382 toolBarLayer.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
383 toolBarLayer.ApplyConstraint( Dali::Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ), Dali::SourceWidthFixedHeight( style.mToolBarHeight * dpi.y / style.mDpi ) ) );
384 float toolBarLayerHeight = style.mToolBarHeight * dpi.y / style.mDpi;
385 toolBarLayer.SetSize( 0.0f, toolBarLayerHeight );
387 // Add tool bar layer to the view.
388 root.AddChild( toolBarLayer, TableView::CellPosition( 0, 0 ) );
389 root.SetFixedHeight( 0, toolBarLayerHeight );
391 // Raise tool bar layer to the top.
392 toolBarLayer.RaiseToTop();
395 Dali::Image image = Dali::Image::New( toolbarImagePath );
396 Dali::ImageActor toolBarBackground = Dali::ImageActor::New( image );
397 Dali::Toolkit::ToolBar toolBar = Dali::Toolkit::ToolBar::New();
398 toolBar.SetBackground( toolBarBackground );
399 toolBar.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
400 toolBar.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
401 toolBar.ApplyConstraint( Dali::Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ), Dali::EqualToConstraint() ) );
402 toolBar.SetSize( 0.0f, style.mToolBarHeight * dpi.y / style.mDpi );
403 toolBarBackground.SetZ( -1.0f );
405 // Add the tool bar to the too bar layer.
406 toolBarLayer.Add( toolBar );
408 Dali::Font font = Dali::Font::New();
413 Dali::Toolkit::TextView titleActor = Dali::Toolkit::TextView::New();
414 titleActor.SetName( "ToolbarTitle" );
415 titleActor.SetText( title );
416 titleActor.SetSize( font.MeasureText( title ) );
417 titleActor.SetStyleToCurrentText( GetDefaultTextStyle() );
418 titleActor.SetZ( 1.0f );
420 // Add title to the tool bar.
421 const float padding( style.mToolBarPadding * dpi.x / style.mDpi );
422 toolBar.AddControl( titleActor, style.mToolBarTitlePercentage, Dali::Toolkit::Alignment::HorizontalCenter,
423 Dali::Toolkit::Alignment::Padding( padding, padding, padding, padding ) );
427 void DaliTableView::Populate()
429 const Vector2 stageSize = Stage::GetCurrent().GetSize();
431 const Size demoTileSize( 0.25f * stageSize.width, 0.25f * stageSize.height );
433 mTotalPages = ( mExampleList.size() + EXAMPLES_PER_PAGE - 1 ) / EXAMPLES_PER_PAGE;
435 // Populate ScrollView.
436 if( mExampleList.size() > 0 )
438 if( mSortAlphabetically )
440 sort( mExampleList.begin(), mExampleList.end(), CompareByTitle );
443 unsigned int exampleCount = 0;
444 ExampleListConstIter iter = mExampleList.begin();
445 for( int t = 0; t < mTotalPages; t++ )
447 // Create Table. (contains up to 9 Examples)
448 TableView tableView = TableView::New( 4, 3 );
449 // Add tableView to container.
450 mScrollView.Add( tableView );
451 ApplyEffectToPage( tableView, TABLE_RELATIVE_SIZE );
453 tableView.SetAnchorPoint( AnchorPoint::CENTER );
454 tableView.SetParentOrigin( ParentOrigin::CENTER );
455 // 2 pixels of padding
456 tableView.SetCellPadding( Size( 2.0f, 2.0f ) );
458 Constraint constraint = Constraint::New<Vector3>( Actor::SCALE,
459 LocalSource( Actor::SIZE ),
460 ParentSource( Actor::SIZE ),
461 ScaleToFitConstraint() );
462 tableView.ApplyConstraint(constraint);
464 // Apply visibility constraint to table view
465 Constraint visibleConstraint = Constraint::New< bool >( Actor::VISIBLE,
466 LocalSource( Actor::POSITION ),
467 ParentSource( Actor::SIZE ),
468 TableViewVisibilityConstraint() );
469 visibleConstraint.SetRemoveAction( Constraint::Discard );
470 tableView.ApplyConstraint( visibleConstraint );
472 // add cells to table
473 for( int y = 0; y < ROWS_PER_PAGE; y++ )
475 for( int x = 0; x < EXAMPLES_PER_ROW; x++ )
477 const Example& example = ( *iter );
479 Actor tile = CreateTile( example.name, example.title, demoTileSize, true );
480 FocusManager focusManager = FocusManager::Get();
481 focusManager.SetFocusOrder( tile, ++exampleCount );
482 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_LABEL,
484 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_TRAIT, "Tile" );
485 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_HINT,
486 "You can run this example" );
488 tableView.AddChild( tile, TableView::CellPosition( y, x ) );
491 if( iter == mExampleList.end() )
496 if( iter == mExampleList.end() )
503 tableView.SetFixedHeight( 3, BOTTOM_ROW_HEIGHT );
505 std::stringstream out;
506 out << ( t + 1 ) << " of " << mTotalPages;
507 Actor pageNumberText = CreateTile( "", out.str(), Size( 0.8f * stageSize.width, BOTTOM_ROW_HEIGHT ), false );
509 pageNumberText.ApplyConstraint( Constraint::New< Vector3 >( Actor::POSITION, Source( tableView, Actor::WORLD_POSITION),
510 TranslateLocalConstraint( Vector3( 0.0f, stageSize.y * 0.4f, 0.0f ) ) ) );
511 pageNumberText.ApplyConstraint( Constraint::New< Quaternion >( Actor::ROTATION, Source( tableView, Actor::WORLD_ROTATION ), EqualToConstraint() ) );
512 pageNumberText.ApplyConstraint( Constraint::New< Vector4 >( Actor::COLOR, Source( tableView, Actor::COLOR ), EqualToConstraint() ) );
514 //Stage::GetCurrent().Add( pageNumberText );
516 // Set tableview position
517 Vector3 tableViewPos( stageSize.x * TABLE_RELATIVE_SIZE.x * t, 0.0f, 0.0f );
518 tableView.SetPosition( tableViewPos );
520 mTableViewList.push_back( tableView );
522 if( iter == mExampleList.end() )
529 // Update Ruler info.
530 mScrollRulerX = new FixedRuler( stageSize.width * TABLE_RELATIVE_SIZE.x );
531 mScrollRulerY = new DefaultRuler();
532 mScrollRulerX->SetDomain( RulerDomain( 0.0f, mTotalPages * stageSize.width * TABLE_RELATIVE_SIZE.x, true ) );
533 mScrollRulerY->Disable();
534 mScrollView.SetRulerX( mScrollRulerX );
535 mScrollView.SetRulerY( mScrollRulerY );
538 void DaliTableView::OrientationChanged( Orientation orientation )
540 // TODO: Implement if orientation change required
543 void DaliTableView::Rotate( unsigned int degrees )
545 // Resize the root actor
546 Vector2 stageSize = Stage::GetCurrent().GetSize();
547 Vector3 targetSize( stageSize.x, stageSize.y, 1.0f );
549 if( degrees == 90 || degrees == 270 )
551 targetSize = Vector3( stageSize.y, stageSize.x, 1.0f );
554 if( mRotateAnimation )
556 mRotateAnimation.Stop();
557 mRotateAnimation.Clear();
560 mRotateAnimation = Animation::New( ROTATE_ANIMATION_TIME );
561 mRotateAnimation.RotateTo( mRootActor, Degree( 360 - degrees ), Vector3::ZAXIS, AlphaFunctions::EaseOut );
562 mRotateAnimation.Resize( mRootActor, targetSize, AlphaFunctions::EaseOut );
563 mRotateAnimation.Play();
566 Actor DaliTableView::CreateTile( const string& name, const string& title, const Size& parentSize, bool addBackground )
568 Actor tile = Actor::New();
569 tile.SetName( name );
570 tile.SetAnchorPoint( AnchorPoint::CENTER );
571 tile.SetParentOrigin( ParentOrigin::CENTER );
573 // make the tile 100% of parent
574 tile.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
576 Actor content = Actor::New();
577 content.SetAnchorPoint( AnchorPoint::CENTER );
578 content.SetParentOrigin( ParentOrigin::CENTER );
579 content.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
582 // create background image
585 Image bg = Image::New( TILE_BACKGROUND );
586 ImageActor image = ImageActor::New( bg );
587 image.SetAnchorPoint( AnchorPoint::CENTER );
588 image.SetParentOrigin( ParentOrigin::CENTER );
589 // make the image 100% of tile
590 image.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
591 // move image back to get text appear in front
593 image.SetStyle( ImageActor::STYLE_NINE_PATCH );
594 image.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
596 content.Add( image );
599 ImageActor stencil = NewStencilImage();
600 stencil.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
601 image.Add( stencil );
604 TextView text = TextView::New( title );
605 text.SetAnchorPoint( AnchorPoint::CENTER );
606 text.SetParentOrigin( ParentOrigin::CENTER );
607 text.SetWidthExceedPolicy( Toolkit::TextView::ShrinkToFit );
608 text.SetMultilinePolicy( Toolkit::TextView::SplitByWord );
609 text.SetLineJustification( Toolkit::TextView::Center );
610 text.SetTextAlignment( Toolkit::Alignment::Type( Alignment::HorizontalCenter | Alignment::VerticalCenter ) );
611 text.SetColor( Color::WHITE );
613 // make the text 90% of tile
614 text.SetSize( 0.9f * parentSize.width, 0.9f * parentSize.height );
615 text.SetStyleToCurrentText( GetTableTextStyle() );
616 text.SetSnapshotModeEnabled( false );
619 // Set the tile to be keyboard focusable
620 tile.SetKeyboardFocusable(true);
622 // connect to the touch events
623 tile.TouchedSignal().Connect( this, &DaliTableView::OnTilePressed );
628 ImageActor DaliTableView::NewStencilImage()
630 Image alpha = Image::New( TILE_BACKGROUND_ALPHA );
632 ImageActor stencilActor = ImageActor::New( alpha );
633 stencilActor.SetStyle( ImageActor::STYLE_NINE_PATCH );
634 stencilActor.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
636 stencilActor.SetParentOrigin( ParentOrigin::CENTER );
637 stencilActor.SetAnchorPoint( AnchorPoint::CENTER );
638 stencilActor.SetDrawMode( DrawMode::STENCIL );
640 Dali::ShaderEffect shaderEffect = AlphaDiscardEffect::New();
641 stencilActor.SetShaderEffect( shaderEffect );
646 bool DaliTableView::OnTilePressed( Actor actor, const TouchEvent& event )
648 bool consumed = false;
650 const TouchPoint& point = event.GetPoint( 0 );
651 if( TouchPoint::Down == point.state )
653 mPressedActor = actor;
657 // A button press is only valid if the Down & Up events
658 // both occurred within the button.
659 if( ( TouchPoint::Up == point.state ) &&
660 ( mPressedActor == actor ) )
662 std::string name = actor.GetName();
663 ExampleMapConstIter iter = mExampleMap.find( name );
665 FocusManager focusManager = FocusManager::Get();
667 if( iter != mExampleMap.end() )
669 // ignore Example button presses when scrolling or button animating.
670 if( ( !mScrolling ) && ( !mPressedAnimation ) )
672 // do nothing, until pressed animation finished.
679 mPressedAnimation = Animation::New( BUTTON_PRESS_ANIMATION_TIME );
680 mPressedAnimation.SetDestroyAction( Animation::Discard );
682 // scale the content actor within the Tile, as to not affect the placement within the Table.
683 Actor content = actor.GetChildAt(0);
684 mPressedAnimation.ScaleTo( content, Vector3( 0.9f, 0.9f, 1.0f ), AlphaFunctions::EaseInOut, 0.0f,
685 BUTTON_PRESS_ANIMATION_TIME * 0.5f );
686 mPressedAnimation.ScaleTo( content, Vector3::ONE, AlphaFunctions::EaseInOut, BUTTON_PRESS_ANIMATION_TIME * 0.5f,
687 BUTTON_PRESS_ANIMATION_TIME * 0.5f );
688 mPressedAnimation.Play();
689 mPressedAnimation.FinishedSignal().Connect( this, &DaliTableView::OnPressedAnimationFinished );
695 void DaliTableView::OnPressedAnimationFinished( Dali::Animation& source )
697 mPressedAnimation.Reset();
700 std::string name = mPressedActor.GetName();
701 ExampleMapConstIter iter = mExampleMap.find( name );
703 if( iter == mExampleMap.end() )
705 if( name == BUTTON_QUIT )
707 // Move focus to the OK button
708 FocusManager focusManager = FocusManager::Get();
710 // Enable the group mode and wrap mode
711 focusManager.SetGroupMode( true );
712 focusManager.SetWrapMode( true );
717 const Example& example( iter->second );
719 aul_open_app( example.name.c_str() );
721 std::stringstream stream;
722 stream << DALI_EXAMPLE_BIN << example.name.c_str();
726 execlp( stream.str().c_str(), example.name.c_str(), NULL );
727 DALI_ASSERT_ALWAYS(false && "exec failed!");
731 mPressedActor.Reset();
735 void DaliTableView::OnScrollStart( const Dali::Vector3& position )
742 void DaliTableView::OnScrollComplete( const Dali::Vector3& position )
746 // move focus to 1st item of new page
747 FocusManager focusManager = FocusManager::Get();
748 focusManager.SetCurrentFocusActor(mTableViewList[mScrollView.GetCurrentPage()].GetChildAt(TableView::CellPosition(1, 0)) );
752 bool DaliTableView::OnScrollTouched( Actor actor, const TouchEvent& event )
754 const TouchPoint& point = event.GetPoint( 0 );
755 if( TouchPoint::Down == point.state )
757 mPressedActor = actor;
763 void DaliTableView::ApplyScrollViewEffect()
765 // Remove old effect if exists.
767 if( mScrollViewEffect )
769 mScrollView.RemoveEffect( mScrollViewEffect );
772 // Just one effect for now
773 SetupInnerPageCubeEffect();
775 mScrollView.ApplyEffect( mScrollViewEffect );
778 void DaliTableView::SetupInnerPageCubeEffect()
780 ScrollViewCustomEffect customEffect;
781 mScrollViewEffect = customEffect = ScrollViewCustomEffect::New();
782 mScrollView.SetScrollSnapDuration( EFFECT_SNAP_DURATION );
783 mScrollView.SetScrollFlickDuration( EFFECT_FLICK_DURATION );
784 mScrollView.SetScrollSnapAlphaFunction( AlphaFunctions::EaseOutBack );
785 mScrollView.SetScrollFlickAlphaFunction( AlphaFunctions::EaseOutBack );
786 mScrollView.RemoveConstraintsFromChildren();
788 customEffect.SetPageSpacing( Vector2( 30.0f, 30.0f ) );
789 customEffect.SetAngledOriginPageRotation( ANGLE_CUBE_PAGE_ROTATE );
790 customEffect.SetSwingAngle( ANGLE_CUBE_PAGE_ROTATE.x, Vector3( 0, -1, 0 ) );
791 customEffect.SetOpacityThreshold( 0.5f ); // Make fade out on edges
794 void DaliTableView::ApplyEffectToPage( Actor page, const Vector3& tableRelativeSize )
796 page.RemoveConstraints();
798 Constraint constraint = Constraint::New<Vector3>( Actor::SCALE,
799 LocalSource( Actor::SIZE ),
800 ParentSource( Actor::SIZE ),
801 ScaleToFitConstraint() );
802 page.ApplyConstraint(constraint);
804 ApplyCustomEffectToPage( page );
807 void DaliTableView::ApplyCustomEffectToPage( Actor page )
809 ScrollViewCustomEffect customEffect = ScrollViewCustomEffect::DownCast( mScrollViewEffect );
810 Vector2 vStageSize( Stage::GetCurrent().GetSize() );
811 customEffect.ApplyToPage( page, Vector3( vStageSize.x, vStageSize.y, 1.0f ) );
814 void DaliTableView::OnKeyEvent( const KeyEvent& event )
816 if( event.state == KeyEvent::Down )
818 if ( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
825 Actor CreateBackgroundActor( const Vector2& size )
827 Actor layer = Actor::New();
828 layer.SetAnchorPoint( AnchorPoint::CENTER );
829 layer.SetParentOrigin( ParentOrigin::CENTER );
830 layer.SetSize( size );
834 void DaliTableView::SetupBackground( Actor addToLayer, const Vector2& size )
836 // Create distance field shape
837 BitmapImage distanceField;
838 Size imageSize( 512, 512 );
839 CreateShapeImage( CIRCLE, imageSize, distanceField );
842 Actor backgroundAnimLayer0 = CreateBackgroundActor( size );
843 Actor backgroundAnimLayer1 = CreateBackgroundActor( size );
844 Actor backgroundAnimLayer2 = CreateBackgroundActor( size );
847 Constraint animConstraint0 = Constraint::New < Vector3 > ( Actor::POSITION,
848 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
849 AnimScrollConstraint( backgroundAnimLayer0.GetCurrentPosition(), 0.75f ) );
850 backgroundAnimLayer0.ApplyConstraint( animConstraint0 );
852 Constraint animConstraint1 = Constraint::New < Vector3 > ( Actor::POSITION,
853 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
854 AnimScrollConstraint( backgroundAnimLayer1.GetCurrentPosition(), 0.5f ) );
855 backgroundAnimLayer1.ApplyConstraint( animConstraint1 );
857 Constraint animConstraint2 = Constraint::New < Vector3 > ( Actor::POSITION,
858 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
859 AnimScrollConstraint( backgroundAnimLayer2.GetCurrentPosition(), 0.25f ) );
860 backgroundAnimLayer2.ApplyConstraint( animConstraint2 );
863 ImageActor layer = Dali::Toolkit::CreateSolidColorActor( BACKGROUND_COLOR );
864 layer.SetAnchorPoint( AnchorPoint::CENTER );
865 layer.SetParentOrigin( ParentOrigin::CENTER );
866 layer.SetSize( size * BACKGROUND_SIZE_SCALE );
867 layer.SetZ( BACKGROUND_Z );
868 layer.SetPositionInheritanceMode( DONT_INHERIT_POSITION );
870 addToLayer.Add( layer );
873 addToLayer.Add( backgroundAnimLayer0 );
874 addToLayer.Add( backgroundAnimLayer1 );
875 addToLayer.Add( backgroundAnimLayer2 );
877 // Add all the children
878 AddBackgroundActors( backgroundAnimLayer0, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
879 AddBackgroundActors( backgroundAnimLayer1, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
880 AddBackgroundActors( backgroundAnimLayer2, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
883 void DaliTableView::AddBackgroundActors( Actor layer, int count, BitmapImage distanceField, const Dali::Vector2& size )
885 for( int i = 0; i < count; ++i )
887 float randSize = Random::Range( 10.0f, 400.0f );
888 float hue = Random::Range( 0.3f, 1.0f );
889 Vector4 randColour( hue, hue*0.5, 0.0f, Random::Range( 0.3f, 0.6f ));
891 ImageActor dfActor = ImageActor::New( distanceField );
892 mBackgroundActors.push_back( dfActor );
893 dfActor.SetSize( Vector2( randSize, randSize ) );
894 dfActor.SetParentOrigin( ParentOrigin::CENTER );
896 Toolkit::DistanceFieldEffect effect = Toolkit::DistanceFieldEffect::New();
897 dfActor.SetShaderEffect( effect );
898 dfActor.SetColor( randColour );
899 effect.SetOutlineParams( Vector2( 0.55f, 0.00f ) );
900 effect.SetSmoothingEdge( 0.5f );
901 layer.Add( dfActor );
905 Random::Range( -size.x * 0.5f * BACKGROUND_SPREAD_SCALE, size.x * 0.5f * BACKGROUND_SPREAD_SCALE ),
906 Random::Range( -size.y * 0.5f - randSize, size.y * 0.5f + randSize ),
907 Random::Range(-1.0f, 0.0f) );
908 dfActor.SetPosition( actorPos );
909 dfActor.SetSortModifier(size.x * 0.7f);
911 Constraint movementConstraint = Constraint::New < Vector3 > ( Actor::POSITION,
912 LocalSource( Actor::SIZE ),
913 ParentSource( Actor::SIZE ),
914 ShapeMovementConstraint );
915 dfActor.ApplyConstraint( movementConstraint );
918 Animation animation = Animation::New( Random::Range( 40.0f, 200.0f ) );
919 KeyFrames keyframes = KeyFrames::New();
920 keyframes.Add( 0.0f, actorPos );
921 Vector3 toPos( actorPos );
922 toPos.y -= ( size.y + randSize );
923 keyframes.Add( 1.0f, toPos );
924 animation.AnimateBetween( Property( dfActor, Actor::POSITION ), keyframes );
925 animation.SetLooping( true );
927 mBackgroundAnimations.push_back( animation );
931 void DaliTableView::CreateShapeImage( ShapeType shapeType, const Size& size, BitmapImage& distanceFieldOut )
933 // this bitmap will hold the alpha map for the distance field shader
934 distanceFieldOut = BitmapImage::New( size.width, size.height, Pixel::A8 );
936 // Generate bit pattern
937 std::vector< unsigned char > imageDataA8;
938 imageDataA8.reserve( size.width * size.height ); // A8
943 GenerateCircle( size, imageDataA8 );
946 GenerateSquare( size, imageDataA8 );
952 GenerateDistanceFieldMap( &imageDataA8[ 0 ], size, distanceFieldOut.GetBuffer(), size, 8.0f, size );
953 distanceFieldOut.Update();
956 void DaliTableView::GenerateSquare( const Size& size, std::vector< unsigned char >& distanceFieldOut )
958 for( int h = 0; h < size.height; ++h )
960 for( int w = 0; w < size.width; ++w )
962 distanceFieldOut.push_back( 0xFF );
967 void DaliTableView::GenerateCircle( const Size& size, std::vector< unsigned char >& distanceFieldOut )
969 const float radius = size.width * 0.5f * size.width * 0.5f;
970 Vector2 center( size.width / 2, size.height / 2 );
972 for( int h = 0; h < size.height; ++h )
974 for( int w = 0; w < size.width; ++w )
977 Vector2 dist = pos - center;
979 if( dist.x * dist.x + dist.y * dist.y > radius )
981 distanceFieldOut.push_back( 0x00 );
985 distanceFieldOut.push_back( 0xFF );
991 ImageActor DaliTableView::CreateLogo( std::string imagePath )
993 Image image = Image::New( imagePath );
994 image.LoadingFinishedSignal().Connect( this, &DaliTableView::OnLogoLoaded );
996 ImageActor logo = ImageActor::New( image );
998 logo.SetAnchorPoint( AnchorPoint::CENTER );
999 logo.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
1004 void DaliTableView::OnLogoLoaded( Dali::Image image )
1006 mRootActor.SetFixedHeight( 1, image.GetHeight() + LOGO_BOTTOM_PADDING_HEIGHT );
1009 bool DaliTableView::PauseBackgroundAnimation()
1016 void DaliTableView::PauseAnimation()
1018 if( mBackgroundAnimsPlaying )
1020 for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
1022 Animation anim = *animIter;
1027 mBackgroundAnimsPlaying = false;
1031 void DaliTableView::PlayAnimation()
1033 if ( !mBackgroundAnimsPlaying )
1035 for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
1037 Animation anim = *animIter;
1042 mBackgroundAnimsPlaying = true;
1045 mAnimationTimer.SetInterval( BACKGROUND_ANIMATION_DURATION );
1048 Dali::Actor DaliTableView::OnKeyboardPreFocusChange( Dali::Actor current, Dali::Actor proposed, Dali::Toolkit::Control::KeyboardFocusNavigationDirection direction )
1050 Actor nextFocusActor = proposed;
1052 if ( !current && !proposed )
1054 // Set the initial focus to the first tile in the current page should be focused.
1055 nextFocusActor = mTableViewList[mScrollView.GetCurrentPage()].GetChildAt(TableView::CellPosition(0, 0));
1057 else if( !proposed || (proposed && proposed == mScrollViewLayer) )
1059 // ScrollView is being focused but nothing in the current page can be focused further
1060 // in the given direction. We should work out which page to scroll to next.
1061 int currentPage = mScrollView.GetCurrentPage();
1062 int newPage = currentPage;
1063 if( direction == Dali::Toolkit::Control::Left )
1067 else if( direction == Dali::Toolkit::Control::Right )
1072 newPage = std::max(0, std::min(static_cast<int>(mScrollRulerX->GetTotalPages() - 1), newPage));
1073 if( newPage == currentPage )
1075 if( direction == Dali::Toolkit::Control::Left )
1077 newPage = mScrollRulerX->GetTotalPages() - 1;
1078 } else if( direction == Dali::Toolkit::Control::Right )
1084 // Scroll to the page in the given direction
1085 mScrollView.ScrollTo(newPage);
1087 if( direction == Dali::Toolkit::Control::Left )
1089 // Work out the cell position for the last tile
1090 int remainingExamples = mExampleList.size() - newPage * EXAMPLES_PER_PAGE;
1091 int rowPos = (remainingExamples >= EXAMPLES_PER_PAGE) ? ROWS_PER_PAGE - 1 : ( (remainingExamples % EXAMPLES_PER_PAGE + EXAMPLES_PER_ROW) / EXAMPLES_PER_ROW - 1 );
1092 int colPos = remainingExamples >= EXAMPLES_PER_PAGE ? EXAMPLES_PER_ROW - 1 : ( remainingExamples % EXAMPLES_PER_PAGE - rowPos * EXAMPLES_PER_ROW - 1 );
1094 // Move the focus to the last tile in the new page.
1095 nextFocusActor = mTableViewList[newPage].GetChildAt(TableView::CellPosition(rowPos, colPos));
1099 // Move the focus to the first tile in the new page.
1100 nextFocusActor = mTableViewList[newPage].GetChildAt(TableView::CellPosition(0, 0));
1104 return nextFocusActor;
1107 void DaliTableView::OnFocusedActorActivated( Dali::Actor activatedActor )
1111 mPressedActor = activatedActor;
1113 // Activate the current focused actor;
1114 TouchEvent touchEventUp;
1115 touchEventUp.points.push_back( TouchPoint ( 0, TouchPoint::Up, 0.0f, 0.0f ) );
1116 OnTilePressed(mPressedActor, touchEventUp);