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"
25 #include <dali/devel-api/images/distance-field.h>
26 #include <dali-toolkit/devel-api/shader-effects/alpha-discard-effect.h>
27 #include <dali-toolkit/devel-api/shader-effects/distance-field-effect.h>
30 #include "shared/view.h"
33 using namespace Dali::Toolkit;
35 ///////////////////////////////////////////////////////////////////////////////
40 const std::string BUTTON_BACKWARD( "Backward" );
41 const std::string BUTTON_FORWARD( "Forward" );
42 const std::string BUTTON_QUIT( "Quit" );
43 const std::string BUTTON_OK( "Ok" );
44 const std::string BUTTON_CANCEL( "Cancel" );
46 const std::string DEFAULT_BACKGROUND_IMAGE_PATH( DALI_IMAGE_DIR "background-gradient.jpg" );
47 const std::string LOGO_PATH( DALI_IMAGE_DIR "dali-logo.png" );
48 const std::string DEFAULT_TOOLBAR_IMAGE_PATH( DALI_IMAGE_DIR "top-bar.png" );
49 const std::string BUTTON_BACKGROUND(DALI_IMAGE_DIR "button-background.png");
50 const std::string TILE_BACKGROUND(DALI_IMAGE_DIR "item-background.png");
51 const std::string TILE_BACKGROUND_ALPHA(DALI_IMAGE_DIR "item-background-alpha.png");
53 const char * const DEFAULT_TOOLBAR_TEXT( "TOUCH TO LAUNCH EXAMPLE" );
55 const float BUTTON_PRESS_ANIMATION_TIME = 0.25f; ///< Time to perform button scale effect.
56 const float ROTATE_ANIMATION_TIME = 0.5f; ///< Time to perform rotate effect.
57 const int MAX_PAGES = 256; ///< Maximum pages (arbitrary safety limit)
58 const int EXAMPLES_PER_ROW = 3;
59 const int ROWS_PER_PAGE = 3;
60 const int EXAMPLES_PER_PAGE = EXAMPLES_PER_ROW * ROWS_PER_PAGE;
61 const float LOGO_MARGIN_RATIO = 0.1f / 0.3f;
62 const float BOTTOM_PADDING_RATIO = 0.4f / 0.9f;
63 const Vector3 SCROLLVIEW_RELATIVE_SIZE(0.9f, 1.0f, 0.8f ); ///< ScrollView's relative size to its parent
64 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.
65 const float STENCIL_RELATIVE_SIZE = 1.0f;
67 const float EFFECT_SNAP_DURATION = 0.66f; ///< Scroll Snap Duration for Effects
68 const float EFFECT_FLICK_DURATION = 0.5f; ///< Scroll Flick Duration for Effects
69 const Vector3 ANGLE_CUBE_PAGE_ROTATE(Math::PI * 0.5f, Math::PI * 0.5f, 0.0f);
73 const int NUM_BACKGROUND_IMAGES = 18;
74 const float BACKGROUND_SWIPE_SCALE = 0.025f;
75 const float BACKGROUND_SPREAD_SCALE = 1.5f;
76 const float SCALE_MOD = 1000.0f * Math::PI * 2.0f;
77 const float SCALE_SPEED = 10.0f;
78 const float SCALE_SPEED_SIN = 0.1f;
80 const unsigned int BACKGROUND_ANIMATION_DURATION = 15000; // 15 secs
82 const float BACKGROUND_Z = -1.0f;
83 const Vector4 BACKGROUND_COLOR( 1.0f, 1.0f, 1.0f, 1.0f );
85 const float BUBBLE_MIN_Z = -1.0;
86 const float BUBBLE_MAX_Z = 0.0f;
89 * Creates the background image
91 ImageActor CreateBackground( std::string imagePath )
93 Image image = ResourceImage::New( imagePath );
94 ImageActor background = ImageActor::New( image );
95 background.SetName( "BACKGROUND" );
96 background.SetAnchorPoint( AnchorPoint::CENTER );
97 background.SetParentOrigin( ParentOrigin::CENTER );
98 background.SetZ( -1.0f );
99 background.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
104 // These values depend on the tile image
105 const float IMAGE_BORDER_LEFT = 11.0f;
106 const float IMAGE_BORDER_RIGHT = IMAGE_BORDER_LEFT;
107 const float IMAGE_BORDER_TOP = IMAGE_BORDER_LEFT;
108 const float IMAGE_BORDER_BOTTOM = IMAGE_BORDER_LEFT;
111 * Constraint to return a position for a bubble based on the scroll value and vertical wrapping
113 struct AnimateBubbleConstraint
116 AnimateBubbleConstraint( const Vector3& initialPos, float scale )
117 : mInitialX( initialPos.x ),
122 void operator()( Vector3& position, const PropertyInputContainer& inputs )
124 const Vector3& parentSize = inputs[1]->GetVector3();
125 const Vector3& childSize = inputs[2]->GetVector3();
127 // Wrap bubbles verically.
128 if( position.y + childSize.y * 0.5f < -parentSize.y * 0.5f )
130 position.y = parentSize.y * 0.5f + childSize.y * 0.5f;
133 // Bubbles X position moves parallax to horizontal
134 // panning by a scale factor unique to each bubble.
135 position.x = mInitialX + ( inputs[0]->GetVector2().x * mScale );
144 bool CompareByTitle( const Example& lhs, const Example& rhs )
146 return lhs.title < rhs.title;
151 DaliTableView::DaliTableView( Application& application )
152 : mApplication( application ),
168 mButtonsPageRelativeSize(),
172 mBackgroundAnimations(),
175 mBackgroundImagePath( DEFAULT_BACKGROUND_IMAGE_PATH ),
178 mSortAlphabetically( false ),
179 mBackgroundAnimsPlaying( false ),
180 mVersionPopupShown( false )
182 application.InitSignal().Connect( this, &DaliTableView::Initialize );
185 DaliTableView::~DaliTableView()
189 void DaliTableView::AddExample( Example example )
191 mExampleList.push_back( example );
192 mExampleMap[ example.name ] = example;
195 void DaliTableView::SetBackgroundPath( std::string imagePath )
197 mBackgroundImagePath = imagePath;
200 void DaliTableView::SortAlphabetically( bool sortAlphabetically )
202 mSortAlphabetically = sortAlphabetically;
205 void DaliTableView::Initialize( Application& application )
207 Stage::GetCurrent().KeyEventSignal().Connect( this, &DaliTableView::OnKeyEvent );
209 const Vector2 stageSize = Stage::GetCurrent().GetSize();
212 Actor background = CreateBackground( mBackgroundImagePath );
213 Stage::GetCurrent().Add( background );
215 // Render entire content as overlays, as is all on same 2D plane.
216 mRootActor = TableView::New( 4, 1 );
217 mRootActor.SetAnchorPoint( AnchorPoint::CENTER );
218 mRootActor.SetParentOrigin( ParentOrigin::CENTER );
219 mRootActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
220 Stage::GetCurrent().Add( mRootActor );
223 Dali::Toolkit::ToolBar toolbar;
224 Dali::Layer toolBarLayer = DemoHelper::CreateToolbar(toolbar,
225 DEFAULT_TOOLBAR_IMAGE_PATH,
226 DEFAULT_TOOLBAR_TEXT,
227 DemoHelper::DEFAULT_VIEW_STYLE);
229 mRootActor.AddChild( toolBarLayer, TableView::CellPosition( 0, 0 ) );
230 mRootActor.SetFitHeight( 0 );
233 Dali::ImageActor logo = CreateLogo( LOGO_PATH );
234 logo.SetName( "LOGO_IMAGE" );
235 logo.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
236 const float paddingHeight = ( ( 1.f-TABLE_RELATIVE_SIZE.y ) * stageSize.y );
237 const float logoMargin = paddingHeight * LOGO_MARGIN_RATIO;
239 // Show version in a popup when log is tapped
240 mLogoTapDetector = TapGestureDetector::New();
241 mLogoTapDetector.Attach( logo );
242 mLogoTapDetector.DetectedSignal().Connect( this, &DaliTableView::OnLogoTapped );
244 const float bottomMargin = paddingHeight * BOTTOM_PADDING_RATIO;
246 Alignment alignment = Alignment::New();
247 alignment.SetName( "LOGO_ALIGNMENT" );
248 alignment.Add( logo );
249 alignment.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
250 alignment.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::HEIGHT );
251 Actor alignmentActor = alignment;
252 alignmentActor.SetPadding( Padding( 0.0f, 0.0f, logoMargin, logoMargin ));
253 mRootActor.AddChild( alignment, TableView::CellPosition( 1, 0 ) );
254 mRootActor.SetFitHeight( 1 );
256 // scrollview occupying the majority of the screen
257 mScrollView = ScrollView::New();
259 mScrollView.SetAnchorPoint( AnchorPoint::CENTER );
260 mScrollView.SetParentOrigin( ParentOrigin::CENTER );
261 mScrollView.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
262 const float buttonsPageMargin = ( 1.0f - TABLE_RELATIVE_SIZE.x ) * 0.5f * stageSize.width;
263 mScrollView.SetPadding( Padding( buttonsPageMargin, buttonsPageMargin, 0.0f, 0.0f ) );
265 mScrollView.SetAxisAutoLock( true );
266 mScrollView.ScrollCompletedSignal().Connect( this, &DaliTableView::OnScrollComplete );
267 mScrollView.ScrollStartedSignal().Connect( this, &DaliTableView::OnScrollStart );
268 mScrollView.TouchedSignal().Connect( this, &DaliTableView::OnScrollTouched );
270 mScrollViewLayer = Layer::New();
271 mScrollViewLayer.SetAnchorPoint( AnchorPoint::CENTER );
272 mScrollViewLayer.SetParentOrigin( ParentOrigin::CENTER );
273 mScrollViewLayer.SetDrawMode( DrawMode::OVERLAY );
274 mScrollViewLayer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
276 // Create solid background colour.
277 ImageActor backgroundColourActor = Dali::Toolkit::CreateSolidColorActor( BACKGROUND_COLOR );
278 backgroundColourActor.SetAnchorPoint( AnchorPoint::CENTER );
279 backgroundColourActor.SetParentOrigin( ParentOrigin::CENTER );
280 backgroundColourActor.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
281 backgroundColourActor.SetSizeModeFactor( Vector3( 1.0f, 1.5f, 1.0f ) );
282 backgroundColourActor.SetZ( BACKGROUND_Z );
283 mScrollViewLayer.Add( backgroundColourActor );
285 // Populate background and bubbles - needs to be scrollViewLayer so scroll ends show
286 Actor bubbleContainer = Actor::New();
287 bubbleContainer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
288 bubbleContainer.SetAnchorPoint( AnchorPoint::CENTER );
289 bubbleContainer.SetParentOrigin( ParentOrigin::CENTER );
290 mScrollViewLayer.Add( bubbleContainer );
292 SetupBackground( bubbleContainer );
294 Alignment buttonsAlignment = Alignment::New();
295 buttonsAlignment.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
296 buttonsAlignment.Add( mScrollViewLayer );
298 mScrollViewLayer.Add( mScrollView );
300 mRootActor.AddChild( buttonsAlignment, TableView::CellPosition( 2, 0 ) );
302 mRootActor.SetFixedHeight( 3, bottomMargin );
304 // Add scroll view effect and setup constraints on pages
305 ApplyScrollViewEffect();
307 // Add pages and tiles
310 // Remove constraints for inner cube effect
311 ApplyCubeEffectToPages();
313 Dali::Window winHandle = application.GetWindow();
314 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
315 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE );
316 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
317 winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
319 // Set initial orientation
320 unsigned int degrees = 0;
323 //orientation.ChangedSignal().Connect( this, &DaliTableView::OrientationChanged );
325 winHandle.ShowIndicator( Dali::Window::INVISIBLE );
327 // Background animation
328 mAnimationTimer = Timer::New( BACKGROUND_ANIMATION_DURATION );
329 mAnimationTimer.TickSignal().Connect( this, &DaliTableView::PauseBackgroundAnimation );
330 mAnimationTimer.Start();
331 mBackgroundAnimsPlaying = true;
333 KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &DaliTableView::OnKeyboardPreFocusChange );
334 KeyboardFocusManager::Get().FocusedActorActivatedSignal().Connect( this, &DaliTableView::OnFocusedActorActivated );
337 void DaliTableView::ApplyCubeEffectToPages()
339 ScrollViewPagePathEffect effect = ScrollViewPagePathEffect::DownCast( mScrollViewEffect );
340 unsigned int pageCount(0);
341 for( std::vector< Actor >::iterator pageIter = mPages.begin(); pageIter != mPages.end(); ++pageIter )
343 Actor page = *pageIter;
344 effect.ApplyToPage( page, pageCount++ );
348 void DaliTableView::OnButtonsPageRelayout( const Dali::Actor& actor )
353 void DaliTableView::Populate()
355 const Vector2 stageSize = Stage::GetCurrent().GetSize();
357 mTotalPages = ( mExampleList.size() + EXAMPLES_PER_PAGE - 1 ) / EXAMPLES_PER_PAGE;
359 // Populate ScrollView.
360 if( mExampleList.size() > 0 )
362 if( mSortAlphabetically )
364 sort( mExampleList.begin(), mExampleList.end(), CompareByTitle );
367 unsigned int exampleCount = 0;
368 ExampleListConstIter iter = mExampleList.begin();
370 for( int t = 0; t < mTotalPages; t++ )
372 // Create Table. (contains up to 9 Examples)
373 TableView page = TableView::New( 3, 3 );
374 page.SetAnchorPoint( AnchorPoint::CENTER );
375 page.SetParentOrigin( ParentOrigin::CENTER );
376 page.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
377 mScrollView.Add( page );
379 // Calculate the number of images going across (columns) within a page, according to the screen resolution and dpi.
380 const float margin = 2.0f;
381 const float tileParentMultiplier = 1.0f / EXAMPLES_PER_ROW;
383 for(int row = 0; row < ROWS_PER_PAGE; row++)
385 for(int column = 0; column < EXAMPLES_PER_ROW; column++)
387 const Example& example = ( *iter );
389 Actor tile = CreateTile( example.name, example.title, Vector3( tileParentMultiplier, tileParentMultiplier, 1.0f ), true );
390 FocusManager focusManager = FocusManager::Get();
391 focusManager.SetFocusOrder( tile, ++exampleCount );
392 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_LABEL,
394 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_TRAIT, "Tile" );
395 focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_HINT,
396 "You can run this example" );
398 tile.SetPadding( Padding( margin, margin, margin, margin ) );
400 page.AddChild( tile, TableView::CellPosition( row, column ) );
404 if( iter == mExampleList.end() )
410 if( iter == mExampleList.end() )
416 // Set tableview position
417 Vector3 pagePos( stageSize.width * TABLE_RELATIVE_SIZE.x * t, 0.0f, 0.0f );
418 page.SetPosition( pagePos );
420 mPages.push_back( page );
422 if( iter == mExampleList.end() )
429 // Update Ruler info.
430 mScrollRulerX = new FixedRuler( stageSize.width * TABLE_RELATIVE_SIZE.x * 0.5f );
431 mScrollRulerY = new DefaultRuler();
432 mScrollRulerX->SetDomain( RulerDomain( 0.0f, (mTotalPages+1) * stageSize.width * TABLE_RELATIVE_SIZE.x * 0.5f, true ) );
433 mScrollRulerY->Disable();
434 mScrollView.SetRulerX( mScrollRulerX );
435 mScrollView.SetRulerY( mScrollRulerY );
438 void DaliTableView::OrientationChanged( Orientation orientation )
440 // TODO: Implement if orientation change required
443 void DaliTableView::Rotate( unsigned int degrees )
445 // Resize the root actor
446 Vector2 stageSize = Stage::GetCurrent().GetSize();
447 Vector3 targetSize( stageSize.x, stageSize.y, 1.0f );
449 if( degrees == 90 || degrees == 270 )
451 targetSize = Vector3( stageSize.y, stageSize.x, 1.0f );
454 if( mRotateAnimation )
456 mRotateAnimation.Stop();
457 mRotateAnimation.Clear();
460 mRotateAnimation = Animation::New( ROTATE_ANIMATION_TIME );
461 mRotateAnimation.AnimateTo( Property( mRootActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( 360 - degrees ) ), Vector3::ZAXIS ), AlphaFunction::EASE_OUT );
462 mRotateAnimation.AnimateTo( Property( mRootActor, Actor::Property::SIZE ), targetSize, AlphaFunction::EASE_OUT );
463 mRotateAnimation.Play();
466 Actor DaliTableView::CreateTile( const std::string& name, const std::string& title, const Dali::Vector3& sizeMultiplier, bool addBackground )
468 Actor content = Actor::New();
469 content.SetName( name );
470 content.SetAnchorPoint( AnchorPoint::CENTER );
471 content.SetParentOrigin( ParentOrigin::CENTER );
472 content.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
473 content.SetSizeModeFactor( sizeMultiplier );
475 // create background image
478 Image bg = ResourceImage::New( TILE_BACKGROUND );
479 ImageActor image = ImageActor::New( bg );
480 image.SetAnchorPoint( AnchorPoint::CENTER );
481 image.SetParentOrigin( ParentOrigin::CENTER );
482 // make the image 100% of tile
483 image.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
485 // move image back to get text appear in front
487 image.SetStyle( ImageActor::STYLE_NINE_PATCH );
488 image.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
489 content.Add( image );
492 ImageActor stencil = NewStencilImage();
493 stencil.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
494 image.Add( stencil );
497 TextLabel label = TextLabel::New();
498 label.SetAnchorPoint( AnchorPoint::TOP_LEFT );
499 label.SetProperty( Control::Property::STYLE_NAME, "launcherlabel" );
500 label.SetProperty( TextLabel::Property::MULTI_LINE, true );
501 label.SetProperty( TextLabel::Property::TEXT, title );
502 label.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
503 label.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
504 label.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
505 content.Add( label );
507 // Set the tile to be keyboard focusable
508 content.SetKeyboardFocusable(true);
510 // connect to the touch events
511 content.TouchedSignal().Connect( this, &DaliTableView::OnTilePressed );
512 content.HoveredSignal().Connect( this, &DaliTableView::OnTileHovered );
517 ImageActor DaliTableView::NewStencilImage()
519 Image alpha = ResourceImage::New( TILE_BACKGROUND_ALPHA );
521 ImageActor stencilActor = ImageActor::New( alpha );
522 stencilActor.SetStyle( ImageActor::STYLE_NINE_PATCH );
523 stencilActor.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
525 stencilActor.SetParentOrigin( ParentOrigin::CENTER );
526 stencilActor.SetAnchorPoint( AnchorPoint::CENTER );
527 stencilActor.SetDrawMode( DrawMode::STENCIL );
529 Dali::ShaderEffect shaderEffect = AlphaDiscardEffect::New();
530 stencilActor.SetShaderEffect( shaderEffect );
535 bool DaliTableView::OnTilePressed( Actor actor, const TouchEvent& event )
537 bool consumed = false;
539 const TouchPoint& point = event.GetPoint( 0 );
540 if( TouchPoint::Down == point.state )
542 mPressedActor = actor;
546 // A button press is only valid if the Down & Up events
547 // both occurred within the button.
548 if( ( TouchPoint::Up == point.state ) &&
549 ( mPressedActor == actor ) )
551 std::string name = actor.GetName();
552 ExampleMapConstIter iter = mExampleMap.find( name );
554 FocusManager focusManager = FocusManager::Get();
556 if( iter != mExampleMap.end() )
558 // ignore Example button presses when scrolling or button animating.
559 if( ( !mScrolling ) && ( !mPressedAnimation ) )
561 // do nothing, until pressed animation finished.
568 mPressedAnimation = Animation::New( BUTTON_PRESS_ANIMATION_TIME );
569 mPressedAnimation.SetEndAction( Animation::Discard );
571 // scale the content actor within the Tile, as to not affect the placement within the Table.
572 Actor content = actor.GetChildAt(0);
573 mPressedAnimation.AnimateTo( Property( content, Actor::Property::SCALE ), Vector3( 0.9f, 0.9f, 1.0f ), AlphaFunction::EASE_IN_OUT,
574 TimePeriod( 0.0f, BUTTON_PRESS_ANIMATION_TIME * 0.5f ) );
575 mPressedAnimation.AnimateTo( Property( content, Actor::Property::SCALE ), Vector3::ONE, AlphaFunction::EASE_IN_OUT,
576 TimePeriod( BUTTON_PRESS_ANIMATION_TIME * 0.5f, BUTTON_PRESS_ANIMATION_TIME * 0.5f ) );
577 mPressedAnimation.Play();
578 mPressedAnimation.FinishedSignal().Connect( this, &DaliTableView::OnPressedAnimationFinished );
584 void DaliTableView::OnPressedAnimationFinished( Dali::Animation& source )
586 mPressedAnimation.Reset();
589 std::string name = mPressedActor.GetName();
590 ExampleMapConstIter iter = mExampleMap.find( name );
592 if( iter == mExampleMap.end() )
594 if( name == BUTTON_QUIT )
596 // Move focus to the OK button
597 FocusManager focusManager = FocusManager::Get();
599 // Enable the group mode and wrap mode
600 focusManager.SetGroupMode( true );
601 focusManager.SetWrapMode( true );
606 const Example& example( iter->second );
608 std::stringstream stream;
609 stream << DALI_EXAMPLE_BIN << example.name.c_str();
613 execlp( stream.str().c_str(), example.name.c_str(), NULL );
614 DALI_ASSERT_ALWAYS(false && "exec failed!");
617 mPressedActor.Reset();
621 void DaliTableView::OnScrollStart( const Dali::Vector2& position )
628 void DaliTableView::OnScrollComplete( const Dali::Vector2& position )
632 // move focus to 1st item of new page
633 FocusManager focusManager = FocusManager::Get();
634 focusManager.SetCurrentFocusActor(mPages[mScrollView.GetCurrentPage()].GetChildAt(0) );
637 bool DaliTableView::OnScrollTouched( Actor actor, const TouchEvent& event )
639 const TouchPoint& point = event.GetPoint( 0 );
640 if( TouchPoint::Down == point.state )
642 mPressedActor = actor;
648 void DaliTableView::ApplyScrollViewEffect()
650 // Remove old effect if exists.
652 if( mScrollViewEffect )
654 mScrollView.RemoveEffect( mScrollViewEffect );
657 // Just one effect for now
658 SetupInnerPageCubeEffect();
660 mScrollView.ApplyEffect( mScrollViewEffect );
663 void DaliTableView::SetupInnerPageCubeEffect()
665 const Vector2 stageSize = Stage::GetCurrent().GetSize();
667 Dali::Path path = Dali::Path::New();
668 Dali::Property::Array points;
670 points[0] = Vector3( stageSize.x*0.5, 0.0f, stageSize.x*0.5f);
671 points[1] = Vector3( 0.0f, 0.0f, 0.0f );
672 points[2] = Vector3( -stageSize.x*0.5f, 0.0f, stageSize.x*0.5f);
673 path.SetProperty( Path::Property::POINTS, points );
675 Dali::Property::Array controlPoints;
676 controlPoints.Resize(4);
677 controlPoints[0] = Vector3( stageSize.x*0.5f, 0.0f, stageSize.x*0.3f );
678 controlPoints[1] = Vector3( stageSize.x*0.3f, 0.0f, 0.0f );
679 controlPoints[2] = Vector3(-stageSize.x*0.3f, 0.0f, 0.0f );
680 controlPoints[3] = Vector3(-stageSize.x*0.5f, 0.0f, stageSize.x*0.3f );
681 path.SetProperty( Path::Property::CONTROL_POINTS, controlPoints );
684 mScrollViewEffect = ScrollViewPagePathEffect::New(path,
685 Vector3(-1.0f,0.0f,0.0f),
686 Toolkit::ScrollView::Property::SCROLL_FINAL_X,
687 Vector3(stageSize.x*TABLE_RELATIVE_SIZE.x,stageSize.y*TABLE_RELATIVE_SIZE.y,0.0f),mTotalPages);
690 void DaliTableView::OnKeyEvent( const KeyEvent& event )
692 if( event.state == KeyEvent::Down )
694 if ( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
696 if ( mVersionPopup && mVersionPopupShown )
708 void DaliTableView::SetupBackground( Actor bubbleContainer )
710 // Create distance field shape.
711 BufferImage distanceField;
712 Size imageSize( 512, 512 );
713 CreateShapeImage( CIRCLE, imageSize, distanceField );
715 // Add bubbles to the bubbleContainer.
716 // Note: The bubbleContainer is parented externally to this function.
717 AddBackgroundActors( bubbleContainer, NUM_BACKGROUND_IMAGES, distanceField );
720 void DaliTableView::InitialiseBackgroundActors( Actor actor )
722 // Delete current animations
723 mBackgroundAnimations.clear();
725 // Create new animations
726 const Vector3 size = actor.GetTargetSize();
728 for( unsigned int i = 0, childCount = actor.GetChildCount(); i < childCount; ++i )
730 Actor child = actor.GetChildAt( i );
732 // Calculate a random position
733 Vector3 childPos( Random::Range( -size.x * 0.5f * BACKGROUND_SPREAD_SCALE, size.x * 0.5f * BACKGROUND_SPREAD_SCALE ),
734 Random::Range( -size.y, size.y ),
735 Random::Range( BUBBLE_MIN_Z, BUBBLE_MAX_Z ) );
737 child.SetPosition( childPos );
739 // Define bubble horizontal parallax and vertical wrapping
740 Constraint animConstraint = Constraint::New < Vector3 > ( child, Actor::Property::POSITION, AnimateBubbleConstraint( childPos, Random::Range( -0.85f, 0.25f ) ) );
741 animConstraint.AddSource( Source( mScrollView, ScrollView::Property::SCROLL_POSITION ) );
742 animConstraint.AddSource( Dali::ParentSource( Dali::Actor::Property::SIZE ) );
743 animConstraint.AddSource( Dali::LocalSource( Dali::Actor::Property::SIZE ) );
744 animConstraint.Apply();
747 Animation animation = Animation::New( Random::Range( 40.0f, 80.0f ) );
748 animation.AnimateBy( Property( child, Actor::Property::POSITION ), Vector3( 0.0f, -1.0f, 0.0f ), AlphaFunction::LINEAR );
749 animation.SetLooping( true );
751 mBackgroundAnimations.push_back( animation );
755 void DaliTableView::AddBackgroundActors( Actor layer, int count, BufferImage distanceField )
757 for( int i = 0; i < count; ++i )
759 float randSize = Random::Range( 10.0f, 400.0f );
760 float hue = Random::Range( 0.3f, 1.0f );
761 Vector4 randColour( hue, hue * 0.5, 0.0f, Random::Range( 0.3f, 0.6f ));
763 ImageActor dfActor = ImageActor::New( distanceField );
764 dfActor.SetSize( Vector2( randSize, randSize ) );
765 dfActor.SetParentOrigin( ParentOrigin::CENTER );
767 Toolkit::DistanceFieldEffect effect = Toolkit::DistanceFieldEffect::New();
768 dfActor.SetShaderEffect( effect );
769 dfActor.SetColor( randColour );
770 effect.SetOutlineParams( Vector2( 0.55f, 0.00f ) );
771 effect.SetSmoothingEdge( 0.5f );
772 layer.Add( dfActor );
775 // Positioning will occur when the layer is relaid out
776 layer.OnRelayoutSignal().Connect( this, &DaliTableView::InitialiseBackgroundActors );
779 void DaliTableView::CreateShapeImage( ShapeType shapeType, const Size& size, BufferImage& distanceFieldOut )
781 // this bitmap will hold the alpha map for the distance field shader
782 distanceFieldOut = BufferImage::New( size.width, size.height, Pixel::A8 );
784 // Generate bit pattern
785 std::vector< unsigned char > imageDataA8;
786 imageDataA8.reserve( size.width * size.height ); // A8
791 GenerateCircle( size, imageDataA8 );
794 GenerateSquare( size, imageDataA8 );
800 PixelBuffer* buffer = distanceFieldOut.GetBuffer();
803 GenerateDistanceFieldMap( &imageDataA8[ 0 ], size, buffer, size, 8.0f, size );
804 distanceFieldOut.Update();
808 void DaliTableView::GenerateSquare( const Size& size, std::vector< unsigned char >& distanceFieldOut )
810 for( int h = 0; h < size.height; ++h )
812 for( int w = 0; w < size.width; ++w )
814 distanceFieldOut.push_back( 0xFF );
819 void DaliTableView::GenerateCircle( const Size& size, std::vector< unsigned char >& distanceFieldOut )
821 const float radius = size.width * 0.5f * size.width * 0.5f;
822 Vector2 center( size.width / 2, size.height / 2 );
824 for( int h = 0; h < size.height; ++h )
826 for( int w = 0; w < size.width; ++w )
829 Vector2 dist = pos - center;
831 if( dist.x * dist.x + dist.y * dist.y > radius )
833 distanceFieldOut.push_back( 0x00 );
837 distanceFieldOut.push_back( 0xFF );
843 ImageActor DaliTableView::CreateLogo( std::string imagePath )
845 Image image = ResourceImage::New( imagePath );
846 ImageActor logo = ImageActor::New( image );
848 logo.SetAnchorPoint( AnchorPoint::CENTER );
849 logo.SetParentOrigin( ParentOrigin::CENTER );
854 bool DaliTableView::PauseBackgroundAnimation()
861 void DaliTableView::PauseAnimation()
863 if( mBackgroundAnimsPlaying )
865 for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
867 Animation anim = *animIter;
872 mBackgroundAnimsPlaying = false;
876 void DaliTableView::PlayAnimation()
878 if ( !mBackgroundAnimsPlaying )
880 for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
882 Animation anim = *animIter;
887 mBackgroundAnimsPlaying = true;
890 mAnimationTimer.SetInterval( BACKGROUND_ANIMATION_DURATION );
893 Dali::Actor DaliTableView::OnKeyboardPreFocusChange( Dali::Actor current, Dali::Actor proposed, Dali::Toolkit::Control::KeyboardFocus::Direction direction )
895 Actor nextFocusActor = proposed;
897 if ( !current && !proposed )
899 // Set the initial focus to the first tile in the current page should be focused.
900 nextFocusActor = mPages[mScrollView.GetCurrentPage()].GetChildAt(0);
902 else if( !proposed || (proposed && proposed == mScrollViewLayer) )
904 // ScrollView is being focused but nothing in the current page can be focused further
905 // in the given direction. We should work out which page to scroll to next.
906 int currentPage = mScrollView.GetCurrentPage();
907 int newPage = currentPage;
908 if( direction == Dali::Toolkit::Control::KeyboardFocus::LEFT )
912 else if( direction == Dali::Toolkit::Control::KeyboardFocus::RIGHT )
917 newPage = std::max(0, std::min(mTotalPages - 1, newPage));
918 if( newPage == currentPage )
920 if( direction == Dali::Toolkit::Control::KeyboardFocus::LEFT )
922 newPage = mTotalPages - 1;
923 } else if( direction == Dali::Toolkit::Control::KeyboardFocus::RIGHT )
929 // Scroll to the page in the given direction
930 mScrollView.ScrollTo(newPage);
932 if( direction == Dali::Toolkit::Control::KeyboardFocus::LEFT )
934 // Work out the cell position for the last tile
935 int remainingExamples = mExampleList.size() - newPage * EXAMPLES_PER_PAGE;
936 int rowPos = (remainingExamples >= EXAMPLES_PER_PAGE) ? ROWS_PER_PAGE - 1 : ( (remainingExamples % EXAMPLES_PER_PAGE + EXAMPLES_PER_ROW) / EXAMPLES_PER_ROW - 1 );
937 int colPos = remainingExamples >= EXAMPLES_PER_PAGE ? EXAMPLES_PER_ROW - 1 : ( remainingExamples % EXAMPLES_PER_PAGE - rowPos * EXAMPLES_PER_ROW - 1 );
939 // Move the focus to the last tile in the new page.
940 nextFocusActor = mPages[newPage].GetChildAt(rowPos * EXAMPLES_PER_ROW + colPos);
944 // Move the focus to the first tile in the new page.
945 nextFocusActor = mPages[newPage].GetChildAt(0);
949 return nextFocusActor;
952 void DaliTableView::OnFocusedActorActivated( Dali::Actor activatedActor )
956 mPressedActor = activatedActor;
958 // Activate the current focused actor;
959 TouchEvent touchEventUp;
960 touchEventUp.points.push_back( TouchPoint ( 0, TouchPoint::Up, 0.0f, 0.0f ) );
961 OnTilePressed(mPressedActor, touchEventUp);
965 bool DaliTableView::OnTileHovered( Actor actor, const HoverEvent& event )
967 KeyboardFocusManager::Get().SetCurrentFocusActor( actor );
971 void DaliTableView::OnLogoTapped( Dali::Actor actor, const Dali::TapGesture& tap )
973 if ( !mVersionPopupShown )
975 if ( !mVersionPopup )
977 std::ostringstream stream;
978 stream << "DALi Core: " << CORE_MAJOR_VERSION << "." << CORE_MINOR_VERSION << "." << CORE_MICRO_VERSION << std::endl << "(" << CORE_BUILD_DATE << ")" << std::endl << std::endl;
979 stream << "DALi Adaptor: " << ADAPTOR_MAJOR_VERSION << "." << ADAPTOR_MINOR_VERSION << "." << ADAPTOR_MICRO_VERSION << std::endl << "(" << ADAPTOR_BUILD_DATE << ")" << std::endl << std::endl;
980 stream << "DALi Toolkit: " << TOOLKIT_MAJOR_VERSION << "." << TOOLKIT_MINOR_VERSION << "." << TOOLKIT_MICRO_VERSION << std::endl << "(" << TOOLKIT_BUILD_DATE << ")";
982 mVersionPopup = Dali::Toolkit::Popup::New();
983 mVersionPopup.SetParentOrigin( ParentOrigin::CENTER );
984 mVersionPopup.SetAnchorPoint( AnchorPoint::CENTER );
985 mVersionPopup.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::WIDTH );
986 mVersionPopup.SetSizeModeFactor( Vector3( 0.75f, 1.0f, 1.0f ) );
987 mVersionPopup.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::HEIGHT );
988 mVersionPopup.SetTitle( stream.str() );
989 mVersionPopup.HideTail();
990 mVersionPopup.OutsideTouchedSignal().Connect( this, &DaliTableView::HideVersionPopup );
991 mVersionPopup.HiddenSignal().Connect( this, &DaliTableView::PopupHidden );
994 mVersionPopup.Show();
995 mVersionPopupShown = true;
999 void DaliTableView::HideVersionPopup()
1001 if ( mVersionPopup )
1003 mVersionPopup.Hide();
1007 void DaliTableView::PopupHidden()
1009 if ( mVersionPopup )
1011 mVersionPopupShown = false;