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"
27 using namespace Dali::Toolkit;
30 ///////////////////////////////////////////////////////////////////////////////
35 const std::string BUTTON_BACKWARD( "Backward" );
36 const std::string BUTTON_FORWARD( "Forward" );
37 const std::string BUTTON_QUIT( "Quit" );
38 const std::string BUTTON_OK( "Ok" );
39 const std::string BUTTON_CANCEL( "Cancel" );
41 const std::string DEFAULT_BACKGROUND_IMAGE_PATH( DALI_IMAGE_DIR "background-gradient.jpg" );
42 const std::string LOGO_PATH( DALI_IMAGE_DIR "dali-logo.png" );
43 const std::string DEFAULT_TOOLBAR_IMAGE_PATH( DALI_IMAGE_DIR "top-bar.png" );
44 const std::string BUTTON_BACKGROUND(DALI_IMAGE_DIR "button-background.png");
45 const std::string TILE_BACKGROUND(DALI_IMAGE_DIR "item-background.png");
46 const std::string TILE_BACKGROUND_ALPHA(DALI_IMAGE_DIR "item-background-alpha.png");
48 const char * const DEFAULT_TOOLBAR_TEXT( "TOUCH TO LAUNCH EXAMPLE" );
50 const float BUTTON_PRESS_ANIMATION_TIME = 0.25f; ///< Time to perform button scale effect.
51 const float ROTATE_ANIMATION_TIME = 0.5f; ///< Time to perform rotate effect.
52 const int MAX_PAGES = 256; ///< Maximum pages (arbitrary safety limit)
53 const int EXAMPLES_PER_ROW = 3;
54 const int ROWS_PER_PAGE = 3;
55 const int EXAMPLES_PER_PAGE = EXAMPLES_PER_ROW * ROWS_PER_PAGE;
56 const float TOP_ROW_HEIGHT = 35.0f;
57 const float BOTTOM_ROW_HEIGHT = 35.0f;
58 const int BOTTOM_PADDING_HEIGHT = 40;
59 const int LOGO_BOTTOM_PADDING_HEIGHT = 30;
60 const Vector3 TABLE_RELATIVE_SIZE(0.9f, 1.0f, 0.8f ); ///< TableView's relative size to the entire stage.
61 const float STENCIL_RELATIVE_SIZE = 1.0f;
63 const float EFFECT_SNAP_DURATION = 0.66f; ///< Scroll Snap Duration for Effects
64 const float EFFECT_FLICK_DURATION = 0.5f; ///< Scroll Flick Duration for Effects
65 const Vector3 ANGLE_CUBE_PAGE_ROTATE(Math::PI * 0.5f, Math::PI * 0.5f, 0.0f);
67 const int NUM_BACKGROUND_IMAGES = 20;
68 const float BACKGROUND_SWIPE_SCALE = 0.025f;
69 const float BACKGROUND_SPREAD_SCALE = 1.5f;
70 const float SCALE_MOD = 1000.0f * Math::PI * 2.0f;
71 const float SCALE_SPEED = 10.0f;
72 const float SCALE_SPEED_SIN = 0.1f;
74 const unsigned int BACKGROUND_ANIMATION_DURATION = 15000; // 15 secs
76 const float BACKGROUND_Z = -1000.0f;
77 const float BACKGROUND_SIZE_SCALE = 2.0f;
78 const Vector4 BACKGROUND_COLOR( 1.0f, 1.0f, 1.0f, 1.0f );
81 const std::string DEFAULT_TEXT_STYLE_FONT_FAMILY("HelveticaNeue");
82 const std::string DEFAULT_TEXT_STYLE_FONT_STYLE("Regular");
83 const Dali::PointSize DEFAULT_TEXT_STYLE_POINT_SIZE( 8.0f );
84 const Dali::TextStyle::Weight DEFAULT_TEXT_STYLE_WEIGHT(Dali::TextStyle::REGULAR);
85 const Dali::Vector4 DEFAULT_TEXT_STYLE_COLOR(0.7f, 0.7f, 0.7f, 1.0f);
87 const std::string TABLE_TEXT_STYLE_FONT_FAMILY("HelveticaNeue");
88 const std::string TABLE_TEXT_STYLE_FONT_STYLE("Regular");
89 const Dali::PointSize TABLE_TEXT_STYLE_POINT_SIZE( 8.0f );
90 const Dali::TextStyle::Weight TABLE_TEXT_STYLE_WEIGHT(Dali::TextStyle::LIGHT);
91 const Dali::Vector4 TABLE_TEXT_STYLE_COLOR(0.0f, 0.0f, 0.0f, 1.0f);
93 TextStyle GetDefaultTextStyle()
96 textStyle.SetFontName(DEFAULT_TEXT_STYLE_FONT_FAMILY);
97 textStyle.SetFontStyle(DEFAULT_TEXT_STYLE_FONT_STYLE);
98 textStyle.SetFontPointSize(DEFAULT_TEXT_STYLE_POINT_SIZE);
99 textStyle.SetWeight(DEFAULT_TEXT_STYLE_WEIGHT);
100 textStyle.SetTextColor(DEFAULT_TEXT_STYLE_COLOR);
101 textStyle.SetShadow( true );
105 TextStyle GetTableTextStyle()
108 textStyle.SetFontName(TABLE_TEXT_STYLE_FONT_FAMILY);
109 textStyle.SetFontStyle(TABLE_TEXT_STYLE_FONT_STYLE);
110 textStyle.SetFontPointSize(TABLE_TEXT_STYLE_POINT_SIZE);
111 textStyle.SetWeight(TABLE_TEXT_STYLE_WEIGHT);
112 textStyle.SetTextColor(TABLE_TEXT_STYLE_COLOR);
117 * Creates the background image
119 ImageActor CreateBackground( std::string imagePath )
121 Image image = Image::New( imagePath );
122 ImageActor background = ImageActor::New( image );
124 background.SetAnchorPoint( AnchorPoint::CENTER );
125 background.SetParentOrigin( ParentOrigin::CENTER );
126 background.SetZ( -1.0f );
131 // These values depend on the tile image
132 const float IMAGE_BORDER_LEFT = 11.0f;
133 const float IMAGE_BORDER_RIGHT = IMAGE_BORDER_LEFT;
134 const float IMAGE_BORDER_TOP = IMAGE_BORDER_LEFT;
135 const float IMAGE_BORDER_BOTTOM = IMAGE_BORDER_LEFT;
138 * TableViewVisibilityConstraint
140 struct TableViewVisibilityConstraint
142 bool operator()( const bool& current,
143 const PropertyInput& pagePositionProperty,
144 const PropertyInput& pageSizeProperty )
146 // Only the tableview in the current page should be visible.
147 const Vector3& pagePosition = pagePositionProperty.GetVector3();
148 const Vector3& pageSize = pageSizeProperty.GetVector3();
149 return fabsf( pagePosition.x ) < pageSize.x;
154 * Constraint to wrap an actor in y that is moving vertically
156 Vector3 ShapeMovementConstraint( const Vector3& current,
157 const PropertyInput& shapeSizeProperty,
158 const PropertyInput& parentSizeProperty )
160 const Vector3& shapeSize = shapeSizeProperty.GetVector3();
161 const Vector3& parentSize = parentSizeProperty.GetVector3();
163 Vector3 pos( current );
164 if( pos.y + shapeSize.y * 0.5f < -parentSize.y * 0.5f )
166 pos.y += parentSize.y + shapeSize.y;
173 * Constraint to return a bool value based on the alpha channel value
175 bool AlphaVisibleConstraint( bool current, const PropertyInput& alphaProperty )
177 Vector4 colour = alphaProperty.GetVector4();
178 return ( colour.a > Math::MACHINE_EPSILON_10000 );
182 * Constraint to return a position for the background based on the scroll value
184 struct AnimScrollConstraint
188 AnimScrollConstraint( const Vector3& initialPos, float scale )
189 : mInitialPos( initialPos ),
195 Vector3 operator()( const Vector3& current, const PropertyInput& scrollProperty )
197 float scrollPos = scrollProperty.GetVector3().x;
199 return mInitialPos + Vector3( -scrollPos * mScale, 0.0f, 0.0f );
208 * Constraint to return a tracked world position added to the constant local position
210 struct TranslateLocalConstraint
214 TranslateLocalConstraint( const Vector3& localPos )
215 : mLocalPos( localPos )
219 Vector3 operator()( const Vector3& current, const PropertyInput& pagePosProperty )
221 Vector3 worldPos = pagePosProperty.GetVector3();
223 return ( worldPos + mLocalPos );
231 bool CompareByTitle( const Example& lhs, const Example& rhs )
233 return lhs.title < rhs.title;
238 DaliTableView::DaliTableView( Application& application )
239 : mApplication( application ),
241 mBackgroundImagePath( DEFAULT_BACKGROUND_IMAGE_PATH ),
242 mSortAlphabetically( false ),
243 mBackgroundAnimsPlaying( false )
245 application.InitSignal().Connect( this, &DaliTableView::Initialize );
248 DaliTableView::~DaliTableView()
252 void DaliTableView::AddExample( Example example )
254 mExampleList.push_back( example );
255 mExampleMap[ example.name ] = example;
258 void DaliTableView::SetBackgroundPath( std::string imagePath )
260 mBackgroundImagePath = imagePath;
263 void DaliTableView::SortAlphabetically( bool sortAlphabetically )
265 mSortAlphabetically = sortAlphabetically;
268 void DaliTableView::Initialize( Application& application )
270 Stage::GetCurrent().KeyEventSignal().Connect( this, &DaliTableView::OnKeyEvent );
272 Vector2 stageSize = Stage::GetCurrent().GetSize();
275 mBackground = CreateBackground( mBackgroundImagePath );
276 // set same size as parent actor
277 mBackground.SetSize( stageSize );
278 Stage::GetCurrent().Add( mBackground );
280 // Render entire content as overlays, as is all on same 2D plane.
281 mRootActor = TableView::New( 4, 1 );
282 mRootActor.SetAnchorPoint( AnchorPoint::CENTER );
283 mRootActor.SetParentOrigin( ParentOrigin::CENTER );
284 mRootActor.SetFixedHeight( 3, BOTTOM_PADDING_HEIGHT );
285 Stage::GetCurrent().Add( mRootActor );
288 CreateToolbar( mRootActor, DEFAULT_TOOLBAR_TEXT, DEFAULT_TOOLBAR_IMAGE_PATH );
291 mLogo = CreateLogo( LOGO_PATH );
292 Alignment alignment = Alignment::New();
293 alignment.Add(mLogo);
294 mRootActor.AddChild( alignment, TableView::CellPosition( 1, 0 ) );
296 // scrollview occupying the majority of the screen
297 mScrollView = ScrollView::New();
299 mScrollView.SetAnchorPoint( AnchorPoint::CENTER );
300 mScrollView.SetParentOrigin( ParentOrigin::CENTER );
301 mScrollView.ApplyConstraint( Dali::Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ), Dali::RelativeToConstraint( TABLE_RELATIVE_SIZE ) ) );
302 mScrollView.SetAxisAutoLock( true );
303 mScrollView.ScrollCompletedSignal().Connect( this, &DaliTableView::OnScrollComplete );
304 mScrollView.ScrollStartedSignal().Connect( this, &DaliTableView::OnScrollStart );
305 mScrollView.TouchedSignal().Connect( this, &DaliTableView::OnScrollTouched );
307 mScrollViewLayer = Layer::New();
308 mScrollViewLayer.SetAnchorPoint( AnchorPoint::CENTER );
309 mScrollViewLayer.SetParentOrigin( ParentOrigin::CENTER );
310 mScrollViewLayer.SetSize( stageSize );
311 mScrollViewLayer.Add( mScrollView );
312 mRootActor.AddChild( mScrollViewLayer, TableView::CellPosition( 2, 0 ) );
314 // Setup the scenegraph
315 // 1) Add scroll view effect and setup constraints on pages
316 ApplyScrollViewEffect();
318 // 2) Add pages and tiles
321 // 3) Populate scrollview with background so constraints on background layers can work with scrollview
322 SetupBackground( mScrollView, stageSize );
324 // 4) Remove constraints for inner cube effect
325 for( TableViewListIter pageIter = mTableViewList.begin(); pageIter != mTableViewList.end(); ++pageIter )
327 TableView page = *pageIter;
329 unsigned int numChildren = page.GetChildCount();
330 Actor pageActor = page;
331 for( unsigned int i=0; i<numChildren; ++i)
333 // Remove old effect's manual constraints.
334 Actor child = pageActor.GetChildAt(i);
337 child.RemoveConstraints();
342 // Set initial orientation
343 unsigned int degrees = application.GetOrientation().GetDegrees();
346 Dali::Window winHandle = application.GetWindow();
347 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
348 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE );
349 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
350 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
352 Dali::Orientation orientation = winHandle.GetOrientation();
353 orientation.ChangedSignal().Connect( this, &DaliTableView::OrientationChanged );
355 winHandle.ShowIndicator( false );
358 mAnimationTimer = Timer::New( BACKGROUND_ANIMATION_DURATION );
359 mAnimationTimer.TickSignal().Connect( this, &DaliTableView::PauseBackgroundAnimation );
360 mAnimationTimer.Start();
361 mBackgroundAnimsPlaying = true;
363 KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &DaliTableView::OnKeyboardPreFocusChange );
364 KeyboardFocusManager::Get().FocusedActorActivatedSignal().Connect( this, &DaliTableView::OnFocusedActorActivated );
367 void DaliTableView::CreateToolbar( TableView root, const std::string& title, const std::string& toolbarImagePath,
368 const ViewStyle& style )
370 // Create default ToolBar
371 Dali::Stage stage = Dali::Stage::GetCurrent();
372 Dali::Vector2 dpi = stage.GetDpi();
374 // Create toolbar layer.
375 Dali::Layer toolBarLayer = Dali::Layer::New();
376 toolBarLayer.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
377 toolBarLayer.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
378 toolBarLayer.ApplyConstraint( Dali::Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ), Dali::SourceWidthFixedHeight( style.mToolBarHeight * dpi.y / style.mDpi ) ) );
379 float toolBarLayerHeight = style.mToolBarHeight * dpi.y / style.mDpi;
380 toolBarLayer.SetSize( 0.0f, toolBarLayerHeight );
382 // Add tool bar layer to the view.
383 root.AddChild( toolBarLayer, TableView::CellPosition( 0, 0 ) );
384 root.SetFixedHeight( 0, toolBarLayerHeight );
386 // Raise tool bar layer to the top.
387 toolBarLayer.RaiseToTop();
390 Dali::Image image = Dali::Image::New( toolbarImagePath );
391 Dali::ImageActor toolBarBackground = Dali::ImageActor::New( image );
392 Dali::Toolkit::ToolBar toolBar = Dali::Toolkit::ToolBar::New();
393 toolBar.SetBackground( toolBarBackground );
394 toolBar.SetParentOrigin( Dali::ParentOrigin::TOP_CENTER );
395 toolBar.SetAnchorPoint( Dali::AnchorPoint::TOP_CENTER );
396 toolBar.ApplyConstraint( Dali::Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ), Dali::EqualToConstraint() ) );
397 toolBar.SetSize( 0.0f, style.mToolBarHeight * dpi.y / style.mDpi );
398 toolBarBackground.SetZ( -1.0f );
400 // Add the tool bar to the too bar layer.
401 toolBarLayer.Add( toolBar );
403 Dali::Font font = Dali::Font::New();
408 Dali::Toolkit::TextView titleActor = Dali::Toolkit::TextView::New();
409 titleActor.SetName( "ToolbarTitle" );
410 titleActor.SetText( title );
411 titleActor.SetSize( font.MeasureText( title ) );
412 titleActor.SetStyleToCurrentText( GetDefaultTextStyle() );
413 titleActor.SetZ( 1.0f );
415 // Add title to the tool bar.
416 const float padding( style.mToolBarPadding * dpi.x / style.mDpi );
417 toolBar.AddControl( titleActor, style.mToolBarTitlePercentage, Dali::Toolkit::Alignment::HorizontalCenter,
418 Dali::Toolkit::Alignment::Padding( padding, padding, padding, padding ) );
422 void DaliTableView::Populate()
424 const Vector2 stageSize = Stage::GetCurrent().GetSize();
426 const Size demoTileSize( 0.25f * stageSize.width, 0.25f * stageSize.height );
428 mTotalPages = ( mExampleList.size() + EXAMPLES_PER_PAGE - 1 ) / EXAMPLES_PER_PAGE;
430 // Populate ScrollView.
431 if( mExampleList.size() > 0 )
433 if( mSortAlphabetically )
435 sort( mExampleList.begin(), mExampleList.end(), CompareByTitle );
438 unsigned int exampleCount = 0;
439 ExampleListConstIter iter = mExampleList.begin();
440 for( int t = 0; t < mTotalPages; t++ )
442 // Create Table. (contains up to 9 Examples)
443 TableView tableView = TableView::New( 4, 3 );
444 // Add tableView to container.
445 mScrollView.Add( tableView );
446 ApplyEffectToPage( tableView, TABLE_RELATIVE_SIZE );
448 tableView.SetAnchorPoint( AnchorPoint::CENTER );
449 tableView.SetParentOrigin( ParentOrigin::CENTER );
450 // 2 pixels of padding
451 tableView.SetCellPadding( Size( 2.0f, 2.0f ) );
453 Constraint constraint = Constraint::New<Vector3>( Actor::SCALE,
454 LocalSource( Actor::SIZE ),
455 ParentSource( Actor::SIZE ),
456 ScaleToFitConstraint() );
457 tableView.ApplyConstraint(constraint);
459 // Apply visibility constraint to table view
460 Constraint visibleConstraint = Constraint::New< bool >( Actor::VISIBLE,
461 LocalSource( Actor::POSITION ),
462 ParentSource( Actor::SIZE ),
463 TableViewVisibilityConstraint() );
464 visibleConstraint.SetRemoveAction( Constraint::Discard );
465 tableView.ApplyConstraint( visibleConstraint );
467 // add cells to table
468 for( int y = 0; y < ROWS_PER_PAGE; y++ )
470 for( int x = 0; x < EXAMPLES_PER_ROW; x++ )
472 const Example& example = ( *iter );
474 Actor tile = CreateTile( example.name, example.title, demoTileSize, true );
475 FocusManager focusManager = FocusManager::Get();
476 focusManager.SetFocusOrder( tile, ++exampleCount );
477 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_LABEL,
479 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_TRAIT, "Tile" );
480 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_HINT,
481 "You can run this example" );
483 tableView.AddChild( tile, TableView::CellPosition( y, x ) );
486 if( iter == mExampleList.end() )
491 if( iter == mExampleList.end() )
498 tableView.SetFixedHeight( 3, BOTTOM_ROW_HEIGHT );
500 std::stringstream out;
501 out << ( t + 1 ) << " of " << mTotalPages;
502 Actor pageNumberText = CreateTile( "", out.str(), Size( 0.8f * stageSize.width, BOTTOM_ROW_HEIGHT ), false );
504 pageNumberText.ApplyConstraint( Constraint::New< Vector3 >( Actor::POSITION, Source( tableView, Actor::WORLD_POSITION),
505 TranslateLocalConstraint( Vector3( 0.0f, stageSize.y * 0.4f, 0.0f ) ) ) );
506 pageNumberText.ApplyConstraint( Constraint::New< Quaternion >( Actor::ROTATION, Source( tableView, Actor::WORLD_ROTATION ), EqualToConstraint() ) );
507 pageNumberText.ApplyConstraint( Constraint::New< Vector4 >( Actor::COLOR, Source( tableView, Actor::COLOR ), EqualToConstraint() ) );
509 //Stage::GetCurrent().Add( pageNumberText );
511 // Set tableview position
512 Vector3 tableViewPos( stageSize.x * TABLE_RELATIVE_SIZE.x * t, 0.0f, 0.0f );
513 tableView.SetPosition( tableViewPos );
515 mTableViewList.push_back( tableView );
517 if( iter == mExampleList.end() )
524 // Update Ruler info.
525 mScrollRulerX = new FixedRuler( stageSize.width * TABLE_RELATIVE_SIZE.x );
526 mScrollRulerY = new DefaultRuler();
527 mScrollRulerX->SetDomain( RulerDomain( 0.0f, mTotalPages * stageSize.width * TABLE_RELATIVE_SIZE.x, true ) );
528 mScrollRulerY->Disable();
529 mScrollView.SetRulerX( mScrollRulerX );
530 mScrollView.SetRulerY( mScrollRulerY );
533 void DaliTableView::OrientationChanged( Orientation orientation )
535 // TODO: Implement if orientation change required
538 void DaliTableView::Rotate( unsigned int degrees )
540 // Resize the root actor
541 Vector2 stageSize = Stage::GetCurrent().GetSize();
542 Vector3 targetSize( stageSize.x, stageSize.y, 1.0f );
544 if( degrees == 90 || degrees == 270 )
546 targetSize = Vector3( stageSize.y, stageSize.x, 1.0f );
549 if( mRotateAnimation )
551 mRotateAnimation.Stop();
552 mRotateAnimation.Clear();
555 mRotateAnimation = Animation::New( ROTATE_ANIMATION_TIME );
556 mRotateAnimation.RotateTo( mRootActor, Degree( 360 - degrees ), Vector3::ZAXIS, AlphaFunctions::EaseOut );
557 mRotateAnimation.Resize( mRootActor, targetSize, AlphaFunctions::EaseOut );
558 mRotateAnimation.Play();
561 Actor DaliTableView::CreateTile( const string& name, const string& title, const Size& parentSize, bool addBackground )
563 Actor tile = Actor::New();
564 tile.SetName( name );
565 tile.SetAnchorPoint( AnchorPoint::CENTER );
566 tile.SetParentOrigin( ParentOrigin::CENTER );
568 // make the tile 100% of parent
569 tile.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
571 Actor content = Actor::New();
572 content.SetAnchorPoint( AnchorPoint::CENTER );
573 content.SetParentOrigin( ParentOrigin::CENTER );
574 content.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
577 // create background image
580 Image bg = Image::New( TILE_BACKGROUND );
581 ImageActor image = ImageActor::New( bg );
582 image.SetAnchorPoint( AnchorPoint::CENTER );
583 image.SetParentOrigin( ParentOrigin::CENTER );
584 // make the image 100% of tile
585 image.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
586 // move image back to get text appear in front
588 image.SetStyle( ImageActor::STYLE_NINE_PATCH );
589 image.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
591 content.Add( image );
594 ImageActor stencil = NewStencilImage();
595 stencil.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
596 image.Add( stencil );
599 TextView text = TextView::New( title );
600 text.SetAnchorPoint( AnchorPoint::CENTER );
601 text.SetParentOrigin( ParentOrigin::CENTER );
602 text.SetWidthExceedPolicy( Toolkit::TextView::ShrinkToFit );
603 text.SetMultilinePolicy( Toolkit::TextView::SplitByWord );
604 text.SetLineJustification( Toolkit::TextView::Center );
605 text.SetTextAlignment( Toolkit::Alignment::Type( Alignment::HorizontalCenter | Alignment::VerticalCenter ) );
606 text.SetColor( Color::WHITE );
608 // make the text 90% of tile
609 text.SetSize( 0.9f * parentSize.width, 0.9f * parentSize.height );
610 text.SetStyleToCurrentText( GetTableTextStyle() );
611 text.SetSnapshotModeEnabled( false );
614 // Set the tile to be keyboard focusable
615 tile.SetKeyboardFocusable(true);
617 // connect to the touch events
618 tile.TouchedSignal().Connect( this, &DaliTableView::OnTilePressed );
623 ImageActor DaliTableView::NewStencilImage()
625 Image alpha = Image::New( TILE_BACKGROUND_ALPHA );
627 ImageActor stencilActor = ImageActor::New( alpha );
628 stencilActor.SetStyle( ImageActor::STYLE_NINE_PATCH );
629 stencilActor.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
631 stencilActor.SetParentOrigin( ParentOrigin::CENTER );
632 stencilActor.SetAnchorPoint( AnchorPoint::CENTER );
633 stencilActor.SetDrawMode( DrawMode::STENCIL );
635 Dali::ShaderEffect shaderEffect = AlphaDiscardEffect::New();
636 stencilActor.SetShaderEffect( shaderEffect );
641 bool DaliTableView::OnTilePressed( Actor actor, const TouchEvent& event )
643 bool consumed = false;
645 const TouchPoint& point = event.GetPoint( 0 );
646 if( TouchPoint::Down == point.state )
648 mPressedActor = actor;
652 // A button press is only valid if the Down & Up events
653 // both occurred within the button.
654 if( ( TouchPoint::Up == point.state ) &&
655 ( mPressedActor == actor ) )
657 std::string name = actor.GetName();
658 ExampleMapConstIter iter = mExampleMap.find( name );
660 FocusManager focusManager = FocusManager::Get();
662 if( iter != mExampleMap.end() )
664 // ignore Example button presses when scrolling or button animating.
665 if( ( !mScrolling ) && ( !mPressedAnimation ) )
667 // do nothing, until pressed animation finished.
674 mPressedAnimation = Animation::New( BUTTON_PRESS_ANIMATION_TIME );
675 mPressedAnimation.SetDestroyAction( Animation::Discard );
677 // scale the content actor within the Tile, as to not affect the placement within the Table.
678 Actor content = actor.GetChildAt(0);
679 mPressedAnimation.ScaleTo( content, Vector3( 0.9f, 0.9f, 1.0f ), AlphaFunctions::EaseInOut, 0.0f,
680 BUTTON_PRESS_ANIMATION_TIME * 0.5f );
681 mPressedAnimation.ScaleTo( content, Vector3::ONE, AlphaFunctions::EaseInOut, BUTTON_PRESS_ANIMATION_TIME * 0.5f,
682 BUTTON_PRESS_ANIMATION_TIME * 0.5f );
683 mPressedAnimation.Play();
684 mPressedAnimation.FinishedSignal().Connect( this, &DaliTableView::OnPressedAnimationFinished );
690 void DaliTableView::OnPressedAnimationFinished( Dali::Animation& source )
692 mPressedAnimation.Reset();
695 std::string name = mPressedActor.GetName();
696 ExampleMapConstIter iter = mExampleMap.find( name );
698 if( iter == mExampleMap.end() )
700 if( name == BUTTON_QUIT )
702 // Move focus to the OK button
703 FocusManager focusManager = FocusManager::Get();
705 // Enable the group mode and wrap mode
706 focusManager.SetGroupMode( true );
707 focusManager.SetWrapMode( true );
712 const Example& example( iter->second );
714 std::stringstream stream;
715 stream << DALI_EXAMPLE_BIN << example.name.c_str();
719 execlp( stream.str().c_str(), example.name.c_str(), NULL );
720 DALI_ASSERT_ALWAYS(false && "exec failed!");
723 mPressedActor.Reset();
727 void DaliTableView::OnScrollStart( const Dali::Vector3& position )
734 void DaliTableView::OnScrollComplete( const Dali::Vector3& position )
738 // move focus to 1st item of new page
739 FocusManager focusManager = FocusManager::Get();
740 focusManager.SetCurrentFocusActor(mTableViewList[mScrollView.GetCurrentPage()].GetChildAt(TableView::CellPosition(1, 0)) );
744 bool DaliTableView::OnScrollTouched( Actor actor, const TouchEvent& event )
746 const TouchPoint& point = event.GetPoint( 0 );
747 if( TouchPoint::Down == point.state )
749 mPressedActor = actor;
755 void DaliTableView::ApplyScrollViewEffect()
757 // Remove old effect if exists.
759 if( mScrollViewEffect )
761 mScrollView.RemoveEffect( mScrollViewEffect );
764 // Just one effect for now
765 SetupInnerPageCubeEffect();
767 mScrollView.ApplyEffect( mScrollViewEffect );
770 void DaliTableView::SetupInnerPageCubeEffect()
772 ScrollViewCustomEffect customEffect;
773 mScrollViewEffect = customEffect = ScrollViewCustomEffect::New();
774 mScrollView.SetScrollSnapDuration( EFFECT_SNAP_DURATION );
775 mScrollView.SetScrollFlickDuration( EFFECT_FLICK_DURATION );
776 mScrollView.SetScrollSnapAlphaFunction( AlphaFunctions::EaseOutBack );
777 mScrollView.SetScrollFlickAlphaFunction( AlphaFunctions::EaseOutBack );
778 mScrollView.RemoveConstraintsFromChildren();
780 customEffect.SetPageSpacing( Vector2( 30.0f, 30.0f ) );
781 customEffect.SetAngledOriginPageRotation( ANGLE_CUBE_PAGE_ROTATE );
782 customEffect.SetSwingAngle( ANGLE_CUBE_PAGE_ROTATE.x, Vector3( 0, -1, 0 ) );
783 customEffect.SetOpacityThreshold( 0.5f ); // Make fade out on edges
786 void DaliTableView::ApplyEffectToPage( Actor page, const Vector3& tableRelativeSize )
788 page.RemoveConstraints();
790 Constraint constraint = Constraint::New<Vector3>( Actor::SCALE,
791 LocalSource( Actor::SIZE ),
792 ParentSource( Actor::SIZE ),
793 ScaleToFitConstraint() );
794 page.ApplyConstraint(constraint);
796 ApplyCustomEffectToPage( page );
799 void DaliTableView::ApplyCustomEffectToPage( Actor page )
801 ScrollViewCustomEffect customEffect = ScrollViewCustomEffect::DownCast( mScrollViewEffect );
802 Vector2 vStageSize( Stage::GetCurrent().GetSize() );
803 customEffect.ApplyToPage( page, Vector3( vStageSize.x, vStageSize.y, 1.0f ) );
806 void DaliTableView::OnKeyEvent( const KeyEvent& event )
808 if( event.state == KeyEvent::Down )
810 if ( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
817 Actor CreateBackgroundActor( const Vector2& size )
819 Actor layer = Actor::New();
820 layer.SetAnchorPoint( AnchorPoint::CENTER );
821 layer.SetParentOrigin( ParentOrigin::CENTER );
822 layer.SetSize( size );
826 void DaliTableView::SetupBackground( Actor addToLayer, const Vector2& size )
828 // Create distance field shape
829 BitmapImage distanceField;
830 Size imageSize( 512, 512 );
831 CreateShapeImage( CIRCLE, imageSize, distanceField );
834 Actor backgroundAnimLayer0 = CreateBackgroundActor( size );
835 Actor backgroundAnimLayer1 = CreateBackgroundActor( size );
836 Actor backgroundAnimLayer2 = CreateBackgroundActor( size );
839 Constraint animConstraint0 = Constraint::New < Vector3 > ( Actor::POSITION,
840 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
841 AnimScrollConstraint( backgroundAnimLayer0.GetCurrentPosition(), 0.75f ) );
842 backgroundAnimLayer0.ApplyConstraint( animConstraint0 );
844 Constraint animConstraint1 = Constraint::New < Vector3 > ( Actor::POSITION,
845 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
846 AnimScrollConstraint( backgroundAnimLayer1.GetCurrentPosition(), 0.5f ) );
847 backgroundAnimLayer1.ApplyConstraint( animConstraint1 );
849 Constraint animConstraint2 = Constraint::New < Vector3 > ( Actor::POSITION,
850 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
851 AnimScrollConstraint( backgroundAnimLayer2.GetCurrentPosition(), 0.25f ) );
852 backgroundAnimLayer2.ApplyConstraint( animConstraint2 );
855 ImageActor layer = Dali::Toolkit::CreateSolidColorActor( BACKGROUND_COLOR );
856 layer.SetAnchorPoint( AnchorPoint::CENTER );
857 layer.SetParentOrigin( ParentOrigin::CENTER );
858 layer.SetSize( size * BACKGROUND_SIZE_SCALE );
859 layer.SetZ( BACKGROUND_Z );
860 layer.SetPositionInheritanceMode( DONT_INHERIT_POSITION );
862 addToLayer.Add( layer );
865 addToLayer.Add( backgroundAnimLayer0 );
866 addToLayer.Add( backgroundAnimLayer1 );
867 addToLayer.Add( backgroundAnimLayer2 );
869 // Add all the children
870 AddBackgroundActors( backgroundAnimLayer0, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
871 AddBackgroundActors( backgroundAnimLayer1, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
872 AddBackgroundActors( backgroundAnimLayer2, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
875 void DaliTableView::AddBackgroundActors( Actor layer, int count, BitmapImage distanceField, const Dali::Vector2& size )
877 for( int i = 0; i < count; ++i )
879 float randSize = Random::Range( 10.0f, 400.0f );
880 float hue = Random::Range( 0.3f, 1.0f );
881 Vector4 randColour( hue, hue*0.5, 0.0f, Random::Range( 0.3f, 0.6f ));
883 ImageActor dfActor = ImageActor::New( distanceField );
884 mBackgroundActors.push_back( dfActor );
885 dfActor.SetSize( Vector2( randSize, randSize ) );
886 dfActor.SetParentOrigin( ParentOrigin::CENTER );
888 Toolkit::DistanceFieldEffect effect = Toolkit::DistanceFieldEffect::New();
889 dfActor.SetShaderEffect( effect );
890 dfActor.SetColor( randColour );
891 effect.SetOutlineParams( Vector2( 0.55f, 0.00f ) );
892 effect.SetSmoothingEdge( 0.5f );
893 layer.Add( dfActor );
897 Random::Range( -size.x * 0.5f * BACKGROUND_SPREAD_SCALE, size.x * 0.5f * BACKGROUND_SPREAD_SCALE ),
898 Random::Range( -size.y * 0.5f - randSize, size.y * 0.5f + randSize ),
899 Random::Range(-1.0f, 0.0f) );
900 dfActor.SetPosition( actorPos );
901 dfActor.SetSortModifier(size.x * 0.7f);
903 Constraint movementConstraint = Constraint::New < Vector3 > ( Actor::POSITION,
904 LocalSource( Actor::SIZE ),
905 ParentSource( Actor::SIZE ),
906 ShapeMovementConstraint );
907 dfActor.ApplyConstraint( movementConstraint );
910 Animation animation = Animation::New( Random::Range( 40.0f, 200.0f ) );
911 KeyFrames keyframes = KeyFrames::New();
912 keyframes.Add( 0.0f, actorPos );
913 Vector3 toPos( actorPos );
914 toPos.y -= ( size.y + randSize );
915 keyframes.Add( 1.0f, toPos );
916 animation.AnimateBetween( Property( dfActor, Actor::POSITION ), keyframes );
917 animation.SetLooping( true );
919 mBackgroundAnimations.push_back( animation );
923 void DaliTableView::CreateShapeImage( ShapeType shapeType, const Size& size, BitmapImage& distanceFieldOut )
925 // this bitmap will hold the alpha map for the distance field shader
926 distanceFieldOut = BitmapImage::New( size.width, size.height, Pixel::A8 );
928 // Generate bit pattern
929 std::vector< unsigned char > imageDataA8;
930 imageDataA8.reserve( size.width * size.height ); // A8
935 GenerateCircle( size, imageDataA8 );
938 GenerateSquare( size, imageDataA8 );
944 GenerateDistanceFieldMap( &imageDataA8[ 0 ], size, distanceFieldOut.GetBuffer(), size, 8.0f, size );
945 distanceFieldOut.Update();
948 void DaliTableView::GenerateSquare( const Size& size, std::vector< unsigned char >& distanceFieldOut )
950 for( int h = 0; h < size.height; ++h )
952 for( int w = 0; w < size.width; ++w )
954 distanceFieldOut.push_back( 0xFF );
959 void DaliTableView::GenerateCircle( const Size& size, std::vector< unsigned char >& distanceFieldOut )
961 const float radius = size.width * 0.5f * size.width * 0.5f;
962 Vector2 center( size.width / 2, size.height / 2 );
964 for( int h = 0; h < size.height; ++h )
966 for( int w = 0; w < size.width; ++w )
969 Vector2 dist = pos - center;
971 if( dist.x * dist.x + dist.y * dist.y > radius )
973 distanceFieldOut.push_back( 0x00 );
977 distanceFieldOut.push_back( 0xFF );
983 ImageActor DaliTableView::CreateLogo( std::string imagePath )
985 Image image = Image::New( imagePath );
986 image.LoadingFinishedSignal().Connect( this, &DaliTableView::OnLogoLoaded );
988 ImageActor logo = ImageActor::New( image );
990 logo.SetAnchorPoint( AnchorPoint::CENTER );
991 logo.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
996 void DaliTableView::OnLogoLoaded( Dali::Image image )
998 mRootActor.SetFixedHeight( 1, image.GetHeight() + LOGO_BOTTOM_PADDING_HEIGHT );
1001 bool DaliTableView::PauseBackgroundAnimation()
1008 void DaliTableView::PauseAnimation()
1010 if( mBackgroundAnimsPlaying )
1012 for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
1014 Animation anim = *animIter;
1019 mBackgroundAnimsPlaying = false;
1023 void DaliTableView::PlayAnimation()
1025 if ( !mBackgroundAnimsPlaying )
1027 for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
1029 Animation anim = *animIter;
1034 mBackgroundAnimsPlaying = true;
1037 mAnimationTimer.SetInterval( BACKGROUND_ANIMATION_DURATION );
1040 Dali::Actor DaliTableView::OnKeyboardPreFocusChange( Dali::Actor current, Dali::Actor proposed, Dali::Toolkit::Control::KeyboardFocusNavigationDirection direction )
1042 Actor nextFocusActor = proposed;
1044 if ( !current && !proposed )
1046 // Set the initial focus to the first tile in the current page should be focused.
1047 nextFocusActor = mTableViewList[mScrollView.GetCurrentPage()].GetChildAt(TableView::CellPosition(0, 0));
1049 else if( !proposed || (proposed && proposed == mScrollViewLayer) )
1051 // ScrollView is being focused but nothing in the current page can be focused further
1052 // in the given direction. We should work out which page to scroll to next.
1053 int currentPage = mScrollView.GetCurrentPage();
1054 int newPage = currentPage;
1055 if( direction == Dali::Toolkit::Control::Left )
1059 else if( direction == Dali::Toolkit::Control::Right )
1064 newPage = std::max(0, std::min(static_cast<int>(mScrollRulerX->GetTotalPages() - 1), newPage));
1065 if( newPage == currentPage )
1067 if( direction == Dali::Toolkit::Control::Left )
1069 newPage = mScrollRulerX->GetTotalPages() - 1;
1070 } else if( direction == Dali::Toolkit::Control::Right )
1076 // Scroll to the page in the given direction
1077 mScrollView.ScrollTo(newPage);
1079 if( direction == Dali::Toolkit::Control::Left )
1081 // Work out the cell position for the last tile
1082 int remainingExamples = mExampleList.size() - newPage * EXAMPLES_PER_PAGE;
1083 int rowPos = (remainingExamples >= EXAMPLES_PER_PAGE) ? ROWS_PER_PAGE - 1 : ( (remainingExamples % EXAMPLES_PER_PAGE + EXAMPLES_PER_ROW) / EXAMPLES_PER_ROW - 1 );
1084 int colPos = remainingExamples >= EXAMPLES_PER_PAGE ? EXAMPLES_PER_ROW - 1 : ( remainingExamples % EXAMPLES_PER_PAGE - rowPos * EXAMPLES_PER_ROW - 1 );
1086 // Move the focus to the last tile in the new page.
1087 nextFocusActor = mTableViewList[newPage].GetChildAt(TableView::CellPosition(rowPos, colPos));
1091 // Move the focus to the first tile in the new page.
1092 nextFocusActor = mTableViewList[newPage].GetChildAt(TableView::CellPosition(0, 0));
1096 return nextFocusActor;
1099 void DaliTableView::OnFocusedActorActivated( Dali::Actor activatedActor )
1103 mPressedActor = activatedActor;
1105 // Activate the current focused actor;
1106 TouchEvent touchEventUp;
1107 touchEventUp.points.push_back( TouchPoint ( 0, TouchPoint::Up, 0.0f, 0.0f ) );
1108 OnTilePressed(mPressedActor, touchEventUp);