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