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