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