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 // Respond to key events
100 Stage::GetCurrent().KeyEventSignal().Connect(this, &ButtonsController::OnKeyEvent);
102 // Creates a default view with a default tool bar.
103 // The view is added to the stage.
104 mContentLayer = DemoHelper::CreateView( application,
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 );
119 for( unsigned int i = 0; i < contentTable.GetRows(); ++i )
121 contentTable.SetFitHeight( i );
124 contentTable.SetPosition( 0.0f, TOP_MARGIN );
126 mContentLayer.Add( contentTable );
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 );
138 contentTable.Add( radioGroup2Background );
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 )
145 radioButtonsGroup2.SetFitHeight( i );
147 radioButtonsGroup2.SetFitWidth( 0 );
149 radioGroup2Background.AddChild( radioButtonsGroup2, Toolkit::TableView::CellPosition( 0, 0 ) );
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 );
163 radioButtonsGroup2.Add( mRadioButtonImage1 );
168 radioY += RADIO_LABEL_THUMBNAIL_SIZE + RADIO_IMAGE_SPACING;
170 ImageActor imageActor = ImageActor::New( ResourceImage::New( SMALL_IMAGE_2 ) );
171 imageActor.SetSize( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) );
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) );
178 radioButtonsGroup2.Add( mRadioButtonImage2 );
183 radioY += RADIO_LABEL_THUMBNAIL_SIZE + RADIO_IMAGE_SPACING;
185 ImageActor imageActor = ImageActor::New( ResourceImage::New( SMALL_IMAGE_3 ) );
186 imageActor.SetSize( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) );
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) );
193 radioButtonsGroup2.Add( mRadioButtonImage3 );
196 // Create select button
197 mUpdateButton = Toolkit::PushButton::New();
198 mUpdateButton.SetLabel( "Select" );
199 mUpdateButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
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 ) );
205 mUpdateButton.ClickedSignal().Connect( this, &ButtonsController::OnButtonClicked );
207 radioGroup2Background.AddChild( mUpdateButton, Toolkit::TableView::CellPosition( 1, 0 ) );
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 );
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 ) );
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 );
227 contentTable.Add( radioGroup1Background );
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 )
235 radioButtonsGroup1.SetFitHeight( i );
237 radioButtonsGroup1.SetFitWidth( 0 );
239 radioGroup1Background.Add( radioButtonsGroup1 );
241 // First radio button
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 );
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 ) );
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 ) );
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 );
267 radioButton.StateChangedSignal().Connect( this, &ButtonsController::EnableSelectButton );
269 radioButtonsGroup1.Add( radioButton );
272 // Second radio button
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) );
280 radioButton.StateChangedSignal().Connect( this, &ButtonsController::EnableSelectButton );
282 radioButtonsGroup1.Add( radioButton );
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 ) );
292 for( unsigned int i = 0; i < checkBoxBackground.GetRows(); ++i )
294 checkBoxBackground.SetFitHeight( i );
297 contentTable.Add( checkBoxBackground );
299 Dali::Image unselected = Dali::ResourceImage::New( CHECKBOX_UNSELECTED_IMAGE );
300 Dali::Image selected = Dali::ResourceImage::New( CHECKBOX_SELECTED_IMAGE );
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 );
310 checkBoxBackground.Add( checkBox );
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 );
322 checkBoxBackground.Add( checkBox );
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 );
333 checkBoxBackground.Add( checkBox );
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 ) );
343 for( unsigned int i = 0; i < toggleBackground.GetRows(); ++i )
345 toggleBackground.SetFitHeight( i );
348 contentTable.Add( toggleBackground );
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 );
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 ) );
360 toggleButton.StateChangedSignal().Connect( this, &ButtonsController::OnButtonSelected );
362 toggleBackground.Add( toggleButton );
365 void OnKeyEvent( const KeyEvent& event )
367 if( event.state == KeyEvent::Down )
369 if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
371 // Exit application when click back or escape.
377 bool OnButtonSelected( Toolkit::Button button )
379 Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( button );
382 if( button.IsSelected() )
384 pushButton.SetLabel( "Selected" );
388 pushButton.SetLabel( "Unselected" );
395 bool EnableSelectButton( Toolkit::Button button )
397 if( button.GetName() == "radio-select-enable" && button.IsSelected() == true )
399 mUpdateButton.SetDisabled( false );
401 else if( button.GetName() == "radio-select-disable" && button.IsSelected() == true )
403 mUpdateButton.SetDisabled( true );
409 bool OnButtonClicked(Toolkit::Button button)
411 if( mRadioButtonImage1.IsSelected() )
413 mImage.SetImage( mBigImage1 );
415 else if( mRadioButtonImage2.IsSelected() )
417 mImage.SetImage( mBigImage2 );
419 else if( mRadioButtonImage3.IsSelected() )
421 mImage.SetImage( mBigImage3 );
426 bool OnCheckBoxesSelected( Toolkit::Button button )
428 if( button.GetName() == "checkbox1" )
430 if( button.IsSelected() )
432 button.SetLabel("CheckBox1 is selected");
436 button.SetLabel("CheckBox1 is unselected");
440 if( button.GetName() == "checkbox2" )
442 if( button.IsSelected() )
444 button.SetLabel("CheckBox2 is selected");
448 button.SetLabel("CheckBox2 is unselected");
452 if( button.GetName() == "checkbox3" )
454 if( button.IsSelected() )
456 button.SetLabel("CheckBox3 is selected");
460 button.SetLabel("CheckBox3 is unselected");
467 bool OnTouchEvent( Actor actor, const TouchEvent& event )
469 if( 1u == event.GetPointCount() )
471 const TouchPoint::State state = event.GetPoint(0u).state;
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 ) );
476 if( TouchPoint::Down == state )
478 mLastPoint = localPoint;
479 mAnimation = Animation::New( 0.25f );
481 else if( TouchPoint::Motion == state )
485 mAnimation.AnimateBy( Property(actor, Actor::Property::POSITION), Vector3( 0.f, localPoint - mLastPoint, 0.f ), AlphaFunction::LINEAR );
487 mLastPoint = localPoint;
497 Application& mApplication;
498 Toolkit::Control mView; ///< The View instance.
499 Toolkit::ToolBar mToolBar; ///< The View's Toolbar.
500 Layer mContentLayer; ///< Content layer
502 Toolkit::RadioButton mRadioButtonImage1;
503 Toolkit::RadioButton mRadioButtonImage2;
504 Toolkit::RadioButton mRadioButtonImage3;
506 Toolkit::PushButton mUpdateButton;
508 Animation mAnimation;
517 void RunTest( Application& application )
519 ButtonsController test( application );
521 application.MainLoop();
524 // Entry point for Linux & Tizen applications
526 int main( int argc, char **argv )
528 Application application = Application::New( &argc, &argv, DALI_DEMO_THEME_PATH );
530 RunTest( application );