Move more public-api headers to devel-api. PART 3
[platform/core/uifw/dali-demo.git] / examples / buttons / buttons-example.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 #include "shared/view.h"
19 #include <dali/dali.h>
20 #include <dali-toolkit/dali-toolkit.h>
21 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
22
23 using namespace Dali;
24
25 // Define this so that it is interchangeable
26 // "DP" stands for Device independent Pixels
27 #define DP(x) x
28
29
30 namespace
31 {
32
33 const char* const BACKGROUND_IMAGE = DALI_IMAGE_DIR "background-gradient.jpg";
34 const char* const TOOLBAR_IMAGE = DALI_IMAGE_DIR "top-bar.png";
35
36 const char* const TOOLBAR_TITLE = "Buttons";
37
38 const char* const SMALL_IMAGE_1 = DALI_IMAGE_DIR "gallery-small-14.jpg";
39 const char* const BIG_IMAGE_1 = DALI_IMAGE_DIR "gallery-large-4.jpg";
40
41 const char* const SMALL_IMAGE_2 = DALI_IMAGE_DIR "gallery-small-20.jpg";
42 const char* const BIG_IMAGE_2 = DALI_IMAGE_DIR "gallery-large-11.jpg";
43
44 const char* const SMALL_IMAGE_3 = DALI_IMAGE_DIR "gallery-small-25.jpg";
45 const char* const BIG_IMAGE_3 = DALI_IMAGE_DIR "gallery-large-13.jpg";
46
47 const char* const ENABLED_IMAGE = DALI_IMAGE_DIR "item-select-check.png";
48
49 const char* const PUSHBUTTON_PRESS_IMAGE = DALI_IMAGE_DIR "button-down.9.png";
50 const char* const PUSHBUTTON_DISABLED_IMAGE = DALI_IMAGE_DIR "button-disabled.9.png";
51 const char* const PUSHBUTTON_BUTTON_IMAGE = DALI_IMAGE_DIR "button-up.9.png";
52
53 const char* const CHECKBOX_UNSELECTED_IMAGE = DALI_IMAGE_DIR "checkbox-unselected.png";
54 const char* const CHECKBOX_SELECTED_IMAGE = DALI_IMAGE_DIR "checkbox-selected.png";
55
56 const Vector4 BACKGROUND_COLOUR( 1.0f, 1.0f, 1.0f, 0.15f );
57
58 // Layout sizes
59 const int RADIO_LABEL_THUMBNAIL_SIZE = 60;
60 const int RADIO_IMAGE_SPACING = 8;
61 const int BUTTON_HEIGHT = 48;
62
63 const int MARGIN_SIZE = 10;
64 const int TOP_MARGIN = 85;
65 const int GROUP2_HEIGHT = 238;
66 const int GROUP1_HEIGHT = 120;
67 const int GROUP3_HEIGHT = 190;
68 const int GROUP4_HEIGHT = BUTTON_HEIGHT + MARGIN_SIZE * 2;
69
70 }  // namespace
71
72 /** This example shows how to create and use different buttons.
73  *
74  * 1. First group of radio buttons with image actor labels selects an image to load
75  * 2. A push button loads the selected thumbnail image into the larger image pane
76  * 3. Second group of radio buttons with a table view label containing a text view and image view, and a normal text view.
77  *    Selecting one of these will enable/disable the image loading push button
78  * 4. A group of check boxes
79  */
80 class ButtonsController: public ConnectionTracker
81 {
82  public:
83
84   ButtonsController( Application& application )
85     : mApplication( application )
86   {
87     // Connect to the Application's Init signal
88     mApplication.InitSignal().Connect( this, &ButtonsController::Create );
89   }
90
91   ~ButtonsController()
92   {
93     // Nothing to do here
94   }
95
96   void Create( Application& application )
97   {
98     // The Init signal is received once (only) during the Application lifetime
99
100     DemoHelper::RequestThemeChange();
101
102     // Respond to key events
103     Stage::GetCurrent().KeyEventSignal().Connect(this, &ButtonsController::OnKeyEvent);
104
105     // Creates a default view with a default tool bar.
106     // The view is added to the stage.
107     mContentLayer = DemoHelper::CreateView( application,
108                                             mView,
109                                             mToolBar,
110                                             BACKGROUND_IMAGE,
111                                             TOOLBAR_IMAGE,
112                                             TOOLBAR_TITLE );
113
114     Toolkit::TableView contentTable = Toolkit::TableView::New( 4, 1 );
115     contentTable.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
116     contentTable.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
117     contentTable.SetAnchorPoint( AnchorPoint::TOP_LEFT );
118     contentTable.SetParentOrigin( ParentOrigin::TOP_LEFT );
119     contentTable.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5f ) );
120 //    contentTable.TouchedSignal().Connect( this, &ButtonsController::OnTouchEvent );
121
122     for( unsigned int i = 0; i < contentTable.GetRows(); ++i )
123     {
124       contentTable.SetFitHeight( i );
125     }
126
127     contentTable.SetPosition( 0.0f, TOP_MARGIN );
128
129     mContentLayer.Add( contentTable );
130
131     // Image selector radio group
132     Toolkit::TableView radioGroup2Background = Toolkit::TableView::New( 2, 2 );
133     radioGroup2Background.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
134     radioGroup2Background.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
135     radioGroup2Background.SetBackgroundColor( BACKGROUND_COLOUR );
136     radioGroup2Background.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE ) );
137     radioGroup2Background.SetFitHeight( 0 );
138     radioGroup2Background.SetFitHeight( 1 );
139     radioGroup2Background.SetFitWidth( 0 );
140
141     contentTable.Add( radioGroup2Background );
142
143     Toolkit::TableView radioButtonsGroup2 = Toolkit::TableView::New( 3, 1 );
144     radioButtonsGroup2.SetCellPadding( Size( 0.0f, MARGIN_SIZE * 0.5f ) );
145     radioButtonsGroup2.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
146     for( unsigned int i = 0; i < radioButtonsGroup2.GetRows(); ++i )
147     {
148       radioButtonsGroup2.SetFitHeight( i );
149     }
150     radioButtonsGroup2.SetFitWidth( 0 );
151
152     radioGroup2Background.AddChild( radioButtonsGroup2, Toolkit::TableView::CellPosition( 0, 0 ) );
153
154     int radioY = 0;
155
156     // Radio 1
157     {
158       ImageActor imageActor = ImageActor::New( ResourceImage::New( SMALL_IMAGE_1 ) );
159       imageActor.SetSize( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) );
160       mRadioButtonImage1 = Dali::Toolkit::RadioButton::New( imageActor );
161       mRadioButtonImage1.SetParentOrigin( ParentOrigin::TOP_LEFT );
162       mRadioButtonImage1.SetAnchorPoint( AnchorPoint::TOP_LEFT );
163       mRadioButtonImage1.SetPosition( 0, DP(radioY) );
164       mRadioButtonImage1.SetSelected( true );
165
166       radioButtonsGroup2.Add( mRadioButtonImage1 );
167     }
168
169     // Radio 2
170     {
171       radioY += RADIO_LABEL_THUMBNAIL_SIZE + RADIO_IMAGE_SPACING;
172
173       ImageActor imageActor = ImageActor::New( ResourceImage::New( SMALL_IMAGE_2 ) );
174       imageActor.SetSize( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) );
175
176       mRadioButtonImage2 = Dali::Toolkit::RadioButton::New( imageActor );
177       mRadioButtonImage2.SetParentOrigin( ParentOrigin::TOP_LEFT );
178       mRadioButtonImage2.SetAnchorPoint( AnchorPoint::TOP_LEFT );
179       mRadioButtonImage2.SetPosition( 0, DP(radioY) );
180
181       radioButtonsGroup2.Add( mRadioButtonImage2 );
182     }
183
184     // Radio 3
185     {
186       radioY += RADIO_LABEL_THUMBNAIL_SIZE + RADIO_IMAGE_SPACING;
187
188       ImageActor imageActor = ImageActor::New( ResourceImage::New( SMALL_IMAGE_3 ) );
189       imageActor.SetSize( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) );
190
191       mRadioButtonImage3 = Dali::Toolkit::RadioButton::New( imageActor );
192       mRadioButtonImage3.SetParentOrigin( ParentOrigin::TOP_LEFT );
193       mRadioButtonImage3.SetAnchorPoint( AnchorPoint::TOP_LEFT );
194       mRadioButtonImage3.SetPosition( 0, DP(radioY) );
195
196       radioButtonsGroup2.Add( mRadioButtonImage3 );
197     }
198
199     // Create select button
200     mUpdateButton = Toolkit::PushButton::New();
201     mUpdateButton.SetLabel( "Select" );
202     mUpdateButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
203
204     mUpdateButton.SetSelectedImage( Dali::ResourceImage::New( PUSHBUTTON_PRESS_IMAGE ) );
205     mUpdateButton.SetDisabledImage( Dali::ResourceImage::New( PUSHBUTTON_DISABLED_IMAGE ) );
206     mUpdateButton.SetButtonImage( Dali::ResourceImage::New( PUSHBUTTON_BUTTON_IMAGE ) );
207
208     mUpdateButton.ClickedSignal().Connect( this, &ButtonsController::OnButtonClicked );
209
210     radioGroup2Background.AddChild( mUpdateButton, Toolkit::TableView::CellPosition( 1, 0 ) );
211
212     // ImageActor to display selected image
213     mBigImage1 = ResourceImage::New( BIG_IMAGE_1 );
214     mBigImage2 = ResourceImage::New( BIG_IMAGE_2 );
215     mBigImage3 = ResourceImage::New( BIG_IMAGE_3 );
216
217     mImage = ImageActor::New( mBigImage1 );
218     mImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
219     mImage.SetSizeScalePolicy( SizeScalePolicy::FIT_WITH_ASPECT_RATIO );
220     radioGroup2Background.AddChild( mImage, Toolkit::TableView::CellPosition( 0, 1, 2, 1 ) );
221
222     // The enable/disable radio group
223     Toolkit::TableView radioGroup1Background = Toolkit::TableView::New( 1, 1 );
224     radioGroup1Background.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
225     radioGroup1Background.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
226     radioGroup1Background.SetBackgroundColor( BACKGROUND_COLOUR );
227     radioGroup1Background.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE ) );
228     radioGroup1Background.SetFitHeight( 0 );
229
230     contentTable.Add( radioGroup1Background );
231
232     // Radio group
233     Toolkit::TableView radioButtonsGroup1 = Toolkit::TableView::New( 2, 1 );
234     radioButtonsGroup1.SetCellPadding( Size( 0.0f, MARGIN_SIZE * 0.5f ) );
235     radioButtonsGroup1.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
236     for( unsigned int i = 0; i < radioButtonsGroup1.GetRows(); ++i )
237     {
238       radioButtonsGroup1.SetFitHeight( i );
239     }
240     radioButtonsGroup1.SetFitWidth( 0 );
241
242     radioGroup1Background.Add( radioButtonsGroup1 );
243
244     // First radio button
245     {
246       Toolkit::TableView tableView = Toolkit::TableView::New( 1, 2 );
247       tableView.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
248       tableView.SetFitHeight( 0 );
249       tableView.SetFitWidth( 0 );
250       tableView.SetFitWidth( 1 );
251
252       Toolkit::TextLabel textLabel = Toolkit::TextLabel::New( "Select enabled" );
253       textLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::WIDTH );
254       textLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
255       textLabel.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
256       tableView.AddChild( textLabel, Toolkit::TableView::CellPosition( 0, 0 ) );
257
258       ImageActor imageActor = ImageActor::New( ResourceImage::New( ENABLED_IMAGE ) );
259       imageActor.SetSize( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) );
260       imageActor.SetPadding( Padding( DP(20.0f), 0.0f, 0.0f, 0.0f ) );
261       tableView.AddChild( imageActor, Toolkit::TableView::CellPosition( 0, 1 ) );
262
263       Toolkit::RadioButton radioButton = Dali::Toolkit::RadioButton::New( tableView );
264       radioButton.SetName( "radio-select-enable" );
265       radioButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
266       radioButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
267       radioButton.SetPosition( 0, 0 );
268       radioButton.SetSelected( true );
269
270       radioButton.StateChangedSignal().Connect( this, &ButtonsController::EnableSelectButton );
271
272       radioButtonsGroup1.Add( radioButton );
273     }
274
275     // Second radio button
276     {
277       Toolkit::RadioButton radioButton = Dali::Toolkit::RadioButton::New( "Select disabled" );
278       radioButton.SetName( "radio-select-disable" );
279       radioButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
280       radioButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
281       radioButton.SetPosition( 0, DP(50) );
282
283       radioButton.StateChangedSignal().Connect( this, &ButtonsController::EnableSelectButton );
284
285       radioButtonsGroup1.Add( radioButton );
286     }
287
288     // CheckBoxes
289     Toolkit::TableView checkBoxBackground = Toolkit::TableView::New( 3, 1 );
290     checkBoxBackground.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
291     checkBoxBackground.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
292     checkBoxBackground.SetBackgroundColor( BACKGROUND_COLOUR );
293     checkBoxBackground.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE ) );
294
295     for( unsigned int i = 0; i < checkBoxBackground.GetRows(); ++i )
296     {
297       checkBoxBackground.SetFitHeight( i );
298     }
299
300     contentTable.Add( checkBoxBackground );
301
302     Dali::Image unselected = Dali::ResourceImage::New( CHECKBOX_UNSELECTED_IMAGE );
303     Dali::Image selected = Dali::ResourceImage::New( CHECKBOX_SELECTED_IMAGE );
304
305     {
306       Toolkit::CheckBoxButton checkBox = Toolkit::CheckBoxButton::New();
307       checkBox.SetName( "checkbox1" );
308       checkBox.SetBackgroundImage( unselected );
309       checkBox.SetSelectedImage( selected );
310       checkBox.SetLabel( "CheckBox1 is unselected" );
311       checkBox.StateChangedSignal().Connect( this, &ButtonsController::OnCheckBoxesSelected );
312
313       checkBoxBackground.Add( checkBox );
314     }
315
316     {
317       Toolkit::CheckBoxButton checkBox = Toolkit::CheckBoxButton::New();
318       checkBox.SetName( "checkbox2" );
319       checkBox.SetBackgroundImage( unselected );
320       checkBox.SetSelectedImage( selected );
321       checkBox.SetLabel( "CheckBox2 is selected" );
322       checkBox.SetSelected( true );
323       checkBox.StateChangedSignal().Connect( this, &ButtonsController::OnCheckBoxesSelected );
324
325       checkBoxBackground.Add( checkBox );
326     }
327
328     {
329       Toolkit::CheckBoxButton checkBox = Toolkit::CheckBoxButton::New();
330       checkBox.SetName( "checkbox3" );
331       checkBox.SetBackgroundImage( unselected );
332       checkBox.SetSelectedImage( selected );
333       checkBox.SetLabel( "CheckBox3 is unselected" );
334       checkBox.StateChangedSignal().Connect( this, &ButtonsController::OnCheckBoxesSelected );
335
336       checkBoxBackground.Add( checkBox );
337     }
338
339     // Create togglabe button
340     Toolkit::TableView toggleBackground = Toolkit::TableView::New( 3, 1 );
341     toggleBackground.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
342     toggleBackground.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
343     toggleBackground.SetBackgroundColor( BACKGROUND_COLOUR );
344     toggleBackground.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE ) );
345
346     for( unsigned int i = 0; i < toggleBackground.GetRows(); ++i )
347     {
348       toggleBackground.SetFitHeight( i );
349     }
350
351     contentTable.Add( toggleBackground );
352
353     Toolkit::PushButton toggleButton = Toolkit::PushButton::New();
354     toggleButton.SetTogglableButton( true );
355     toggleButton.SetLabel( "Unselected" );
356     toggleButton.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
357     toggleButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
358
359     toggleButton.SetSelectedImage( Dali::ResourceImage::New( PUSHBUTTON_PRESS_IMAGE ) );
360     toggleButton.SetDisabledImage( Dali::ResourceImage::New( PUSHBUTTON_DISABLED_IMAGE ) );
361     toggleButton.SetButtonImage( Dali::ResourceImage::New( PUSHBUTTON_BUTTON_IMAGE ) );
362
363     toggleButton.StateChangedSignal().Connect( this, &ButtonsController::OnButtonSelected );
364
365     toggleBackground.Add( toggleButton );
366   }
367
368   void OnKeyEvent( const KeyEvent& event )
369   {
370     if( event.state == KeyEvent::Down )
371     {
372       if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
373       {
374         // Exit application when click back or escape.
375         mApplication.Quit();
376       }
377     }
378   }
379
380   bool OnButtonSelected( Toolkit::Button button )
381   {
382     Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( button );
383     if( pushButton )
384     {
385       if( button.IsSelected() )
386       {
387         pushButton.SetLabel( "Selected" );
388       }
389       else
390       {
391         pushButton.SetLabel( "Unselected" );
392       }
393     }
394
395     return true;
396   }
397
398   bool EnableSelectButton( Toolkit::Button button )
399   {
400     if( button.GetName() == "radio-select-enable" && button.IsSelected() == true )
401     {
402       mUpdateButton.SetDisabled( false );
403     }
404     else if( button.GetName() == "radio-select-disable" && button.IsSelected() == true )
405     {
406       mUpdateButton.SetDisabled( true );
407     }
408
409     return true;
410   }
411
412   bool OnButtonClicked(Toolkit::Button button)
413   {
414     if( mRadioButtonImage1.IsSelected() )
415     {
416       mImage.SetImage( mBigImage1 );
417     }
418     else if( mRadioButtonImage2.IsSelected() )
419     {
420       mImage.SetImage( mBigImage2 );
421     }
422     else if( mRadioButtonImage3.IsSelected() )
423     {
424       mImage.SetImage( mBigImage3 );
425     }
426     return true;
427   }
428
429   bool OnCheckBoxesSelected( Toolkit::Button button )
430   {
431     if( button.GetName() == "checkbox1" )
432     {
433       if( button.IsSelected() )
434       {
435         button.SetLabel("CheckBox1 is selected");
436       }
437       else
438       {
439         button.SetLabel("CheckBox1 is unselected");
440       }
441     }
442
443     if( button.GetName() == "checkbox2" )
444     {
445       if( button.IsSelected() )
446       {
447         button.SetLabel("CheckBox2 is selected");
448       }
449       else
450       {
451         button.SetLabel("CheckBox2 is unselected");
452       }
453     }
454
455     if( button.GetName() == "checkbox3" )
456     {
457       if( button.IsSelected() )
458       {
459         button.SetLabel("CheckBox3 is selected");
460       }
461       else
462       {
463         button.SetLabel("CheckBox3 is unselected");
464       }
465     }
466
467     return true;
468   }
469
470   bool OnTouchEvent( Actor actor, const TouchEvent& event )
471   {
472     if( 1u == event.GetPointCount() )
473     {
474       const TouchPoint::State state = event.GetPoint(0u).state;
475
476       // Clamp to integer values; this is to reduce flicking due to pixel misalignment
477       const float localPoint = static_cast<float>( static_cast<int>( event.GetPoint( 0 ).local.y ) );
478
479       if( TouchPoint::Down == state )
480       {
481         mLastPoint = localPoint;
482         mAnimation = Animation::New( 0.25f );
483       }
484       else if( TouchPoint::Motion == state )
485       {
486         if( mAnimation )
487         {
488           mAnimation.AnimateBy( Property(actor, Actor::Property::POSITION), Vector3( 0.f, localPoint - mLastPoint, 0.f ), AlphaFunction::LINEAR );
489           mAnimation.Play();
490           mLastPoint = localPoint;
491         }
492       }
493     }
494
495     return true;
496   }
497
498  private:
499
500   Application&      mApplication;
501   Toolkit::Control  mView;                              ///< The View instance.
502   Toolkit::ToolBar  mToolBar;                           ///< The View's Toolbar.
503   Layer             mContentLayer;                      ///< Content layer
504
505   Toolkit::RadioButton mRadioButtonImage1;
506   Toolkit::RadioButton mRadioButtonImage2;
507   Toolkit::RadioButton mRadioButtonImage3;
508
509   Toolkit::PushButton mUpdateButton;
510
511   Animation      mAnimation;
512   float          mLastPoint;
513
514   Image mBigImage1;
515   Image mBigImage2;
516   Image mBigImage3;
517   ImageActor mImage;
518 };
519
520 void RunTest( Application& application )
521 {
522   ButtonsController test( application );
523
524   application.MainLoop();
525 }
526
527 // Entry point for Linux & Tizen applications
528 //
529 int main( int argc, char **argv )
530 {
531   Application application = Application::New( &argc, &argv );
532
533   RunTest( application );
534
535   return 0;
536 }