Use the default alpha function for snap animation
[platform/core/uifw/dali-demo.git] / demo / dali-table-view.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include "dali-table-view.h"
20 #include "examples/shared/view.h"
21
22 // EXTERNAL INCLUDES
23 #include <algorithm>
24 #include <sstream>
25 #include<unistd.h>
26
27 using namespace Dali;
28 using namespace Dali::Toolkit;
29
30 ///////////////////////////////////////////////////////////////////////////////
31
32 namespace
33 {
34
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" );
40
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");
47
48 const char * const DEFAULT_TOOLBAR_TEXT( "TOUCH TO LAUNCH EXAMPLE" );
49
50 const float BUTTON_PRESS_ANIMATION_TIME = 0.25f;                ///< Time to perform button scale effect.
51 const float ROTATE_ANIMATION_TIME = 0.5f;                       ///< Time to perform rotate effect.
52 const int MAX_PAGES = 256;                                      ///< Maximum pages (arbitrary safety limit)
53 const int EXAMPLES_PER_ROW = 3;
54 const int ROWS_PER_PAGE = 3;
55 const int EXAMPLES_PER_PAGE = EXAMPLES_PER_ROW * ROWS_PER_PAGE;
56 const float TOP_ROW_HEIGHT = 35.0f;
57 const float BOTTOM_ROW_HEIGHT = 35.0f;
58 const int BOTTOM_PADDING_HEIGHT = 40;
59 const int LOGO_BOTTOM_PADDING_HEIGHT = 30;
60 const Vector3 TABLE_RELATIVE_SIZE(0.9f, 1.0f, 0.8f );          ///< TableView's relative size to the entire stage.
61 const float STENCIL_RELATIVE_SIZE = 1.0f;
62
63 const float EFFECT_SNAP_DURATION = 0.66f;                       ///< Scroll Snap Duration for Effects
64 const float EFFECT_FLICK_DURATION = 0.5f;                       ///< Scroll Flick Duration for Effects
65 const Vector3 ANGLE_CUBE_PAGE_ROTATE(Math::PI * 0.5f, Math::PI * 0.5f, 0.0f);
66
67 const int NUM_BACKGROUND_IMAGES = 20;
68 const float BACKGROUND_SWIPE_SCALE = 0.025f;
69 const float BACKGROUND_SPREAD_SCALE = 1.5f;
70 const float SCALE_MOD = 1000.0f * Math::PI * 2.0f;
71 const float SCALE_SPEED = 10.0f;
72 const float SCALE_SPEED_SIN = 0.1f;
73
74 const unsigned int BACKGROUND_ANIMATION_DURATION = 15000; // 15 secs
75
76 const float BACKGROUND_Z = -1000.0f;
77 const float BACKGROUND_SIZE_SCALE = 2.0f;
78 const Vector4 BACKGROUND_COLOR( 1.0f, 1.0f, 1.0f, 1.0f );
79
80
81 const std::string             DEFAULT_TEXT_STYLE_FONT_FAMILY("HelveticaNeue");
82 const std::string             DEFAULT_TEXT_STYLE_FONT_STYLE("Regular");
83 const Dali::PointSize         DEFAULT_TEXT_STYLE_POINT_SIZE( 8.0f );
84 const Dali::TextStyle::Weight DEFAULT_TEXT_STYLE_WEIGHT(Dali::TextStyle::REGULAR);
85 const Dali::Vector4           DEFAULT_TEXT_STYLE_COLOR(0.7f, 0.7f, 0.7f, 1.0f);
86
87 const std::string             TABLE_TEXT_STYLE_FONT_FAMILY("HelveticaNeue");
88 const std::string             TABLE_TEXT_STYLE_FONT_STYLE("Regular");
89 const Dali::PointSize         TABLE_TEXT_STYLE_POINT_SIZE( 8.0f );
90 const Dali::TextStyle::Weight TABLE_TEXT_STYLE_WEIGHT(Dali::TextStyle::LIGHT);
91 const Dali::Vector4           TABLE_TEXT_STYLE_COLOR(0.0f, 0.0f, 0.0f, 1.0f);
92
93
94 TextStyle GetTableTextStyle()
95 {
96   TextStyle textStyle;
97   textStyle.SetFontName(TABLE_TEXT_STYLE_FONT_FAMILY);
98   textStyle.SetFontStyle(TABLE_TEXT_STYLE_FONT_STYLE);
99   textStyle.SetFontPointSize( Dali::PointSize(DemoHelper::ScalePointSize(TABLE_TEXT_STYLE_POINT_SIZE)));
100   textStyle.SetWeight(TABLE_TEXT_STYLE_WEIGHT);
101   textStyle.SetTextColor(TABLE_TEXT_STYLE_COLOR);
102   return textStyle;
103 }
104
105 /**
106  * Creates the background image
107  */
108 ImageActor CreateBackground( std::string imagePath )
109 {
110   Image image = Image::New( imagePath );
111   ImageActor background = ImageActor::New( image );
112
113   background.SetAnchorPoint( AnchorPoint::CENTER );
114   background.SetParentOrigin( ParentOrigin::CENTER );
115   background.SetZ( -1.0f );
116
117   return background;
118 }
119
120 // These values depend on the tile image
121 const float IMAGE_BORDER_LEFT = 11.0f;
122 const float IMAGE_BORDER_RIGHT = IMAGE_BORDER_LEFT;
123 const float IMAGE_BORDER_TOP = IMAGE_BORDER_LEFT;
124 const float IMAGE_BORDER_BOTTOM = IMAGE_BORDER_LEFT;
125
126 /**
127  * TableViewVisibilityConstraint
128  */
129 struct TableViewVisibilityConstraint
130 {
131   bool operator()( const bool& current,
132               const PropertyInput& pagePositionProperty,
133               const PropertyInput& pageSizeProperty )
134   {
135     // Only the tableview in the current page should be visible.
136     const Vector3& pagePosition = pagePositionProperty.GetVector3();
137     const Vector3& pageSize = pageSizeProperty.GetVector3();
138     return fabsf( pagePosition.x ) < pageSize.x;
139   }
140 };
141
142 /**
143  * Constraint to wrap an actor in y that is moving vertically
144  */
145 Vector3 ShapeMovementConstraint( const Vector3& current,
146                          const PropertyInput& shapeSizeProperty,
147                          const PropertyInput& parentSizeProperty )
148 {
149   const Vector3& shapeSize = shapeSizeProperty.GetVector3();
150   const Vector3& parentSize = parentSizeProperty.GetVector3();
151
152   Vector3 pos( current );
153   if( pos.y + shapeSize.y * 0.5f < -parentSize.y * 0.5f )
154   {
155     pos.y += parentSize.y + shapeSize.y;
156   }
157
158   return pos;
159 }
160
161 /**
162  * Constraint to return a position for the background based on the scroll value
163  */
164 struct AnimScrollConstraint
165 {
166 public:
167
168   AnimScrollConstraint( const Vector3& initialPos, float scale )
169       : mInitialPos( initialPos ),
170         mScale( scale )
171   {
172
173   }
174
175   Vector3 operator()( const Vector3& current, const PropertyInput& scrollProperty )
176   {
177     float scrollPos = scrollProperty.GetVector3().x;
178
179     return mInitialPos + Vector3( -scrollPos * mScale, 0.0f, 0.0f );
180   }
181
182 private:
183   Vector3 mInitialPos;
184   float mScale;
185 };
186
187 /**
188  * Constraint to return a tracked world position added to the constant local position
189  */
190 struct TranslateLocalConstraint
191 {
192 public:
193
194   TranslateLocalConstraint( const Vector3& localPos )
195       : mLocalPos( localPos )
196   {
197   }
198
199   Vector3 operator()( const Vector3& current, const PropertyInput& pagePosProperty )
200   {
201     Vector3 worldPos = pagePosProperty.GetVector3();
202
203     return ( worldPos + mLocalPos );
204   }
205
206 private:
207   Vector3 mLocalPos;
208 };
209
210
211 bool CompareByTitle( const Example& lhs, const Example& rhs )
212 {
213   return lhs.title < rhs.title;
214 }
215
216 } // namespace
217
218 DaliTableView::DaliTableView( Application& application )
219     : mApplication( application ),
220         mScrolling( false ),
221         mBackgroundImagePath( DEFAULT_BACKGROUND_IMAGE_PATH ),
222         mSortAlphabetically( false ),
223         mBackgroundAnimsPlaying( false )
224 {
225   application.InitSignal().Connect( this, &DaliTableView::Initialize );
226 }
227
228 DaliTableView::~DaliTableView()
229 {
230 }
231
232 void DaliTableView::AddExample( Example example )
233 {
234   mExampleList.push_back( example );
235   mExampleMap[ example.name ] = example;
236 }
237
238 void DaliTableView::SetBackgroundPath( std::string imagePath )
239 {
240   mBackgroundImagePath = imagePath;
241 }
242
243 void DaliTableView::SortAlphabetically( bool sortAlphabetically )
244 {
245   mSortAlphabetically = sortAlphabetically;
246 }
247
248 void DaliTableView::Initialize( Application& application )
249 {
250   Stage::GetCurrent().KeyEventSignal().Connect( this, &DaliTableView::OnKeyEvent );
251
252   Vector2 stageSize = Stage::GetCurrent().GetSize();
253
254   // Background
255   mBackground = CreateBackground( mBackgroundImagePath );
256   // set same size as parent actor
257   mBackground.SetSize( stageSize );
258   Stage::GetCurrent().Add( mBackground );
259
260   // Render entire content as overlays, as is all on same 2D plane.
261   mRootActor = TableView::New( 4, 1 );
262   mRootActor.SetAnchorPoint( AnchorPoint::CENTER );
263   mRootActor.SetParentOrigin( ParentOrigin::CENTER );
264   mRootActor.SetFixedHeight( 3, BOTTOM_PADDING_HEIGHT );
265   Stage::GetCurrent().Add( mRootActor );
266
267   // Toolbar at top
268   Dali::Toolkit::ToolBar toolbar;
269   Dali::Layer toolBarLayer = DemoHelper::CreateToolbar(toolbar,
270                                                        DEFAULT_TOOLBAR_IMAGE_PATH,
271                                                        DEFAULT_TOOLBAR_TEXT,
272                                                        DemoHelper::DEFAULT_VIEW_STYLE,
273                                                        DemoHelper::GetDefaultTextStyle());
274
275   mRootActor.AddChild( toolBarLayer, TableView::CellPosition( 0, 0 ) );
276   mRootActor.SetFixedHeight( 0, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight );
277
278   // Add logo
279   mLogo = CreateLogo( LOGO_PATH );
280   Alignment alignment = Alignment::New();
281   alignment.Add(mLogo);
282   mRootActor.AddChild( alignment, TableView::CellPosition( 1, 0 ) );
283
284   // scrollview occupying the majority of the screen
285   mScrollView = ScrollView::New();
286
287   mScrollView.SetAnchorPoint( AnchorPoint::CENTER );
288   mScrollView.SetParentOrigin( ParentOrigin::CENTER );
289   mScrollView.ApplyConstraint( Dali::Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ), Dali::RelativeToConstraint( TABLE_RELATIVE_SIZE ) ) );
290   mScrollView.SetAxisAutoLock( true );
291   mScrollView.ScrollCompletedSignal().Connect( this, &DaliTableView::OnScrollComplete );
292   mScrollView.ScrollStartedSignal().Connect( this, &DaliTableView::OnScrollStart );
293   mScrollView.TouchedSignal().Connect( this, &DaliTableView::OnScrollTouched );
294
295   mScrollViewLayer = Layer::New();
296   mScrollViewLayer.SetAnchorPoint( AnchorPoint::CENTER );
297   mScrollViewLayer.SetParentOrigin( ParentOrigin::CENTER );
298   mScrollViewLayer.SetSize( stageSize );
299   mScrollViewLayer.Add( mScrollView );
300   mRootActor.AddChild( mScrollViewLayer, TableView::CellPosition( 2, 0 ) );
301
302   // Setup the scenegraph
303   // 1) Add scroll view effect and setup constraints on pages
304   ApplyScrollViewEffect();
305
306   // 2) Add pages and tiles
307   Populate();
308
309   // 3) Populate scrollview with background so constraints on background layers can work with scrollview
310   SetupBackground( mScrollView, stageSize );
311
312   // 4) Remove constraints for inner cube effect
313   for( TableViewListIter pageIter = mTableViewList.begin(); pageIter != mTableViewList.end(); ++pageIter )
314   {
315     TableView page = *pageIter;
316
317     unsigned int numChildren = page.GetChildCount();
318     Actor pageActor = page;
319     for( unsigned int i=0; i<numChildren; ++i)
320     {
321       // Remove old effect's manual constraints.
322       Actor child = pageActor.GetChildAt(i);
323       if( child )
324       {
325         child.RemoveConstraints();
326       }
327     }
328   }
329
330   // Set initial orientation
331   unsigned int degrees = application.GetOrientation().GetDegrees();
332   Rotate( degrees );
333
334   Dali::Window winHandle = application.GetWindow();
335   winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
336   winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE );
337   winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
338   winHandle.RemoveAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
339
340   Dali::Orientation orientation = winHandle.GetOrientation();
341   orientation.ChangedSignal().Connect( this, &DaliTableView::OrientationChanged );
342
343   winHandle.ShowIndicator( Dali::Window::INVISIBLE );
344
345   //
346   mAnimationTimer = Timer::New( BACKGROUND_ANIMATION_DURATION );
347   mAnimationTimer.TickSignal().Connect( this, &DaliTableView::PauseBackgroundAnimation );
348   mAnimationTimer.Start();
349   mBackgroundAnimsPlaying = true;
350
351   KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &DaliTableView::OnKeyboardPreFocusChange );
352   KeyboardFocusManager::Get().FocusedActorActivatedSignal().Connect( this, &DaliTableView::OnFocusedActorActivated );
353 }
354
355 void DaliTableView::Populate()
356 {
357   const Vector2 stageSize = Stage::GetCurrent().GetSize();
358
359   const Size demoTileSize( 0.25f * stageSize.width, 0.25f * stageSize.height );
360
361   mTotalPages = ( mExampleList.size() + EXAMPLES_PER_PAGE - 1 ) / EXAMPLES_PER_PAGE;
362
363   // Populate ScrollView.
364   if( mExampleList.size() > 0 )
365   {
366     if( mSortAlphabetically )
367     {
368       sort( mExampleList.begin(), mExampleList.end(), CompareByTitle );
369     }
370
371     unsigned int exampleCount = 0;
372     ExampleListConstIter iter = mExampleList.begin();
373     for( int t = 0; t < mTotalPages; t++ )
374     {
375       // Create Table. (contains up to 9 Examples)
376       TableView tableView = TableView::New( 4, 3 );
377       // Add tableView to container.
378       mScrollView.Add( tableView );
379       ApplyEffectToPage( tableView, TABLE_RELATIVE_SIZE );
380
381       tableView.SetAnchorPoint( AnchorPoint::CENTER );
382       tableView.SetParentOrigin( ParentOrigin::CENTER );
383       // 2 pixels of padding
384       tableView.SetCellPadding( Size( 2.0f, 2.0f ) );
385
386       Constraint constraint = Constraint::New<Vector3>( Actor::SCALE,
387                                                         LocalSource( Actor::SIZE ),
388                                                         ParentSource( Actor::SIZE ),
389                                                         ScaleToFitConstraint() );
390       tableView.ApplyConstraint(constraint);
391
392       // Apply visibility constraint to table view
393       Constraint visibleConstraint = Constraint::New< bool >( Actor::VISIBLE,
394                                                               LocalSource( Actor::POSITION ),
395                                                               ParentSource( Actor::SIZE ),
396                                                               TableViewVisibilityConstraint() );
397       visibleConstraint.SetRemoveAction( Constraint::Discard );
398       tableView.ApplyConstraint( visibleConstraint );
399
400       // add cells to table
401       for( int y = 0; y < ROWS_PER_PAGE; y++ )
402       {
403         for( int x = 0; x < EXAMPLES_PER_ROW; x++ )
404         {
405           const Example& example = ( *iter );
406
407           Actor tile = CreateTile( example.name, example.title, demoTileSize, true );
408           FocusManager focusManager = FocusManager::Get();
409           focusManager.SetFocusOrder( tile, ++exampleCount );
410           focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_LABEL,
411                                                   example.title );
412           focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_TRAIT, "Tile" );
413           focusManager.SetAccessibilityAttribute( tile, Dali::Toolkit::FocusManager::ACCESSIBILITY_HINT,
414                                                   "You can run this example" );
415
416           tableView.AddChild( tile, TableView::CellPosition( y, x ) );
417           iter++;
418
419           if( iter == mExampleList.end() )
420           {
421             break;
422           }
423         }
424         if( iter == mExampleList.end() )
425         {
426           break;
427         }
428       }
429
430       // last row is thin.
431       tableView.SetFixedHeight( 3, BOTTOM_ROW_HEIGHT );
432
433       std::stringstream out;
434       out << ( t + 1 ) << " of " << mTotalPages;
435       Actor pageNumberText = CreateTile( "", out.str(), Size( 0.8f * stageSize.width, BOTTOM_ROW_HEIGHT ), false );
436
437       pageNumberText.ApplyConstraint( Constraint::New< Vector3 >( Actor::POSITION, Source( tableView, Actor::WORLD_POSITION),
438                                                                    TranslateLocalConstraint( Vector3( 0.0f, stageSize.y * 0.4f, 0.0f ) ) ) );
439       pageNumberText.ApplyConstraint( Constraint::New< Quaternion >( Actor::ROTATION, Source( tableView, Actor::WORLD_ROTATION ), EqualToConstraint() ) );
440       pageNumberText.ApplyConstraint( Constraint::New< Vector4 >( Actor::COLOR, Source( tableView, Actor::COLOR ), EqualToConstraint() ) );
441
442       //Stage::GetCurrent().Add( pageNumberText );
443
444       // Set tableview position
445       Vector3 tableViewPos( stageSize.x * TABLE_RELATIVE_SIZE.x * t, 0.0f, 0.0f );
446       tableView.SetPosition( tableViewPos );
447
448       mTableViewList.push_back( tableView );
449
450       if( iter == mExampleList.end() )
451       {
452         break;
453       }
454     }
455   }
456
457   // Update Ruler info.
458   mScrollRulerX = new FixedRuler( stageSize.width * TABLE_RELATIVE_SIZE.x );
459   mScrollRulerY = new DefaultRuler();
460   mScrollRulerX->SetDomain( RulerDomain( 0.0f, mTotalPages * stageSize.width * TABLE_RELATIVE_SIZE.x, true ) );
461   mScrollRulerY->Disable();
462   mScrollView.SetRulerX( mScrollRulerX );
463   mScrollView.SetRulerY( mScrollRulerY );
464 }
465
466 void DaliTableView::OrientationChanged( Orientation orientation )
467 {
468   // TODO: Implement if orientation change required
469 }
470
471 void DaliTableView::Rotate( unsigned int degrees )
472 {
473   // Resize the root actor
474   Vector2 stageSize = Stage::GetCurrent().GetSize();
475   Vector3 targetSize( stageSize.x, stageSize.y, 1.0f );
476
477   if( degrees == 90 || degrees == 270 )
478   {
479     targetSize = Vector3( stageSize.y, stageSize.x, 1.0f );
480   }
481
482   if( mRotateAnimation )
483   {
484     mRotateAnimation.Stop();
485     mRotateAnimation.Clear();
486   }
487
488   mRotateAnimation = Animation::New( ROTATE_ANIMATION_TIME );
489   mRotateAnimation.RotateTo( mRootActor, Degree( 360 - degrees ), Vector3::ZAXIS, AlphaFunctions::EaseOut );
490   mRotateAnimation.Resize( mRootActor, targetSize, AlphaFunctions::EaseOut );
491   mRotateAnimation.Play();
492 }
493
494 Actor DaliTableView::CreateTile( const std::string& name, const std::string& title, const Size& parentSize, bool addBackground )
495 {
496   Actor tile = Actor::New();
497   tile.SetName( name );
498   tile.SetAnchorPoint( AnchorPoint::CENTER );
499   tile.SetParentOrigin( ParentOrigin::CENTER );
500
501   // make the tile 100% of parent
502   tile.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
503
504   Actor content = Actor::New();
505   content.SetAnchorPoint( AnchorPoint::CENTER );
506   content.SetParentOrigin( ParentOrigin::CENTER );
507   content.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
508   tile.Add(content);
509
510   // create background image
511   if( addBackground )
512   {
513     Image bg = Image::New( TILE_BACKGROUND );
514     ImageActor image = ImageActor::New( bg );
515     image.SetAnchorPoint( AnchorPoint::CENTER );
516     image.SetParentOrigin( ParentOrigin::CENTER );
517     // make the image 100% of tile
518     image.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
519     // move image back to get text appear in front
520     image.SetZ( -1 );
521     image.SetStyle( ImageActor::STYLE_NINE_PATCH );
522     image.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
523
524     content.Add( image );
525
526     // Add stencil
527     ImageActor stencil = NewStencilImage();
528     stencil.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
529     image.Add( stencil );
530   }
531
532   TextView text = TextView::New( title );
533   text.SetAnchorPoint( AnchorPoint::CENTER );
534   text.SetParentOrigin( ParentOrigin::CENTER );
535   text.SetWidthExceedPolicy( Toolkit::TextView::ShrinkToFit );
536   text.SetMultilinePolicy( Toolkit::TextView::SplitByWord );
537   text.SetLineJustification( Toolkit::TextView::Center );
538   text.SetTextAlignment( Toolkit::Alignment::Type( Alignment::HorizontalCenter | Alignment::VerticalCenter ) );
539   text.SetColor( Color::WHITE );
540   text.SetZ( 1 );
541   // make the text 90% of tile
542   text.SetSize( 0.9f * parentSize.width, 0.9f * parentSize.height );
543   text.SetStyleToCurrentText( GetTableTextStyle() );
544   text.SetSnapshotModeEnabled( false );
545   content.Add( text );
546
547   // Set the tile to be keyboard focusable
548   tile.SetKeyboardFocusable(true);
549
550   // connect to the touch events
551   tile.TouchedSignal().Connect( this, &DaliTableView::OnTilePressed );
552   tile.HoveredSignal().Connect( this, &DaliTableView::OnTileHovered );
553
554   return tile;
555 }
556
557 ImageActor DaliTableView::NewStencilImage()
558 {
559   Image alpha = Image::New( TILE_BACKGROUND_ALPHA );
560
561   ImageActor stencilActor = ImageActor::New( alpha );
562   stencilActor.SetStyle( ImageActor::STYLE_NINE_PATCH );
563   stencilActor.SetNinePatchBorder( Vector4( IMAGE_BORDER_LEFT, IMAGE_BORDER_TOP, IMAGE_BORDER_RIGHT, IMAGE_BORDER_BOTTOM ) );
564
565   stencilActor.SetParentOrigin( ParentOrigin::CENTER );
566   stencilActor.SetAnchorPoint( AnchorPoint::CENTER );
567   stencilActor.SetDrawMode( DrawMode::STENCIL );
568
569   Dali::ShaderEffect shaderEffect = AlphaDiscardEffect::New();
570   stencilActor.SetShaderEffect( shaderEffect );
571
572   return stencilActor;
573 }
574
575 bool DaliTableView::OnTilePressed( Actor actor, const TouchEvent& event )
576 {
577   bool consumed = false;
578
579   const TouchPoint& point = event.GetPoint( 0 );
580   if( TouchPoint::Down == point.state )
581   {
582     mPressedActor = actor;
583     consumed = true;
584   }
585
586   // A button press is only valid if the Down & Up events
587   // both occurred within the button.
588   if( ( TouchPoint::Up == point.state ) &&
589       ( mPressedActor == actor ) )
590   {
591     std::string name = actor.GetName();
592     ExampleMapConstIter iter = mExampleMap.find( name );
593
594     FocusManager focusManager = FocusManager::Get();
595
596     if( iter != mExampleMap.end() )
597     {
598       // ignore Example button presses when scrolling or button animating.
599       if( ( !mScrolling ) && ( !mPressedAnimation ) )
600       {
601         // do nothing, until pressed animation finished.
602         consumed = true;
603       }
604     }
605
606     if( consumed )
607     {
608       mPressedAnimation = Animation::New( BUTTON_PRESS_ANIMATION_TIME );
609       mPressedAnimation.SetEndAction( Animation::Discard );
610
611       // scale the content actor within the Tile, as to not affect the placement within the Table.
612       Actor content = actor.GetChildAt(0);
613       mPressedAnimation.ScaleTo( content, Vector3( 0.9f, 0.9f, 1.0f ), AlphaFunctions::EaseInOut, 0.0f,
614                                  BUTTON_PRESS_ANIMATION_TIME * 0.5f );
615       mPressedAnimation.ScaleTo( content, Vector3::ONE, AlphaFunctions::EaseInOut, BUTTON_PRESS_ANIMATION_TIME * 0.5f,
616                                  BUTTON_PRESS_ANIMATION_TIME * 0.5f );
617       mPressedAnimation.Play();
618       mPressedAnimation.FinishedSignal().Connect( this, &DaliTableView::OnPressedAnimationFinished );
619     }
620   }
621   return consumed;
622 }
623
624 void DaliTableView::OnPressedAnimationFinished( Dali::Animation& source )
625 {
626   mPressedAnimation.Reset();
627   if( mPressedActor )
628   {
629     std::string name = mPressedActor.GetName();
630     ExampleMapConstIter iter = mExampleMap.find( name );
631
632     if( iter == mExampleMap.end() )
633     {
634       if( name == BUTTON_QUIT )
635       {
636         // Move focus to the OK button
637         FocusManager focusManager = FocusManager::Get();
638
639         // Enable the group mode and wrap mode
640         focusManager.SetGroupMode( true );
641         focusManager.SetWrapMode( true );
642       }
643     }
644     else
645     {
646       const Example& example( iter->second );
647
648       std::stringstream stream;
649       stream << DALI_EXAMPLE_BIN << example.name.c_str();
650       pid_t pid = fork();
651       if( pid == 0)
652       {
653         execlp( stream.str().c_str(), example.name.c_str(), NULL );
654         DALI_ASSERT_ALWAYS(false && "exec failed!");
655       }
656     }
657     mPressedActor.Reset();
658   }
659 }
660
661 void DaliTableView::OnScrollStart( const Dali::Vector3& position )
662 {
663   mScrolling = true;
664
665   PlayAnimation();
666 }
667
668 void DaliTableView::OnScrollComplete( const Dali::Vector3& position )
669 {
670   mScrolling = false;
671
672   // move focus to 1st item of new page
673   FocusManager focusManager = FocusManager::Get();
674   focusManager.SetCurrentFocusActor(mTableViewList[mScrollView.GetCurrentPage()].GetChildAt(TableView::CellPosition(1, 0)) );
675
676 }
677
678 bool DaliTableView::OnScrollTouched( Actor actor, const TouchEvent& event )
679 {
680   const TouchPoint& point = event.GetPoint( 0 );
681   if( TouchPoint::Down == point.state )
682   {
683     mPressedActor = actor;
684   }
685
686   return false;
687 }
688
689 void DaliTableView::ApplyScrollViewEffect()
690 {
691   // Remove old effect if exists.
692
693   if( mScrollViewEffect )
694   {
695     mScrollView.RemoveEffect( mScrollViewEffect );
696   }
697
698   // Just one effect for now
699   SetupInnerPageCubeEffect();
700
701   mScrollView.ApplyEffect( mScrollViewEffect );
702 }
703
704 void DaliTableView::SetupInnerPageCubeEffect()
705 {
706   ScrollViewCustomEffect customEffect;
707   mScrollViewEffect = customEffect = ScrollViewCustomEffect::New();
708   mScrollView.SetScrollSnapDuration( EFFECT_SNAP_DURATION );
709   mScrollView.SetScrollFlickDuration( EFFECT_FLICK_DURATION );
710   mScrollView.RemoveConstraintsFromChildren();
711
712   customEffect.SetPageSpacing( Vector2( 30.0f, 30.0f ) );
713   customEffect.SetAngledOriginPageRotation( ANGLE_CUBE_PAGE_ROTATE );
714   customEffect.SetSwingAngle( ANGLE_CUBE_PAGE_ROTATE.x, Vector3( 0, -1, 0 ) );
715   customEffect.SetOpacityThreshold( 0.5f );   // Make fade out on edges
716 }
717
718 void DaliTableView::ApplyEffectToPage( Actor page, const Vector3& tableRelativeSize )
719 {
720   page.RemoveConstraints();
721
722   Constraint constraint = Constraint::New<Vector3>( Actor::SCALE,
723                                                     LocalSource( Actor::SIZE ),
724                                                     ParentSource( Actor::SIZE ),
725                                                     ScaleToFitConstraint() );
726   page.ApplyConstraint(constraint);
727
728   ApplyCustomEffectToPage( page );
729 }
730
731 void DaliTableView::ApplyCustomEffectToPage( Actor page )
732 {
733   ScrollViewCustomEffect customEffect = ScrollViewCustomEffect::DownCast( mScrollViewEffect );
734   Vector2 vStageSize( Stage::GetCurrent().GetSize() );
735   customEffect.ApplyToPage( page, Vector3( vStageSize.x, vStageSize.y, 1.0f ) );
736 }
737
738 void DaliTableView::OnKeyEvent( const KeyEvent& event )
739 {
740   if( event.state == KeyEvent::Down )
741   {
742     if ( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
743     {
744       mApplication.Quit();
745     }
746   }
747 }
748
749 Actor CreateBackgroundActor( const Vector2& size )
750 {
751   Actor layer = Actor::New();
752   layer.SetAnchorPoint( AnchorPoint::CENTER );
753   layer.SetParentOrigin( ParentOrigin::CENTER );
754   layer.SetSize( size );
755   return layer;
756 }
757
758 void DaliTableView::SetupBackground( Actor addToLayer, const Vector2& size )
759 {
760   // Create distance field shape
761   BitmapImage distanceField;
762   Size imageSize( 512, 512 );
763   CreateShapeImage( CIRCLE, imageSize, distanceField );
764
765   // Create layers
766   Actor backgroundAnimLayer0 = CreateBackgroundActor( size );
767   Actor backgroundAnimLayer1 = CreateBackgroundActor( size );
768   Actor backgroundAnimLayer2 = CreateBackgroundActor( size );
769
770   // Add constraints
771   Constraint animConstraint0 = Constraint::New < Vector3 > ( Actor::POSITION,
772       Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
773       AnimScrollConstraint( backgroundAnimLayer0.GetCurrentPosition(), 0.75f ) );
774   backgroundAnimLayer0.ApplyConstraint( animConstraint0 );
775
776   Constraint animConstraint1 = Constraint::New < Vector3 > ( Actor::POSITION,
777       Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
778       AnimScrollConstraint( backgroundAnimLayer1.GetCurrentPosition(), 0.5f ) );
779   backgroundAnimLayer1.ApplyConstraint( animConstraint1 );
780
781   Constraint animConstraint2 = Constraint::New < Vector3 > ( Actor::POSITION,
782       Source( mScrollView, mScrollView.GetPropertyIndex( ScrollView::SCROLL_POSITION_PROPERTY_NAME ) ),
783       AnimScrollConstraint( backgroundAnimLayer2.GetCurrentPosition(), 0.25f ) );
784   backgroundAnimLayer2.ApplyConstraint( animConstraint2 );
785
786   // Background
787   ImageActor layer = Dali::Toolkit::CreateSolidColorActor( BACKGROUND_COLOR );
788   layer.SetAnchorPoint( AnchorPoint::CENTER );
789   layer.SetParentOrigin( ParentOrigin::CENTER );
790   layer.SetSize( size * BACKGROUND_SIZE_SCALE );
791   layer.SetZ( BACKGROUND_Z );
792   layer.SetPositionInheritanceMode( DONT_INHERIT_POSITION );
793
794   addToLayer.Add( layer );
795
796   // Parent the layers
797   addToLayer.Add( backgroundAnimLayer0 );
798   addToLayer.Add( backgroundAnimLayer1 );
799   addToLayer.Add( backgroundAnimLayer2 );
800
801   // Add all the children
802   AddBackgroundActors( backgroundAnimLayer0, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
803   AddBackgroundActors( backgroundAnimLayer1, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
804   AddBackgroundActors( backgroundAnimLayer2, NUM_BACKGROUND_IMAGES / 3, distanceField, size );
805 }
806
807 void DaliTableView::AddBackgroundActors( Actor layer, int count, BitmapImage distanceField, const Dali::Vector2& size )
808 {
809   for( int i = 0; i < count; ++i )
810   {
811     float randSize = Random::Range( 10.0f, 400.0f );
812     float hue = Random::Range( 0.3f, 1.0f );
813     Vector4 randColour( hue, hue*0.5, 0.0f, Random::Range( 0.3f, 0.6f ));
814
815     ImageActor dfActor = ImageActor::New( distanceField );
816     mBackgroundActors.push_back( dfActor );
817     dfActor.SetSize( Vector2( randSize, randSize ) );
818     dfActor.SetParentOrigin( ParentOrigin::CENTER );
819
820     Toolkit::DistanceFieldEffect effect = Toolkit::DistanceFieldEffect::New();
821     dfActor.SetShaderEffect( effect );
822     dfActor.SetColor( randColour );
823     effect.SetOutlineParams( Vector2( 0.55f, 0.00f ) );
824     effect.SetSmoothingEdge( 0.5f );
825     layer.Add( dfActor );
826
827     // Setup animation
828     Vector3 actorPos(
829         Random::Range( -size.x * 0.5f * BACKGROUND_SPREAD_SCALE, size.x * 0.5f * BACKGROUND_SPREAD_SCALE ),
830         Random::Range( -size.y * 0.5f - randSize, size.y * 0.5f + randSize ),
831         Random::Range(-1.0f, 0.0f) );
832     dfActor.SetPosition( actorPos );
833
834     Constraint movementConstraint = Constraint::New < Vector3 > ( Actor::POSITION,
835         LocalSource( Actor::SIZE ),
836         ParentSource( Actor::SIZE ),
837         ShapeMovementConstraint );
838     dfActor.ApplyConstraint( movementConstraint );
839
840     // Kickoff animation
841     Animation animation = Animation::New( Random::Range( 40.0f, 200.0f ) );
842     KeyFrames keyframes = KeyFrames::New();
843     keyframes.Add( 0.0f, actorPos );
844     Vector3 toPos( actorPos );
845     toPos.y -= ( size.y + randSize );
846     keyframes.Add( 1.0f, toPos );
847     animation.AnimateBetween( Property( dfActor, Actor::POSITION ), keyframes );
848     animation.SetLooping( true );
849     animation.Play();
850     mBackgroundAnimations.push_back( animation );
851   }
852 }
853
854 void DaliTableView::CreateShapeImage( ShapeType shapeType, const Size& size, BitmapImage& distanceFieldOut )
855 {
856   // this bitmap will hold the alpha map for the distance field shader
857   distanceFieldOut = BitmapImage::New( size.width, size.height, Pixel::A8 );
858
859   // Generate bit pattern
860   std::vector< unsigned char > imageDataA8;
861   imageDataA8.reserve( size.width * size.height ); // A8
862
863   switch( shapeType )
864   {
865     case CIRCLE:
866       GenerateCircle( size, imageDataA8 );
867       break;
868     case SQUARE:
869       GenerateSquare( size, imageDataA8 );
870       break;
871     default:
872       break;
873   }
874
875   PixelBuffer* buffer = distanceFieldOut.GetBuffer();
876   if( buffer )
877   {
878     GenerateDistanceFieldMap( &imageDataA8[ 0 ], size, buffer, size, 8.0f, size );
879     distanceFieldOut.Update();
880   }
881 }
882
883 void DaliTableView::GenerateSquare( const Size& size, std::vector< unsigned char >& distanceFieldOut )
884 {
885   for( int h = 0; h < size.height; ++h )
886   {
887     for( int w = 0; w < size.width; ++w )
888     {
889       distanceFieldOut.push_back( 0xFF );
890     }
891   }
892 }
893
894 void DaliTableView::GenerateCircle( const Size& size, std::vector< unsigned char >& distanceFieldOut )
895 {
896   const float radius = size.width * 0.5f * size.width * 0.5f;
897   Vector2 center( size.width / 2, size.height / 2 );
898
899   for( int h = 0; h < size.height; ++h )
900   {
901     for( int w = 0; w < size.width; ++w )
902     {
903       Vector2 pos( w, h );
904       Vector2 dist = pos - center;
905
906       if( dist.x * dist.x + dist.y * dist.y > radius )
907       {
908         distanceFieldOut.push_back( 0x00 );
909       }
910       else
911       {
912         distanceFieldOut.push_back( 0xFF );
913       }
914     }
915   }
916 }
917
918 ImageActor DaliTableView::CreateLogo( std::string imagePath )
919 {
920   Image image = Image::New( imagePath );
921   image.LoadingFinishedSignal().Connect( this, &DaliTableView::OnLogoLoaded );
922
923   ImageActor logo = ImageActor::New( image );
924
925   logo.SetAnchorPoint( AnchorPoint::CENTER );
926   logo.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
927
928   return logo;
929 }
930
931 void DaliTableView::OnLogoLoaded( Dali::Image image )
932 {
933   mRootActor.SetFixedHeight( 1, image.GetHeight() + LOGO_BOTTOM_PADDING_HEIGHT );
934 }
935
936 bool DaliTableView::PauseBackgroundAnimation()
937 {
938   PauseAnimation();
939
940   return false;
941 }
942
943 void DaliTableView::PauseAnimation()
944 {
945   if( mBackgroundAnimsPlaying )
946   {
947     for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
948     {
949       Animation anim = *animIter;
950
951       anim.Pause();
952     }
953
954     mBackgroundAnimsPlaying = false;
955   }
956 }
957
958 void DaliTableView::PlayAnimation()
959 {
960   if ( !mBackgroundAnimsPlaying )
961   {
962     for( AnimationListIter animIter = mBackgroundAnimations.begin(); animIter != mBackgroundAnimations.end(); ++animIter )
963     {
964       Animation anim = *animIter;
965
966       anim.Play();
967     }
968
969     mBackgroundAnimsPlaying = true;
970   }
971
972   mAnimationTimer.SetInterval( BACKGROUND_ANIMATION_DURATION );
973 }
974
975 Dali::Actor DaliTableView::OnKeyboardPreFocusChange( Dali::Actor current, Dali::Actor proposed, Dali::Toolkit::Control::KeyboardFocusNavigationDirection direction )
976 {
977   Actor nextFocusActor = proposed;
978
979   if ( !current && !proposed  )
980   {
981     // Set the initial focus to the first tile in the current page should be focused.
982     nextFocusActor = mTableViewList[mScrollView.GetCurrentPage()].GetChildAt(TableView::CellPosition(0, 0));
983   }
984   else if( !proposed || (proposed && proposed == mScrollViewLayer) )
985   {
986     // ScrollView is being focused but nothing in the current page can be focused further
987     // in the given direction. We should work out which page to scroll to next.
988     int currentPage = mScrollView.GetCurrentPage();
989     int newPage = currentPage;
990     if( direction == Dali::Toolkit::Control::Left )
991     {
992       newPage--;
993     }
994     else if( direction == Dali::Toolkit::Control::Right )
995     {
996       newPage++;
997     }
998
999     newPage = std::max(0, std::min(static_cast<int>(mScrollRulerX->GetTotalPages() - 1), newPage));
1000     if( newPage == currentPage )
1001     {
1002       if( direction == Dali::Toolkit::Control::Left )
1003       {
1004         newPage = mScrollRulerX->GetTotalPages() - 1;
1005       } else if( direction == Dali::Toolkit::Control::Right )
1006       {
1007         newPage = 0;
1008       }
1009     }
1010
1011     // Scroll to the page in the given direction
1012     mScrollView.ScrollTo(newPage);
1013
1014     if( direction == Dali::Toolkit::Control::Left )
1015     {
1016       // Work out the cell position for the last tile
1017       int remainingExamples = mExampleList.size() - newPage * EXAMPLES_PER_PAGE;
1018       int rowPos = (remainingExamples >= EXAMPLES_PER_PAGE) ? ROWS_PER_PAGE - 1 : ( (remainingExamples % EXAMPLES_PER_PAGE + EXAMPLES_PER_ROW) / EXAMPLES_PER_ROW - 1 );
1019       int colPos = remainingExamples >= EXAMPLES_PER_PAGE ? EXAMPLES_PER_ROW - 1 : ( remainingExamples % EXAMPLES_PER_PAGE - rowPos * EXAMPLES_PER_ROW - 1 );
1020
1021       // Move the focus to the last tile in the new page.
1022       nextFocusActor = mTableViewList[newPage].GetChildAt(TableView::CellPosition(rowPos, colPos));
1023     }
1024     else
1025     {
1026       // Move the focus to the first tile in the new page.
1027       nextFocusActor = mTableViewList[newPage].GetChildAt(TableView::CellPosition(0, 0));
1028     }
1029   }
1030
1031   return nextFocusActor;
1032 }
1033
1034 void DaliTableView::OnFocusedActorActivated( Dali::Actor activatedActor )
1035 {
1036   if(activatedActor)
1037   {
1038     mPressedActor = activatedActor;
1039
1040     // Activate the current focused actor;
1041     TouchEvent touchEventUp;
1042     touchEventUp.points.push_back( TouchPoint ( 0, TouchPoint::Up, 0.0f, 0.0f ) );
1043     OnTilePressed(mPressedActor, touchEventUp);
1044   }
1045 }
1046
1047 bool DaliTableView::OnTileHovered( Actor actor, const HoverEvent& event )
1048 {
1049   KeyboardFocusManager::Get().SetCurrentFocusActor( actor );
1050   return true;
1051 }
1052
1053