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