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 <dali/dali.h>
19 #include <dali-toolkit/dali-toolkit.h>
27 // The name we will use to register our custom property by.
28 const char* const TAG_PROPERTY_NAME = "tagIdentifier";
30 const char* const PUSHBUTTON_PRESS_IMAGE = DALI_IMAGE_DIR "button-down.9.png";
31 const char* const PUSHBUTTON_BUTTON_IMAGE = DALI_IMAGE_DIR "button-up.9.png";
33 // Define the grid geometry.
34 #define BUTTON_ROWS 9.0f
35 #define BUTTON_COLUMNS 7.0f
36 #define BUTTON_GAP 10.0f
41 * This example shows how to register and look-up custom properties.
42 * A button grid is created, each with a new "tag" property which is set to a unique value.
43 * When pressed, the "tag" property is looked up to retrieve the unique value and display it.
45 class PropertyButtonsController: public ConnectionTracker
49 PropertyButtonsController( Application& application )
51 // Connect to the Application's Init signal
52 application.InitSignal().Connect( this, &PropertyButtonsController::Create );
55 ~PropertyButtonsController()
59 void Create( Application& application )
61 // Setup precalculations for button size and start positions.
62 Toolkit::PushButton button;
64 Vector2 stageSize = Stage::GetCurrent().GetSize();
65 float buttonSize = ( stageSize.x - ( BUTTON_GAP * ( BUTTON_COLUMNS + 1 ) ) ) / BUTTON_COLUMNS;
66 float yStart = ( stageSize.y - ( ( buttonSize * BUTTON_ROWS ) + ( BUTTON_GAP * ( BUTTON_ROWS - 1 ) ) ) ) / 2.0f;
68 // Create a grid of buttons.
69 for( int y = 0; y < BUTTON_ROWS; ++y )
71 for( int x = 0; x < BUTTON_COLUMNS; ++x )
73 // Create a button and position it.
74 button = Toolkit::PushButton::New();
75 button.SetParentOrigin( ParentOrigin::TOP_LEFT );
76 button.SetAnchorPoint( AnchorPoint::TOP_LEFT );
77 button.SetPosition( Vector3( BUTTON_GAP + ( x * ( buttonSize + BUTTON_GAP ) ), yStart + ( y * ( buttonSize + BUTTON_GAP ) ), 0.0f ) );
78 button.SetSize( Vector3( buttonSize, buttonSize, 0) );
79 button.SetSelectedImage( Dali::ResourceImage::New( PUSHBUTTON_PRESS_IMAGE ) );
80 button.SetButtonImage( Dali::ResourceImage::New( PUSHBUTTON_BUTTON_IMAGE ) );
82 // Label the button with a unique value.
83 std::stringstream label;
85 button.SetLabel( label.str() );
87 // Register our custom property, and use it to store a unique number.
88 // Store the index to the property so we can look it up later.
89 // Note: This is much faster than looking the property up by name and should always be used if possible.
90 // As all our control types are the same (PushButtons) the indecies to our unique property is the same for each one.
91 Property::Value tag = ( float )index;
92 mTagPropertyIndex = button.RegisterProperty( TAG_PROPERTY_NAME, tag );
94 // Hook a callback when the button is clicked.
95 button.ClickedSignal().Connect( this, &PropertyButtonsController::OnButtonClicked );
97 // Add the button to the stage.
98 Stage::GetCurrent().Add( button );
103 // Create the last selected button text view.
104 mTagText = Toolkit::TextLabel::New( "None selected" );
105 mTagText.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
106 mTagText.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
107 mTagText.SetPosition( Vector3( 0.0f, -30.0f, 0.0f ) );
108 Stage::GetCurrent().Add( mTagText );
112 * Called when any button within the grid is clicked.
113 * param[in] The clicked button control
114 * return Set to true if the signal was consumed correctly
116 bool OnButtonClicked( Toolkit::Button button )
118 std::stringstream valueText;
119 // Look up the tag property by the cached property index.
120 // Note: If the property belongs to a control in another library, or we do not know the index, we can look the index up first with:
121 // Property::Index index = button.GetPropertyIndex( TAG_PROPERTY_NAME );
122 valueText << "Selected: " << button.GetProperty< float >( mTagPropertyIndex );
124 mTagText.SetProperty( TextLabel::Property::TEXT, valueText.str() );
131 Toolkit::TextLabel mTagText; ///< A text label used to show the last button pressed.
132 Property::Index mTagPropertyIndex; ///< A cached property index of our custom tag property.
135 // Entry point for applications.
136 int main( int argc, char **argv )
138 Application application = Application::New( &argc, &argv );
140 PropertyButtonsController test( application );
141 application.MainLoop();