buttons-example.cpp
18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*
* Copyright (c) 2014 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "shared/view.h"
#include <dali/dali.h>
#include <dali-toolkit/dali-toolkit.h>
using namespace Dali;
// Define this so that it is interchangeable
// "DP" stands for Device independent Pixels
#define DP(x) x
namespace
{
const char* const BACKGROUND_IMAGE = DALI_IMAGE_DIR "background-gradient.jpg";
const char* const TOOLBAR_IMAGE = DALI_IMAGE_DIR "top-bar.png";
const char* const TOOLBAR_TITLE = "Buttons";
const char* const SMALL_IMAGE_1 = DALI_IMAGE_DIR "gallery-small-14.jpg";
const char* const BIG_IMAGE_1 = DALI_IMAGE_DIR "gallery-large-4.jpg";
const char* const SMALL_IMAGE_2 = DALI_IMAGE_DIR "gallery-small-20.jpg";
const char* const BIG_IMAGE_2 = DALI_IMAGE_DIR "gallery-large-11.jpg";
const char* const SMALL_IMAGE_3 = DALI_IMAGE_DIR "gallery-small-25.jpg";
const char* const BIG_IMAGE_3 = DALI_IMAGE_DIR "gallery-large-13.jpg";
const char* const ENABLED_IMAGE = DALI_IMAGE_DIR "item-select-check.png";
const char* const PUSHBUTTON_PRESS_IMAGE = DALI_IMAGE_DIR "button-down.9.png";
const char* const PUSHBUTTON_DISABLED_IMAGE = DALI_IMAGE_DIR "button-disabled.9.png";
const char* const PUSHBUTTON_BUTTON_IMAGE = DALI_IMAGE_DIR "button-up.9.png";
const char* const CHECKBOX_UNSELECTED_IMAGE = DALI_IMAGE_DIR "checkbox-unselected.png";
const char* const CHECKBOX_SELECTED_IMAGE = DALI_IMAGE_DIR "checkbox-selected.png";
const Vector4 BACKGROUND_COLOUR( 1.0f, 1.0f, 1.0f, 0.15f );
// Layout sizes
const int RADIO_LABEL_THUMBNAIL_SIZE = 60;
const int RADIO_IMAGE_SPACING = 8;
const int BUTTON_HEIGHT = 48;
const int MARGIN_SIZE = 10;
const int TOP_MARGIN = 85;
const int GROUP2_HEIGHT = 238;
const int GROUP1_HEIGHT = 120;
const int GROUP3_HEIGHT = 190;
const int GROUP4_HEIGHT = BUTTON_HEIGHT + MARGIN_SIZE * 2;
} // namespace
/** This example shows how to create and use different buttons.
*
* 1. First group of radio buttons with image actor labels selects an image to load
* 2. A push button loads the selected thumbnail image into the larger image pane
* 3. Second group of radio buttons with a table view label containing a text view and image view, and a normal text view.
* Selecting one of these will enable/disable the image loading push button
* 4. A group of check boxes
*/
class ButtonsController: public ConnectionTracker
{
public:
ButtonsController( Application& application )
: mApplication( application )
{
// Connect to the Application's Init signal
mApplication.InitSignal().Connect( this, &ButtonsController::Create );
}
~ButtonsController()
{
// Nothing to do here
}
void Create( Application& application )
{
// The Init signal is received once (only) during the Application lifetime
// Respond to key events
Stage::GetCurrent().KeyEventSignal().Connect(this, &ButtonsController::OnKeyEvent);
// Creates a default view with a default tool bar.
// The view is added to the stage.
mContentLayer = DemoHelper::CreateView( application,
mView,
mToolBar,
BACKGROUND_IMAGE,
TOOLBAR_IMAGE,
TOOLBAR_TITLE );
Toolkit::TableView contentTable = Toolkit::TableView::New( 4, 1 );
contentTable.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
contentTable.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
contentTable.SetAnchorPoint( AnchorPoint::TOP_LEFT );
contentTable.SetParentOrigin( ParentOrigin::TOP_LEFT );
contentTable.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5f ) );
// contentTable.TouchedSignal().Connect( this, &ButtonsController::OnTouchEvent );
for( unsigned int i = 0; i < contentTable.GetRows(); ++i )
{
contentTable.SetFitHeight( i );
}
contentTable.SetPosition( 0.0f, TOP_MARGIN );
mContentLayer.Add( contentTable );
// Image selector radio group
Toolkit::TableView radioGroup2Background = Toolkit::TableView::New( 2, 2 );
radioGroup2Background.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
radioGroup2Background.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
radioGroup2Background.SetBackgroundColor( BACKGROUND_COLOUR );
radioGroup2Background.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE ) );
radioGroup2Background.SetFitHeight( 0 );
radioGroup2Background.SetFitHeight( 1 );
radioGroup2Background.SetFitWidth( 0 );
contentTable.Add( radioGroup2Background );
Toolkit::TableView radioButtonsGroup2 = Toolkit::TableView::New( 3, 1 );
radioButtonsGroup2.SetCellPadding( Size( 0.0f, MARGIN_SIZE * 0.5f ) );
radioButtonsGroup2.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
for( unsigned int i = 0; i < radioButtonsGroup2.GetRows(); ++i )
{
radioButtonsGroup2.SetFitHeight( i );
}
radioButtonsGroup2.SetFitWidth( 0 );
radioGroup2Background.AddChild( radioButtonsGroup2, Toolkit::TableView::CellPosition( 0, 0 ) );
int radioY = 0;
// Radio 1
{
ImageActor imageActor = ImageActor::New( ResourceImage::New( SMALL_IMAGE_1 ) );
imageActor.SetSize( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) );
mRadioButtonImage1 = Dali::Toolkit::RadioButton::New( imageActor );
mRadioButtonImage1.SetParentOrigin( ParentOrigin::TOP_LEFT );
mRadioButtonImage1.SetAnchorPoint( AnchorPoint::TOP_LEFT );
mRadioButtonImage1.SetPosition( 0, DP(radioY) );
mRadioButtonImage1.SetSelected( true );
radioButtonsGroup2.Add( mRadioButtonImage1 );
}
// Radio 2
{
radioY += RADIO_LABEL_THUMBNAIL_SIZE + RADIO_IMAGE_SPACING;
ImageActor imageActor = ImageActor::New( ResourceImage::New( SMALL_IMAGE_2 ) );
imageActor.SetSize( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) );
mRadioButtonImage2 = Dali::Toolkit::RadioButton::New( imageActor );
mRadioButtonImage2.SetParentOrigin( ParentOrigin::TOP_LEFT );
mRadioButtonImage2.SetAnchorPoint( AnchorPoint::TOP_LEFT );
mRadioButtonImage2.SetPosition( 0, DP(radioY) );
radioButtonsGroup2.Add( mRadioButtonImage2 );
}
// Radio 3
{
radioY += RADIO_LABEL_THUMBNAIL_SIZE + RADIO_IMAGE_SPACING;
ImageActor imageActor = ImageActor::New( ResourceImage::New( SMALL_IMAGE_3 ) );
imageActor.SetSize( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) );
mRadioButtonImage3 = Dali::Toolkit::RadioButton::New( imageActor );
mRadioButtonImage3.SetParentOrigin( ParentOrigin::TOP_LEFT );
mRadioButtonImage3.SetAnchorPoint( AnchorPoint::TOP_LEFT );
mRadioButtonImage3.SetPosition( 0, DP(radioY) );
radioButtonsGroup2.Add( mRadioButtonImage3 );
}
// Create select button
mUpdateButton = Toolkit::PushButton::New();
mUpdateButton.SetLabel( "Select" );
mUpdateButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
mUpdateButton.SetSelectedImage( Dali::ResourceImage::New( PUSHBUTTON_PRESS_IMAGE ) );
mUpdateButton.SetDisabledImage( Dali::ResourceImage::New( PUSHBUTTON_DISABLED_IMAGE ) );
mUpdateButton.SetButtonImage( Dali::ResourceImage::New( PUSHBUTTON_BUTTON_IMAGE ) );
mUpdateButton.ClickedSignal().Connect( this, &ButtonsController::OnButtonClicked );
radioGroup2Background.AddChild( mUpdateButton, Toolkit::TableView::CellPosition( 1, 0 ) );
// ImageActor to display selected image
mBigImage1 = ResourceImage::New( BIG_IMAGE_1 );
mBigImage2 = ResourceImage::New( BIG_IMAGE_2 );
mBigImage3 = ResourceImage::New( BIG_IMAGE_3 );
mImage = ImageActor::New( mBigImage1 );
mImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
mImage.SetSizeScalePolicy( SizeScalePolicy::FIT_WITH_ASPECT_RATIO );
radioGroup2Background.AddChild( mImage, Toolkit::TableView::CellPosition( 0, 1, 2, 1 ) );
// The enable/disable radio group
Toolkit::TableView radioGroup1Background = Toolkit::TableView::New( 1, 1 );
radioGroup1Background.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
radioGroup1Background.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
radioGroup1Background.SetBackgroundColor( BACKGROUND_COLOUR );
radioGroup1Background.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE ) );
radioGroup1Background.SetFitHeight( 0 );
contentTable.Add( radioGroup1Background );
// Radio group
Toolkit::TableView radioButtonsGroup1 = Toolkit::TableView::New( 2, 1 );
radioButtonsGroup1.SetCellPadding( Size( 0.0f, MARGIN_SIZE * 0.5f ) );
radioButtonsGroup1.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
for( unsigned int i = 0; i < radioButtonsGroup1.GetRows(); ++i )
{
radioButtonsGroup1.SetFitHeight( i );
}
radioButtonsGroup1.SetFitWidth( 0 );
radioGroup1Background.Add( radioButtonsGroup1 );
// First radio button
{
Toolkit::TableView tableView = Toolkit::TableView::New( 1, 2 );
tableView.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
tableView.SetFitHeight( 0 );
tableView.SetFitWidth( 0 );
tableView.SetFitWidth( 1 );
Toolkit::TextLabel textLabel = Toolkit::TextLabel::New( "Select enabled" );
textLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::WIDTH );
textLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
textLabel.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
tableView.AddChild( textLabel, Toolkit::TableView::CellPosition( 0, 0 ) );
ImageActor imageActor = ImageActor::New( ResourceImage::New( ENABLED_IMAGE ) );
imageActor.SetSize( DP(RADIO_LABEL_THUMBNAIL_SIZE), DP(RADIO_LABEL_THUMBNAIL_SIZE) );
imageActor.SetPadding( Padding( DP(20.0f), 0.0f, 0.0f, 0.0f ) );
tableView.AddChild( imageActor, Toolkit::TableView::CellPosition( 0, 1 ) );
Toolkit::RadioButton radioButton = Dali::Toolkit::RadioButton::New( tableView );
radioButton.SetName( "radio-select-enable" );
radioButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
radioButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
radioButton.SetPosition( 0, 0 );
radioButton.SetSelected( true );
radioButton.StateChangedSignal().Connect( this, &ButtonsController::EnableSelectButton );
radioButtonsGroup1.Add( radioButton );
}
// Second radio button
{
Toolkit::RadioButton radioButton = Dali::Toolkit::RadioButton::New( "Select disabled" );
radioButton.SetName( "radio-select-disable" );
radioButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
radioButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
radioButton.SetPosition( 0, DP(50) );
radioButton.StateChangedSignal().Connect( this, &ButtonsController::EnableSelectButton );
radioButtonsGroup1.Add( radioButton );
}
// CheckBoxes
Toolkit::TableView checkBoxBackground = Toolkit::TableView::New( 3, 1 );
checkBoxBackground.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
checkBoxBackground.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
checkBoxBackground.SetBackgroundColor( BACKGROUND_COLOUR );
checkBoxBackground.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE ) );
for( unsigned int i = 0; i < checkBoxBackground.GetRows(); ++i )
{
checkBoxBackground.SetFitHeight( i );
}
contentTable.Add( checkBoxBackground );
Dali::Image unselected = Dali::ResourceImage::New( CHECKBOX_UNSELECTED_IMAGE );
Dali::Image selected = Dali::ResourceImage::New( CHECKBOX_SELECTED_IMAGE );
{
Toolkit::CheckBoxButton checkBox = Toolkit::CheckBoxButton::New();
checkBox.SetName( "checkbox1" );
checkBox.SetBackgroundImage( unselected );
checkBox.SetSelectedImage( selected );
checkBox.SetLabel( "CheckBox1 is unselected" );
checkBox.StateChangedSignal().Connect( this, &ButtonsController::OnCheckBoxesSelected );
checkBoxBackground.Add( checkBox );
}
{
Toolkit::CheckBoxButton checkBox = Toolkit::CheckBoxButton::New();
checkBox.SetName( "checkbox2" );
checkBox.SetBackgroundImage( unselected );
checkBox.SetSelectedImage( selected );
checkBox.SetLabel( "CheckBox2 is selected" );
checkBox.SetSelected( true );
checkBox.StateChangedSignal().Connect( this, &ButtonsController::OnCheckBoxesSelected );
checkBoxBackground.Add( checkBox );
}
{
Toolkit::CheckBoxButton checkBox = Toolkit::CheckBoxButton::New();
checkBox.SetName( "checkbox3" );
checkBox.SetBackgroundImage( unselected );
checkBox.SetSelectedImage( selected );
checkBox.SetLabel( "CheckBox3 is unselected" );
checkBox.StateChangedSignal().Connect( this, &ButtonsController::OnCheckBoxesSelected );
checkBoxBackground.Add( checkBox );
}
// Create togglabe button
Toolkit::TableView toggleBackground = Toolkit::TableView::New( 3, 1 );
toggleBackground.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
toggleBackground.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
toggleBackground.SetBackgroundColor( BACKGROUND_COLOUR );
toggleBackground.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE ) );
for( unsigned int i = 0; i < toggleBackground.GetRows(); ++i )
{
toggleBackground.SetFitHeight( i );
}
contentTable.Add( toggleBackground );
Toolkit::PushButton toggleButton = Toolkit::PushButton::New();
toggleButton.SetTogglableButton( true );
toggleButton.SetLabel( "Unselected" );
toggleButton.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
toggleButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
toggleButton.SetSelectedImage( Dali::ResourceImage::New( PUSHBUTTON_PRESS_IMAGE ) );
toggleButton.SetDisabledImage( Dali::ResourceImage::New( PUSHBUTTON_DISABLED_IMAGE ) );
toggleButton.SetButtonImage( Dali::ResourceImage::New( PUSHBUTTON_BUTTON_IMAGE ) );
toggleButton.StateChangedSignal().Connect( this, &ButtonsController::OnButtonSelected );
toggleBackground.Add( toggleButton );
}
void OnKeyEvent( const KeyEvent& event )
{
if( event.state == KeyEvent::Down )
{
if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
{
// Exit application when click back or escape.
mApplication.Quit();
}
}
}
bool OnButtonSelected( Toolkit::Button button )
{
Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( button );
if( pushButton )
{
if( button.IsSelected() )
{
pushButton.SetLabel( "Selected" );
}
else
{
pushButton.SetLabel( "Unselected" );
}
}
return true;
}
bool EnableSelectButton( Toolkit::Button button )
{
if( button.GetName() == "radio-select-enable" && button.IsSelected() == true )
{
mUpdateButton.SetDisabled( false );
}
else if( button.GetName() == "radio-select-disable" && button.IsSelected() == true )
{
mUpdateButton.SetDisabled( true );
}
return true;
}
bool OnButtonClicked(Toolkit::Button button)
{
if( mRadioButtonImage1.IsSelected() )
{
mImage.SetImage( mBigImage1 );
}
else if( mRadioButtonImage2.IsSelected() )
{
mImage.SetImage( mBigImage2 );
}
else if( mRadioButtonImage3.IsSelected() )
{
mImage.SetImage( mBigImage3 );
}
return true;
}
bool OnCheckBoxesSelected( Toolkit::Button button )
{
if( button.GetName() == "checkbox1" )
{
if( button.IsSelected() )
{
button.SetLabel("CheckBox1 is selected");
}
else
{
button.SetLabel("CheckBox1 is unselected");
}
}
if( button.GetName() == "checkbox2" )
{
if( button.IsSelected() )
{
button.SetLabel("CheckBox2 is selected");
}
else
{
button.SetLabel("CheckBox2 is unselected");
}
}
if( button.GetName() == "checkbox3" )
{
if( button.IsSelected() )
{
button.SetLabel("CheckBox3 is selected");
}
else
{
button.SetLabel("CheckBox3 is unselected");
}
}
return true;
}
bool OnTouchEvent( Actor actor, const TouchEvent& event )
{
if( 1u == event.GetPointCount() )
{
const TouchPoint::State state = event.GetPoint(0u).state;
// Clamp to integer values; this is to reduce flicking due to pixel misalignment
const float localPoint = static_cast<float>( static_cast<int>( event.GetPoint( 0 ).local.y ) );
if( TouchPoint::Down == state )
{
mLastPoint = localPoint;
mAnimation = Animation::New( 0.25f );
}
else if( TouchPoint::Motion == state )
{
if( mAnimation )
{
mAnimation.AnimateBy( Property(actor, Actor::Property::POSITION), Vector3( 0.f, localPoint - mLastPoint, 0.f ), AlphaFunction::LINEAR );
mAnimation.Play();
mLastPoint = localPoint;
}
}
}
return true;
}
private:
Application& mApplication;
Toolkit::Control mView; ///< The View instance.
Toolkit::ToolBar mToolBar; ///< The View's Toolbar.
Layer mContentLayer; ///< Content layer
Toolkit::RadioButton mRadioButtonImage1;
Toolkit::RadioButton mRadioButtonImage2;
Toolkit::RadioButton mRadioButtonImage3;
Toolkit::PushButton mUpdateButton;
Animation mAnimation;
float mLastPoint;
Image mBigImage1;
Image mBigImage2;
Image mBigImage3;
ImageActor mImage;
};
void RunTest( Application& application )
{
ButtonsController test( application );
application.MainLoop();
}
// Entry point for Linux & Tizen applications
//
int main( int argc, char **argv )
{
Application application = Application::New( &argc, &argv, DALI_DEMO_THEME_PATH );
RunTest( application );
return 0;
}