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-gradient.jpg" );
41 const std::string LOGO_PATH( DALI_IMAGE_DIR "dali-logo.png" );
42 const std::string DEFAULT_TOOLBAR_IMAGE_PATH( DALI_IMAGE_DIR "top-bar.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 char * const DEFAULT_TOOLBAR_TEXT( "TOUCH TO LAUNCH EXAMPLE" );
49 const float BUTTON_PRESS_ANIMATION_TIME = 0.25f; ///< Time to perform button scale effect.
50 const float ROTATE_ANIMATION_TIME = 0.5f; ///< Time to perform rotate effect.
51 const int MAX_PAGES = 256; ///< Maximum pages (arbitrary safety limit)
52 const int EXAMPLES_PER_ROW = 3;
53 const int ROWS_PER_PAGE = 3;
54 const int EXAMPLES_PER_PAGE = EXAMPLES_PER_ROW * ROWS_PER_PAGE;
55 const float TOP_ROW_HEIGHT = 35.0f;
56 const float BOTTOM_ROW_HEIGHT = 35.0f;
57 const int BOTTOM_PADDING_HEIGHT = 40;
58 const int LOGO_BOTTOM_PADDING_HEIGHT = 30;
59 const Vector3 TABLE_RELATIVE_SIZE(0.9f, 1.0f, 0.8f ); ///< TableView's relative size to the entire stage.
60 const float STENCIL_RELATIVE_SIZE = 1.0f;
62 const float EFFECT_SNAP_DURATION = 0.66f; ///< Scroll Snap Duration for Effects
63 const float EFFECT_FLICK_DURATION = 0.5f; ///< Scroll Flick Duration for Effects
64 const Vector3 ANGLE_CUBE_PAGE_ROTATE(Math::PI * 0.5f, Math::PI * 0.5f, 0.0f);
66 const int NUM_BACKGROUND_IMAGES = 20;
67 const float BACKGROUND_SWIPE_SCALE = 0.025f;
68 const float BACKGROUND_SPREAD_SCALE = 1.5f;
69 const float SCALE_MOD = 1000.0f * Math::PI * 2.0f;
70 const float SCALE_SPEED = 10.0f;
71 const float SCALE_SPEED_SIN = 0.1f;
73 const unsigned int BACKGROUND_ANIMATION_DURATION = 15000; // 15 secs
75 const float BACKGROUND_Z = -1000.0f;
76 const float BACKGROUND_SIZE_SCALE = 2.0f;
77 const Vector4 BACKGROUND_COLOR( 1.0f, 1.0f, 1.0f, 1.0f );
80 const std::string DEFAULT_TEXT_STYLE_FONT_FAMILY("HelveticaNeue");
81 const std::string DEFAULT_TEXT_STYLE_FONT_STYLE("Regular");
82 const Dali::PointSize DEFAULT_TEXT_STYLE_POINT_SIZE( 8.0f );
83 const Dali::TextStyle::Weight DEFAULT_TEXT_STYLE_WEIGHT(Dali::TextStyle::REGULAR);
84 const Dali::Vector4 DEFAULT_TEXT_STYLE_COLOR(0.7f, 0.7f, 0.7f, 1.0f);
86 const std::string TABLE_TEXT_STYLE_FONT_FAMILY("HelveticaNeue");
87 const std::string TABLE_TEXT_STYLE_FONT_STYLE("Regular");
88 const Dali::PointSize TABLE_TEXT_STYLE_POINT_SIZE( 8.0f );
89 const Dali::TextStyle::Weight TABLE_TEXT_STYLE_WEIGHT(Dali::TextStyle::LIGHT);
90 const Dali::Vector4 TABLE_TEXT_STYLE_COLOR(0.0f, 0.0f, 0.0f, 1.0f);
92 TextStyle GetDefaultTextStyle()
95 textStyle.SetFontName(DEFAULT_TEXT_STYLE_FONT_FAMILY);
96 textStyle.SetFontStyle(DEFAULT_TEXT_STYLE_FONT_STYLE);
97 textStyle.SetFontPointSize(DEFAULT_TEXT_STYLE_POINT_SIZE);
98 textStyle.SetWeight(DEFAULT_TEXT_STYLE_WEIGHT);
99 textStyle.SetTextColor(DEFAULT_TEXT_STYLE_COLOR);
100 textStyle.SetShadow( true );
104 TextStyle GetTableTextStyle()
107 textStyle.SetFontName(TABLE_TEXT_STYLE_FONT_FAMILY);
108 textStyle.SetFontStyle(TABLE_TEXT_STYLE_FONT_STYLE);
109 textStyle.SetFontPointSize(TABLE_TEXT_STYLE_POINT_SIZE);
110 textStyle.SetWeight(TABLE_TEXT_STYLE_WEIGHT);
111 textStyle.SetTextColor(TABLE_TEXT_STYLE_COLOR);
116 * Creates the background image
118 ImageActor CreateBackground( std::string imagePath )
120 Image image = Image::New( imagePath );
121 ImageActor background = ImageActor::New( image );
123 background.SetAnchorPoint( AnchorPoint::CENTER );
124 background.SetParentOrigin( ParentOrigin::CENTER );
125 background.SetZ( -1.0f );
130 // These values depend on the tile image
131 const float IMAGE_BORDER_LEFT = 11.0f;
132 const float IMAGE_BORDER_RIGHT = IMAGE_BORDER_LEFT;
133 const float IMAGE_BORDER_TOP = IMAGE_BORDER_LEFT;
134 const float IMAGE_BORDER_BOTTOM = IMAGE_BORDER_LEFT;
137 * TableViewVisibilityConstraint
139 struct TableViewVisibilityConstraint
141 bool operator()( const bool& current,
142 const PropertyInput& pagePositionProperty,
143 const PropertyInput& pageSizeProperty )
145 // Only the tableview in the current page should be visible.
146 const Vector3& pagePosition = pagePositionProperty.GetVector3();
147 const Vector3& pageSize = pageSizeProperty.GetVector3();
148 return fabsf( pagePosition.x ) < pageSize.x;
153 * Constraint to wrap an actor in y that is moving vertically
155 Vector3 ShapeMovementConstraint( const Vector3& current,
156 const PropertyInput& shapeSizeProperty,
157 const PropertyInput& parentSizeProperty )
159 const Vector3& shapeSize = shapeSizeProperty.GetVector3();
160 const Vector3& parentSize = parentSizeProperty.GetVector3();
162 Vector3 pos( current );
163 if( pos.y + shapeSize.y * 0.5f < -parentSize.y * 0.5f )
165 pos.y += parentSize.y + shapeSize.y;
172 * Constraint to return a bool value based on the alpha channel value
174 bool AlphaVisibleConstraint( bool current, const PropertyInput& alphaProperty )
176 Vector4 colour = alphaProperty.GetVector4();
177 return ( colour.a > Math::MACHINE_EPSILON_10000 );
181 * Constraint to return a position for the background based on the scroll value
183 struct AnimScrollConstraint
187 AnimScrollConstraint( const Vector3& initialPos, float scale )
188 : mInitialPos( initialPos ),
194 Vector3 operator()( const Vector3& current, const PropertyInput& scrollProperty )
196 float scrollPos = scrollProperty.GetFloat();
198 return mInitialPos + Vector3( -scrollPos * mScale, 0.0f, 0.0f );
207 * Constraint to return a tracked world position added to the constant local position
209 struct TranslateLocalConstraint
213 TranslateLocalConstraint( const Vector3& localPos )
214 : mLocalPos( localPos )
218 Vector3 operator()( const Vector3& current, const PropertyInput& pagePosProperty )
220 Vector3 worldPos = pagePosProperty.GetVector3();
222 return ( worldPos + mLocalPos );
230 bool CompareByTitle( const Example& lhs, const Example& rhs )
232 return lhs.title < rhs.title;
237 DaliTableView::DaliTableView( Application& application )
238 : mApplication( application ),
240 mBackgroundImagePath( DEFAULT_BACKGROUND_IMAGE_PATH ),
241 mSortAlphabetically( false ),
242 mBackgroundAnimsPlaying( false )
244 application.InitSignal().Connect( this, &DaliTableView::Initialize );
247 DaliTableView::~DaliTableView()
251 void DaliTableView::AddExample( Example example )
253 mExampleList.push_back( example );
254 mExampleMap[ example.name ] = example;
257 void DaliTableView::SetBackgroundPath( std::string imagePath )
259 mBackgroundImagePath = imagePath;
262 void DaliTableView::SortAlphabetically( bool sortAlphabetically )
264 mSortAlphabetically = sortAlphabetically;
267 void DaliTableView::Initialize( Application& application )
269 Stage::GetCurrent().KeyEventSignal().Connect( this, &DaliTableView::OnKeyEvent );
271 Vector2 stageSize = Stage::GetCurrent().GetSize();
274 mBackground = CreateBackground( mBackgroundImagePath );
275 // set same size as parent actor
276 mBackground.SetSize( stageSize );
277 Stage::GetCurrent().Add( mBackground );
279 // Render entire content as overlays, as is all on same 2D plane.
280 mRootActor = TableView::New( 4, 1 );
281 mRootActor.SetAnchorPoint( AnchorPoint::CENTER );
282 mRootActor.SetParentOrigin( ParentOrigin::CENTER );
283 mRootActor.SetFixedHeight( 3, BOTTOM_PADDING_HEIGHT );
284 Stage::GetCurrent().Add( mRootActor );
287 CreateToolbar( mRootActor, DEFAULT_TOOLBAR_TEXT, DEFAULT_TOOLBAR_IMAGE_PATH );
290 mLogo = CreateLogo( LOGO_PATH );
291 Alignment alignment = Alignment::New();
292 alignment.Add(mLogo);
293 mRootActor.AddChild( alignment, TableView::CellPosition( 1, 0 ) );
295 // scrollview occupying the majority of the screen
296 mScrollView = ScrollView::New();
298 mScrollView.SetAnchorPoint( AnchorPoint::CENTER );
299 mScrollView.SetParentOrigin( ParentOrigin::CENTER );
300 mScrollView.ApplyConstraint( Dali::Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ), Dali::RelativeToConstraint( TABLE_RELATIVE_SIZE ) ) );
301 mScrollView.SetAxisAutoLock( true );
302 mScrollView.ScrollCompletedSignal().Connect( this, &DaliTableView::OnScrollComplete );
303 mScrollView.ScrollStartedSignal().Connect( this, &DaliTableView::OnScrollStart );
304 mScrollView.TouchedSignal().Connect( this, &DaliTableView::OnScrollTouched );
306 mScrollViewLayer = Layer::New();
307 mScrollViewLayer.SetAnchorPoint( AnchorPoint::CENTER );
308 mScrollViewLayer.SetParentOrigin( ParentOrigin::CENTER );
309 mScrollViewLayer.SetSize( stageSize );
310 mScrollViewLayer.Add( mScrollView );
311 mRootActor.AddChild( mScrollViewLayer, TableView::CellPosition( 2, 0 ) );
313 // Setup the scenegraph
314 // 1) Add scroll view effect and setup constraints on pages
315 ApplyScrollViewEffect();
317 // 2) Add pages and tiles
320 // 3) Populate scrollview with background so constraints on background layers can work with scrollview
321 SetupBackground( mScrollView, stageSize );
323 // 4) Remove constraints for inner cube effect
324 for( TableViewListIter pageIter = mTableViewList.begin(); pageIter != mTableViewList.end(); ++pageIter )
326 TableView page = *pageIter;
328 unsigned int numChildren = page.GetChildCount();
329 Actor pageActor = page;
330 for( unsigned int i=0; i<numChildren; ++i)
332 // Remove old effect's manual constraints.
333 Actor child = pageActor.GetChildAt(i);
336 child.RemoveConstraints();
341 // Set initial orientation
342 unsigned int degrees = application.GetOrientation().GetDegrees();
345 Dali::Window winHandle = application.GetWindow();
346 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
347 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE );
348 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
349 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
351 Dali::Orientation orientation = winHandle.GetOrientation();
352 orientation.ChangedSignal().Connect( this, &DaliTableView::OrientationChanged );
354 winHandle.ShowIndicator( false );
357 mAnimationTimer = Timer::New( BACKGROUND_ANIMATION_DURATION );
358 mAnimationTimer.TickSignal().Connect( this, &DaliTableView::PauseBackgroundAnimation );
359 mAnimationTimer.Start();
360 mBackgroundAnimsPlaying = true;
362 KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &DaliTableView::OnKeyboardPreFocusChange );
363 KeyboardFocusManager::Get().FocusedActorActivatedSignal().Connect( this, &DaliTableView::OnFocusedActorActivated );
366 void DaliTableView::CreateToolbar( TableView root, const std::string& title, const std::string& toolbarImagePath,
367 const ViewStyle& style )
369 // Create default ToolBar
370 Dali::Stage stage = Dali::Stage::GetCurrent();
371 Dali::Vector2 dpi = stage.GetDpi();
373 // Create toolbar layer.
374 Dali::Layer toolBarLayer = Dali::Layer::New();
375 toolBarLayer.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
376 toolBarLayer.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
377 toolBarLayer.ApplyConstraint( Dali::Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ), Dali::SourceWidthFixedHeight( style.mToolBarHeight * dpi.y / style.mDpi ) ) );
378 float toolBarLayerHeight = style.mToolBarHeight * dpi.y / style.mDpi;
379 toolBarLayer.SetSize( 0.0f, toolBarLayerHeight );
381 // Add tool bar layer to the view.
382 root.AddChild( toolBarLayer, TableView::CellPosition( 0, 0 ) );
383 root.SetFixedHeight( 0, toolBarLayerHeight );
385 // Raise tool bar layer to the top.
386 toolBarLayer.RaiseToTop();
389 Dali::Image image = Dali::Image::New( toolbarImagePath );
390 Dali::ImageActor toolBarBackground = Dali::ImageActor::New( image );
391 Dali::Toolkit::ToolBar toolBar = Dali::Toolkit::ToolBar::New();
392 toolBar.SetBackground( toolBarBackground );
393 toolBar.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
394 toolBar.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
395 toolBar.ApplyConstraint( Dali::Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ), Dali::EqualToConstraint() ) );
396 toolBar.SetSize( 0.0f, style.mToolBarHeight * dpi.y / style.mDpi );
397 toolBarBackground.SetZ( -1.0f );
399 // Add the tool bar to the too bar layer.
400 toolBarLayer.Add( toolBar );
402 Dali::Font font = Dali::Font::New();
407 Dali::Toolkit::TextView titleActor = Dali::Toolkit::TextView::New();
408 titleActor.SetName( "ToolbarTitle" );
409 titleActor.SetText( title );
410 titleActor.SetSize( font.MeasureText( title ) );
411 titleActor.SetStyleToCurrentText( GetDefaultTextStyle() );
412 titleActor.SetZ( 1.0f );
414 // Add title to the tool bar.
415 const float padding( style.mToolBarPadding * dpi.x / style.mDpi );
416 toolBar.AddControl( titleActor, style.mToolBarTitlePercentage, Dali::Toolkit::Alignment::HorizontalCenter,
417 Dali::Toolkit::Alignment::Padding( padding, padding, padding, padding ) );
421 void DaliTableView::Populate()
423 const Vector2 stageSize = Stage::GetCurrent().GetSize();
425 const Size demoTileSize( 0.25f * stageSize.width, 0.25f * stageSize.height );
427 mTotalPages = ( mExampleList.size() + EXAMPLES_PER_PAGE - 1 ) / EXAMPLES_PER_PAGE;
429 // Populate ScrollView.
430 if( mExampleList.size() > 0 )
432 if( mSortAlphabetically )
434 sort( mExampleList.begin(), mExampleList.end(), CompareByTitle );
437 unsigned int exampleCount = 0;
438 ExampleListConstIter iter = mExampleList.begin();
439 for( int t = 0; t < mTotalPages; t++ )
441 // Create Table. (contains up to 9 Examples)
442 TableView tableView = TableView::New( 4, 3 );
443 // Add tableView to container.
444 mScrollView.Add( tableView );
445 ApplyEffectToPage( tableView, TABLE_RELATIVE_SIZE );
447 tableView.SetAnchorPoint( AnchorPoint::CENTER );
448 tableView.SetParentOrigin( ParentOrigin::CENTER );
449 // 2 pixels of padding
450 tableView.SetCellPadding( Size( 2.0f, 2.0f ) );
452 Constraint constraint = Constraint::New<Vector3>( Actor::SCALE,
453 LocalSource( Actor::SIZE ),
454 ParentSource( Actor::SIZE ),
455 ScaleToFitConstraint() );
456 tableView.ApplyConstraint(constraint);
458 // Apply visibility constraint to table view
459 Constraint visibleConstraint = Constraint::New< bool >( Actor::VISIBLE,
460 LocalSource( Actor::POSITION ),
461 ParentSource( Actor::SIZE ),
462 TableViewVisibilityConstraint() );
463 visibleConstraint.SetRemoveAction( Constraint::Discard );
464 tableView.ApplyConstraint( visibleConstraint );
466 // add cells to table
467 for( int y = 0; y < ROWS_PER_PAGE; y++ )
469 for( int x = 0; x < EXAMPLES_PER_ROW; x++ )
471 const Example& example = ( *iter );
473 Actor tile = CreateTile( example.name, example.title, demoTileSize, true );
474 FocusManager focusManager = FocusManager::Get();
475 focusManager.SetFocusOrder( tile, ++exampleCount );
476 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_LABEL,
478 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_TRAIT, "Tile" );
479 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_HINT,
480 "You can run this example" );
482 tableView.AddChild( tile, TableView::CellPosition( y, x ) );
485 if( iter == mExampleList.end() )
490 if( iter == mExampleList.end() )
497 tableView.SetFixedHeight( 3, BOTTOM_ROW_HEIGHT );
499 std::stringstream out;
500 out << ( t + 1 ) << " of " << mTotalPages;
501 Actor pageNumberText = CreateTile( "", out.str(), Size( 0.8f * stageSize.width, BOTTOM_ROW_HEIGHT ), false );
503 pageNumberText.ApplyConstraint( Constraint::New< Vector3 >( Actor::POSITION, Source( tableView, Actor::WORLD_POSITION),
504 TranslateLocalConstraint( Vector3( 0.0f, stageSize.y * 0.4f, 0.0f ) ) ) );
505 pageNumberText.ApplyConstraint( Constraint::New< Quaternion >( Actor::ROTATION, Source( tableView, Actor::WORLD_ROTATION ), EqualToConstraint() ) );
506 pageNumberText.ApplyConstraint( Constraint::New< Vector4 >( Actor::COLOR, Source( tableView, Actor::COLOR ), EqualToConstraint() ) );
508 //Stage::GetCurrent().Add( pageNumberText );
510 // Set tableview position
511 Vector3 tableViewPos( stageSize.x * TABLE_RELATIVE_SIZE.x * t, 0.0f, 0.0f );
512 tableView.SetPosition( tableViewPos );
514 mTableViewList.push_back( tableView );
516 if( iter == mExampleList.end() )
523 // Update Ruler info.
524 mScrollRulerX = new FixedRuler( stageSize.width * TABLE_RELATIVE_SIZE.x );
525 mScrollRulerY = new DefaultRuler();
526 mScrollRulerX->SetDomain( RulerDomain( 0.0f, mTotalPages * stageSize.width * TABLE_RELATIVE_SIZE.x, true ) );
527 mScrollRulerY->Disable();
528 mScrollView.SetRulerX( mScrollRulerX );
529 mScrollView.SetRulerY( mScrollRulerY );
532 void DaliTableView::OrientationChanged( Orientation orientation )
534 // TODO: Implement if orientation change required
537 void DaliTableView::Rotate( unsigned int degrees )
539 // Resize the root actor
540 Vector2 stageSize = Stage::GetCurrent().GetSize();
541 Vector3 targetSize( stageSize.x, stageSize.y, 1.0f );
543 if( degrees == 90 || degrees == 270 )
545 targetSize = Vector3( stageSize.y, stageSize.x, 1.0f );
548 if( mRotateAnimation )
550 mRotateAnimation.Stop();
551 mRotateAnimation.Clear();
554 mRotateAnimation = Animation::New( ROTATE_ANIMATION_TIME );
555 mRotateAnimation.RotateTo( mRootActor, Degree( 360 - degrees ), Vector3::ZAXIS, AlphaFunctions::EaseOut );
556 mRotateAnimation.Resize( mRootActor, targetSize, AlphaFunctions::EaseOut );
557 mRotateAnimation.Play();
560 Actor DaliTableView::CreateTile( const string& name, const string& title, const Size& parentSize, bool addBackground )
562 Actor tile = Actor::New();
563 tile.SetName( name );
564 tile.SetAnchorPoint( AnchorPoint::CENTER );
565 tile.SetParentOrigin( ParentOrigin::CENTER );
567 // make the tile 100% of parent
568 tile.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
570 Actor content = Actor::New();
571 content.SetAnchorPoint( AnchorPoint::CENTER );
572 content.SetParentOrigin( ParentOrigin::CENTER );
573 content.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
576 // create background image
579 Image bg = Image::New( TILE_BACKGROUND );
580 ImageActor image = ImageActor::New( bg );
581 image.SetAnchorPoint( AnchorPoint::CENTER );
582 image.SetParentOrigin( ParentOrigin::CENTER );
583 // make the image 100% of tile
584 image.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
585 // move image back to get text appear in front
587 image.SetStyle( ImageActor::STYLE_NINE_PATCH );
588 image.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
590 content.Add( image );
593 ImageActor stencil = NewStencilImage();
594 stencil.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
595 image.Add( stencil );
598 TextView text = TextView::New( title );
599 text.SetAnchorPoint( AnchorPoint::CENTER );
600 text.SetParentOrigin( ParentOrigin::CENTER );
601 text.SetWidthExceedPolicy( Toolkit::TextView::ShrinkToFit );
602 text.SetMultilinePolicy( Toolkit::TextView::SplitByWord );
603 text.SetLineJustification( Toolkit::TextView::Center );
604 text.SetTextAlignment( Toolkit::Alignment::Type( Alignment::HorizontalCenter | Alignment::VerticalCenter ) );
605 text.SetColor( Color::WHITE );
607 // make the text 90% of tile
608 text.SetSize( 0.9f * parentSize.width, 0.9f * parentSize.height );
609 text.SetStyleToCurrentText( GetTableTextStyle() );
610 text.SetSnapshotModeEnabled( false );
613 // Set the tile to be keyboard focusable
614 tile.SetKeyboardFocusable(true);
616 // connect to the touch events
617 tile.TouchedSignal().Connect( this, &DaliTableView::OnTilePressed );
622 ImageActor DaliTableView::NewStencilImage()
624 Image alpha = Image::New( TILE_BACKGROUND_ALPHA );
626 ImageActor stencilActor = ImageActor::New( alpha );
627 stencilActor.SetStyle( ImageActor::STYLE_NINE_PATCH );
628 stencilActor.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
630 stencilActor.SetParentOrigin( ParentOrigin::CENTER );
631 stencilActor.SetAnchorPoint( AnchorPoint::CENTER );
632 stencilActor.SetDrawMode( DrawMode::STENCIL );
634 Dali::ShaderEffect shaderEffect = AlphaDiscardEffect::New();
635 stencilActor.SetShaderEffect( shaderEffect );
640 bool DaliTableView::OnTilePressed( Actor actor, const TouchEvent& event )
642 bool consumed = false;
644 const TouchPoint& point = event.GetPoint( 0 );
645 if( TouchPoint::Down == point.state )
647 mPressedActor = actor;
651 // A button press is only valid if the Down & Up events
652 // both occurred within the button.
653 if( ( TouchPoint::Up == point.state ) &&
654 ( mPressedActor == actor ) )
656 std::string name = actor.GetName();
657 ExampleMapConstIter iter = mExampleMap.find( name );
659 FocusManager focusManager = FocusManager::Get();
661 if( iter != mExampleMap.end() )
663 // ignore Example button presses when scrolling or button animating.
664 if( ( !mScrolling ) && ( !mPressedAnimation ) )
666 // do nothing, until pressed animation finished.
673 mPressedAnimation = Animation::New( BUTTON_PRESS_ANIMATION_TIME );
674 mPressedAnimation.SetDestroyAction( Animation::Discard );
676 // scale the content actor within the Tile, as to not affect the placement within the Table.
677 Actor content = actor.GetChildAt(0);
678 mPressedAnimation.ScaleTo( content, Vector3( 0.9f, 0.9f, 1.0f ), AlphaFunctions::EaseInOut, 0.0f,
679 BUTTON_PRESS_ANIMATION_TIME * 0.5f );
680 mPressedAnimation.ScaleTo( content, Vector3::ONE, AlphaFunctions::EaseInOut, BUTTON_PRESS_ANIMATION_TIME * 0.5f,
681 BUTTON_PRESS_ANIMATION_TIME * 0.5f );
682 mPressedAnimation.Play();
683 mPressedAnimation.FinishedSignal().Connect( this, &DaliTableView::OnPressedAnimationFinished );
689 void DaliTableView::OnPressedAnimationFinished( Dali::Animation& source )
691 mPressedAnimation.Reset();
694 std::string name = mPressedActor.GetName();
695 ExampleMapConstIter iter = mExampleMap.find( name );
697 if( iter == mExampleMap.end() )
699 if( name == BUTTON_QUIT )
701 // Move focus to the OK button
702 FocusManager focusManager = FocusManager::Get();
704 // Enable the group mode and wrap mode
705 focusManager.SetGroupMode( true );
706 focusManager.SetWrapMode( true );
711 const Example& example( iter->second );
712 aul_open_app( example.name.c_str() );
714 mPressedActor.Reset();
718 void DaliTableView::OnScrollStart( const Dali::Vector3& position )
725 void DaliTableView::OnScrollComplete( const Dali::Vector3& position )
729 // move focus to 1st item of new page
730 FocusManager focusManager = FocusManager::Get();
731 focusManager.SetCurrentFocusActor(mTableViewList[mScrollView.GetCurrentPage()].GetChildAt(TableView::CellPosition(1, 0)) );
735 bool DaliTableView::OnScrollTouched( Actor actor, const TouchEvent& event )
737 const TouchPoint& point = event.GetPoint( 0 );
738 if( TouchPoint::Down == point.state )
740 mPressedActor = actor;
746 void DaliTableView::ApplyScrollViewEffect()
748 // Remove old effect if exists.
750 if( mScrollViewEffect )
752 mScrollView.RemoveEffect( mScrollViewEffect );
755 // Just one effect for now
756 SetupInnerPageCubeEffect();
758 mScrollView.ApplyEffect( mScrollViewEffect );
761 void DaliTableView::SetupInnerPageCubeEffect()
763 ScrollViewCustomEffect customEffect;
764 mScrollViewEffect = customEffect = ScrollViewCustomEffect::New();
765 mScrollView.SetScrollSnapDuration( EFFECT_SNAP_DURATION );
766 mScrollView.SetScrollFlickDuration( EFFECT_FLICK_DURATION );
767 mScrollView.SetScrollSnapAlphaFunction( AlphaFunctions::EaseOutBack );
768 mScrollView.SetScrollFlickAlphaFunction( AlphaFunctions::EaseOutBack );
769 mScrollView.RemoveConstraintsFromChildren();
771 customEffect.SetPageSpacing( Vector2( 30.0f, 30.0f ) );
772 customEffect.SetAngledOriginPageRotation( ANGLE_CUBE_PAGE_ROTATE );
773 customEffect.SetSwingAngle( ANGLE_CUBE_PAGE_ROTATE.x, Vector3( 0, -1, 0 ) );
774 customEffect.SetOpacityThreshold( 0.5f ); // Make fade out on edges
777 void DaliTableView::ApplyEffectToPage( Actor page, const Vector3& tableRelativeSize )
779 page.RemoveConstraints();
781 Constraint constraint = Constraint::New<Vector3>( Actor::SCALE,
782 LocalSource( Actor::SIZE ),
783 ParentSource( Actor::SIZE ),
784 ScaleToFitConstraint() );
785 page.ApplyConstraint(constraint);
787 ApplyCustomEffectToPage( page );
790 void DaliTableView::ApplyCustomEffectToPage( Actor page )
792 ScrollViewCustomEffect customEffect = ScrollViewCustomEffect::DownCast( mScrollViewEffect );
793 Vector2 vStageSize( Stage::GetCurrent().GetSize() );
794 customEffect.ApplyToPage( page, Vector3( vStageSize.x, vStageSize.y, 1.0f ) );
797 void DaliTableView::OnKeyEvent( const KeyEvent& event )
799 if( event.state == KeyEvent::Down )
801 if ( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
808 Actor CreateBackgroundActor( const Vector2& size )
810 Actor layer = Actor::New();
811 layer.SetAnchorPoint( AnchorPoint::CENTER );
812 layer.SetParentOrigin( ParentOrigin::CENTER );
813 layer.SetSize( size );
817 void DaliTableView::SetupBackground( Actor addToLayer, const Vector2& size )
819 // Create distance field shape
820 BitmapImage distanceField;
821 Size imageSize( 512, 512 );
822 CreateShapeImage( CIRCLE, imageSize, distanceField );
825 Actor backgroundAnimLayer0 = CreateBackgroundActor( size );
826 Actor backgroundAnimLayer1 = CreateBackgroundActor( size );
827 Actor backgroundAnimLayer2 = CreateBackgroundActor( size );
830 Constraint animConstraint0 = Constraint::New < Vector3 > ( Actor::POSITION,
831 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_X_PROPERTY_NAME ) ),
832 AnimScrollConstraint( backgroundAnimLayer0.GetCurrentPosition(), 0.75f ) );
833 backgroundAnimLayer0.ApplyConstraint( animConstraint0 );
835 Constraint animConstraint1 = Constraint::New < Vector3 > ( Actor::POSITION,
836 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_X_PROPERTY_NAME ) ),
837 AnimScrollConstraint( backgroundAnimLayer1.GetCurrentPosition(), 0.5f ) );
838 backgroundAnimLayer1.ApplyConstraint( animConstraint1 );
840 Constraint animConstraint2 = Constraint::New < Vector3 > ( Actor::POSITION,
841 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_X_PROPERTY_NAME ) ),
842 AnimScrollConstraint( backgroundAnimLayer2.GetCurrentPosition(), 0.25f ) );
843 backgroundAnimLayer2.ApplyConstraint( animConstraint2 );
846 ImageActor layer = Dali::Toolkit::CreateSolidColorActor( BACKGROUND_COLOR );
847 layer.SetAnchorPoint( AnchorPoint::CENTER );
848 layer.SetParentOrigin( ParentOrigin::CENTER );
849 layer.SetSize( size * BACKGROUND_SIZE_SCALE );
850 layer.SetZ( BACKGROUND_Z );
851 layer.SetPositionInheritanceMode( DONT_INHERIT_POSITION );
853 addToLayer.Add( layer );
856 addToLayer.Add( backgroundAnimLayer0 );
857 addToLayer.Add( backgroundAnimLayer1 );
858 addToLayer.Add( backgroundAnimLayer2 );
860 // Add all the children
861 AddBackgroundActors( backgroundAnimLayer0, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
862 AddBackgroundActors( backgroundAnimLayer1, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
863 AddBackgroundActors( backgroundAnimLayer2, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
866 void DaliTableView::AddBackgroundActors( Actor layer, int count, BitmapImage distanceField, const Dali::Vector2& size )
868 for( int i = 0; i < count; ++i )
870 float randSize = Random::Range( 10.0f, 400.0f );
871 float hue = Random::Range( 0.3f, 1.0f );
872 Vector4 randColour( hue, hue*0.5, 0.0f, Random::Range( 0.3f, 0.6f ));
874 ImageActor dfActor = ImageActor::New( distanceField );
875 mBackgroundActors.push_back( dfActor );
876 dfActor.SetSize( Vector2( randSize, randSize ) );
877 dfActor.SetParentOrigin( ParentOrigin::CENTER );
879 Toolkit::DistanceFieldEffect effect = Toolkit::DistanceFieldEffect::New();
880 dfActor.SetShaderEffect( effect );
881 dfActor.SetColor( randColour );
882 effect.SetOutlineParams( Vector2( 0.55f, 0.00f ) );
883 effect.SetSmoothingEdge( 0.5f );
884 layer.Add( dfActor );
888 Random::Range( -size.x * 0.5f * BACKGROUND_SPREAD_SCALE, size.x * 0.5f * BACKGROUND_SPREAD_SCALE ),
889 Random::Range( -size.y * 0.5f - randSize, size.y * 0.5f + randSize ),
890 Random::Range(-1.0f, 0.0f) );
891 dfActor.SetPosition( actorPos );
892 dfActor.SetSortModifier(size.x * 0.7f);
894 Constraint movementConstraint = Constraint::New < Vector3 > ( Actor::POSITION,
895 LocalSource( Actor::SIZE ),
896 ParentSource( Actor::SIZE ),
897 ShapeMovementConstraint );
898 dfActor.ApplyConstraint( movementConstraint );
901 Animation animation = Animation::New( Random::Range( 40.0f, 200.0f ) );
902 KeyFrames keyframes = KeyFrames::New();
903 keyframes.Add( 0.0f, actorPos );
904 Vector3 toPos( actorPos );
905 toPos.y -= ( size.y + randSize );
906 keyframes.Add( 1.0f, toPos );
907 animation.AnimateBetween( Property( dfActor, Actor::POSITION ), keyframes );
908 animation.SetLooping( true );
910 mBackgroundAnimations.push_back( animation );
914 void DaliTableView::CreateShapeImage( ShapeType shapeType, const Size& size, BitmapImage& distanceFieldOut )
916 // this bitmap will hold the alpha map for the distance field shader
917 distanceFieldOut = BitmapImage::New( size.width, size.height, Pixel::A8 );
919 // Generate bit pattern
920 std::vector< unsigned char > imageDataA8;
921 imageDataA8.reserve( size.width * size.height ); // A8
926 GenerateCircle( size, imageDataA8 );
929 GenerateSquare( size, imageDataA8 );
935 GenerateDistanceFieldMap( &imageDataA8[ 0 ], size, distanceFieldOut.GetBuffer(), size, 8.0f, size );
936 distanceFieldOut.Update();
939 void DaliTableView::GenerateSquare( const Size& size, std::vector< unsigned char >& distanceFieldOut )
941 for( int h = 0; h < size.height; ++h )
943 for( int w = 0; w < size.width; ++w )
945 distanceFieldOut.push_back( 0xFF );
950 void DaliTableView::GenerateCircle( const Size& size, std::vector< unsigned char >& distanceFieldOut )
952 const float radius = size.width * 0.5f * size.width * 0.5f;
953 Vector2 center( size.width / 2, size.height / 2 );
955 for( int h = 0; h < size.height; ++h )
957 for( int w = 0; w < size.width; ++w )
960 Vector2 dist = pos - center;
962 if( dist.x * dist.x + dist.y * dist.y > radius )
964 distanceFieldOut.push_back( 0x00 );
968 distanceFieldOut.push_back( 0xFF );
974 ImageActor DaliTableView::CreateLogo( std::string imagePath )
976 Image image = Image::New( imagePath );
977 image.LoadingFinishedSignal().Connect( this, &DaliTableView::OnLogoLoaded );
979 ImageActor logo = ImageActor::New( image );
981 logo.SetAnchorPoint( AnchorPoint::CENTER );
982 logo.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
987 void DaliTableView::OnLogoLoaded( Dali::Image image )
989 mRootActor.SetFixedHeight( 1, image.GetHeight() + LOGO_BOTTOM_PADDING_HEIGHT );
992 bool DaliTableView::PauseBackgroundAnimation()
999 void DaliTableView::PauseAnimation()
1001 if( mBackgroundAnimsPlaying )
1003 for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
1005 Animation anim = *animIter;
1010 mBackgroundAnimsPlaying = false;
1014 void DaliTableView::PlayAnimation()
1016 if ( !mBackgroundAnimsPlaying )
1018 for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
1020 Animation anim = *animIter;
1025 mBackgroundAnimsPlaying = true;
1028 mAnimationTimer.SetInterval( BACKGROUND_ANIMATION_DURATION );
1031 Dali::Actor DaliTableView::OnKeyboardPreFocusChange( Dali::Actor current, Dali::Actor proposed, Dali::Toolkit::Control::KeyboardFocusNavigationDirection direction )
1033 Actor nextFocusActor = proposed;
1035 if ( !current && !proposed )
1037 // Set the initial focus to the first tile in the current page should be focused.
1038 nextFocusActor = mTableViewList[mScrollView.GetCurrentPage()].GetChildAt(TableView::CellPosition(0, 0));
1040 else if( !proposed || (proposed && proposed == mScrollViewLayer) )
1042 // ScrollView is being focused but nothing in the current page can be focused further
1043 // in the given direction. We should work out which page to scroll to next.
1044 int currentPage = mScrollView.GetCurrentPage();
1045 int newPage = currentPage;
1046 if( direction == Dali::Toolkit::Control::Left )
1050 else if( direction == Dali::Toolkit::Control::Right )
1055 newPage = std::max(0, std::min(static_cast<int>(mScrollRulerX->GetTotalPages() - 1), newPage));
1056 if( newPage == currentPage )
1058 if( direction == Dali::Toolkit::Control::Left )
1060 newPage = mScrollRulerX->GetTotalPages() - 1;
1061 } else if( direction == Dali::Toolkit::Control::Right )
1067 // Scroll to the page in the given direction
1068 mScrollView.ScrollTo(newPage);
1070 if( direction == Dali::Toolkit::Control::Left )
1072 // Work out the cell position for the last tile
1073 int remainingExamples = mExampleList.size() - newPage * EXAMPLES_PER_PAGE;
1074 int rowPos = (remainingExamples >= EXAMPLES_PER_PAGE) ? ROWS_PER_PAGE - 1 : ( (remainingExamples % EXAMPLES_PER_PAGE + EXAMPLES_PER_ROW) / EXAMPLES_PER_ROW - 1 );
1075 int colPos = remainingExamples >= EXAMPLES_PER_PAGE ? EXAMPLES_PER_ROW - 1 : ( remainingExamples % EXAMPLES_PER_PAGE - rowPos * EXAMPLES_PER_ROW - 1 );
1077 // Move the focus to the last tile in the new page.
1078 nextFocusActor = mTableViewList[newPage].GetChildAt(TableView::CellPosition(rowPos, colPos));
1082 // Move the focus to the first tile in the new page.
1083 nextFocusActor = mTableViewList[newPage].GetChildAt(TableView::CellPosition(0, 0));
1087 return nextFocusActor;
1090 void DaliTableView::OnFocusedActorActivated( Dali::Actor activatedActor )
1094 mPressedActor = activatedActor;
1096 // Activate the current focused actor;
1097 TouchEvent touchEventUp;
1098 touchEventUp.points.push_back( TouchPoint ( 0, TouchPoint::Up, 0.0f, 0.0f ) );
1099 OnTilePressed(mPressedActor, touchEventUp);