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