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