2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "shared/view.h"
19 #include <dali/dali.h>
20 #include <dali-toolkit/dali-toolkit.h>
24 // Define this so that it is interchangeable
25 // "DP" stands for Device independent Pixels
32 const char* const BACKGROUND_IMAGE = DALI_IMAGE_DIR "background-gradient.jpg";
33 const char* const TOOLBAR_IMAGE = DALI_IMAGE_DIR "top-bar.png";
35 const char* const TOOLBAR_TITLE = "Buttons";
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";
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";
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";
46 const char* const ENABLED_IMAGE = DALI_IMAGE_DIR "item-select-check.png";
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";
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";
55 const Vector4 BACKGROUND_COLOUR( 1.0f, 1.0f, 1.0f, 0.15f );
58 const int RADIO_LABEL_THUMBNAIL_SIZE = 60;
59 const int RADIO_IMAGE_SPACING = 8;
60 const int BUTTON_HEIGHT = 48;
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;
71 /** This example shows how to create and use different buttons.
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
79 class ButtonsController: public ConnectionTracker
83 ButtonsController( Application& application )
84 : mApplication( application )
86 // Connect to the Application's Init signal
87 mApplication.InitSignal().Connect( this, &ButtonsController::Create );
95 void Create( Application& application )
97 // The Init signal is received once (only) during the Application lifetime
99 DemoHelper::RequestThemeChange();
101 // Respond to key events
102 Stage::GetCurrent().KeyEventSignal().Connect(this, &ButtonsController::OnKeyEvent);
104 // Creates a default view with a default tool bar.
105 // The view is added to the stage.
106 mContentLayer = DemoHelper::CreateView( application,
113 Toolkit::TableView contentTable = Toolkit::TableView::New( 4, 1 );
114 contentTable.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
115 contentTable.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
116 contentTable.SetAnchorPoint( AnchorPoint::TOP_LEFT );
117 contentTable.SetParentOrigin( ParentOrigin::TOP_LEFT );
118 contentTable.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5f ) );
119 // contentTable.TouchedSignal().Connect( this, &ButtonsController::OnTouchEvent );
121 for( unsigned int i = 0; i < contentTable.GetRows(); ++i )
123 contentTable.SetFitHeight( i );
126 contentTable.SetPosition( 0.0f, TOP_MARGIN );
128 mContentLayer.Add( contentTable );
130 // Image selector radio group
131 Toolkit::TableView radioGroup2Background = Toolkit::TableView::New( 2, 2 );
132 radioGroup2Background.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
133 radioGroup2Background.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
134 radioGroup2Background.SetBackgroundColor( BACKGROUND_COLOUR );
135 radioGroup2Background.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE ) );
136 radioGroup2Background.SetFitHeight( 0 );
137 radioGroup2Background.SetFitHeight( 1 );
138 radioGroup2Background.SetFitWidth( 0 );
140 contentTable.Add( radioGroup2Background );
142 Toolkit::TableView radioButtonsGroup2 = Toolkit::TableView::New( 3, 1 );
143 radioButtonsGroup2.SetCellPadding( Size( 0.0f, MARGIN_SIZE * 0.5f ) );
144 radioButtonsGroup2.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
145 for( unsigned int i = 0; i < radioButtonsGroup2.GetRows(); ++i )
147 radioButtonsGroup2.SetFitHeight( i );
149 radioButtonsGroup2.SetFitWidth( 0 );
151 radioGroup2Background.AddChild( radioButtonsGroup2, Toolkit::TableView::CellPosition( 0, 0 ) );
157 ImageActor imageActor = ImageActor::New( ResourceImage::New( SMALL_IMAGE_1 ) );
158 imageActor.SetSize( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) );
159 mRadioButtonImage1 = Dali::Toolkit::RadioButton::New( imageActor );
160 mRadioButtonImage1.SetParentOrigin( ParentOrigin::TOP_LEFT );
161 mRadioButtonImage1.SetAnchorPoint( AnchorPoint::TOP_LEFT );
162 mRadioButtonImage1.SetPosition( 0, DP(radioY) );
163 mRadioButtonImage1.SetSelected( true );
165 radioButtonsGroup2.Add( mRadioButtonImage1 );
170 radioY += RADIO_LABEL_THUMBNAIL_SIZE + RADIO_IMAGE_SPACING;
172 ImageActor imageActor = ImageActor::New( ResourceImage::New( SMALL_IMAGE_2 ) );
173 imageActor.SetSize( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) );
175 mRadioButtonImage2 = Dali::Toolkit::RadioButton::New( imageActor );
176 mRadioButtonImage2.SetParentOrigin( ParentOrigin::TOP_LEFT );
177 mRadioButtonImage2.SetAnchorPoint( AnchorPoint::TOP_LEFT );
178 mRadioButtonImage2.SetPosition( 0, DP(radioY) );
180 radioButtonsGroup2.Add( mRadioButtonImage2 );
185 radioY += RADIO_LABEL_THUMBNAIL_SIZE + RADIO_IMAGE_SPACING;
187 ImageActor imageActor = ImageActor::New( ResourceImage::New( SMALL_IMAGE_3 ) );
188 imageActor.SetSize( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) );
190 mRadioButtonImage3 = Dali::Toolkit::RadioButton::New( imageActor );
191 mRadioButtonImage3.SetParentOrigin( ParentOrigin::TOP_LEFT );
192 mRadioButtonImage3.SetAnchorPoint( AnchorPoint::TOP_LEFT );
193 mRadioButtonImage3.SetPosition( 0, DP(radioY) );
195 radioButtonsGroup2.Add( mRadioButtonImage3 );
198 // Create select button
199 mUpdateButton = Toolkit::PushButton::New();
200 mUpdateButton.SetLabel( "Select" );
201 mUpdateButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
203 mUpdateButton.SetSelectedImage( Dali::ResourceImage::New( PUSHBUTTON_PRESS_IMAGE ) );
204 mUpdateButton.SetDisabledImage( Dali::ResourceImage::New( PUSHBUTTON_DISABLED_IMAGE ) );
205 mUpdateButton.SetButtonImage( Dali::ResourceImage::New( PUSHBUTTON_BUTTON_IMAGE ) );
207 mUpdateButton.ClickedSignal().Connect( this, &ButtonsController::OnButtonClicked );
209 radioGroup2Background.AddChild( mUpdateButton, Toolkit::TableView::CellPosition( 1, 0 ) );
211 // ImageActor to display selected image
212 mBigImage1 = ResourceImage::New( BIG_IMAGE_1 );
213 mBigImage2 = ResourceImage::New( BIG_IMAGE_2 );
214 mBigImage3 = ResourceImage::New( BIG_IMAGE_3 );
216 mImage = ImageActor::New( mBigImage1 );
217 mImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
218 mImage.SetSizeScalePolicy( SizeScalePolicy::FIT_WITH_ASPECT_RATIO );
219 radioGroup2Background.AddChild( mImage, Toolkit::TableView::CellPosition( 0, 1, 2, 1 ) );
221 // The enable/disable radio group
222 Toolkit::TableView radioGroup1Background = Toolkit::TableView::New( 1, 1 );
223 radioGroup1Background.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
224 radioGroup1Background.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
225 radioGroup1Background.SetBackgroundColor( BACKGROUND_COLOUR );
226 radioGroup1Background.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE ) );
227 radioGroup1Background.SetFitHeight( 0 );
229 contentTable.Add( radioGroup1Background );
232 Toolkit::TableView radioButtonsGroup1 = Toolkit::TableView::New( 2, 1 );
233 radioButtonsGroup1.SetCellPadding( Size( 0.0f, MARGIN_SIZE * 0.5f ) );
234 radioButtonsGroup1.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
235 for( unsigned int i = 0; i < radioButtonsGroup1.GetRows(); ++i )
237 radioButtonsGroup1.SetFitHeight( i );
239 radioButtonsGroup1.SetFitWidth( 0 );
241 radioGroup1Background.Add( radioButtonsGroup1 );
243 // First radio button
245 Toolkit::TableView tableView = Toolkit::TableView::New( 1, 2 );
246 tableView.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
247 tableView.SetFitHeight( 0 );
248 tableView.SetFitWidth( 0 );
249 tableView.SetFitWidth( 1 );
251 Toolkit::TextLabel textLabel = Toolkit::TextLabel::New( "Select enabled" );
252 textLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::WIDTH );
253 textLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
254 textLabel.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
255 tableView.AddChild( textLabel, Toolkit::TableView::CellPosition( 0, 0 ) );
257 ImageActor imageActor = ImageActor::New( ResourceImage::New( ENABLED_IMAGE ) );
258 imageActor.SetSize( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) );
259 imageActor.SetPadding( Padding( DP(20.0f), 0.0f, 0.0f, 0.0f ) );
260 tableView.AddChild( imageActor, Toolkit::TableView::CellPosition( 0, 1 ) );
262 Toolkit::RadioButton radioButton = Dali::Toolkit::RadioButton::New( tableView );
263 radioButton.SetName( "radio-select-enable" );
264 radioButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
265 radioButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
266 radioButton.SetPosition( 0, 0 );
267 radioButton.SetSelected( true );
269 radioButton.StateChangedSignal().Connect( this, &ButtonsController::EnableSelectButton );
271 radioButtonsGroup1.Add( radioButton );
274 // Second radio button
276 Toolkit::RadioButton radioButton = Dali::Toolkit::RadioButton::New( "Select disabled" );
277 radioButton.SetName( "radio-select-disable" );
278 radioButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
279 radioButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
280 radioButton.SetPosition( 0, DP(50) );
282 radioButton.StateChangedSignal().Connect( this, &ButtonsController::EnableSelectButton );
284 radioButtonsGroup1.Add( radioButton );
288 Toolkit::TableView checkBoxBackground = Toolkit::TableView::New( 3, 1 );
289 checkBoxBackground.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
290 checkBoxBackground.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
291 checkBoxBackground.SetBackgroundColor( BACKGROUND_COLOUR );
292 checkBoxBackground.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE ) );
294 for( unsigned int i = 0; i < checkBoxBackground.GetRows(); ++i )
296 checkBoxBackground.SetFitHeight( i );
299 contentTable.Add( checkBoxBackground );
301 Dali::Image unselected = Dali::ResourceImage::New( CHECKBOX_UNSELECTED_IMAGE );
302 Dali::Image selected = Dali::ResourceImage::New( CHECKBOX_SELECTED_IMAGE );
305 Toolkit::CheckBoxButton checkBox = Toolkit::CheckBoxButton::New();
306 checkBox.SetName( "checkbox1" );
307 checkBox.SetBackgroundImage( unselected );
308 checkBox.SetSelectedImage( selected );
309 checkBox.SetLabel( "CheckBox1 is unselected" );
310 checkBox.StateChangedSignal().Connect( this, &ButtonsController::OnCheckBoxesSelected );
312 checkBoxBackground.Add( checkBox );
316 Toolkit::CheckBoxButton checkBox = Toolkit::CheckBoxButton::New();
317 checkBox.SetName( "checkbox2" );
318 checkBox.SetBackgroundImage( unselected );
319 checkBox.SetSelectedImage( selected );
320 checkBox.SetLabel( "CheckBox2 is selected" );
321 checkBox.SetSelected( true );
322 checkBox.StateChangedSignal().Connect( this, &ButtonsController::OnCheckBoxesSelected );
324 checkBoxBackground.Add( checkBox );
328 Toolkit::CheckBoxButton checkBox = Toolkit::CheckBoxButton::New();
329 checkBox.SetName( "checkbox3" );
330 checkBox.SetBackgroundImage( unselected );
331 checkBox.SetSelectedImage( selected );
332 checkBox.SetLabel( "CheckBox3 is unselected" );
333 checkBox.StateChangedSignal().Connect( this, &ButtonsController::OnCheckBoxesSelected );
335 checkBoxBackground.Add( checkBox );
338 // Create togglabe button
339 Toolkit::TableView toggleBackground = Toolkit::TableView::New( 3, 1 );
340 toggleBackground.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
341 toggleBackground.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
342 toggleBackground.SetBackgroundColor( BACKGROUND_COLOUR );
343 toggleBackground.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE ) );
345 for( unsigned int i = 0; i < toggleBackground.GetRows(); ++i )
347 toggleBackground.SetFitHeight( i );
350 contentTable.Add( toggleBackground );
352 Toolkit::PushButton toggleButton = Toolkit::PushButton::New();
353 toggleButton.SetTogglableButton( true );
354 toggleButton.SetLabel( "Unselected" );
355 toggleButton.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
356 toggleButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
358 toggleButton.SetSelectedImage( Dali::ResourceImage::New( PUSHBUTTON_PRESS_IMAGE ) );
359 toggleButton.SetDisabledImage( Dali::ResourceImage::New( PUSHBUTTON_DISABLED_IMAGE ) );
360 toggleButton.SetButtonImage( Dali::ResourceImage::New( PUSHBUTTON_BUTTON_IMAGE ) );
362 toggleButton.StateChangedSignal().Connect( this, &ButtonsController::OnButtonSelected );
364 toggleBackground.Add( toggleButton );
367 void OnKeyEvent( const KeyEvent& event )
369 if( event.state == KeyEvent::Down )
371 if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
373 // Exit application when click back or escape.
379 bool OnButtonSelected( Toolkit::Button button )
381 Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( button );
384 if( button.IsSelected() )
386 pushButton.SetLabel( "Selected" );
390 pushButton.SetLabel( "Unselected" );
397 bool EnableSelectButton( Toolkit::Button button )
399 if( button.GetName() == "radio-select-enable" && button.IsSelected() == true )
401 mUpdateButton.SetDisabled( false );
403 else if( button.GetName() == "radio-select-disable" && button.IsSelected() == true )
405 mUpdateButton.SetDisabled( true );
411 bool OnButtonClicked(Toolkit::Button button)
413 if( mRadioButtonImage1.IsSelected() )
415 mImage.SetImage( mBigImage1 );
417 else if( mRadioButtonImage2.IsSelected() )
419 mImage.SetImage( mBigImage2 );
421 else if( mRadioButtonImage3.IsSelected() )
423 mImage.SetImage( mBigImage3 );
428 bool OnCheckBoxesSelected( Toolkit::Button button )
430 if( button.GetName() == "checkbox1" )
432 if( button.IsSelected() )
434 button.SetLabel("CheckBox1 is selected");
438 button.SetLabel("CheckBox1 is unselected");
442 if( button.GetName() == "checkbox2" )
444 if( button.IsSelected() )
446 button.SetLabel("CheckBox2 is selected");
450 button.SetLabel("CheckBox2 is unselected");
454 if( button.GetName() == "checkbox3" )
456 if( button.IsSelected() )
458 button.SetLabel("CheckBox3 is selected");
462 button.SetLabel("CheckBox3 is unselected");
469 bool OnTouchEvent( Actor actor, const TouchEvent& event )
471 if( 1u == event.GetPointCount() )
473 const TouchPoint::State state = event.GetPoint(0u).state;
475 // Clamp to integer values; this is to reduce flicking due to pixel misalignment
476 const float localPoint = static_cast<float>( static_cast<int>( event.GetPoint( 0 ).local.y ) );
478 if( TouchPoint::Down == state )
480 mLastPoint = localPoint;
481 mAnimation = Animation::New( 0.25f );
483 else if( TouchPoint::Motion == state )
487 mAnimation.AnimateBy( Property(actor, Actor::Property::POSITION), Vector3( 0.f, localPoint - mLastPoint, 0.f ), AlphaFunction::LINEAR );
489 mLastPoint = localPoint;
499 Application& mApplication;
500 Toolkit::Control mView; ///< The View instance.
501 Toolkit::ToolBar mToolBar; ///< The View's Toolbar.
502 Layer mContentLayer; ///< Content layer
504 Toolkit::RadioButton mRadioButtonImage1;
505 Toolkit::RadioButton mRadioButtonImage2;
506 Toolkit::RadioButton mRadioButtonImage3;
508 Toolkit::PushButton mUpdateButton;
510 Animation mAnimation;
519 void RunTest( Application& application )
521 ButtonsController test( application );
523 application.MainLoop();
526 // Entry point for Linux & Tizen applications
528 int main( int argc, char **argv )
530 Application application = Application::New( &argc, &argv );
532 RunTest( application );