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