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>
21 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
25 // Define this so that it is interchangeable
26 // "DP" stands for Device independent Pixels
33 const char* const BACKGROUND_IMAGE = DALI_IMAGE_DIR "background-gradient.jpg";
34 const char* const TOOLBAR_IMAGE = DALI_IMAGE_DIR "top-bar.png";
36 const char* const TOOLBAR_TITLE = "Buttons";
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";
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";
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";
47 const char* const ENABLED_IMAGE = DALI_IMAGE_DIR "item-select-check.png";
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";
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";
56 const Vector4 BACKGROUND_COLOUR( 1.0f, 1.0f, 1.0f, 0.15f );
59 const int RADIO_LABEL_THUMBNAIL_SIZE = 60;
60 const int RADIO_IMAGE_SPACING = 8;
61 const int BUTTON_HEIGHT = 48;
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;
72 /** This example shows how to create and use different buttons.
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
80 class ButtonsController: public ConnectionTracker
84 ButtonsController( Application& application )
85 : mApplication( application )
87 // Connect to the Application's Init signal
88 mApplication.InitSignal().Connect( this, &ButtonsController::Create );
96 void Create( Application& application )
98 // The Init signal is received once (only) during the Application lifetime
100 DemoHelper::RequestThemeChange();
102 // Respond to key events
103 Stage::GetCurrent().KeyEventSignal().Connect(this, &ButtonsController::OnKeyEvent);
105 // Creates a default view with a default tool bar.
106 // The view is added to the stage.
107 mContentLayer = DemoHelper::CreateView( application,
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 );
122 for( unsigned int i = 0; i < contentTable.GetRows(); ++i )
124 contentTable.SetFitHeight( i );
127 contentTable.SetPosition( 0.0f, TOP_MARGIN );
129 mContentLayer.Add( contentTable );
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 );
141 contentTable.Add( radioGroup2Background );
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 )
148 radioButtonsGroup2.SetFitHeight( i );
150 radioButtonsGroup2.SetFitWidth( 0 );
152 radioGroup2Background.AddChild( radioButtonsGroup2, Toolkit::TableView::CellPosition( 0, 0 ) );
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 );
166 radioButtonsGroup2.Add( mRadioButtonImage1 );
171 radioY += RADIO_LABEL_THUMBNAIL_SIZE + RADIO_IMAGE_SPACING;
173 ImageActor imageActor = ImageActor::New( ResourceImage::New( SMALL_IMAGE_2 ) );
174 imageActor.SetSize( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) );
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) );
181 radioButtonsGroup2.Add( mRadioButtonImage2 );
186 radioY += RADIO_LABEL_THUMBNAIL_SIZE + RADIO_IMAGE_SPACING;
188 ImageActor imageActor = ImageActor::New( ResourceImage::New( SMALL_IMAGE_3 ) );
189 imageActor.SetSize( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) );
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) );
196 radioButtonsGroup2.Add( mRadioButtonImage3 );
199 // Create select button
200 mUpdateButton = Toolkit::PushButton::New();
201 mUpdateButton.SetLabel( "Select" );
202 mUpdateButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
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 ) );
208 mUpdateButton.ClickedSignal().Connect( this, &ButtonsController::OnButtonClicked );
210 radioGroup2Background.AddChild( mUpdateButton, Toolkit::TableView::CellPosition( 1, 0 ) );
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 );
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 ) );
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 );
230 contentTable.Add( radioGroup1Background );
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 )
238 radioButtonsGroup1.SetFitHeight( i );
240 radioButtonsGroup1.SetFitWidth( 0 );
242 radioGroup1Background.Add( radioButtonsGroup1 );
244 // First radio button
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 );
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 ) );
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 ) );
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 );
270 radioButton.StateChangedSignal().Connect( this, &ButtonsController::EnableSelectButton );
272 radioButtonsGroup1.Add( radioButton );
275 // Second radio button
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) );
283 radioButton.StateChangedSignal().Connect( this, &ButtonsController::EnableSelectButton );
285 radioButtonsGroup1.Add( radioButton );
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 ) );
295 for( unsigned int i = 0; i < checkBoxBackground.GetRows(); ++i )
297 checkBoxBackground.SetFitHeight( i );
300 contentTable.Add( checkBoxBackground );
302 Dali::Image unselected = Dali::ResourceImage::New( CHECKBOX_UNSELECTED_IMAGE );
303 Dali::Image selected = Dali::ResourceImage::New( CHECKBOX_SELECTED_IMAGE );
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 );
313 checkBoxBackground.Add( checkBox );
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 );
325 checkBoxBackground.Add( checkBox );
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 );
336 checkBoxBackground.Add( checkBox );
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 ) );
346 for( unsigned int i = 0; i < toggleBackground.GetRows(); ++i )
348 toggleBackground.SetFitHeight( i );
351 contentTable.Add( toggleBackground );
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 );
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 ) );
363 toggleButton.StateChangedSignal().Connect( this, &ButtonsController::OnButtonSelected );
365 toggleBackground.Add( toggleButton );
368 void OnKeyEvent( const KeyEvent& event )
370 if( event.state == KeyEvent::Down )
372 if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
374 // Exit application when click back or escape.
380 bool OnButtonSelected( Toolkit::Button button )
382 Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( button );
385 if( button.IsSelected() )
387 pushButton.SetLabel( "Selected" );
391 pushButton.SetLabel( "Unselected" );
398 bool EnableSelectButton( Toolkit::Button button )
400 if( button.GetName() == "radio-select-enable" && button.IsSelected() == true )
402 mUpdateButton.SetDisabled( false );
404 else if( button.GetName() == "radio-select-disable" && button.IsSelected() == true )
406 mUpdateButton.SetDisabled( true );
412 bool OnButtonClicked(Toolkit::Button button)
414 if( mRadioButtonImage1.IsSelected() )
416 mImage.SetImage( mBigImage1 );
418 else if( mRadioButtonImage2.IsSelected() )
420 mImage.SetImage( mBigImage2 );
422 else if( mRadioButtonImage3.IsSelected() )
424 mImage.SetImage( mBigImage3 );
429 bool OnCheckBoxesSelected( Toolkit::Button button )
431 if( button.GetName() == "checkbox1" )
433 if( button.IsSelected() )
435 button.SetLabel("CheckBox1 is selected");
439 button.SetLabel("CheckBox1 is unselected");
443 if( button.GetName() == "checkbox2" )
445 if( button.IsSelected() )
447 button.SetLabel("CheckBox2 is selected");
451 button.SetLabel("CheckBox2 is unselected");
455 if( button.GetName() == "checkbox3" )
457 if( button.IsSelected() )
459 button.SetLabel("CheckBox3 is selected");
463 button.SetLabel("CheckBox3 is unselected");
470 bool OnTouchEvent( Actor actor, const TouchEvent& event )
472 if( 1u == event.GetPointCount() )
474 const TouchPoint::State state = event.GetPoint(0u).state;
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 ) );
479 if( TouchPoint::Down == state )
481 mLastPoint = localPoint;
482 mAnimation = Animation::New( 0.25f );
484 else if( TouchPoint::Motion == state )
488 mAnimation.AnimateBy( Property(actor, Actor::Property::POSITION), Vector3( 0.f, localPoint - mLastPoint, 0.f ), AlphaFunction::LINEAR );
490 mLastPoint = localPoint;
500 Application& mApplication;
501 Toolkit::Control mView; ///< The View instance.
502 Toolkit::ToolBar mToolBar; ///< The View's Toolbar.
503 Layer mContentLayer; ///< Content layer
505 Toolkit::RadioButton mRadioButtonImage1;
506 Toolkit::RadioButton mRadioButtonImage2;
507 Toolkit::RadioButton mRadioButtonImage3;
509 Toolkit::PushButton mUpdateButton;
511 Animation mAnimation;
520 void RunTest( Application& application )
522 ButtonsController test( application );
524 application.MainLoop();
527 // Entry point for Linux & Tizen applications
529 int main( int argc, char **argv )
531 Application application = Application::New( &argc, &argv );
533 RunTest( application );