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 #include "shared/view.h"
30 using namespace Dali::Toolkit;
32 ///////////////////////////////////////////////////////////////////////////////
37 const std::string BUTTON_BACKWARD( "Backward" );
38 const std::string BUTTON_FORWARD( "Forward" );
39 const std::string BUTTON_QUIT( "Quit" );
40 const std::string BUTTON_OK( "Ok" );
41 const std::string BUTTON_CANCEL( "Cancel" );
43 const std::string DEFAULT_BACKGROUND_IMAGE_PATH( DALI_IMAGE_DIR "background-gradient.jpg" );
44 const std::string LOGO_PATH( DALI_IMAGE_DIR "dali-logo.png" );
45 const std::string DEFAULT_TOOLBAR_IMAGE_PATH( DALI_IMAGE_DIR "top-bar.png" );
46 const std::string BUTTON_BACKGROUND(DALI_IMAGE_DIR "button-background.png");
47 const std::string TILE_BACKGROUND(DALI_IMAGE_DIR "item-background.png");
48 const std::string TILE_BACKGROUND_ALPHA(DALI_IMAGE_DIR "item-background-alpha.png");
50 const char * const DEFAULT_TOOLBAR_TEXT( "TOUCH TO LAUNCH EXAMPLE" );
52 const float BUTTON_PRESS_ANIMATION_TIME = 0.25f; ///< Time to perform button scale effect.
53 const float ROTATE_ANIMATION_TIME = 0.5f; ///< Time to perform rotate effect.
54 const int MAX_PAGES = 256; ///< Maximum pages (arbitrary safety limit)
55 const int EXAMPLES_PER_ROW = 3;
56 const int ROWS_PER_PAGE = 3;
57 const int EXAMPLES_PER_PAGE = EXAMPLES_PER_ROW * ROWS_PER_PAGE;
58 const float LOGO_MARGIN_RATIO = 0.1f / 0.3f;
59 const float BOTTOM_PADDING_RATIO = 0.4f / 0.9f;
60 const Vector3 SCROLLVIEW_RELATIVE_SIZE(0.9f, 1.0f, 0.8f ); ///< ScrollView's relative size to its parent
61 const Vector3 TABLE_RELATIVE_SIZE(0.95f, 0.9f, 0.8f ); ///< TableView's relative size to the entire stage. The Y value means sum of the logo and table relative heights.
62 const float STENCIL_RELATIVE_SIZE = 1.0f;
64 const float EFFECT_SNAP_DURATION = 0.66f; ///< Scroll Snap Duration for Effects
65 const float EFFECT_FLICK_DURATION = 0.5f; ///< Scroll Flick Duration for Effects
66 const Vector3 ANGLE_CUBE_PAGE_ROTATE(Math::PI * 0.5f, Math::PI * 0.5f, 0.0f);
70 const int NUM_BACKGROUND_IMAGES = 18;
71 const float BACKGROUND_SWIPE_SCALE = 0.025f;
72 const float BACKGROUND_SPREAD_SCALE = 1.5f;
73 const float SCALE_MOD = 1000.0f * Math::PI * 2.0f;
74 const float SCALE_SPEED = 10.0f;
75 const float SCALE_SPEED_SIN = 0.1f;
77 const unsigned int BACKGROUND_ANIMATION_DURATION = 15000; // 15 secs
79 const float BACKGROUND_Z = -1.0f;
80 const Vector4 BACKGROUND_COLOR( 1.0f, 1.0f, 1.0f, 1.0f );
82 const float BUBBLE_MIN_Z = -1.0;
83 const float BUBBLE_MAX_Z = 0.0f;
86 * Creates the background image
88 ImageActor CreateBackground( std::string imagePath )
90 Image image = ResourceImage::New( imagePath );
91 ImageActor background = ImageActor::New( image );
92 background.SetName( "BACKGROUND" );
93 background.SetAnchorPoint( AnchorPoint::CENTER );
94 background.SetParentOrigin( ParentOrigin::CENTER );
95 background.SetZ( -1.0f );
96 background.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
101 // These values depend on the tile image
102 const float IMAGE_BORDER_LEFT = 11.0f;
103 const float IMAGE_BORDER_RIGHT = IMAGE_BORDER_LEFT;
104 const float IMAGE_BORDER_TOP = IMAGE_BORDER_LEFT;
105 const float IMAGE_BORDER_BOTTOM = IMAGE_BORDER_LEFT;
108 * Constraint to return a position for a bubble based on the scroll value and vertical wrapping
110 struct AnimateBubbleConstraint
113 AnimateBubbleConstraint( const Vector3& initialPos, float scale )
114 : mInitialX( initialPos.x ),
119 void operator()( Vector3& position, const PropertyInputContainer& inputs )
121 const Vector3& parentSize = inputs[1]->GetVector3();
122 const Vector3& childSize = inputs[2]->GetVector3();
124 // Wrap bubbles verically.
125 if( position.y + childSize.y * 0.5f < -parentSize.y * 0.5f )
127 position.y = parentSize.y * 0.5f + childSize.y * 0.5f;
130 // Bubbles X position moves parallax to horizontal
131 // panning by a scale factor unique to each bubble.
132 position.x = mInitialX + ( inputs[0]->GetVector2().x * mScale );
141 bool CompareByTitle( const Example& lhs, const Example& rhs )
143 return lhs.title < rhs.title;
148 DaliTableView::DaliTableView( Application& application )
149 : mApplication( application ),
165 mButtonsPageRelativeSize(),
169 mBackgroundAnimations(),
172 mBackgroundImagePath( DEFAULT_BACKGROUND_IMAGE_PATH ),
175 mSortAlphabetically( false ),
176 mBackgroundAnimsPlaying( false ),
177 mVersionPopupShown( false )
179 application.InitSignal().Connect( this, &DaliTableView::Initialize );
182 DaliTableView::~DaliTableView()
186 void DaliTableView::AddExample( Example example )
188 mExampleList.push_back( example );
189 mExampleMap[ example.name ] = example;
192 void DaliTableView::SetBackgroundPath( std::string imagePath )
194 mBackgroundImagePath = imagePath;
197 void DaliTableView::SortAlphabetically( bool sortAlphabetically )
199 mSortAlphabetically = sortAlphabetically;
202 void DaliTableView::Initialize( Application& application )
204 DemoHelper::RequestThemeChange();
206 Stage::GetCurrent().KeyEventSignal().Connect( this, &DaliTableView::OnKeyEvent );
208 const Vector2 stageSize = Stage::GetCurrent().GetSize();
211 Actor background = CreateBackground( mBackgroundImagePath );
212 Stage::GetCurrent().Add( background );
214 // Render entire content as overlays, as is all on same 2D plane.
215 mRootActor = TableView::New( 4, 1 );
216 mRootActor.SetAnchorPoint( AnchorPoint::CENTER );
217 mRootActor.SetParentOrigin( ParentOrigin::CENTER );
218 mRootActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
219 Stage::GetCurrent().Add( mRootActor );
222 Dali::Toolkit::ToolBar toolbar;
223 Dali::Layer toolBarLayer = DemoHelper::CreateToolbar(toolbar,
224 DEFAULT_TOOLBAR_IMAGE_PATH,
225 DEFAULT_TOOLBAR_TEXT,
226 DemoHelper::DEFAULT_VIEW_STYLE);
228 mRootActor.AddChild( toolBarLayer, TableView::CellPosition( 0, 0 ) );
229 mRootActor.SetFitHeight( 0 );
232 Dali::ImageActor logo = CreateLogo( LOGO_PATH );
233 logo.SetName( "LOGO_IMAGE" );
234 logo.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
235 const float paddingHeight = ( ( 1.f-TABLE_RELATIVE_SIZE.y ) * stageSize.y );
236 const float logoMargin = paddingHeight * LOGO_MARGIN_RATIO;
238 // Show version in a popup when log is tapped
239 mLogoTapDetector = TapGestureDetector::New();
240 mLogoTapDetector.Attach( logo );
241 mLogoTapDetector.DetectedSignal().Connect( this, &DaliTableView::OnLogoTapped );
243 const float bottomMargin = paddingHeight * BOTTOM_PADDING_RATIO;
245 Alignment alignment = Alignment::New();
246 alignment.SetName( "LOGO_ALIGNMENT" );
247 alignment.Add( logo );
248 alignment.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
249 alignment.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::HEIGHT );
250 Actor alignmentActor = alignment;
251 alignmentActor.SetPadding( Padding( 0.0f, 0.0f, logoMargin, logoMargin ));
252 mRootActor.AddChild( alignment, TableView::CellPosition( 1, 0 ) );
253 mRootActor.SetFitHeight( 1 );
255 // scrollview occupying the majority of the screen
256 mScrollView = ScrollView::New();
258 mScrollView.SetAnchorPoint( AnchorPoint::CENTER );
259 mScrollView.SetParentOrigin( ParentOrigin::CENTER );
260 mScrollView.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
261 const float buttonsPageMargin = ( 1.0f - TABLE_RELATIVE_SIZE.x ) * 0.5f * stageSize.width;
262 mScrollView.SetPadding( Padding( buttonsPageMargin, buttonsPageMargin, 0.0f, 0.0f ) );
264 mScrollView.SetAxisAutoLock( true );
265 mScrollView.ScrollCompletedSignal().Connect( this, &DaliTableView::OnScrollComplete );
266 mScrollView.ScrollStartedSignal().Connect( this, &DaliTableView::OnScrollStart );
267 mScrollView.TouchedSignal().Connect( this, &DaliTableView::OnScrollTouched );
269 mScrollViewLayer = Layer::New();
270 mScrollViewLayer.SetAnchorPoint( AnchorPoint::CENTER );
271 mScrollViewLayer.SetParentOrigin( ParentOrigin::CENTER );
272 mScrollViewLayer.SetDrawMode( DrawMode::OVERLAY );
273 mScrollViewLayer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
275 // Create solid background colour.
276 ImageActor backgroundColourActor = Dali::Toolkit::CreateSolidColorActor( BACKGROUND_COLOR );
277 backgroundColourActor.SetAnchorPoint( AnchorPoint::CENTER );
278 backgroundColourActor.SetParentOrigin( ParentOrigin::CENTER );
279 backgroundColourActor.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
280 backgroundColourActor.SetSizeModeFactor( Vector3( 1.0f, 1.5f, 1.0f ) );
281 backgroundColourActor.SetZ( BACKGROUND_Z );
282 mScrollViewLayer.Add( backgroundColourActor );
284 // Populate background and bubbles - needs to be scrollViewLayer so scroll ends show
285 Actor bubbleContainer = Actor::New();
286 bubbleContainer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
287 bubbleContainer.SetAnchorPoint( AnchorPoint::CENTER );
288 bubbleContainer.SetParentOrigin( ParentOrigin::CENTER );
289 mScrollViewLayer.Add( bubbleContainer );
291 SetupBackground( bubbleContainer );
293 Alignment buttonsAlignment = Alignment::New();
294 buttonsAlignment.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
295 buttonsAlignment.Add( mScrollViewLayer );
297 mScrollViewLayer.Add( mScrollView );
299 mRootActor.AddChild( buttonsAlignment, TableView::CellPosition( 2, 0 ) );
301 mRootActor.SetFixedHeight( 3, bottomMargin );
303 // Add scroll view effect and setup constraints on pages
304 ApplyScrollViewEffect();
306 // Add pages and tiles
309 // Remove constraints for inner cube effect
310 ApplyCubeEffectToPages();
312 Dali::Window winHandle = application.GetWindow();
313 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
314 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE );
315 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
316 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
318 // Set initial orientation
319 Dali::Orientation orientation = winHandle.GetOrientation();
321 unsigned int degrees = winHandle.GetOrientation().GetDegrees();
324 orientation.ChangedSignal().Connect( this, &DaliTableView::OrientationChanged );
326 winHandle.ShowIndicator( Dali::Window::INVISIBLE );
328 // Background animation
329 mAnimationTimer = Timer::New( BACKGROUND_ANIMATION_DURATION );
330 mAnimationTimer.TickSignal().Connect( this, &DaliTableView::PauseBackgroundAnimation );
331 mAnimationTimer.Start();
332 mBackgroundAnimsPlaying = true;
334 KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &DaliTableView::OnKeyboardPreFocusChange );
335 KeyboardFocusManager::Get().FocusedActorActivatedSignal().Connect( this, &DaliTableView::OnFocusedActorActivated );
338 void DaliTableView::ApplyCubeEffectToPages()
340 ScrollViewPagePathEffect effect = ScrollViewPagePathEffect::DownCast( mScrollViewEffect );
341 unsigned int pageCount(0);
342 for( std::vector< Actor >::iterator pageIter = mPages.begin(); pageIter != mPages.end(); ++pageIter )
344 Actor page = *pageIter;
345 effect.ApplyToPage( page, pageCount++ );
349 void DaliTableView::OnButtonsPageRelayout( const Dali::Actor& actor )
354 void DaliTableView::Populate()
356 const Vector2 stageSize = Stage::GetCurrent().GetSize();
358 mTotalPages = ( mExampleList.size() + EXAMPLES_PER_PAGE - 1 ) / EXAMPLES_PER_PAGE;
360 // Populate ScrollView.
361 if( mExampleList.size() > 0 )
363 if( mSortAlphabetically )
365 sort( mExampleList.begin(), mExampleList.end(), CompareByTitle );
368 unsigned int exampleCount = 0;
369 ExampleListConstIter iter = mExampleList.begin();
371 for( int t = 0; t < mTotalPages; t++ )
373 // Create Table. (contains up to 9 Examples)
374 TableView page = TableView::New( 3, 3 );
375 page.SetAnchorPoint( AnchorPoint::CENTER );
376 page.SetParentOrigin( ParentOrigin::CENTER );
377 page.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
378 mScrollView.Add( page );
380 // Calculate the number of images going across (columns) within a page, according to the screen resolution and dpi.
381 const float margin = 2.0f;
382 const float tileParentMultiplier = 1.0f / EXAMPLES_PER_ROW;
384 for(int row = 0; row < ROWS_PER_PAGE; row++)
386 for(int column = 0; column < EXAMPLES_PER_ROW; column++)
388 const Example& example = ( *iter );
390 Actor tile = CreateTile( example.name, example.title, Vector3( tileParentMultiplier, tileParentMultiplier, 1.0f ), true );
391 FocusManager focusManager = FocusManager::Get();
392 focusManager.SetFocusOrder( tile, ++exampleCount );
393 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_LABEL,
395 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_TRAIT, "Tile" );
396 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_HINT,
397 "You can run this example" );
399 tile.SetPadding( Padding( margin, margin, margin, margin ) );
401 page.AddChild( tile, TableView::CellPosition( row, column ) );
405 if( iter == mExampleList.end() )
411 if( iter == mExampleList.end() )
417 // Set tableview position
418 Vector3 pagePos( stageSize.width * TABLE_RELATIVE_SIZE.x * t, 0.0f, 0.0f );
419 page.SetPosition( pagePos );
421 mPages.push_back( page );
423 if( iter == mExampleList.end() )
430 // Update Ruler info.
431 mScrollRulerX = new FixedRuler( stageSize.width * TABLE_RELATIVE_SIZE.x * 0.5f );
432 mScrollRulerY = new DefaultRuler();
433 mScrollRulerX->SetDomain( RulerDomain( 0.0f, (mTotalPages+1) * stageSize.width * TABLE_RELATIVE_SIZE.x * 0.5f, true ) );
434 mScrollRulerY->Disable();
435 mScrollView.SetRulerX( mScrollRulerX );
436 mScrollView.SetRulerY( mScrollRulerY );
439 void DaliTableView::OrientationChanged( Orientation orientation )
441 // TODO: Implement if orientation change required
444 void DaliTableView::Rotate( unsigned int degrees )
446 // Resize the root actor
447 Vector2 stageSize = Stage::GetCurrent().GetSize();
448 Vector3 targetSize( stageSize.x, stageSize.y, 1.0f );
450 if( degrees == 90 || degrees == 270 )
452 targetSize = Vector3( stageSize.y, stageSize.x, 1.0f );
455 if( mRotateAnimation )
457 mRotateAnimation.Stop();
458 mRotateAnimation.Clear();
461 mRotateAnimation = Animation::New( ROTATE_ANIMATION_TIME );
462 mRotateAnimation.AnimateTo( Property( mRootActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( 360 - degrees ) ), Vector3::ZAXIS ), AlphaFunction::EASE_OUT );
463 mRotateAnimation.AnimateTo( Property( mRootActor, Actor::Property::SIZE ), targetSize, AlphaFunction::EASE_OUT );
464 mRotateAnimation.Play();
467 Actor DaliTableView::CreateTile( const std::string& name, const std::string& title, const Dali::Vector3& sizeMultiplier, bool addBackground )
469 Actor content = Actor::New();
470 content.SetName( name );
471 content.SetAnchorPoint( AnchorPoint::CENTER );
472 content.SetParentOrigin( ParentOrigin::CENTER );
473 content.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
474 content.SetSizeModeFactor( sizeMultiplier );
476 // create background image
479 Image bg = ResourceImage::New( TILE_BACKGROUND );
480 ImageActor image = ImageActor::New( bg );
481 image.SetAnchorPoint( AnchorPoint::CENTER );
482 image.SetParentOrigin( ParentOrigin::CENTER );
483 // make the image 100% of tile
484 image.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
486 // move image back to get text appear in front
488 image.SetStyle( ImageActor::STYLE_NINE_PATCH );
489 image.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
490 content.Add( image );
493 ImageActor stencil = NewStencilImage();
494 stencil.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
495 image.Add( stencil );
498 TextLabel label = TextLabel::New();
499 label.SetAnchorPoint( AnchorPoint::TOP_LEFT );
500 label.SetProperty( Control::Property::STYLE_NAME, "launcherlabel" );
501 label.SetProperty( TextLabel::Property::MULTI_LINE, true );
502 label.SetProperty( TextLabel::Property::TEXT, title );
503 label.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
504 label.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
505 label.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
506 content.Add( label );
508 // Set the tile to be keyboard focusable
509 content.SetKeyboardFocusable(true);
511 // connect to the touch events
512 content.TouchedSignal().Connect( this, &DaliTableView::OnTilePressed );
513 content.HoveredSignal().Connect( this, &DaliTableView::OnTileHovered );
518 ImageActor DaliTableView::NewStencilImage()
520 Image alpha = ResourceImage::New( TILE_BACKGROUND_ALPHA );
522 ImageActor stencilActor = ImageActor::New( alpha );
523 stencilActor.SetStyle( ImageActor::STYLE_NINE_PATCH );
524 stencilActor.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
526 stencilActor.SetParentOrigin( ParentOrigin::CENTER );
527 stencilActor.SetAnchorPoint( AnchorPoint::CENTER );
528 stencilActor.SetDrawMode( DrawMode::STENCIL );
530 Dali::ShaderEffect shaderEffect = AlphaDiscardEffect::New();
531 stencilActor.SetShaderEffect( shaderEffect );
536 bool DaliTableView::OnTilePressed( Actor actor, const TouchEvent& event )
538 bool consumed = false;
540 const TouchPoint& point = event.GetPoint( 0 );
541 if( TouchPoint::Down == point.state )
543 mPressedActor = actor;
547 // A button press is only valid if the Down & Up events
548 // both occurred within the button.
549 if( ( TouchPoint::Up == point.state ) &&
550 ( mPressedActor == actor ) )
552 std::string name = actor.GetName();
553 ExampleMapConstIter iter = mExampleMap.find( name );
555 FocusManager focusManager = FocusManager::Get();
557 if( iter != mExampleMap.end() )
559 // ignore Example button presses when scrolling or button animating.
560 if( ( !mScrolling ) && ( !mPressedAnimation ) )
562 // do nothing, until pressed animation finished.
569 mPressedAnimation = Animation::New( BUTTON_PRESS_ANIMATION_TIME );
570 mPressedAnimation.SetEndAction( Animation::Discard );
572 // scale the content actor within the Tile, as to not affect the placement within the Table.
573 Actor content = actor.GetChildAt(0);
574 mPressedAnimation.AnimateTo( Property( content, Actor::Property::SCALE ), Vector3( 0.9f, 0.9f, 1.0f ), AlphaFunction::EASE_IN_OUT,
575 TimePeriod( 0.0f, BUTTON_PRESS_ANIMATION_TIME * 0.5f ) );
576 mPressedAnimation.AnimateTo( Property( content, Actor::Property::SCALE ), Vector3::ONE, AlphaFunction::EASE_IN_OUT,
577 TimePeriod( BUTTON_PRESS_ANIMATION_TIME * 0.5f, BUTTON_PRESS_ANIMATION_TIME * 0.5f ) );
578 mPressedAnimation.Play();
579 mPressedAnimation.FinishedSignal().Connect( this, &DaliTableView::OnPressedAnimationFinished );
585 void DaliTableView::OnPressedAnimationFinished( Dali::Animation& source )
587 mPressedAnimation.Reset();
590 std::string name = mPressedActor.GetName();
591 ExampleMapConstIter iter = mExampleMap.find( name );
593 if( iter == mExampleMap.end() )
595 if( name == BUTTON_QUIT )
597 // Move focus to the OK button
598 FocusManager focusManager = FocusManager::Get();
600 // Enable the group mode and wrap mode
601 focusManager.SetGroupMode( true );
602 focusManager.SetWrapMode( true );
607 const Example& example( iter->second );
609 std::stringstream stream;
610 stream << DALI_EXAMPLE_BIN << example.name.c_str();
614 execlp( stream.str().c_str(), example.name.c_str(), NULL );
615 DALI_ASSERT_ALWAYS(false && "exec failed!");
618 mPressedActor.Reset();
622 void DaliTableView::OnScrollStart( const Dali::Vector2& position )
629 void DaliTableView::OnScrollComplete( const Dali::Vector2& position )
633 // move focus to 1st item of new page
634 FocusManager focusManager = FocusManager::Get();
635 focusManager.SetCurrentFocusActor(mPages[mScrollView.GetCurrentPage()].GetChildAt(0) );
638 bool DaliTableView::OnScrollTouched( Actor actor, const TouchEvent& event )
640 const TouchPoint& point = event.GetPoint( 0 );
641 if( TouchPoint::Down == point.state )
643 mPressedActor = actor;
649 void DaliTableView::ApplyScrollViewEffect()
651 // Remove old effect if exists.
653 if( mScrollViewEffect )
655 mScrollView.RemoveEffect( mScrollViewEffect );
658 // Just one effect for now
659 SetupInnerPageCubeEffect();
661 mScrollView.ApplyEffect( mScrollViewEffect );
664 void DaliTableView::SetupInnerPageCubeEffect()
666 const Vector2 stageSize = Stage::GetCurrent().GetSize();
668 Dali::Path path = Dali::Path::New();
669 Dali::Property::Array points;
671 points[0] = Vector3( stageSize.x*0.5, 0.0f, stageSize.x*0.5f);
672 points[1] = Vector3( 0.0f, 0.0f, 0.0f );
673 points[2] = Vector3( -stageSize.x*0.5f, 0.0f, stageSize.x*0.5f);
674 path.SetProperty( Path::Property::POINTS, points );
676 Dali::Property::Array controlPoints;
677 controlPoints.Resize(4);
678 controlPoints[0] = Vector3( stageSize.x*0.5f, 0.0f, stageSize.x*0.3f );
679 controlPoints[1] = Vector3( stageSize.x*0.3f, 0.0f, 0.0f );
680 controlPoints[2] = Vector3(-stageSize.x*0.3f, 0.0f, 0.0f );
681 controlPoints[3] = Vector3(-stageSize.x*0.5f, 0.0f, stageSize.x*0.3f );
682 path.SetProperty( Path::Property::CONTROL_POINTS, controlPoints );
685 mScrollViewEffect = ScrollViewPagePathEffect::New(path,
686 Vector3(-1.0f,0.0f,0.0f),
687 Toolkit::ScrollView::Property::SCROLL_FINAL_X,
688 Vector3(stageSize.x*TABLE_RELATIVE_SIZE.x,stageSize.y*TABLE_RELATIVE_SIZE.y,0.0f),mTotalPages);
691 void DaliTableView::OnKeyEvent( const KeyEvent& event )
693 if( event.state == KeyEvent::Down )
695 if ( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
697 if ( mVersionPopup && mVersionPopupShown )
709 void DaliTableView::SetupBackground( Actor bubbleContainer )
711 // Create distance field shape.
712 BufferImage distanceField;
713 Size imageSize( 512, 512 );
714 CreateShapeImage( CIRCLE, imageSize, distanceField );
716 // Add bubbles to the bubbleContainer.
717 // Note: The bubbleContainer is parented externally to this function.
718 AddBackgroundActors( bubbleContainer, NUM_BACKGROUND_IMAGES, distanceField );
721 void DaliTableView::InitialiseBackgroundActors( Actor actor )
723 // Delete current animations
724 mBackgroundAnimations.clear();
726 // Create new animations
727 const Vector3 size = actor.GetTargetSize();
729 for( unsigned int i = 0, childCount = actor.GetChildCount(); i < childCount; ++i )
731 Actor child = actor.GetChildAt( i );
733 // Calculate a random position
734 Vector3 childPos( Random::Range( -size.x * 0.5f * BACKGROUND_SPREAD_SCALE, size.x * 0.5f * BACKGROUND_SPREAD_SCALE ),
735 Random::Range( -size.y, size.y ),
736 Random::Range( BUBBLE_MIN_Z, BUBBLE_MAX_Z ) );
738 child.SetPosition( childPos );
740 // Define bubble horizontal parallax and vertical wrapping
741 Constraint animConstraint = Constraint::New < Vector3 > ( child, Actor::Property::POSITION, AnimateBubbleConstraint( childPos, Random::Range( -0.85f, 0.25f ) ) );
742 animConstraint.AddSource( Source( mScrollView, ScrollView::Property::SCROLL_POSITION ) );
743 animConstraint.AddSource( Dali::ParentSource( Dali::Actor::Property::SIZE ) );
744 animConstraint.AddSource( Dali::LocalSource( Dali::Actor::Property::SIZE ) );
745 animConstraint.Apply();
748 Animation animation = Animation::New( Random::Range( 40.0f, 80.0f ) );
749 animation.AnimateBy( Property( child, Actor::Property::POSITION ), Vector3( 0.0f, -1.0f, 0.0f ), AlphaFunction::LINEAR );
750 animation.SetLooping( true );
752 mBackgroundAnimations.push_back( animation );
756 void DaliTableView::AddBackgroundActors( Actor layer, int count, BufferImage distanceField )
758 for( int i = 0; i < count; ++i )
760 float randSize = Random::Range( 10.0f, 400.0f );
761 float hue = Random::Range( 0.3f, 1.0f );
762 Vector4 randColour( hue, hue * 0.5, 0.0f, Random::Range( 0.3f, 0.6f ));
764 ImageActor dfActor = ImageActor::New( distanceField );
765 dfActor.SetSize( Vector2( randSize, randSize ) );
766 dfActor.SetParentOrigin( ParentOrigin::CENTER );
768 Toolkit::DistanceFieldEffect effect = Toolkit::DistanceFieldEffect::New();
769 dfActor.SetShaderEffect( effect );
770 dfActor.SetColor( randColour );
771 effect.SetOutlineParams( Vector2( 0.55f, 0.00f ) );
772 effect.SetSmoothingEdge( 0.5f );
773 layer.Add( dfActor );
776 // Positioning will occur when the layer is relaid out
777 layer.OnRelayoutSignal().Connect( this, &DaliTableView::InitialiseBackgroundActors );
780 void DaliTableView::CreateShapeImage( ShapeType shapeType, const Size& size, BufferImage& distanceFieldOut )
782 // this bitmap will hold the alpha map for the distance field shader
783 distanceFieldOut = BufferImage::New( size.width, size.height, Pixel::A8 );
785 // Generate bit pattern
786 std::vector< unsigned char > imageDataA8;
787 imageDataA8.reserve( size.width * size.height ); // A8
792 GenerateCircle( size, imageDataA8 );
795 GenerateSquare( size, imageDataA8 );
801 PixelBuffer* buffer = distanceFieldOut.GetBuffer();
804 GenerateDistanceFieldMap( &imageDataA8[ 0 ], size, buffer, size, 8.0f, size );
805 distanceFieldOut.Update();
809 void DaliTableView::GenerateSquare( const Size& size, std::vector< unsigned char >& distanceFieldOut )
811 for( int h = 0; h < size.height; ++h )
813 for( int w = 0; w < size.width; ++w )
815 distanceFieldOut.push_back( 0xFF );
820 void DaliTableView::GenerateCircle( const Size& size, std::vector< unsigned char >& distanceFieldOut )
822 const float radius = size.width * 0.5f * size.width * 0.5f;
823 Vector2 center( size.width / 2, size.height / 2 );
825 for( int h = 0; h < size.height; ++h )
827 for( int w = 0; w < size.width; ++w )
830 Vector2 dist = pos - center;
832 if( dist.x * dist.x + dist.y * dist.y > radius )
834 distanceFieldOut.push_back( 0x00 );
838 distanceFieldOut.push_back( 0xFF );
844 ImageActor DaliTableView::CreateLogo( std::string imagePath )
846 Image image = ResourceImage::New( imagePath );
847 ImageActor logo = ImageActor::New( image );
849 logo.SetAnchorPoint( AnchorPoint::CENTER );
850 logo.SetParentOrigin( ParentOrigin::CENTER );
855 bool DaliTableView::PauseBackgroundAnimation()
862 void DaliTableView::PauseAnimation()
864 if( mBackgroundAnimsPlaying )
866 for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
868 Animation anim = *animIter;
873 mBackgroundAnimsPlaying = false;
877 void DaliTableView::PlayAnimation()
879 if ( !mBackgroundAnimsPlaying )
881 for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
883 Animation anim = *animIter;
888 mBackgroundAnimsPlaying = true;
891 mAnimationTimer.SetInterval( BACKGROUND_ANIMATION_DURATION );
894 Dali::Actor DaliTableView::OnKeyboardPreFocusChange( Dali::Actor current, Dali::Actor proposed, Dali::Toolkit::Control::KeyboardFocusNavigationDirection direction )
896 Actor nextFocusActor = proposed;
898 if ( !current && !proposed )
900 // Set the initial focus to the first tile in the current page should be focused.
901 nextFocusActor = mPages[mScrollView.GetCurrentPage()].GetChildAt(0);
903 else if( !proposed || (proposed && proposed == mScrollViewLayer) )
905 // ScrollView is being focused but nothing in the current page can be focused further
906 // in the given direction. We should work out which page to scroll to next.
907 int currentPage = mScrollView.GetCurrentPage();
908 int newPage = currentPage;
909 if( direction == Dali::Toolkit::Control::Left )
913 else if( direction == Dali::Toolkit::Control::Right )
918 newPage = std::max(0, std::min(static_cast<int>(mScrollRulerX->GetTotalPages() - 1), newPage));
919 if( newPage == currentPage )
921 if( direction == Dali::Toolkit::Control::Left )
923 newPage = mScrollRulerX->GetTotalPages() - 1;
924 } else if( direction == Dali::Toolkit::Control::Right )
930 // Scroll to the page in the given direction
931 mScrollView.ScrollTo(newPage);
933 if( direction == Dali::Toolkit::Control::Left )
935 // Work out the cell position for the last tile
936 int remainingExamples = mExampleList.size() - newPage * EXAMPLES_PER_PAGE;
937 int rowPos = (remainingExamples >= EXAMPLES_PER_PAGE) ? ROWS_PER_PAGE - 1 : ( (remainingExamples % EXAMPLES_PER_PAGE + EXAMPLES_PER_ROW) / EXAMPLES_PER_ROW - 1 );
938 int colPos = remainingExamples >= EXAMPLES_PER_PAGE ? EXAMPLES_PER_ROW - 1 : ( remainingExamples % EXAMPLES_PER_PAGE - rowPos * EXAMPLES_PER_ROW - 1 );
940 // Move the focus to the last tile in the new page.
941 nextFocusActor = mPages[newPage].GetChildAt(colPos * EXAMPLES_PER_ROW + rowPos);
945 // Move the focus to the first tile in the new page.
946 nextFocusActor = mPages[newPage].GetChildAt(0);
950 return nextFocusActor;
953 void DaliTableView::OnFocusedActorActivated( Dali::Actor activatedActor )
957 mPressedActor = activatedActor;
959 // Activate the current focused actor;
960 TouchEvent touchEventUp;
961 touchEventUp.points.push_back( TouchPoint ( 0, TouchPoint::Up, 0.0f, 0.0f ) );
962 OnTilePressed(mPressedActor, touchEventUp);
966 bool DaliTableView::OnTileHovered( Actor actor, const HoverEvent& event )
968 KeyboardFocusManager::Get().SetCurrentFocusActor( actor );
972 void DaliTableView::OnLogoTapped( Dali::Actor actor, const Dali::TapGesture& tap )
974 if ( !mVersionPopupShown )
976 if ( !mVersionPopup )
978 std::ostringstream stream;
979 stream << "DALi Core: " << CORE_MAJOR_VERSION << "." << CORE_MINOR_VERSION << "." << CORE_MICRO_VERSION << std::endl << "(" << CORE_BUILD_DATE << ")" << std::endl << std::endl;
980 stream << "DALi Adaptor: " << ADAPTOR_MAJOR_VERSION << "." << ADAPTOR_MINOR_VERSION << "." << ADAPTOR_MICRO_VERSION << std::endl << "(" << ADAPTOR_BUILD_DATE << ")" << std::endl << std::endl;
981 stream << "DALi Toolkit: " << TOOLKIT_MAJOR_VERSION << "." << TOOLKIT_MINOR_VERSION << "." << TOOLKIT_MICRO_VERSION << std::endl << "(" << TOOLKIT_BUILD_DATE << ")";
983 mVersionPopup = Dali::Toolkit::Popup::New();
984 mVersionPopup.SetParentOrigin( ParentOrigin::CENTER );
985 mVersionPopup.SetAnchorPoint( AnchorPoint::CENTER );
986 mVersionPopup.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::WIDTH );
987 mVersionPopup.SetSizeModeFactor( Vector3( 0.75f, 1.0f, 1.0f ) );
988 mVersionPopup.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::HEIGHT );
989 mVersionPopup.SetTitle( stream.str() );
990 mVersionPopup.HideTail();
991 mVersionPopup.OutsideTouchedSignal().Connect( this, &DaliTableView::HideVersionPopup );
992 mVersionPopup.HiddenSignal().Connect( this, &DaliTableView::PopupHidden );
995 mVersionPopup.Show();
996 mVersionPopupShown = true;
1000 void DaliTableView::HideVersionPopup()
1002 if ( mVersionPopup )
1004 mVersionPopup.Hide();
1008 void DaliTableView::PopupHidden()
1010 if ( mVersionPopup )
1012 mVersionPopupShown = false;