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