2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "dali-table-view.h"
20 #include "examples/shared/view.h"
28 using namespace Dali::Toolkit;
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 LOGO_MARGIN_RATIO = 0.5f / 0.9f;
57 const float BOTTOM_PADDING_RATIO = 0.4f / 0.9f;
58 const Vector3 SCROLLVIEW_RELATIVE_SIZE(0.9f, 1.0f, 0.8f ); ///< ScrollView's relative size to its parent
59 const Vector3 TABLE_RELATIVE_SIZE(0.9f, 0.9f, 0.8f ); ///< TableView's relative size to the entire stage. The Y value means sum of the logo and table relative heights.
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 = -1.0f;
76 const float BACKGROUND_SIZE_SCALE = 1.0f;
77 const Vector4 BACKGROUND_COLOR( 1.0f, 1.0f, 1.0f, 1.0f );
79 const float BUBBLE_MIN_Z = -1.0;
80 const float BUBBLE_MAX_Z = 0.0f;
82 // 3D Effect constants
83 const Vector2 ANGLE_SWING_3DEFFECT( Math::PI_2 * 0.75, Math::PI_2 * 0.75f ); ///< Angle Swing in radians
84 const Vector2 POSITION_SWING_3DEFFECT( 0.55f, 0.4f ); ///< Position Swing relative to stage size.
85 const Vector3 ANCHOR_3DEFFECT_STYLE0( -105.0f, 30.0f, -240.0f ); ///< Rotation Anchor position for 3D Effect (Style 0)
86 const Vector3 ANCHOR_3DEFFECT_STYLE1( 65.0f, -70.0f, -500.0f ); ///< Rotation Anchor position for 3D Effect (Style 1)
88 const std::string DEFAULT_TEXT_STYLE_FONT_FAMILY("HelveticaNeue");
89 const std::string DEFAULT_TEXT_STYLE_FONT_STYLE("Regular");
90 const Dali::PointSize DEFAULT_TEXT_STYLE_POINT_SIZE( 8.0f );
91 const Dali::TextStyle::Weight DEFAULT_TEXT_STYLE_WEIGHT(Dali::TextStyle::REGULAR);
92 const Dali::Vector4 DEFAULT_TEXT_STYLE_COLOR(0.7f, 0.7f, 0.7f, 1.0f);
94 const std::string TABLE_TEXT_STYLE_FONT_FAMILY("HelveticaNeue");
95 const std::string TABLE_TEXT_STYLE_FONT_STYLE("Regular");
96 const Dali::PointSize TABLE_TEXT_STYLE_POINT_SIZE( 8.0f );
97 const Dali::TextStyle::Weight TABLE_TEXT_STYLE_WEIGHT(Dali::TextStyle::LIGHT);
98 const Dali::Vector4 TABLE_TEXT_STYLE_COLOR(0.0f, 0.0f, 0.0f, 1.0f);
100 Vector3 ScalePointSize(const Vector3& vec)
102 return Vector3( DemoHelper::ScalePointSize( vec.x ), DemoHelper::ScalePointSize( vec.y ), DemoHelper::ScalePointSize( vec.z ) );
105 #define DP(x) DemoHelper::ScalePointSize(x)
107 TextStyle GetTableTextStyle()
110 textStyle.SetFontName(TABLE_TEXT_STYLE_FONT_FAMILY);
111 textStyle.SetFontStyle(TABLE_TEXT_STYLE_FONT_STYLE);
112 textStyle.SetFontPointSize( Dali::PointSize(DemoHelper::ScalePointSize(TABLE_TEXT_STYLE_POINT_SIZE)));
113 textStyle.SetWeight(TABLE_TEXT_STYLE_WEIGHT);
114 textStyle.SetTextColor(TABLE_TEXT_STYLE_COLOR);
119 * Creates the background image
121 ImageActor CreateBackground( std::string imagePath )
123 Image image = Image::New( imagePath );
124 ImageActor background = ImageActor::New( image );
126 background.SetAnchorPoint( AnchorPoint::CENTER );
127 background.SetParentOrigin( ParentOrigin::CENTER );
128 background.SetZ( -1.0f );
133 // These values depend on the tile image
134 const float IMAGE_BORDER_LEFT = 11.0f;
135 const float IMAGE_BORDER_RIGHT = IMAGE_BORDER_LEFT;
136 const float IMAGE_BORDER_TOP = IMAGE_BORDER_LEFT;
137 const float IMAGE_BORDER_BOTTOM = IMAGE_BORDER_LEFT;
140 * TableViewVisibilityConstraint
142 struct TableViewVisibilityConstraint
144 bool operator()( const bool& current,
145 const PropertyInput& pagePositionProperty,
146 const PropertyInput& pageSizeProperty )
148 // Only the tableview in the current page should be visible.
149 const Vector3& pagePosition = pagePositionProperty.GetVector3();
150 const Vector3& pageSize = pageSizeProperty.GetVector3();
151 return fabsf( pagePosition.x ) < pageSize.x;
156 * Constraint to wrap an actor in y that is moving vertically
158 Vector3 ShapeMovementConstraint( const Vector3& current,
159 const PropertyInput& shapeSizeProperty,
160 const PropertyInput& parentSizeProperty )
162 const Vector3& shapeSize = shapeSizeProperty.GetVector3();
163 const Vector3& parentSize = parentSizeProperty.GetVector3();
165 Vector3 pos( current );
166 if( pos.y + shapeSize.y * 0.5f < -parentSize.y * 0.5f )
168 pos.y += parentSize.y + shapeSize.y;
175 * Constraint to return a position for the background based on the scroll value
177 struct AnimScrollConstraint
181 AnimScrollConstraint( const Vector3& initialPos, float scale )
182 : mInitialPos( initialPos ),
188 Vector3 operator()( const Vector3& current, const PropertyInput& scrollProperty )
190 float scrollPos = scrollProperty.GetVector3().x;
192 return mInitialPos + Vector3( -scrollPos * mScale, 0.0f, 0.0f );
201 * Constraint to return a tracked world position added to the constant local position
203 struct TranslateLocalConstraint
207 TranslateLocalConstraint( const Vector3& localPos )
208 : mLocalPos( localPos )
212 Vector3 operator()( const Vector3& current, const PropertyInput& pagePosProperty )
214 Vector3 worldPos = pagePosProperty.GetVector3();
216 return ( worldPos + mLocalPos );
224 bool CompareByTitle( const Example& lhs, const Example& rhs )
226 return lhs.title < rhs.title;
231 DaliTableView::DaliTableView( Application& application )
232 : mApplication( application ),
234 mBackgroundImagePath( DEFAULT_BACKGROUND_IMAGE_PATH ),
235 mSortAlphabetically( false ),
236 mBackgroundAnimsPlaying( false )
238 application.InitSignal().Connect( this, &DaliTableView::Initialize );
241 DaliTableView::~DaliTableView()
245 void DaliTableView::AddExample( Example example )
247 mExampleList.push_back( example );
248 mExampleMap[ example.name ] = example;
251 void DaliTableView::SetBackgroundPath( std::string imagePath )
253 mBackgroundImagePath = imagePath;
256 void DaliTableView::SortAlphabetically( bool sortAlphabetically )
258 mSortAlphabetically = sortAlphabetically;
261 void DaliTableView::Initialize( Application& application )
263 Stage::GetCurrent().KeyEventSignal().Connect( this, &DaliTableView::OnKeyEvent );
265 const Vector2 stageSize = Stage::GetCurrent().GetSize();
268 mBackground = CreateBackground( mBackgroundImagePath );
269 // set same size as parent actor
270 mBackground.SetSize( stageSize );
271 Stage::GetCurrent().Add( mBackground );
273 // Render entire content as overlays, as is all on same 2D plane.
274 mRootActor = TableView::New( 4, 1 );
275 mRootActor.SetAnchorPoint( AnchorPoint::CENTER );
276 mRootActor.SetParentOrigin( ParentOrigin::CENTER );
277 Stage::GetCurrent().Add( mRootActor );
280 Dali::Toolkit::ToolBar toolbar;
281 Dali::Layer toolBarLayer = DemoHelper::CreateToolbar(toolbar,
282 DEFAULT_TOOLBAR_IMAGE_PATH,
283 DEFAULT_TOOLBAR_TEXT,
284 DemoHelper::DEFAULT_VIEW_STYLE,
285 DemoHelper::GetDefaultTextStyle());
287 mRootActor.AddChild( toolBarLayer, TableView::CellPosition( 0, 0 ) );
288 const float toolbarHeight = DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight;
289 mRootActor.SetFixedHeight( 0, toolbarHeight );
292 mLogo = CreateLogo( LOGO_PATH );
293 const float paddingHeight = ( ( 1.f-TABLE_RELATIVE_SIZE.y ) * stageSize.y );
294 const float logoMargin = paddingHeight * LOGO_MARGIN_RATIO;
295 const float logoHeight = mLogo.GetImage().GetHeight() + logoMargin;
296 mRootActor.SetFixedHeight( 1, logoHeight );
298 const float bottomMargin = paddingHeight * BOTTOM_PADDING_RATIO;
299 mButtonsPageRelativeSize = Vector3( TABLE_RELATIVE_SIZE.x, 1.f - ( toolbarHeight + logoHeight + bottomMargin) / stageSize.height, TABLE_RELATIVE_SIZE.z );
300 mRootActor.SetFixedHeight( 2, mButtonsPageRelativeSize.y * stageSize.height );
302 Alignment alignment = Alignment::New();
303 alignment.Add(mLogo);
304 mRootActor.AddChild( alignment, TableView::CellPosition( 1, 0 ) );
306 // scrollview occupying the majority of the screen
307 mScrollView = ScrollView::New();
309 mScrollView.SetAnchorPoint( AnchorPoint::CENTER );
310 mScrollView.SetParentOrigin( ParentOrigin::CENTER );
311 mScrollView.ApplyConstraint( Dali::Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ),
312 Dali::RelativeToConstraint( SCROLLVIEW_RELATIVE_SIZE ) ) );
313 mScrollView.SetAxisAutoLock( true );
314 mScrollView.ScrollCompletedSignal().Connect( this, &DaliTableView::OnScrollComplete );
315 mScrollView.ScrollStartedSignal().Connect( this, &DaliTableView::OnScrollStart );
316 mScrollView.TouchedSignal().Connect( this, &DaliTableView::OnScrollTouched );
318 mScrollViewLayer = Layer::New();
319 mScrollViewLayer.SetAnchorPoint( AnchorPoint::CENTER );
320 mScrollViewLayer.SetParentOrigin( ParentOrigin::CENTER );
321 mScrollViewLayer.SetDrawMode( DrawMode::OVERLAY );
323 // Populate background and bubbles - needs to be scrollViewLayer so scroll ends show
324 SetupBackground( mScrollView, mScrollViewLayer, stageSize );
326 mScrollViewLayer.Add( mScrollView );
327 mRootActor.AddChild( mScrollViewLayer, TableView::CellPosition( 2, 0 ) );
329 // Add scroll view effect and setup constraints on pages
330 ApplyScrollViewEffect();
332 // Add pages and tiles
335 // Remove constraints for inner cube effect
336 ApplyCubeEffectToActors();
338 // Set initial orientation
339 unsigned int degrees = application.GetOrientation().GetDegrees();
342 Dali::Window winHandle = application.GetWindow();
343 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
344 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE );
345 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
346 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
348 Dali::Orientation orientation = winHandle.GetOrientation();
349 orientation.ChangedSignal().Connect( this, &DaliTableView::OrientationChanged );
351 winHandle.ShowIndicator( Dali::Window::INVISIBLE );
354 mAnimationTimer = Timer::New( BACKGROUND_ANIMATION_DURATION );
355 mAnimationTimer.TickSignal().Connect( this, &DaliTableView::PauseBackgroundAnimation );
356 mAnimationTimer.Start();
357 mBackgroundAnimsPlaying = true;
359 KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &DaliTableView::OnKeyboardPreFocusChange );
360 KeyboardFocusManager::Get().FocusedActorActivatedSignal().Connect( this, &DaliTableView::OnFocusedActorActivated );
363 void DaliTableView::ApplyCubeEffectToActors()
365 for( ActorIter pageIter = mPages.begin(); pageIter != mPages.end(); ++pageIter )
367 Actor page = *pageIter;
369 unsigned int numChildren = page.GetChildCount();
370 Actor pageActor = page;
371 for( unsigned int i=0; i<numChildren; ++i)
373 // Remove old effect's manual constraints.
374 Actor child = pageActor.GetChildAt(i);
377 ApplyCubeEffectToActor( child );
382 void DaliTableView::Populate()
384 const Vector2 stageSize = Stage::GetCurrent().GetSize();
386 mTotalPages = ( mExampleList.size() + EXAMPLES_PER_PAGE - 1 ) / EXAMPLES_PER_PAGE;
388 // Populate ScrollView.
389 if( mExampleList.size() > 0 )
391 if( mSortAlphabetically )
393 sort( mExampleList.begin(), mExampleList.end(), CompareByTitle );
396 unsigned int exampleCount = 0;
397 ExampleListConstIter iter = mExampleList.begin();
399 for( int t = 0; t < mTotalPages; t++ )
401 // Create Table. (contains up to 9 Examples)
402 Actor page = Actor::New();
404 // Add tableView to container.
405 mScrollView.Add( page );
407 page.SetAnchorPoint( AnchorPoint::CENTER );
408 page.SetParentOrigin( ParentOrigin::CENTER );
409 page.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
411 // add cells to table
412 const float margin = 4.0f;
414 // Calculate the number of images going across (columns) within a page, according to the screen resolution and dpi.
415 const Size tileSize((stageSize.x * mButtonsPageRelativeSize.x / EXAMPLES_PER_ROW) - margin, (stageSize.y * mButtonsPageRelativeSize.y / ROWS_PER_PAGE) - margin );
417 for(int row = 0; row < ROWS_PER_PAGE; row++)
419 for(int column = 0; column < EXAMPLES_PER_ROW; column++)
421 const Example& example = ( *iter );
423 Actor tile = CreateTile( example.name, example.title, tileSize, true );
424 FocusManager focusManager = FocusManager::Get();
425 focusManager.SetFocusOrder( tile, ++exampleCount );
426 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_LABEL,
428 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_TRAIT, "Tile" );
429 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_HINT,
430 "You can run this example" );
432 Vector3 position( margin * 0.5f + (tileSize.x + margin) * column - stageSize.width * mButtonsPageRelativeSize.x * 0.5f,
433 margin * 0.5f + (tileSize.y + margin) * row - stageSize.height * mButtonsPageRelativeSize.y * 0.5f,
435 tile.SetPosition( position + Vector3( tileSize.x, tileSize.y, 0.0f ) * 0.5f );
436 tile.SetSize( tileSize );
441 if( iter == mExampleList.end() )
447 if( iter == mExampleList.end() )
453 // Set tableview position
454 Vector3 pagePos( stageSize.x * mButtonsPageRelativeSize.x * t, 0.0f, 0.0f );
455 page.SetPosition( pagePos );
457 mPages.push_back( page );
459 if( iter == mExampleList.end() )
466 // Update Ruler info.
467 mScrollRulerX = new FixedRuler( stageSize.width * mButtonsPageRelativeSize.x );
468 mScrollRulerY = new DefaultRuler();
469 mScrollRulerX->SetDomain( RulerDomain( 0.0f, mTotalPages * stageSize.width * mButtonsPageRelativeSize.x, true ) );
470 mScrollRulerY->Disable();
471 mScrollView.SetRulerX( mScrollRulerX );
472 mScrollView.SetRulerY( mScrollRulerY );
475 void DaliTableView::OrientationChanged( Orientation orientation )
477 // TODO: Implement if orientation change required
480 void DaliTableView::Rotate( unsigned int degrees )
482 // Resize the root actor
483 Vector2 stageSize = Stage::GetCurrent().GetSize();
484 Vector3 targetSize( stageSize.x, stageSize.y, 1.0f );
486 if( degrees == 90 || degrees == 270 )
488 targetSize = Vector3( stageSize.y, stageSize.x, 1.0f );
491 if( mRotateAnimation )
493 mRotateAnimation.Stop();
494 mRotateAnimation.Clear();
497 mRotateAnimation = Animation::New( ROTATE_ANIMATION_TIME );
498 mRotateAnimation.RotateTo( mRootActor, Degree( 360 - degrees ), Vector3::ZAXIS, AlphaFunctions::EaseOut );
499 mRotateAnimation.Resize( mRootActor, targetSize, AlphaFunctions::EaseOut );
500 mRotateAnimation.Play();
503 Actor DaliTableView::CreateTile( const std::string& name, const std::string& title, const Size& parentSize, bool addBackground )
505 Actor tile = Actor::New();
506 tile.SetName( name );
507 tile.SetAnchorPoint( AnchorPoint::CENTER );
508 tile.SetParentOrigin( ParentOrigin::CENTER );
510 Actor content = Actor::New();
511 content.SetAnchorPoint( AnchorPoint::CENTER );
512 content.SetParentOrigin( ParentOrigin::CENTER );
513 content.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
516 // create background image
519 Image bg = Image::New( TILE_BACKGROUND );
520 ImageActor image = ImageActor::New( bg );
521 image.SetAnchorPoint( AnchorPoint::CENTER );
522 image.SetParentOrigin( ParentOrigin::CENTER );
523 // make the image 100% of tile
524 image.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
525 // move image back to get text appear in front
527 image.SetStyle( ImageActor::STYLE_NINE_PATCH );
528 image.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
530 content.Add( image );
533 ImageActor stencil = NewStencilImage();
534 stencil.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
535 image.Add( stencil );
538 TextView text = TextView::New( title );
539 text.SetAnchorPoint( AnchorPoint::CENTER );
540 text.SetParentOrigin( ParentOrigin::CENTER );
541 text.SetWidthExceedPolicy( Toolkit::TextView::ShrinkToFit );
542 text.SetMultilinePolicy( Toolkit::TextView::SplitByWord );
543 text.SetLineJustification( Toolkit::TextView::Center );
544 text.SetTextAlignment( Toolkit::Alignment::Type( Alignment::HorizontalCenter | Alignment::VerticalCenter ) );
545 text.SetColor( Color::WHITE );
547 // make the text 90% of tile
548 text.SetSize( 0.9f * parentSize.width, 0.9f * parentSize.height );
549 text.SetStyleToCurrentText( GetTableTextStyle() );
550 text.SetSnapshotModeEnabled( false );
553 // Set the tile to be keyboard focusable
554 tile.SetKeyboardFocusable(true);
556 // connect to the touch events
557 tile.TouchedSignal().Connect( this, &DaliTableView::OnTilePressed );
558 tile.HoveredSignal().Connect( this, &DaliTableView::OnTileHovered );
563 ImageActor DaliTableView::NewStencilImage()
565 Image alpha = Image::New( TILE_BACKGROUND_ALPHA );
567 ImageActor stencilActor = ImageActor::New( alpha );
568 stencilActor.SetStyle( ImageActor::STYLE_NINE_PATCH );
569 stencilActor.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
571 stencilActor.SetParentOrigin( ParentOrigin::CENTER );
572 stencilActor.SetAnchorPoint( AnchorPoint::CENTER );
573 stencilActor.SetDrawMode( DrawMode::STENCIL );
575 Dali::ShaderEffect shaderEffect = AlphaDiscardEffect::New();
576 stencilActor.SetShaderEffect( shaderEffect );
581 bool DaliTableView::OnTilePressed( Actor actor, const TouchEvent& event )
583 bool consumed = false;
585 const TouchPoint& point = event.GetPoint( 0 );
586 if( TouchPoint::Down == point.state )
588 mPressedActor = actor;
592 // A button press is only valid if the Down & Up events
593 // both occurred within the button.
594 if( ( TouchPoint::Up == point.state ) &&
595 ( mPressedActor == actor ) )
597 std::string name = actor.GetName();
598 ExampleMapConstIter iter = mExampleMap.find( name );
600 FocusManager focusManager = FocusManager::Get();
602 if( iter != mExampleMap.end() )
604 // ignore Example button presses when scrolling or button animating.
605 if( ( !mScrolling ) && ( !mPressedAnimation ) )
607 // do nothing, until pressed animation finished.
614 mPressedAnimation = Animation::New( BUTTON_PRESS_ANIMATION_TIME );
615 mPressedAnimation.SetEndAction( Animation::Discard );
617 // scale the content actor within the Tile, as to not affect the placement within the Table.
618 Actor content = actor.GetChildAt(0);
619 mPressedAnimation.ScaleTo( content, Vector3( 0.9f, 0.9f, 1.0f ), AlphaFunctions::EaseInOut, 0.0f,
620 BUTTON_PRESS_ANIMATION_TIME * 0.5f );
621 mPressedAnimation.ScaleTo( content, Vector3::ONE, AlphaFunctions::EaseInOut, BUTTON_PRESS_ANIMATION_TIME * 0.5f,
622 BUTTON_PRESS_ANIMATION_TIME * 0.5f );
623 mPressedAnimation.Play();
624 mPressedAnimation.FinishedSignal().Connect( this, &DaliTableView::OnPressedAnimationFinished );
630 void DaliTableView::OnPressedAnimationFinished( Dali::Animation& source )
632 mPressedAnimation.Reset();
635 std::string name = mPressedActor.GetName();
636 ExampleMapConstIter iter = mExampleMap.find( name );
638 if( iter == mExampleMap.end() )
640 if( name == BUTTON_QUIT )
642 // Move focus to the OK button
643 FocusManager focusManager = FocusManager::Get();
645 // Enable the group mode and wrap mode
646 focusManager.SetGroupMode( true );
647 focusManager.SetWrapMode( true );
652 const Example& example( iter->second );
654 std::stringstream stream;
655 stream << DALI_EXAMPLE_BIN << example.name.c_str();
659 execlp( stream.str().c_str(), example.name.c_str(), NULL );
660 DALI_ASSERT_ALWAYS(false && "exec failed!");
663 mPressedActor.Reset();
667 void DaliTableView::OnScrollStart( const Dali::Vector3& position )
674 void DaliTableView::OnScrollComplete( const Dali::Vector3& position )
678 // move focus to 1st item of new page
679 FocusManager focusManager = FocusManager::Get();
680 focusManager.SetCurrentFocusActor(mPages[mScrollView.GetCurrentPage()].GetChildAt(0) );
682 ApplyCubeEffectToActors();
685 bool DaliTableView::OnScrollTouched( Actor actor, const TouchEvent& event )
687 const TouchPoint& point = event.GetPoint( 0 );
688 if( TouchPoint::Down == point.state )
690 mPressedActor = actor;
696 void DaliTableView::ApplyScrollViewEffect()
698 // Remove old effect if exists.
700 if( mScrollViewEffect )
702 mScrollView.RemoveEffect( mScrollViewEffect );
705 // Just one effect for now
706 SetupInnerPageCubeEffect();
708 mScrollView.ApplyEffect( mScrollViewEffect );
711 void DaliTableView::SetupInnerPageCubeEffect()
713 mScrollViewEffect = ScrollViewCubeEffect::New();
714 mScrollView.SetScrollSnapDuration( EFFECT_SNAP_DURATION );
715 mScrollView.SetScrollFlickDuration( EFFECT_FLICK_DURATION );
716 mScrollView.RemoveConstraintsFromChildren();
719 void DaliTableView::ApplyCubeEffectToActor( Actor actor )
721 actor.RemoveConstraints();
723 ScrollViewCubeEffect cubeEffect = ScrollViewCubeEffect::DownCast(mScrollViewEffect);
724 cubeEffect.ApplyToActor( actor,
725 ScalePointSize( ( rand() & 1 ) ? ANCHOR_3DEFFECT_STYLE0 : ANCHOR_3DEFFECT_STYLE1 ),
726 ANGLE_SWING_3DEFFECT,
727 POSITION_SWING_3DEFFECT * Vector2(Stage::GetCurrent().GetSize()));
730 void DaliTableView::OnKeyEvent( const KeyEvent& event )
732 if( event.state == KeyEvent::Down )
734 if ( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
741 Actor CreateBackgroundActor( const Vector2& size )
743 Actor layer = Actor::New();
744 layer.SetAnchorPoint( AnchorPoint::CENTER );
745 layer.SetParentOrigin( ParentOrigin::CENTER );
746 layer.SetSize( size );
750 void DaliTableView::SetupBackground( Actor bubbleLayer, Actor backgroundLayer, const Vector2& size )
752 // Create distance field shape
753 BitmapImage distanceField;
754 Size imageSize( 512, 512 );
755 CreateShapeImage( CIRCLE, imageSize, distanceField );
758 Actor backgroundAnimLayer0 = CreateBackgroundActor( size );
759 Actor backgroundAnimLayer1 = CreateBackgroundActor( size );
760 Actor backgroundAnimLayer2 = CreateBackgroundActor( size );
763 Constraint animConstraint0 = Constraint::New < Vector3 > ( Actor::POSITION,
764 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
765 AnimScrollConstraint( backgroundAnimLayer0.GetCurrentPosition(), 0.75f ) );
766 backgroundAnimLayer0.ApplyConstraint( animConstraint0 );
768 Constraint animConstraint1 = Constraint::New < Vector3 > ( Actor::POSITION,
769 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
770 AnimScrollConstraint( backgroundAnimLayer1.GetCurrentPosition(), 0.5f ) );
771 backgroundAnimLayer1.ApplyConstraint( animConstraint1 );
773 Constraint animConstraint2 = Constraint::New < Vector3 > ( Actor::POSITION,
774 Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
775 AnimScrollConstraint( backgroundAnimLayer2.GetCurrentPosition(), 0.25f ) );
776 backgroundAnimLayer2.ApplyConstraint( animConstraint2 );
779 ImageActor layer = Dali::Toolkit::CreateSolidColorActor( BACKGROUND_COLOR );
780 layer.SetAnchorPoint( AnchorPoint::CENTER );
781 layer.SetParentOrigin( ParentOrigin::CENTER );
782 layer.SetSize( size * BACKGROUND_SIZE_SCALE );
783 layer.SetZ( BACKGROUND_Z );
784 layer.SetPositionInheritanceMode( DONT_INHERIT_POSITION );
785 backgroundLayer.Add( layer );
788 bubbleLayer.Add( backgroundAnimLayer0 );
789 bubbleLayer.Add( backgroundAnimLayer1 );
790 bubbleLayer.Add( backgroundAnimLayer2 );
792 // Add all the children
793 AddBackgroundActors( backgroundAnimLayer0, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
794 AddBackgroundActors( backgroundAnimLayer1, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
795 AddBackgroundActors( backgroundAnimLayer2, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
798 void DaliTableView::AddBackgroundActors( Actor layer, int count, BitmapImage distanceField, const Dali::Vector2& size )
800 for( int i = 0; i < count; ++i )
802 float randSize = Random::Range( 10.0f, 400.0f );
803 float hue = Random::Range( 0.3f, 1.0f );
804 Vector4 randColour( hue, hue*0.5, 0.0f, Random::Range( 0.3f, 0.6f ));
806 ImageActor dfActor = ImageActor::New( distanceField );
807 mBackgroundActors.push_back( dfActor );
808 dfActor.SetSize( Vector2( randSize, randSize ) );
809 dfActor.SetParentOrigin( ParentOrigin::CENTER );
811 Toolkit::DistanceFieldEffect effect = Toolkit::DistanceFieldEffect::New();
812 dfActor.SetShaderEffect( effect );
813 dfActor.SetColor( randColour );
814 effect.SetOutlineParams( Vector2( 0.55f, 0.00f ) );
815 effect.SetSmoothingEdge( 0.5f );
816 layer.Add( dfActor );
820 Random::Range( -size.x * 0.5f * BACKGROUND_SPREAD_SCALE, size.x * 0.5f * BACKGROUND_SPREAD_SCALE ),
821 Random::Range( -size.y * 0.5f - randSize, size.y * 0.5f + randSize ),
822 Random::Range( BUBBLE_MIN_Z, BUBBLE_MAX_Z ) );
823 dfActor.SetPosition( actorPos );
825 Constraint movementConstraint = Constraint::New < Vector3 > ( Actor::POSITION,
826 LocalSource( Actor::SIZE ),
827 ParentSource( Actor::SIZE ),
828 ShapeMovementConstraint );
829 dfActor.ApplyConstraint( movementConstraint );
832 Animation animation = Animation::New( Random::Range( 40.0f, 200.0f ) );
833 KeyFrames keyframes = KeyFrames::New();
834 keyframes.Add( 0.0f, actorPos );
835 Vector3 toPos( actorPos );
836 toPos.y -= ( size.y + randSize );
837 keyframes.Add( 1.0f, toPos );
838 animation.AnimateBetween( Property( dfActor, Actor::POSITION ), keyframes );
839 animation.SetLooping( true );
841 mBackgroundAnimations.push_back( animation );
845 void DaliTableView::CreateShapeImage( ShapeType shapeType, const Size& size, BitmapImage& distanceFieldOut )
847 // this bitmap will hold the alpha map for the distance field shader
848 distanceFieldOut = BitmapImage::New( size.width, size.height, Pixel::A8 );
850 // Generate bit pattern
851 std::vector< unsigned char > imageDataA8;
852 imageDataA8.reserve( size.width * size.height ); // A8
857 GenerateCircle( size, imageDataA8 );
860 GenerateSquare( size, imageDataA8 );
866 PixelBuffer* buffer = distanceFieldOut.GetBuffer();
869 GenerateDistanceFieldMap( &imageDataA8[ 0 ], size, buffer, size, 8.0f, size );
870 distanceFieldOut.Update();
874 void DaliTableView::GenerateSquare( const Size& size, std::vector< unsigned char >& distanceFieldOut )
876 for( int h = 0; h < size.height; ++h )
878 for( int w = 0; w < size.width; ++w )
880 distanceFieldOut.push_back( 0xFF );
885 void DaliTableView::GenerateCircle( const Size& size, std::vector< unsigned char >& distanceFieldOut )
887 const float radius = size.width * 0.5f * size.width * 0.5f;
888 Vector2 center( size.width / 2, size.height / 2 );
890 for( int h = 0; h < size.height; ++h )
892 for( int w = 0; w < size.width; ++w )
895 Vector2 dist = pos - center;
897 if( dist.x * dist.x + dist.y * dist.y > radius )
899 distanceFieldOut.push_back( 0x00 );
903 distanceFieldOut.push_back( 0xFF );
909 ImageActor DaliTableView::CreateLogo( std::string imagePath )
911 Image image = Image::New( imagePath );
912 ImageActor logo = ImageActor::New( image );
914 logo.SetAnchorPoint( AnchorPoint::CENTER );
915 logo.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
920 bool DaliTableView::PauseBackgroundAnimation()
927 void DaliTableView::PauseAnimation()
929 if( mBackgroundAnimsPlaying )
931 for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
933 Animation anim = *animIter;
938 mBackgroundAnimsPlaying = false;
942 void DaliTableView::PlayAnimation()
944 if ( !mBackgroundAnimsPlaying )
946 for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
948 Animation anim = *animIter;
953 mBackgroundAnimsPlaying = true;
956 mAnimationTimer.SetInterval( BACKGROUND_ANIMATION_DURATION );
959 Dali::Actor DaliTableView::OnKeyboardPreFocusChange( Dali::Actor current, Dali::Actor proposed, Dali::Toolkit::Control::KeyboardFocusNavigationDirection direction )
961 Actor nextFocusActor = proposed;
963 if ( !current && !proposed )
965 // Set the initial focus to the first tile in the current page should be focused.
966 nextFocusActor = mPages[mScrollView.GetCurrentPage()].GetChildAt(0);
968 else if( !proposed || (proposed && proposed == mScrollViewLayer) )
970 // ScrollView is being focused but nothing in the current page can be focused further
971 // in the given direction. We should work out which page to scroll to next.
972 int currentPage = mScrollView.GetCurrentPage();
973 int newPage = currentPage;
974 if( direction == Dali::Toolkit::Control::Left )
978 else if( direction == Dali::Toolkit::Control::Right )
983 newPage = std::max(0, std::min(static_cast<int>(mScrollRulerX->GetTotalPages() - 1), newPage));
984 if( newPage == currentPage )
986 if( direction == Dali::Toolkit::Control::Left )
988 newPage = mScrollRulerX->GetTotalPages() - 1;
989 } else if( direction == Dali::Toolkit::Control::Right )
995 // Scroll to the page in the given direction
996 mScrollView.ScrollTo(newPage);
998 if( direction == Dali::Toolkit::Control::Left )
1000 // Work out the cell position for the last tile
1001 int remainingExamples = mExampleList.size() - newPage * EXAMPLES_PER_PAGE;
1002 int rowPos = (remainingExamples >= EXAMPLES_PER_PAGE) ? ROWS_PER_PAGE - 1 : ( (remainingExamples % EXAMPLES_PER_PAGE + EXAMPLES_PER_ROW) / EXAMPLES_PER_ROW - 1 );
1003 int colPos = remainingExamples >= EXAMPLES_PER_PAGE ? EXAMPLES_PER_ROW - 1 : ( remainingExamples % EXAMPLES_PER_PAGE - rowPos * EXAMPLES_PER_ROW - 1 );
1005 // Move the focus to the last tile in the new page.
1006 nextFocusActor = mPages[newPage].GetChildAt(colPos * EXAMPLES_PER_ROW + rowPos);
1010 // Move the focus to the first tile in the new page.
1011 nextFocusActor = mPages[newPage].GetChildAt(0);
1015 return nextFocusActor;
1018 void DaliTableView::OnFocusedActorActivated( Dali::Actor activatedActor )
1022 mPressedActor = activatedActor;
1024 // Activate the current focused actor;
1025 TouchEvent touchEventUp;
1026 touchEventUp.points.push_back( TouchPoint ( 0, TouchPoint::Up, 0.0f, 0.0f ) );
1027 OnTilePressed(mPressedActor, touchEventUp);
1031 bool DaliTableView::OnTileHovered( Actor actor, const HoverEvent& event )
1033 KeyboardFocusManager::Get().SetCurrentFocusActor( actor );