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.SetSize( 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.SetSize( DP(100), DP(160) );
141 radioGroup2Background.Add( radioButtonsGroup2 );
147 ImageActor imageActor = ImageActor::New( ResourceImage::New( SMALL_IMAGE_1 ) );
148 imageActor.SetSize( 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.SetSize( 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.SetSize( 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.SetSize( 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.SetSize( 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.SetSize( 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.SetSize( DP(260), 0.0f );
239 tableView.SetResizePolicy( USE_NATURAL_SIZE, HEIGHT );
241 Toolkit::TextView textView = Toolkit::TextView::New( "Select enabled" );
242 tableView.AddChild( textView, Toolkit::TableView::CellPosition( 0, 0 ) );
244 ImageActor imageActor = ImageActor::New( ResourceImage::New( ENABLED_IMAGE ) );
245 imageActor.SetSize( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) );
246 imageActor.SetResizePolicy( FIXED, ALL_DIMENSIONS );
247 imageActor.SetPadding( Padding( DP(20.0f), 0.0f, 0.0f, 0.0f ) );
248 tableView.AddChild( imageActor, Toolkit::TableView::CellPosition( 0, 1 ) );
250 Toolkit::RadioButton radioButton = Dali::Toolkit::RadioButton::New( tableView );
251 radioButton.SetName( "radio-select-enable" );
252 radioButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
253 radioButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
254 radioButton.SetPosition( 0, 0 );
255 radioButton.SetSelected( true );
257 radioButton.StateChangedSignal().Connect( this, &ButtonsController::EnableSelectButton );
259 radioButtonsGroup1.Add( radioButton );
262 // Second radio button
264 Toolkit::RadioButton radioButton = Dali::Toolkit::RadioButton::New( "Select disabled" );
265 radioButton.SetName( "radio-select-disable" );
266 radioButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
267 radioButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
268 radioButton.SetPosition( 0, DP(50) );
270 radioButton.StateChangedSignal().Connect( this, &ButtonsController::EnableSelectButton );
272 radioButtonsGroup1.Add( radioButton );
276 yPos += GROUP1_HEIGHT + MARGIN_SIZE;
278 Actor checkBoxBackground = Toolkit::CreateSolidColorActor( BACKGROUND_COLOUR );
279 checkBoxBackground.SetAnchorPoint( AnchorPoint::TOP_LEFT );
280 checkBoxBackground.SetParentOrigin( ParentOrigin::TOP_LEFT );
281 checkBoxBackground.SetPosition( DP(MARGIN_SIZE), DP(yPos) );
282 checkBoxBackground.SetRelayoutEnabled( true );
283 checkBoxBackground.SetSize( DP(430), DP(GROUP3_HEIGHT) );
284 mContentLayer.Add( checkBoxBackground );
286 Dali::Image unselected = Dali::ResourceImage::New( CHECKBOX_UNSELECTED_IMAGE );
287 Dali::Image selected = Dali::ResourceImage::New( CHECKBOX_SELECTED_IMAGE );
289 int checkYPos = MARGIN_SIZE;
292 Toolkit::CheckBoxButton checkBox = Toolkit::CheckBoxButton::New();
293 checkBox.SetName( "checkbox1" );
294 checkBox.SetPosition( DP(MARGIN_SIZE), DP(checkYPos) );
295 checkBox.SetParentOrigin( ParentOrigin::TOP_LEFT );
296 checkBox.SetAnchorPoint( AnchorPoint::TOP_LEFT );
297 checkBox.SetBackgroundImage( unselected );
298 checkBox.SetSelectedImage( selected );
299 checkBox.SetLabel( "CheckBox1 is unselected" );
300 checkBox.StateChangedSignal().Connect( this, &ButtonsController::OnCheckBoxesSelected );
302 checkBoxBackground.Add( checkBox );
308 Toolkit::CheckBoxButton checkBox = Toolkit::CheckBoxButton::New();
309 checkBox.SetName( "checkbox2" );
310 checkBox.SetPosition( DP(MARGIN_SIZE), DP(checkYPos) );
311 checkBox.SetAnchorPoint( AnchorPoint::TOP_LEFT );
312 checkBox.SetBackgroundImage( unselected );
313 checkBox.SetSelectedImage( selected );
314 checkBox.SetLabel( "CheckBox2 is selected" );
315 checkBox.SetSelected( true );
316 checkBox.StateChangedSignal().Connect( this, &ButtonsController::OnCheckBoxesSelected );
318 checkBoxBackground.Add( checkBox );
324 Toolkit::CheckBoxButton checkBox = Toolkit::CheckBoxButton::New();
325 checkBox.SetName( "checkbox3" );
326 checkBox.SetPosition( DP(MARGIN_SIZE), DP(checkYPos) );
327 checkBox.SetAnchorPoint( AnchorPoint::TOP_LEFT );
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 yPos += GROUP3_HEIGHT + MARGIN_SIZE;
339 Actor toggleBackground = Toolkit::CreateSolidColorActor( BACKGROUND_COLOUR );
340 toggleBackground.SetAnchorPoint( AnchorPoint::TOP_LEFT );
341 toggleBackground.SetParentOrigin( ParentOrigin::TOP_LEFT );
342 toggleBackground.SetPosition( DP(MARGIN_SIZE), DP(yPos) );
343 toggleBackground.SetRelayoutEnabled( true );
344 toggleBackground.SetSize( DP(150 + MARGIN_SIZE * 2), DP(GROUP4_HEIGHT) );
345 mContentLayer.Add( toggleBackground );
347 Toolkit::PushButton toggleButton = Toolkit::PushButton::New();
348 toggleButton.SetTogglableButton( true );
349 toggleButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
350 toggleButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
351 toggleButton.SetPosition( DP(MARGIN_SIZE), DP(MARGIN_SIZE) );
352 toggleButton.SetLabel( "Unselected" );
353 toggleButton.SetSize( DP(150), DP(BUTTON_HEIGHT) );
355 toggleButton.SetSelectedImage( Dali::ResourceImage::New( PUSHBUTTON_PRESS_IMAGE ) );
356 toggleButton.SetDisabledImage( Dali::ResourceImage::New( PUSHBUTTON_DISABLED_IMAGE ) );
357 toggleButton.SetButtonImage( Dali::ResourceImage::New( PUSHBUTTON_BUTTON_IMAGE ) );
359 toggleButton.StateChangedSignal().Connect( this, &ButtonsController::OnButtonSelected );
361 toggleBackground.Add( toggleButton );
364 void OnKeyEvent( const KeyEvent& event )
366 if( event.state == KeyEvent::Down )
368 if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
370 // Exit application when click back or escape.
376 bool OnButtonSelected( Toolkit::Button button )
378 Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( button );
381 if( button.IsSelected() )
383 pushButton.SetLabel( "Selected" );
387 pushButton.SetLabel( "Unselected" );
394 bool EnableSelectButton( Toolkit::Button button )
396 if( button.GetName() == "radio-select-enable" && button.IsSelected() == true )
398 mUpdateButton.SetDisabled( false );
400 else if( button.GetName() == "radio-select-disable" && button.IsSelected() == true )
402 mUpdateButton.SetDisabled( true );
408 bool OnButtonClicked(Toolkit::Button button)
410 if( mRadioButtonImage1.IsSelected() )
412 mImage.SetImage( mBigImage1 );
414 else if( mRadioButtonImage2.IsSelected() )
416 mImage.SetImage( mBigImage2 );
418 else if( mRadioButtonImage3.IsSelected() )
420 mImage.SetImage( mBigImage3 );
425 bool OnCheckBoxesSelected( Toolkit::Button button )
427 if( button.GetName() == "checkbox1" )
429 if( button.IsSelected() )
431 button.SetLabel("CheckBox1 is selected");
435 button.SetLabel("CheckBox1 is unselected");
439 if( button.GetName() == "checkbox2" )
441 if( button.IsSelected() )
443 button.SetLabel("CheckBox2 is selected");
447 button.SetLabel("CheckBox2 is unselected");
451 if( button.GetName() == "checkbox3" )
453 if( button.IsSelected() )
455 button.SetLabel("CheckBox3 is selected");
459 button.SetLabel("CheckBox3 is unselected");
468 Application& mApplication;
469 Toolkit::View mView; ///< The View instance.
470 Toolkit::ToolBar mToolBar; ///< The View's Toolbar.
471 Layer mContentLayer; ///< Content layer
473 Toolkit::RadioButton mRadioButtonImage1;
474 Toolkit::RadioButton mRadioButtonImage2;
475 Toolkit::RadioButton mRadioButtonImage3;
477 Toolkit::PushButton mUpdateButton;
485 void RunTest( Application& application )
487 ButtonsController test( application );
489 application.MainLoop();
492 // Entry point for Linux & Tizen applications
494 int main( int argc, char **argv )
496 Application application = Application::New( &argc, &argv );
498 RunTest( application );