Merge "Reduced complexity of dali-table-view" into devel/master
[platform/core/uifw/dali-demo.git] / shared / dali-table-view.cpp
1 /*
2  * Copyright (c) 2020 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 <dali/devel-api/actors/actor-devel.h>
23 #include <dali/devel-api/images/distance-field.h>
24 #include <dali-toolkit/devel-api/controls/control-devel.h>
25 #include <dali-toolkit/devel-api/shader-effects/alpha-discard-effect.h>
26 #include <dali-toolkit/devel-api/shader-effects/distance-field-effect.h>
27 #include <dali-toolkit/dali-toolkit.h>
28 #include <dali-toolkit/devel-api/accessibility-manager/accessibility-manager.h>
29 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
30 #include <dali-toolkit/devel-api/shader-effects/alpha-discard-effect.h>
31 #include <dali-toolkit/devel-api/shader-effects/distance-field-effect.h>
32 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
33 #include <dali/devel-api/actors/actor-devel.h>
34 #include <dali/devel-api/images/distance-field.h>
35 #include <algorithm>
36
37 // INTERNAL INCLUDES
38 #include "shared/execute-process.h"
39 #include "shared/utility.h"
40 #include "shared/view.h"
41
42 using namespace Dali;
43 using namespace Dali::Toolkit;
44
45 ///////////////////////////////////////////////////////////////////////////////
46
47 namespace
48 {
49 const std::string LOGO_PATH(DEMO_IMAGE_DIR "Logo-for-demo.png");
50
51 // Keyboard focus effect constants.
52 const float   KEYBOARD_FOCUS_ANIMATION_DURATION   = 1.0f;                                                                                                     ///< The total duration of the keyboard focus animation
53 const int32_t KEYBOARD_FOCUS_ANIMATION_LOOP_COUNT = 5;                                                                                                        ///< The number of loops for the keyboard focus animation
54 const float   KEYBOARD_FOCUS_FINAL_SCALE_FLOAT    = 1.05f;                                                                                                    ///< The final scale of the focus highlight
55 const float   KEYBOARD_FOCUS_ANIMATED_SCALE_FLOAT = 1.18f;                                                                                                    ///< The scale of the focus highlight during the animation
56 const float   KEYBOARD_FOCUS_FINAL_ALPHA          = 0.7f;                                                                                                     ///< The final alpha of the focus highlight
57 const float   KEYBOARD_FOCUS_ANIMATING_ALPHA      = 0.0f;                                                                                                     ///< The alpha of the focus highlight during the animation
58 const float   KEYBOARD_FOCUS_FADE_PERCENTAGE      = 0.16f;                                                                                                    ///< The duration of the fade (from translucent to the final-alpha) as a percentage of the overall animation duration
59 const Vector3 KEYBOARD_FOCUS_FINAL_SCALE(KEYBOARD_FOCUS_FINAL_SCALE_FLOAT, KEYBOARD_FOCUS_FINAL_SCALE_FLOAT, KEYBOARD_FOCUS_FINAL_SCALE_FLOAT);               ///< @see KEYBOARD_FOCUS_START_SCALE_FLOAT
60 const Vector3 FOCUS_INDICATOR_ANIMATING_SCALE(KEYBOARD_FOCUS_ANIMATED_SCALE_FLOAT, KEYBOARD_FOCUS_ANIMATED_SCALE_FLOAT, KEYBOARD_FOCUS_ANIMATED_SCALE_FLOAT); ///< @see KEYBOARD_FOCUS_END_SCALE_FLOAT
61 const float   KEYBOARD_FOCUS_MID_KEY_FRAME_TIME = KEYBOARD_FOCUS_ANIMATION_DURATION - (KEYBOARD_FOCUS_ANIMATION_DURATION * KEYBOARD_FOCUS_FADE_PERCENTAGE);   ///< Time of the mid key-frame
62
63 const float   TILE_LABEL_PADDING          = 8.0f;  ///< Border between edge of tile and the example text
64 const float   BUTTON_PRESS_ANIMATION_TIME = 0.35f; ///< Time to perform button scale effect.
65 const int     EXAMPLES_PER_ROW            = 3;
66 const int     ROWS_PER_PAGE               = 3;
67 const int     EXAMPLES_PER_PAGE           = EXAMPLES_PER_ROW * ROWS_PER_PAGE;
68 const Vector3 TABLE_RELATIVE_SIZE(0.95f, 0.9f, 0.8f);     ///< TableView's relative size to the entire stage. The Y value means sum of the logo and table relative heights.
69
70 const char* const DEMO_BUILD_DATE = __DATE__ " " __TIME__;
71
72 /**
73  * Creates the background image
74  */
75 Actor CreateBackground(std::string stylename)
76 {
77   Control background = Control::New();
78   background.SetStyleName(stylename);
79   background.SetProperty(Actor::Property::NAME, "BACKGROUND");
80   background.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
81   background.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
82   background.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
83   return background;
84 }
85
86 /**
87  * Constraint to precalculate values from the scroll-view
88  * and tile positions to pass to the tile shader.
89  */
90 struct TileShaderPositionConstraint
91 {
92   TileShaderPositionConstraint(float pageWidth, float tileXOffset)
93   : mPageWidth(pageWidth),
94     mTileXOffset(tileXOffset)
95   {
96   }
97
98   void operator()(Vector3& position, const PropertyInputContainer& inputs)
99   {
100     // Set up position.x as the tiles X offset (0.0 -> 1.0).
101     position.x = mTileXOffset;
102     // Set up position.z as the linear scroll-view X offset (0.0 -> 1.0).
103     position.z = 1.0f * (-fmod(inputs[0]->GetVector2().x, mPageWidth) / mPageWidth);
104     // Set up position.y as a rectified version of the scroll-views X offset.
105     // IE. instead of 0.0 -> 1.0, it moves between 0.0 -> 0.5 -> 0.0 within the same span.
106     if(position.z > 0.5f)
107     {
108       position.y = 1.0f - position.z;
109     }
110     else
111     {
112       position.y = position.z;
113     }
114   }
115
116 private:
117   float mPageWidth;
118   float mTileXOffset;
119 };
120
121 /**
122  * Creates a popup that shows the version information of the DALi libraries and demo
123  */
124 Dali::Toolkit::Popup CreateVersionPopup(Application& application, ConnectionTrackerInterface& connectionTracker)
125 {
126   std::ostringstream stream;
127   stream << "DALi Core: " << CORE_MAJOR_VERSION << "." << CORE_MINOR_VERSION << "." << CORE_MICRO_VERSION << std::endl
128          << "(" << CORE_BUILD_DATE << ")\n";
129   stream << "DALi Adaptor: " << ADAPTOR_MAJOR_VERSION << "." << ADAPTOR_MINOR_VERSION << "." << ADAPTOR_MICRO_VERSION << std::endl
130          << "(" << ADAPTOR_BUILD_DATE << ")\n";
131   stream << "DALi Toolkit: " << TOOLKIT_MAJOR_VERSION << "." << TOOLKIT_MINOR_VERSION << "." << TOOLKIT_MICRO_VERSION << std::endl
132          << "(" << TOOLKIT_BUILD_DATE << ")\n";
133   stream << "DALi Demo:"
134          << "\n(" << DEMO_BUILD_DATE << ")\n";
135
136   Dali::Toolkit::Popup popup = Dali::Toolkit::Popup::New();
137
138   Toolkit::TextLabel titleActor = Toolkit::TextLabel::New("Version information");
139   titleActor.SetProperty(Actor::Property::NAME, "titleActor");
140   titleActor.SetProperty(Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, HorizontalAlignment::CENTER);
141   titleActor.SetProperty(Toolkit::TextLabel::Property::TEXT_COLOR, Color::WHITE);
142
143   Toolkit::TextLabel contentActor = Toolkit::TextLabel::New(stream.str());
144   contentActor.SetProperty(Actor::Property::NAME, "contentActor");
145   contentActor.SetProperty(Toolkit::TextLabel::Property::MULTI_LINE, true);
146   contentActor.SetProperty(Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, HorizontalAlignment::CENTER);
147   contentActor.SetProperty(Toolkit::TextLabel::Property::TEXT_COLOR, Color::WHITE);
148   contentActor.SetProperty(Actor::Property::PADDING, Padding(0.0f, 0.0f, 20.0f, 0.0f));
149
150   popup.SetTitle(titleActor);
151   popup.SetContent(contentActor);
152
153   popup.SetResizePolicy(ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::WIDTH);
154   popup.SetProperty(Actor::Property::SIZE_MODE_FACTOR, Vector3(0.75f, 1.0f, 1.0f));
155   popup.SetResizePolicy(ResizePolicy::FIT_TO_CHILDREN, Dimension::HEIGHT);
156
157   application.GetWindow().Add(popup);
158
159   // Hide the popup when touched outside
160   popup.OutsideTouchedSignal().Connect(
161     &connectionTracker,
162     [popup]() mutable
163     {
164       if(popup && (popup.GetDisplayState() == Toolkit::Popup::SHOWN))
165       {
166         popup.SetDisplayState(Popup::HIDDEN);
167       }
168     });
169
170   return popup;
171 }
172
173 /// Sets up the inner cube effect
174 Dali::Toolkit::ScrollViewEffect SetupInnerPageCubeEffect(const Vector2 windowSize, int totalPages)
175 {
176   Dali::Path            path = Dali::Path::New();
177   Dali::Property::Array points;
178   points.Resize(3);
179   points[0] = Vector3(windowSize.x * 0.5, 0.0f, windowSize.x * 0.5f);
180   points[1] = Vector3(0.0f, 0.0f, 0.0f);
181   points[2] = Vector3(-windowSize.x * 0.5f, 0.0f, windowSize.x * 0.5f);
182   path.SetProperty(Path::Property::POINTS, points);
183
184   Dali::Property::Array controlPoints;
185   controlPoints.Resize(4);
186   controlPoints[0] = Vector3(windowSize.x * 0.5f, 0.0f, windowSize.x * 0.3f);
187   controlPoints[1] = Vector3(windowSize.x * 0.3f, 0.0f, 0.0f);
188   controlPoints[2] = Vector3(-windowSize.x * 0.3f, 0.0f, 0.0f);
189   controlPoints[3] = Vector3(-windowSize.x * 0.5f, 0.0f, windowSize.x * 0.3f);
190   path.SetProperty(Path::Property::CONTROL_POINTS, controlPoints);
191
192   return ScrollViewPagePathEffect::New(path,
193                                        Vector3(-1.0f, 0.0f, 0.0f),
194                                        Toolkit::ScrollView::Property::SCROLL_FINAL_X,
195                                        Vector3(windowSize.x * TABLE_RELATIVE_SIZE.x, windowSize.y * TABLE_RELATIVE_SIZE.y, 0.0f),
196                                        totalPages);
197 }
198
199 /// Sets up the scroll view rulers
200 void SetupScrollViewRulers(ScrollView& scrollView, const uint16_t windowWidth, const float pageWidth, const int totalPages)
201 {
202   // Update Ruler info. for the scroll-view
203   Dali::Toolkit::RulerPtr rulerX = new FixedRuler(pageWidth);
204   Dali::Toolkit::RulerPtr rulerY = new DefaultRuler();
205   rulerX->SetDomain(RulerDomain(0.0f, (totalPages + 1) * windowWidth * TABLE_RELATIVE_SIZE.x * 0.5f, true));
206   rulerY->Disable();
207   scrollView.SetRulerX(rulerX);
208   scrollView.SetRulerY(rulerY);
209 }
210
211 } // namespace
212
213 DaliTableView::DaliTableView(Application& application)
214 : mApplication(application),
215   mRootActor(),
216   mPressedAnimation(),
217   mScrollView(),
218   mScrollViewEffect(),
219   mPressedActor(),
220   mLogoTapDetector(),
221   mVersionPopup(),
222   mPages(),
223   mExampleList(),
224   mPageWidth(0.0f),
225   mTotalPages(),
226   mScrolling(false),
227   mSortAlphabetically(false)
228 {
229   application.InitSignal().Connect(this, &DaliTableView::Initialize);
230 }
231
232 void DaliTableView::AddExample(Example example)
233 {
234   mExampleList.push_back(example);
235 }
236
237 void DaliTableView::SortAlphabetically(bool sortAlphabetically)
238 {
239   mSortAlphabetically = sortAlphabetically;
240 }
241
242 void DaliTableView::Initialize(Application& application)
243 {
244   Window window = application.GetWindow();
245   window.KeyEventSignal().Connect(this, &DaliTableView::OnKeyEvent);
246   const Window::WindowSize windowSize = window.GetSize();
247
248   // Background
249   mRootActor = CreateBackground("LauncherBackground");
250   window.Add(mRootActor);
251
252   // Add logo
253   ImageView logo = ImageView::New(LOGO_PATH);
254   logo.SetProperty(Actor::Property::NAME, "LOGO_IMAGE");
255   logo.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER);
256   logo.SetProperty(Actor::Property::PARENT_ORIGIN, Vector3(0.5f, 0.1f, 0.5f));
257   logo.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS);
258   // The logo should appear on top of everything.
259   logo.SetProperty(Actor::Property::DRAW_MODE, DrawMode::OVERLAY_2D);
260   mRootActor.Add(logo);
261
262   // Show version in a popup when log is tapped
263   mLogoTapDetector = TapGestureDetector::New();
264   mLogoTapDetector.Attach(logo);
265   mLogoTapDetector.DetectedSignal().Connect(this, &DaliTableView::OnLogoTapped);
266
267   // Scrollview occupying the majority of the screen
268   mScrollView = ScrollView::New();
269   mScrollView.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER);
270   mScrollView.SetProperty(Actor::Property::PARENT_ORIGIN, Vector3(0.5f, 1.0f - 0.05f, 0.5f));
271   mScrollView.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
272   mScrollView.SetResizePolicy(ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::HEIGHT);
273   mScrollView.SetProperty(Actor::Property::SIZE_MODE_FACTOR, Vector3(0.0f, 0.6f, 0.0f));
274
275   const float buttonsPageMargin = (1.0f - TABLE_RELATIVE_SIZE.x) * 0.5f * windowSize.GetWidth();
276   mScrollView.SetProperty(Actor::Property::PADDING, Padding(buttonsPageMargin, buttonsPageMargin, 0.0f, 0.0f));
277
278   mScrollView.SetAxisAutoLock(true);
279   mScrollView.ScrollCompletedSignal().Connect(this, &DaliTableView::OnScrollComplete);
280   mScrollView.ScrollStartedSignal().Connect(this, &DaliTableView::OnScrollStart);
281   mScrollView.TouchedSignal().Connect(this, &DaliTableView::OnScrollTouched);
282
283   mPageWidth = windowSize.GetWidth() * TABLE_RELATIVE_SIZE.x * 0.5f;
284
285   mBubbleAnimator.Initialize(mRootActor, mScrollView);
286
287   mRootActor.Add(mScrollView);
288
289   // Add scroll view effect and setup constraints on pages
290   ApplyScrollViewEffect();
291
292   // Add pages and tiles
293   Populate();
294
295   // Remove constraints for inner cube effect
296   ApplyCubeEffectToPages();
297
298   Dali::Window winHandle = application.GetWindow();
299
300   if(windowSize.GetWidth() <= windowSize.GetHeight())
301   {
302     winHandle.AddAvailableOrientation(Dali::WindowOrientation::PORTRAIT);
303     winHandle.RemoveAvailableOrientation(Dali::WindowOrientation::LANDSCAPE);
304     winHandle.AddAvailableOrientation(Dali::WindowOrientation::PORTRAIT_INVERSE);
305     winHandle.RemoveAvailableOrientation(Dali::WindowOrientation::LANDSCAPE_INVERSE);
306   }
307   else
308   {
309     winHandle.AddAvailableOrientation(Dali::WindowOrientation::LANDSCAPE);
310     winHandle.RemoveAvailableOrientation(Dali::WindowOrientation::PORTRAIT);
311     winHandle.AddAvailableOrientation(Dali::WindowOrientation::LANDSCAPE_INVERSE);
312     winHandle.RemoveAvailableOrientation(Dali::WindowOrientation::PORTRAIT_INVERSE);
313   }
314
315   CreateFocusEffect();
316 }
317
318 void DaliTableView::CreateFocusEffect()
319 {
320   // Hook the required signals to manage the focus.
321   auto keyboardFocusManager = KeyboardFocusManager::Get();
322   keyboardFocusManager.PreFocusChangeSignal().Connect(this, &DaliTableView::OnKeyboardPreFocusChange);
323   keyboardFocusManager.FocusedActorEnterKeySignal().Connect(this, &DaliTableView::OnFocusedActorActivated);
324
325   // Loop to create both actors for the focus highlight effect.
326   for(unsigned int i = 0; i < FOCUS_ANIMATION_ACTOR_NUMBER; ++i)
327   {
328     mFocusEffect[i].actor = ImageView::New();
329     mFocusEffect[i].actor.SetStyleName("FocusActor");
330     mFocusEffect[i].actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
331     mFocusEffect[i].actor.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
332     mFocusEffect[i].actor.SetProperty(Actor::Property::INHERIT_SCALE, false);
333     mFocusEffect[i].actor.SetProperty(Actor::Property::COLOR_MODE, USE_OWN_COLOR);
334
335     KeyFrames alphaKeyFrames = KeyFrames::New();
336     alphaKeyFrames.Add(0.0f, KEYBOARD_FOCUS_FINAL_ALPHA);
337     alphaKeyFrames.Add(KEYBOARD_FOCUS_MID_KEY_FRAME_TIME, KEYBOARD_FOCUS_ANIMATING_ALPHA);
338     alphaKeyFrames.Add(KEYBOARD_FOCUS_ANIMATION_DURATION, KEYBOARD_FOCUS_FINAL_ALPHA);
339
340     KeyFrames scaleKeyFrames = KeyFrames::New();
341     scaleKeyFrames.Add(0.0f, KEYBOARD_FOCUS_FINAL_SCALE);
342     scaleKeyFrames.Add(KEYBOARD_FOCUS_MID_KEY_FRAME_TIME, FOCUS_INDICATOR_ANIMATING_SCALE);
343     scaleKeyFrames.Add(KEYBOARD_FOCUS_ANIMATION_DURATION, KEYBOARD_FOCUS_FINAL_SCALE);
344
345     mFocusEffect[i].animation = Animation::New(KEYBOARD_FOCUS_ANIMATION_DURATION);
346     mFocusEffect[i].animation.AnimateBetween(Property(mFocusEffect[i].actor, Actor::Property::COLOR_ALPHA), alphaKeyFrames);
347     mFocusEffect[i].animation.AnimateBetween(Property(mFocusEffect[i].actor, Actor::Property::SCALE), scaleKeyFrames);
348
349     mFocusEffect[i].animation.SetLoopCount(KEYBOARD_FOCUS_ANIMATION_LOOP_COUNT);
350   }
351
352   // Parent the secondary effect from the primary.
353   mFocusEffect[0].actor.Add(mFocusEffect[1].actor);
354
355   keyboardFocusManager.SetFocusIndicatorActor(mFocusEffect[0].actor);
356
357   // Connect to the on & off scene signals of the indicator which represents when it is enabled & disabled respectively
358   mFocusEffect[0].actor.OnSceneSignal().Connect(this, &DaliTableView::OnFocusIndicatorEnabled);
359   mFocusEffect[0].actor.OffSceneSignal().Connect(this, &DaliTableView::OnFocusIndicatorDisabled);
360 }
361
362 void DaliTableView::OnFocusIndicatorEnabled(Actor /* actor */)
363 {
364   // Play the animation on the 1st glow object.
365   mFocusEffect[0].animation.Play();
366
367   // Stagger the animation on the 2nd glow object half way through.
368   mFocusEffect[1].animation.PlayFrom(KEYBOARD_FOCUS_ANIMATION_DURATION / 2.0f);
369 }
370
371 void DaliTableView::OnFocusIndicatorDisabled(Dali::Actor /* actor */)
372 {
373   // Stop the focus effect animations
374   for(auto i = 0u; i < FOCUS_ANIMATION_ACTOR_NUMBER; ++i)
375   {
376     mFocusEffect[i].animation.Stop();
377   }
378 }
379
380 void DaliTableView::ApplyCubeEffectToPages()
381 {
382   ScrollViewPagePathEffect effect = ScrollViewPagePathEffect::DownCast(mScrollViewEffect);
383   unsigned int             pageCount(0);
384   for(std::vector<Actor>::iterator pageIter = mPages.begin(); pageIter != mPages.end(); ++pageIter)
385   {
386     Actor page = *pageIter;
387     effect.ApplyToPage(page, pageCount++);
388   }
389 }
390
391 void DaliTableView::Populate()
392 {
393   const Window::WindowSize windowSize = mApplication.GetWindow().GetSize();
394
395   mTotalPages = (mExampleList.size() + EXAMPLES_PER_PAGE - 1) / EXAMPLES_PER_PAGE;
396
397   // Populate ScrollView.
398   if(mExampleList.size() > 0)
399   {
400     if(mSortAlphabetically)
401     {
402       sort(mExampleList.begin(), mExampleList.end(), [](auto& lhs, auto& rhs)->bool { return lhs.title < rhs.title; } );
403     }
404
405     unsigned int         exampleCount = 0;
406     ExampleListConstIter iter         = mExampleList.begin();
407
408     for(int t = 0; t < mTotalPages && iter != mExampleList.end(); t++)
409     {
410       // Create Table
411       TableView page = TableView::New(ROWS_PER_PAGE, EXAMPLES_PER_ROW);
412       page.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
413       page.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
414       page.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
415       mScrollView.Add(page);
416
417       // Calculate the number of images going across (columns) within a page, according to the screen resolution and dpi.
418       const float margin               = 2.0f;
419       const float tileParentMultiplier = 1.0f / EXAMPLES_PER_ROW;
420
421       for(int row = 0; row < ROWS_PER_PAGE && iter != mExampleList.end(); row++)
422       {
423         for(int column = 0; column < EXAMPLES_PER_ROW && iter != mExampleList.end(); column++)
424         {
425           const Example& example = (*iter);
426
427           // Calculate the tiles relative position on the page (between 0 & 1 in each dimension).
428           Vector2              position(static_cast<float>(column) / (EXAMPLES_PER_ROW - 1.0f), static_cast<float>(row) / (EXAMPLES_PER_ROW - 1.0f));
429           Actor                tile                 = CreateTile(example.name, example.title, Vector3(tileParentMultiplier, tileParentMultiplier, 1.0f), position);
430           AccessibilityManager accessibilityManager = AccessibilityManager::Get();
431           accessibilityManager.SetFocusOrder(tile, ++exampleCount);
432           accessibilityManager.SetAccessibilityAttribute(tile, Dali::Toolkit::AccessibilityManager::ACCESSIBILITY_LABEL, example.title);
433           accessibilityManager.SetAccessibilityAttribute(tile, Dali::Toolkit::AccessibilityManager::ACCESSIBILITY_TRAIT, "Tile");
434           accessibilityManager.SetAccessibilityAttribute(tile, Dali::Toolkit::AccessibilityManager::ACCESSIBILITY_HINT, "You can run this example");
435
436           tile.SetProperty(Actor::Property::PADDING, Padding(margin, margin, margin, margin));
437           page.AddChild(tile, TableView::CellPosition(row, column));
438
439           iter++;
440         }
441       }
442
443       mPages.push_back(page);
444     }
445   }
446
447   SetupScrollViewRulers(mScrollView, windowSize.GetWidth(), mPageWidth, mTotalPages);
448 }
449
450 Actor DaliTableView::CreateTile(const std::string& name, const std::string& title, const Dali::Vector3& sizeMultiplier, Vector2& position)
451 {
452   Toolkit::ImageView focusableTile = ImageView::New();
453
454   focusableTile.SetStyleName("DemoTile");
455   focusableTile.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
456   focusableTile.SetResizePolicy(ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS);
457   focusableTile.SetProperty(Actor::Property::SIZE_MODE_FACTOR, sizeMultiplier);
458   focusableTile.SetProperty(Actor::Property::NAME, name);
459
460   // Set the tile to be keyboard focusable
461   focusableTile.SetProperty(Actor::Property::KEYBOARD_FOCUSABLE, true);
462
463   // Register a property with the ImageView. This allows us to inject the scroll-view position into the shader.
464   Property::Value value         = Vector3(0.0f, 0.0f, 0.0f);
465   Property::Index propertyIndex = focusableTile.RegisterProperty("uCustomPosition", value);
466
467   // We create a constraint to perform a precalculation on the scroll-view X offset
468   // and pass it to the shader uniform, along with the tile's position.
469   Constraint shaderPosition = Constraint::New<Vector3>(focusableTile, propertyIndex, TileShaderPositionConstraint(mPageWidth, position.x));
470   shaderPosition.AddSource(Source(mScrollView, ScrollView::Property::SCROLL_POSITION));
471   shaderPosition.SetRemoveAction(Constraint::DISCARD);
472   shaderPosition.Apply();
473   //focusableTile.Add( tileContent );
474
475   // Create an ImageView for the 9-patch border around the tile.
476   ImageView borderImage = ImageView::New();
477   borderImage.SetStyleName("DemoTileBorder");
478   borderImage.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
479   borderImage.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
480   borderImage.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
481   borderImage.SetProperty(Actor::Property::OPACITY, 0.8f);
482   DevelControl::AppendAccessibilityRelation(borderImage, focusableTile, Accessibility::RelationType::CONTROLLED_BY);
483   focusableTile.Add(borderImage);
484
485   TextLabel label = TextLabel::New();
486   label.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
487   label.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
488   label.SetStyleName("LauncherLabel");
489   label.SetProperty(TextLabel::Property::MULTI_LINE, true);
490   label.SetProperty(TextLabel::Property::TEXT, title);
491   label.SetProperty(TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER");
492   label.SetProperty(TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER");
493   label.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT);
494
495   // Pad around the label as its size is the same as the 9-patch border. It will overlap it without padding.
496   label.SetProperty(Actor::Property::PADDING, Padding(TILE_LABEL_PADDING, TILE_LABEL_PADDING, TILE_LABEL_PADDING, TILE_LABEL_PADDING));
497   DevelControl::AppendAccessibilityRelation(label, focusableTile, Accessibility::RelationType::CONTROLLED_BY);
498   focusableTile.Add(label);
499
500   // Connect to the touch events
501   focusableTile.TouchedSignal().Connect(this, &DaliTableView::OnTilePressed);
502   focusableTile.HoveredSignal().Connect(this, &DaliTableView::OnTileHovered);
503   focusableTile.SetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_ROLE, Dali::Accessibility::Role::PUSH_BUTTON);
504   DevelControl::AccessibilityActivateSignal(focusableTile).Connect(this, [=](){
505     DoTilePress(focusableTile, PointState::DOWN);
506     DoTilePress(focusableTile, PointState::UP);
507   });
508
509   return focusableTile;
510 }
511
512 bool DaliTableView::OnTilePressed(Actor actor, const TouchEvent& event)
513 {
514   return DoTilePress(actor, event.GetState(0));
515 }
516
517 bool DaliTableView::DoTilePress(Actor actor, PointState::Type pointState)
518 {
519   bool consumed = false;
520
521   if(PointState::DOWN == pointState)
522   {
523     mPressedActor = actor;
524     consumed      = true;
525   }
526
527   // A button press is only valid if the Down & Up events
528   // both occurred within the button.
529   if((PointState::UP == pointState) &&
530      (mPressedActor == actor))
531   {
532     // ignore Example button presses when scrolling or button animating.
533     if((!mScrolling) && (!mPressedAnimation))
534     {
535       std::string           name = actor.GetProperty<std::string>(Dali::Actor::Property::NAME);
536       for(Example& example : mExampleList)
537       {
538         if(example.name == name)
539         {
540           // do nothing, until pressed animation finished.
541           consumed = true;
542           break;
543         }
544       }
545     }
546
547     if(consumed)
548     {
549       mPressedAnimation = Animation::New(BUTTON_PRESS_ANIMATION_TIME);
550       mPressedAnimation.SetEndAction(Animation::DISCARD);
551
552       // scale the content actor within the Tile, as to not affect the placement within the Table.
553       Actor content = actor.GetChildAt(0);
554       mPressedAnimation.AnimateTo(Property(content, Actor::Property::SCALE), Vector3(0.7f, 0.7f, 1.0f), AlphaFunction::EASE_IN_OUT, TimePeriod(0.0f, BUTTON_PRESS_ANIMATION_TIME * 0.5f));
555       mPressedAnimation.AnimateTo(Property(content, Actor::Property::SCALE), Vector3::ONE, AlphaFunction::EASE_IN_OUT, TimePeriod(BUTTON_PRESS_ANIMATION_TIME * 0.5f, BUTTON_PRESS_ANIMATION_TIME * 0.5f));
556
557       // Rotate button on the Y axis when pressed.
558       mPressedAnimation.AnimateBy(Property(content, Actor::Property::ORIENTATION), Quaternion(Degree(0.0f), Degree(180.0f), Degree(0.0f)));
559
560       mPressedAnimation.Play();
561       mPressedAnimation.FinishedSignal().Connect(this, &DaliTableView::OnPressedAnimationFinished);
562     }
563   }
564   return consumed;
565 }
566
567 void DaliTableView::OnPressedAnimationFinished(Dali::Animation& source)
568 {
569   mPressedAnimation.Reset();
570   if(mPressedActor)
571   {
572     std::string name = mPressedActor.GetProperty<std::string>(Dali::Actor::Property::NAME);
573
574     ExecuteProcess(name, mApplication);
575
576     mPressedActor.Reset();
577   }
578 }
579
580 void DaliTableView::OnScrollStart(const Dali::Vector2& position)
581 {
582   mScrolling = true;
583
584   mBubbleAnimator.PlayAnimation();
585 }
586
587 void DaliTableView::OnScrollComplete(const Dali::Vector2& position)
588 {
589   mScrolling = false;
590
591   // move focus to 1st item of new page
592   AccessibilityManager accessibilityManager = AccessibilityManager::Get();
593   accessibilityManager.SetCurrentFocusActor(mPages[mScrollView.GetCurrentPage()].GetChildAt(0));
594 }
595
596 bool DaliTableView::OnScrollTouched(Actor actor, const TouchEvent& event)
597 {
598   if(PointState::DOWN == event.GetState(0))
599   {
600     mPressedActor = actor;
601   }
602
603   return false;
604 }
605
606 void DaliTableView::ApplyScrollViewEffect()
607 {
608   // Remove old effect if exists.
609
610   if(mScrollViewEffect)
611   {
612     mScrollView.RemoveEffect(mScrollViewEffect);
613   }
614
615   // Just one effect for now
616   mScrollViewEffect = SetupInnerPageCubeEffect(mApplication.GetWindow().GetSize(), mTotalPages);
617
618   mScrollView.ApplyEffect(mScrollViewEffect);
619 }
620
621 void DaliTableView::OnKeyEvent(const KeyEvent& event)
622 {
623   if(event.GetState() == KeyEvent::DOWN)
624   {
625     if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
626     {
627       // If there's a Popup, Hide it if it's contributing to the display in any way (EG. transitioning in or out).
628       // Otherwise quit.
629       if(mVersionPopup && (mVersionPopup.GetDisplayState() != Toolkit::Popup::HIDDEN))
630       {
631         mVersionPopup.SetDisplayState(Popup::HIDDEN);
632       }
633       else
634       {
635         mApplication.Quit();
636       }
637     }
638   }
639 }
640
641 Dali::Actor DaliTableView::OnKeyboardPreFocusChange(Dali::Actor current, Dali::Actor proposed, Dali::Toolkit::Control::KeyboardFocus::Direction direction)
642 {
643   Actor nextFocusActor = proposed;
644
645   if(!current && !proposed)
646   {
647     // Set the initial focus to the first tile in the current page should be focused.
648     nextFocusActor = mPages[mScrollView.GetCurrentPage()].GetChildAt(0);
649   }
650   else if(!proposed)
651   {
652     // ScrollView is being focused but nothing in the current page can be focused further
653     // in the given direction. We should work out which page to scroll to next.
654     int currentPage = mScrollView.GetCurrentPage();
655     int newPage     = currentPage;
656     if(direction == Dali::Toolkit::Control::KeyboardFocus::LEFT)
657     {
658       newPage--;
659     }
660     else if(direction == Dali::Toolkit::Control::KeyboardFocus::RIGHT)
661     {
662       newPage++;
663     }
664
665     newPage = std::max(0, std::min(mTotalPages - 1, newPage));
666     if(newPage == currentPage)
667     {
668       if(direction == Dali::Toolkit::Control::KeyboardFocus::LEFT)
669       {
670         newPage = mTotalPages - 1;
671       }
672       else if(direction == Dali::Toolkit::Control::KeyboardFocus::RIGHT)
673       {
674         newPage = 0;
675       }
676     }
677
678     // Scroll to the page in the given direction
679     mScrollView.ScrollTo(newPage);
680
681     if(direction == Dali::Toolkit::Control::KeyboardFocus::LEFT)
682     {
683       // Work out the cell position for the last tile
684       int remainingExamples = mExampleList.size() - newPage * EXAMPLES_PER_PAGE;
685       int rowPos            = (remainingExamples >= EXAMPLES_PER_PAGE) ? ROWS_PER_PAGE - 1 : ((remainingExamples % EXAMPLES_PER_PAGE + EXAMPLES_PER_ROW) / EXAMPLES_PER_ROW - 1);
686       int colPos            = remainingExamples >= EXAMPLES_PER_PAGE ? EXAMPLES_PER_ROW - 1 : (remainingExamples % EXAMPLES_PER_PAGE - rowPos * EXAMPLES_PER_ROW - 1);
687
688       // Move the focus to the last tile in the new page.
689       nextFocusActor = mPages[newPage].GetChildAt(rowPos * EXAMPLES_PER_ROW + colPos);
690     }
691     else
692     {
693       // Move the focus to the first tile in the new page.
694       nextFocusActor = mPages[newPage].GetChildAt(0);
695     }
696   }
697
698   return nextFocusActor;
699 }
700
701 void DaliTableView::OnFocusedActorActivated(Dali::Actor activatedActor)
702 {
703   if(activatedActor)
704   {
705     mPressedActor = activatedActor;
706
707     // Activate the current focused actor;
708     DoTilePress(mPressedActor, PointState::UP);
709   }
710 }
711
712 bool DaliTableView::OnTileHovered(Actor actor, const HoverEvent& event)
713 {
714   KeyboardFocusManager::Get().SetCurrentFocusActor(actor);
715   return true;
716 }
717
718 void DaliTableView::OnLogoTapped(Dali::Actor actor, const Dali::TapGesture& tap)
719 {
720   // Only show if currently fully hidden. If transitioning-out, the transition will not be interrupted.
721   if(!mVersionPopup || (mVersionPopup.GetDisplayState() == Toolkit::Popup::HIDDEN))
722   {
723     if(!mVersionPopup)
724     {
725       mVersionPopup = CreateVersionPopup(mApplication, *this);
726     }
727
728     mVersionPopup.SetDisplayState(Popup::SHOWN);
729   }
730 }