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