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>
26 // Used to produce visually same dimensions on desktop and device builds
27 float ScalePointSize( int pointSize )
29 Dali::Vector2 dpi = Dali::Stage::GetCurrent().GetDpi();
30 float meanDpi = (dpi.height + dpi.width) * 0.5f;
31 return pointSize * meanDpi / 220.0f;
36 // Define this so that it is interchangeable
37 // "DP" stands for Device independent Pixels
38 #define DP(x) ScalePointSize(x)
44 const char* const BACKGROUND_IMAGE = DALI_IMAGE_DIR "background-gradient.jpg";
45 const char* const TOOLBAR_IMAGE = DALI_IMAGE_DIR "top-bar.png";
47 const char* const TOOLBAR_TITLE = "Buttons";
49 const char* const SMALL_IMAGE_1 = DALI_IMAGE_DIR "gallery-small-14.jpg";
50 const char* const BIG_IMAGE_1 = DALI_IMAGE_DIR "gallery-large-4.jpg";
52 const char* const SMALL_IMAGE_2 = DALI_IMAGE_DIR "gallery-small-20.jpg";
53 const char* const BIG_IMAGE_2 = DALI_IMAGE_DIR "gallery-large-11.jpg";
55 const char* const SMALL_IMAGE_3 = DALI_IMAGE_DIR "gallery-small-25.jpg";
56 const char* const BIG_IMAGE_3 = DALI_IMAGE_DIR "gallery-large-13.jpg";
58 const char* const ENABLED_IMAGE = DALI_IMAGE_DIR "item-select-check.png";
60 const char* const PUSHBUTTON_PRESS_IMAGE = DALI_IMAGE_DIR "button-down.9.png";
61 const char* const PUSHBUTTON_DISABLED_IMAGE = DALI_IMAGE_DIR "button-disabled.9.png";
62 const char* const PUSHBUTTON_BUTTON_IMAGE = DALI_IMAGE_DIR "button-up.9.png";
64 const char* const CHECKBOX_UNSELECTED_IMAGE = DALI_IMAGE_DIR "checkbox-unselected.png";
65 const char* const CHECKBOX_SELECTED_IMAGE = DALI_IMAGE_DIR "checkbox-selected.png";
67 const Vector4 BACKGROUND_COLOUR( 1.0f, 1.0f, 1.0f, 0.15f );
70 const int RADIO_LABEL_THUMBNAIL_SIZE = 48;
71 const int RADIO_IMAGE_SPACING = 8;
72 const int BUTTON_HEIGHT = 48;
74 const int MARGIN_SIZE = 10;
75 const int TOP_MARGIN = 85;
76 const int GROUP2_HEIGHT = 238;
77 const int GROUP1_HEIGHT = 120;
78 const int GROUP3_HEIGHT = 190;
79 const int GROUP4_HEIGHT = BUTTON_HEIGHT + MARGIN_SIZE * 2;
83 /** This example shows how to create and use different buttons.
85 * 1. First group of radio buttons with image actor labels selects an image to load
86 * 2. A push button loads the selected thumbnail image into the larger image pane
87 * 3. Second group of radio buttons with a table view label containing a text view and image view, and a normal text view.
88 * Selecting one of these will enable/disable the image loading push button
89 * 4. A group of check boxes
91 class ButtonsController: public ConnectionTracker
95 ButtonsController( Application& application )
96 : mApplication( application )
98 // Connect to the Application's Init signal
99 mApplication.InitSignal().Connect( this, &ButtonsController::Create );
104 // Nothing to do here
107 void Create( Application& application )
109 // The Init signal is received once (only) during the Application lifetime
111 // Respond to key events
112 Stage::GetCurrent().KeyEventSignal().Connect(this, &ButtonsController::OnKeyEvent);
114 // Creates a default view with a default tool bar.
115 // The view is added to the stage.
116 mContentLayer = DemoHelper::CreateView( application,
123 int yPos = TOP_MARGIN + MARGIN_SIZE;
125 // Image selector radio group
126 Actor radioGroup2Background = Toolkit::CreateSolidColorActor( BACKGROUND_COLOUR );
127 radioGroup2Background.SetAnchorPoint( AnchorPoint::TOP_LEFT );
128 radioGroup2Background.SetParentOrigin( ParentOrigin::TOP_LEFT );
129 radioGroup2Background.SetPosition( DP(MARGIN_SIZE), DP(yPos) );
130 radioGroup2Background.SetRelayoutEnabled( true );
131 radioGroup2Background.SetPreferredSize( Vector2( DP(348), DP(GROUP2_HEIGHT) ) );
132 mContentLayer.Add( radioGroup2Background );
134 Actor radioButtonsGroup2 = Actor::New();
135 radioButtonsGroup2.SetParentOrigin( ParentOrigin::TOP_LEFT );
136 radioButtonsGroup2.SetAnchorPoint( AnchorPoint::TOP_LEFT );
137 radioButtonsGroup2.SetPosition( DP(MARGIN_SIZE), DP(MARGIN_SIZE) );
138 radioButtonsGroup2.SetRelayoutEnabled( true );
139 radioButtonsGroup2.SetPreferredSize( Vector2( DP(100), DP(160) ) );
141 radioGroup2Background.Add( radioButtonsGroup2 );
147 ImageActor imageActor = ImageActor::New( ResourceImage::New( SMALL_IMAGE_1 ) );
148 imageActor.SetPreferredSize( Vector2( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) ) );
149 mRadioButtonImage1 = Dali::Toolkit::RadioButton::New( imageActor );
150 mRadioButtonImage1.SetParentOrigin( ParentOrigin::TOP_LEFT );
151 mRadioButtonImage1.SetAnchorPoint( AnchorPoint::TOP_LEFT );
152 mRadioButtonImage1.SetPosition( 0, DP(radioY) );
153 mRadioButtonImage1.SetSelected( true );
155 radioButtonsGroup2.Add( mRadioButtonImage1 );
160 radioY += RADIO_LABEL_THUMBNAIL_SIZE + RADIO_IMAGE_SPACING;
162 ImageActor imageActor = ImageActor::New( ResourceImage::New( SMALL_IMAGE_2 ) );
163 imageActor.SetPreferredSize( Vector2( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) ) );
165 mRadioButtonImage2 = Dali::Toolkit::RadioButton::New( imageActor );
166 mRadioButtonImage2.SetParentOrigin( ParentOrigin::TOP_LEFT );
167 mRadioButtonImage2.SetAnchorPoint( AnchorPoint::TOP_LEFT );
168 mRadioButtonImage2.SetPosition( 0, DP(radioY) );
170 radioButtonsGroup2.Add( mRadioButtonImage2 );
175 radioY += RADIO_LABEL_THUMBNAIL_SIZE + RADIO_IMAGE_SPACING;
177 ImageActor imageActor = ImageActor::New( ResourceImage::New( SMALL_IMAGE_3 ) );
178 imageActor.SetPreferredSize( Vector2( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) ) );
180 mRadioButtonImage3 = Dali::Toolkit::RadioButton::New( imageActor );
181 mRadioButtonImage3.SetParentOrigin( ParentOrigin::TOP_LEFT );
182 mRadioButtonImage3.SetAnchorPoint( AnchorPoint::TOP_LEFT );
183 mRadioButtonImage3.SetPosition( 0, DP(radioY) );
185 radioButtonsGroup2.Add( mRadioButtonImage3 );
188 // Create select button
189 mUpdateButton = Toolkit::PushButton::New();
190 mUpdateButton.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
191 mUpdateButton.SetAnchorPoint( AnchorPoint::TOP_CENTER );
192 mUpdateButton.SetPosition( 0, DP(MARGIN_SIZE) );
193 mUpdateButton.SetLabel( "Select" );
194 mUpdateButton.SetPreferredSize( Vector2( DP(100), DP(BUTTON_HEIGHT) ) );
196 mUpdateButton.SetSelectedImage( Dali::ResourceImage::New( PUSHBUTTON_PRESS_IMAGE ) );
197 mUpdateButton.SetDisabledImage( Dali::ResourceImage::New( PUSHBUTTON_DISABLED_IMAGE ) );
198 mUpdateButton.SetButtonImage( Dali::ResourceImage::New( PUSHBUTTON_BUTTON_IMAGE ) );
200 mUpdateButton.ClickedSignal().Connect( this, &ButtonsController::OnButtonClicked );
202 radioButtonsGroup2.Add(mUpdateButton);
204 // ImageActor to display selected image
205 mBigImage1 = ResourceImage::New( BIG_IMAGE_1 );
206 mBigImage2 = ResourceImage::New( BIG_IMAGE_2 );
207 mBigImage3 = ResourceImage::New( BIG_IMAGE_3 );
209 mImage = ImageActor::New( mBigImage1 );
210 mImage.SetParentOrigin( ParentOrigin::TOP_RIGHT );
211 mImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
212 mImage.SetPosition( DP(MARGIN_SIZE), 0 );
213 mImage.SetPreferredSize( Vector2( DP(218), DP(218) ) );
214 radioButtonsGroup2.Add( mImage );
216 // The enable/disable radio group
217 yPos += GROUP2_HEIGHT + MARGIN_SIZE;
219 Actor radioGroup1Background = Toolkit::CreateSolidColorActor( BACKGROUND_COLOUR );
220 radioGroup1Background.SetAnchorPoint( AnchorPoint::TOP_LEFT );
221 radioGroup1Background.SetParentOrigin( ParentOrigin::TOP_LEFT );
222 radioGroup1Background.SetPosition( DP(MARGIN_SIZE), DP(yPos) );
223 radioGroup1Background.SetRelayoutEnabled( true );
224 radioGroup1Background.SetPreferredSize( Vector2( DP(348), DP(GROUP1_HEIGHT) ) );
225 mContentLayer.Add( radioGroup1Background );
228 Actor radioButtonsGroup1 = Actor::New();
229 radioButtonsGroup1.SetParentOrigin( ParentOrigin::TOP_LEFT );
230 radioButtonsGroup1.SetAnchorPoint( AnchorPoint::TOP_LEFT );
231 radioButtonsGroup1.SetPosition( DP(MARGIN_SIZE), DP(MARGIN_SIZE) );
233 radioGroup1Background.Add( radioButtonsGroup1 );
235 // First radio button
237 Toolkit::TableView tableView = Toolkit::TableView::New( 1, 2 );
238 tableView.SetPreferredSize( Vector2( DP(260), 0.0f ) );
239 tableView.SetResizePolicy( USE_NATURAL_SIZE, HEIGHT );
241 ImageActor imageActor = ImageActor::New( ResourceImage::New( ENABLED_IMAGE ) );
242 imageActor.SetPreferredSize( Vector2( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) ) );
243 imageActor.SetResizePolicy( FIXED, ALL_DIMENSIONS );
244 imageActor.SetPadding( Padding( DP(20.0f), 0.0f, 0.0f, 0.0f ) );
245 tableView.AddChild( imageActor, Toolkit::TableView::CellPosition( 0, 1 ) );
247 Toolkit::RadioButton radioButton = Dali::Toolkit::RadioButton::New( tableView );
248 radioButton.SetName( "radio-select-enable" );
249 radioButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
250 radioButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
251 radioButton.SetPosition( 0, 0 );
252 radioButton.SetSelected( true );
254 radioButton.StateChangedSignal().Connect( this, &ButtonsController::EnableSelectButton );
256 radioButtonsGroup1.Add( radioButton );
259 // Second radio button
261 Toolkit::RadioButton radioButton = Dali::Toolkit::RadioButton::New( "Select disabled" );
262 radioButton.SetName( "radio-select-disable" );
263 radioButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
264 radioButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
265 radioButton.SetPosition( 0, DP(50) );
267 radioButton.StateChangedSignal().Connect( this, &ButtonsController::EnableSelectButton );
269 radioButtonsGroup1.Add( radioButton );
273 yPos += GROUP1_HEIGHT + MARGIN_SIZE;
275 Actor checkBoxBackground = Toolkit::CreateSolidColorActor( BACKGROUND_COLOUR );
276 checkBoxBackground.SetAnchorPoint( AnchorPoint::TOP_LEFT );
277 checkBoxBackground.SetParentOrigin( ParentOrigin::TOP_LEFT );
278 checkBoxBackground.SetPosition( DP(MARGIN_SIZE), DP(yPos) );
279 checkBoxBackground.SetRelayoutEnabled( true );
280 checkBoxBackground.SetPreferredSize( Vector2( DP(430), DP(GROUP3_HEIGHT) ) );
281 mContentLayer.Add( checkBoxBackground );
283 Dali::Image unselected = Dali::ResourceImage::New( CHECKBOX_UNSELECTED_IMAGE );
284 Dali::Image selected = Dali::ResourceImage::New( CHECKBOX_SELECTED_IMAGE );
286 int checkYPos = MARGIN_SIZE;
289 Toolkit::CheckBoxButton checkBox = Toolkit::CheckBoxButton::New();
290 checkBox.SetName( "checkbox1" );
291 checkBox.SetPosition( DP(MARGIN_SIZE), DP(checkYPos) );
292 checkBox.SetParentOrigin( ParentOrigin::TOP_LEFT );
293 checkBox.SetAnchorPoint( AnchorPoint::TOP_LEFT );
294 checkBox.SetBackgroundImage( unselected );
295 checkBox.SetSelectedImage( selected );
296 checkBox.SetLabel( "CheckBox1 is unselected" );
297 checkBox.StateChangedSignal().Connect( this, &ButtonsController::OnCheckBoxesSelected );
299 checkBoxBackground.Add( checkBox );
305 Toolkit::CheckBoxButton checkBox = Toolkit::CheckBoxButton::New();
306 checkBox.SetName( "checkbox2" );
307 checkBox.SetPosition( DP(MARGIN_SIZE), DP(checkYPos) );
308 checkBox.SetAnchorPoint( AnchorPoint::TOP_LEFT );
309 checkBox.SetBackgroundImage( unselected );
310 checkBox.SetSelectedImage( selected );
311 checkBox.SetLabel( "CheckBox2 is selected" );
312 checkBox.SetSelected( true );
313 checkBox.StateChangedSignal().Connect( this, &ButtonsController::OnCheckBoxesSelected );
315 checkBoxBackground.Add( checkBox );
321 Toolkit::CheckBoxButton checkBox = Toolkit::CheckBoxButton::New();
322 checkBox.SetName( "checkbox3" );
323 checkBox.SetPosition( DP(MARGIN_SIZE), DP(checkYPos) );
324 checkBox.SetAnchorPoint( AnchorPoint::TOP_LEFT );
325 checkBox.SetBackgroundImage( unselected );
326 checkBox.SetSelectedImage( selected );
327 checkBox.SetLabel( "CheckBox3 is unselected" );
328 checkBox.StateChangedSignal().Connect( this, &ButtonsController::OnCheckBoxesSelected );
330 checkBoxBackground.Add( checkBox );
333 // Create togglabe button
334 yPos += GROUP3_HEIGHT + MARGIN_SIZE;
336 Actor toggleBackground = Toolkit::CreateSolidColorActor( BACKGROUND_COLOUR );
337 toggleBackground.SetAnchorPoint( AnchorPoint::TOP_LEFT );
338 toggleBackground.SetParentOrigin( ParentOrigin::TOP_LEFT );
339 toggleBackground.SetPosition( DP(MARGIN_SIZE), DP(yPos) );
340 toggleBackground.SetRelayoutEnabled( true );
341 toggleBackground.SetPreferredSize( Vector2( DP(150 + MARGIN_SIZE * 2), DP(GROUP4_HEIGHT) ) );
342 mContentLayer.Add( toggleBackground );
344 Toolkit::PushButton toggleButton = Toolkit::PushButton::New();
345 toggleButton.SetTogglableButton( true );
346 toggleButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
347 toggleButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
348 toggleButton.SetPosition( DP(MARGIN_SIZE), DP(MARGIN_SIZE) );
349 toggleButton.SetLabel( "Unselected" );
350 toggleButton.SetPreferredSize( Vector2( DP(150), DP(BUTTON_HEIGHT) ) );
352 toggleButton.SetSelectedImage( Dali::ResourceImage::New( PUSHBUTTON_PRESS_IMAGE ) );
353 toggleButton.SetDisabledImage( Dali::ResourceImage::New( PUSHBUTTON_DISABLED_IMAGE ) );
354 toggleButton.SetButtonImage( Dali::ResourceImage::New( PUSHBUTTON_BUTTON_IMAGE ) );
356 toggleButton.StateChangedSignal().Connect( this, &ButtonsController::OnButtonSelected );
358 toggleBackground.Add( toggleButton );
361 void OnKeyEvent( const KeyEvent& event )
363 if( event.state == KeyEvent::Down )
365 if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
367 // Exit application when click back or escape.
373 bool OnButtonSelected( Toolkit::Button button )
375 Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( button );
378 if( button.IsSelected() )
380 pushButton.SetLabel( "Selected" );
384 pushButton.SetLabel( "Unselected" );
391 bool EnableSelectButton( Toolkit::Button button )
393 if( button.GetName() == "radio-select-enable" && button.IsSelected() == true )
395 mUpdateButton.SetDisabled( false );
397 else if( button.GetName() == "radio-select-disable" && button.IsSelected() == true )
399 mUpdateButton.SetDisabled( true );
405 bool OnButtonClicked(Toolkit::Button button)
407 if( mRadioButtonImage1.IsSelected() )
409 mImage.SetImage( mBigImage1 );
411 else if( mRadioButtonImage2.IsSelected() )
413 mImage.SetImage( mBigImage2 );
415 else if( mRadioButtonImage3.IsSelected() )
417 mImage.SetImage( mBigImage3 );
422 bool OnCheckBoxesSelected( Toolkit::Button button )
429 Application& mApplication;
430 Toolkit::View mView; ///< The View instance.
431 Toolkit::ToolBar mToolBar; ///< The View's Toolbar.
432 Layer mContentLayer; ///< Content layer
434 Toolkit::RadioButton mRadioButtonImage1;
435 Toolkit::RadioButton mRadioButtonImage2;
436 Toolkit::RadioButton mRadioButtonImage3;
438 Toolkit::PushButton mUpdateButton;
446 void RunTest( Application& application )
448 ButtonsController test( application );
450 application.MainLoop();
453 // Entry point for Linux & Tizen applications
455 int main( int argc, char **argv )
457 Application application = Application::New( &argc, &argv );
459 RunTest( application );