Demo of Dali toolkit and core capabilities
[platform/core/uifw/dali-demo.git] / demo / dali-table-view.h
1 #ifndef __DALI_DEMO_H__
2 #define __DALI_DEMO_H__
3
4 //
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 //
7 // Licensed under the Flora License, Version 1.0 (the License);
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //     http://floralicense.org/license/
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an AS IS BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19
20 #include <dali/dali.h>
21 #include <dali-toolkit/dali-toolkit.h>
22
23 class Example;
24
25 typedef std::vector<Example> ExampleList;
26 typedef std::map<std::string,Example> ExampleMap;
27 typedef ExampleList::iterator ExampleListIter;
28 typedef ExampleList::const_iterator ExampleListConstIter;
29 typedef ExampleMap::iterator ExampleMapIter;
30 typedef ExampleMap::const_iterator ExampleMapConstIter;
31
32 typedef std::vector<Dali::Toolkit::TableView> TableViewList;
33 typedef TableViewList::iterator TableViewListIter;
34 typedef TableViewList::const_iterator TableViewListConstIter;
35
36 typedef std::vector<Dali::ImageActor> ImageActorList;
37 typedef ImageActorList::iterator ImageActorListIter;
38 typedef ImageActorList::const_iterator ImageActorListConstIter;
39
40 typedef std::vector<Dali::Animation> AnimationList;
41 typedef AnimationList::iterator AnimationListIter;
42 typedef AnimationList::const_iterator AnimationListConstIter;
43
44
45 /**
46  * Example information
47  *
48  * Represents a single Example.
49  */
50 struct Example
51 {
52   // Constructors
53
54   /**
55    * @param[in] name unique name of example
56    * @param[in] title The caption for the example to appear on a tile button.
57    */
58   Example(std::string name, std::string title)
59   : name(name),
60     title(title)
61   {
62   }
63
64   Example()
65   {
66   }
67
68   // Data
69
70   std::string name;                       ///< unique name of example
71   std::string title;                      ///< title (caption) of example to appear on tile button.
72 };
73
74 /**
75  * Provide a style for the view and its tool bar.
76  */
77 struct ViewStyle
78 {
79   ViewStyle( float toolBarButtonPercentage, float toolBarTitlePercentage, float dpi, float toolBarHeight, float toolBarPadding )
80   : mToolBarButtonPercentage( toolBarButtonPercentage ),
81     mToolBarTitlePercentage( toolBarTitlePercentage ),
82     mDpi( dpi ),
83     mToolBarHeight( toolBarHeight ),
84     mToolBarPadding( toolBarPadding )
85   {}
86
87   float mToolBarButtonPercentage; ///< The tool bar button width is a percentage of the tool bar width.
88   float mToolBarTitlePercentage;  ///< The tool bar title width is a percentage of the tool bar width.
89   float mDpi;                     ///< This style is indented for the given dpi.
90   float mToolBarHeight;           ///< The tool bar height for the given dpi above.
91   float mToolBarPadding;          ///< The tool bar padding between controls for the given dpi above.
92 };
93
94 const ViewStyle DEFAULT_VIEW_STYLE( 0.1f, 0.7f, 315.f, 80.f, 4.f );
95
96 /**
97  * Dali-Demo instance
98  */
99 class DaliTableView : public Dali::ConnectionTracker
100 {
101 public:
102
103   DaliTableView(Dali::Application& application);
104   ~DaliTableView();
105
106 public:
107
108   /**
109    * Adds an Example to our demo showcase
110    *
111    * @param[in] example The Example description.
112    *
113    * @note Should be called before the Application MainLoop is started.
114    */
115   void AddExample(Example example);
116
117   /**
118    * Sets the background image PATH.
119    *
120    * @param[in] imagePath The file path to the image to use as the background.
121    *
122    * @note Should be called before the Application MainLoop is started.
123    */
124   void SetBackgroundPath( std::string imagePath );
125
126   /**
127    * Sorts the example list alphabetically by Title if parameter is true.
128    *
129    * @param[in] sortAlphabetically If true, example list is sorted alphabetically.
130    *
131    * @note Should be called before the Application MainLoop is started.
132    * @note By default the examples are NOT sorted alphabetically by Title.
133    */
134   void SortAlphabetically( bool sortAlphabetically );
135
136 private: // Application callbacks & implementation
137
138   /**
139    * Shape enum for create function
140    */
141   enum ShapeType
142   {
143     CIRCLE,
144     SQUARE
145   };
146
147   /**
148    * Initialize application.
149    *
150    * @param[in] app Application instance
151    */
152   void Initialize( Dali::Application& app );
153
154   /**
155    * Populates the contents (ScrollView) with all the
156    * Examples that have been Added using the AddExample(...)
157    * call
158    */
159   void Populate();
160
161   /**
162    * Rotate callback from the device.
163    *
164    * @param[in] orientation that device notified.
165    */
166   void OrientationChanged( Dali::Orientation orientation );
167
168   /**
169    * Rotates RootActor orientation to that specified.
170    *
171    * @param[in] degrees The requested angle.
172    */
173   void Rotate( unsigned int degrees );
174
175   /**
176    * Creates a tile for the main menu and toolbar.
177    *
178    * @param[in] name The unique name for this Tile
179    * @param[in] title The text caption that appears on the Tile
180    * @param[in] parentSize Tile's parent size.
181    * @param[in] addBackground Whether to add a background graphic to the tile or not
182    *
183    * @return The Actor for the created tile.
184    */
185   Dali::Actor CreateTile( const std::string& name, const std::string& title, const Dali::Size& parentSize, bool addBackground );
186
187   /**
188    * Create a stencil image
189    *
190    * @return The stencil image
191    */
192   Dali::ImageActor NewStencilImage();
193
194   // Signal handlers
195
196   /**
197    * Signal emitted when any tile has been pressed
198    *
199    * @param[in] actor The Actor representing this tile.
200    * @param[in] event The TouchEvent
201    *
202    * @return Consume flag
203    */
204   bool OnTilePressed( Dali::Actor actor, const Dali::TouchEvent& event );
205
206   /**
207    * Signal emitted when the pressed animation has completed.
208    *
209    * @param[in] source The animation source.
210    */
211   void OnPressedAnimationFinished(Dali::Animation& source);
212
213   /**
214    * Signal emitted when the button has been clicked
215    *
216    * @param[in] button The Button that is clicked.
217    *
218    * @return Consume flag
219    */
220   bool OnButtonClicked( Dali::Toolkit::Button& button );
221
222   /**
223    * Signal emitted when scrolling has started.
224    *
225    * @param[in] position The current position of the scroll contents.
226    */
227   void OnScrollStart(const Dali::Vector3& position);
228
229   /**
230    * Signal emitted when scrolling has completed.
231    *
232    * @param[in] position The current position of the scroll contents.
233    */
234   void OnScrollComplete(const Dali::Vector3& position);
235
236   /**
237    * Signal emitted when any Sensitive Actor has been touched
238    * (other than those touches consumed by OnTilePressed)
239    *
240    * @param[in] actor The Actor touched.
241    * @param[in] event The TouchEvent
242    *
243    * @return Consume flag
244    */
245   bool OnScrollTouched( Dali::Actor actor, const Dali::TouchEvent& event );
246
247   /**
248    * Setup the effect on the scroll view
249    */
250   void ApplyScrollViewEffect();
251
252   /**
253    * Setup the inner cube effect
254    */
255   void SetupInnerPageCubeEffect();
256
257   /**
258    * Apply the scroll view effect to a page
259    */
260   void ApplyEffectToPage(Dali::Actor page, const Dali::Vector3& tableRelativeSize);
261
262   /**
263    * Apply a custom effect scroll view effect to a page
264    */
265   void ApplyCustomEffectToPage(Dali::Actor page);
266
267   /**
268    * Apply a shader effect to a table tile
269    */
270   void ApplyEffectToTile(Dali::Actor tile);
271
272   /**
273    * Apply effect to the content of a tile
274    */
275   void ApplyEffectToTileContent(Dali::Actor tileContent);
276
277   /**
278    * Create a toolbar
279    */
280   void CreateToolbar(Dali::Toolkit::TableView root, const std::string& title, const std::string& toolbarImagePath, const ViewStyle& style = DEFAULT_VIEW_STYLE);
281
282   /**
283    * Key event handler
284    */
285   void OnKeyEvent( const Dali::KeyEvent& event );
286
287   /**
288    * Create a depth field background
289    *
290    * @param[in] addToLayer Add the graphics to this layer
291    */
292   void SetupBackground( Dali::Actor addToLayer, const Dali::Vector2& size );
293
294   /**
295    * Create background actors for the given layer
296    *
297    * @param[in] layer The layer to add the actors to
298    * @param[in] count The number of actors to generate
299    * @param[in] distanceField The distance field bitmap to use
300    * @param[in] size The size of the actor
301    */
302   void AddBackgroundActors( Dali::Actor layer, int count, Dali::BitmapImage distanceField, const Dali::Vector2& size );
303
304   /**
305    * Create a bitmap with the specified shape and also output a distance field
306    *
307    * @param[in] shapeType The shape to generate
308    * @param[in] size The size of the bitmap to create
309    * @param[out] imageOut The return bitmap
310    * @param[out] distanceFieldOut The return depth field alpha map
311    */
312   void CreateShapeImage( ShapeType shapeType, const Dali::Size& size, Dali::BitmapImage& distanceFieldOut );
313
314   /**
315    * Generate a square bit pattern and depth field
316    *
317    * @param[in] size The size of the bitmap to create
318    * @param[out] imageOut The return bitmap
319    * @param[out] distanceFieldOut The return depth field alpha map
320    */
321   void GenerateSquare( const Dali::Size& size, std::vector<unsigned char>& distanceFieldOut );
322
323   /**
324    * Generate a circle bit pattern and depth field
325    *
326    * @param[in] size The size of the bitmap to create
327    * @param[out] imageOut The return bitmap
328    * @param[out] distanceFieldOut The return depth field alpha map
329    */
330   void GenerateCircle( const Dali::Size& size, std::vector<unsigned char>& distanceFieldOut );
331
332   /**
333    * Creates the logo.
334    *
335    * @param[in] imagePath The path to the image file to load
336    *
337    * @return The created image actor
338    */
339   Dali::ImageActor CreateLogo( std::string imagePath );
340
341   /**
342    * Callback for when the logo image is loaded
343    *
344    * @param[in] image The loaded logo image
345    */
346   void OnLogoLoaded( Dali::Image image );
347
348   /**
349    * Timer handler for ending background animation
350    *
351    * @return Return value for timer handler
352    */
353   bool PauseBackgroundAnimation();
354
355   /**
356    * Pause all animations
357    */
358   void PauseAnimation();
359
360   /**
361    * Resume all animations
362    */
363   void PlayAnimation();
364
365   /**
366    * Callback when the keyboard focus is going to be changed.
367    *
368    * @param[in] current The current focused actor
369    * @param[in] proposed The actor proposed by the keyboard focus manager to move the focus to
370    * @param[in] direction The direction to move the focus
371    * @return The actor to move the keyboard focus to.
372    */
373   Dali::Actor OnKeyboardPreFocusChange( Dali::Actor current, Dali::Actor proposed, Dali::Toolkit::Control::KeyboardFocusNavigationDirection direction );
374
375   /**
376    * Callback when the keyboard focused actor is activated.
377    *
378    * @param[in] activatedActor The activated actor
379    */
380   void OnFocusedActorActivated( Dali::Actor activatedActor );
381
382 private:
383
384   Dali::Application&              mApplication;         ///< Application instance.
385   Dali::Layer                     mBackgroundLayer;     ///< Background resides on a separate layer.
386   Dali::Toolkit::TableView        mRootActor;           ///< All content (excluding background is anchored to this Actor)
387   Dali::Animation                 mRotateAnimation;     ///< Animation to rotate and resize mRootActor.
388   Dali::ImageActor                mBackground;          ///< Background's static image.
389   Dali::ImageActor                mLogo;                ///< Logo's static image.
390   Dali::Animation                 mPressedAnimation;    ///< Button press scaling animation.
391   Dali::Layer                     mScrollViewLayer;     ///< ScrollView resides on a separate layer.
392   Dali::Toolkit::ScrollView       mScrollView;          ///< ScrollView container (for all Examples)
393   Dali::Toolkit::ScrollViewEffect mScrollViewEffect;    ///< Effect to be applied to the scroll view
394   bool                            mScrolling;           ///< Flag indicating whether view is currently being scrolled
395   Dali::Toolkit::RulerPtr         mScrollRulerX;        ///< ScrollView X (horizontal) ruler
396   Dali::Toolkit::RulerPtr         mScrollRulerY;        ///< ScrollView Y (vertical) ruler
397   Dali::Toolkit::TableView        mButtons;             ///< Navigation buttons
398   ExampleList                     mExampleList;         ///< List of examples.
399   ExampleMap                      mExampleMap;          ///< Map LUT for examples.
400   TableViewList                   mTableViewList;       ///< List of tableviews
401   Dali::Actor                     mPressedActor;        ///< The currently pressed actor.
402   int                             mTotalPages;          ///< Total pages within scrollview.
403   std::string                     mBackgroundImagePath; ///< The path to the background image.
404   bool                            mSortAlphabetically;  ///< Sort examples alphabetically.
405
406   Dali::ActorContainer            mTableViewImages;     ///< Offscreen render of tableview
407   Dali::ActorContainer            mBackgroundActors;    ///< List of background actors used in the effect
408
409   AnimationList                   mBackgroundAnimations;///< List of background bubble animations
410   Dali::Timer                     mAnimationTimer;      ///< Timer used to turn off animation after a specific time period
411   bool                            mBackgroundAnimsPlaying; ///< Are background animations playing
412 };
413
414 #endif // __DALI_DEMO_H__