Text-related Programming guide updates
[platform/core/uifw/dali-toolkit.git] / docs / content / example-code / property-example.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <dali/dali.h>
19 #include <dali-toolkit/dali-toolkit.h>
20 #include <sstream>
21
22 using namespace Dali;
23
24 namespace
25 {
26
27 // The name we will use to register our custom property by.
28 const char* const TAG_PROPERTY_NAME = "tag-identifier";
29
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";
32
33 // Define the grid geometry.
34 #define BUTTON_ROWS    9.0f
35 #define BUTTON_COLUMNS 7.0f
36 #define BUTTON_GAP     10.0f
37
38 }  // namespace
39
40 /**
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.
44  */
45 class PropertyButtonsController: public ConnectionTracker
46 {
47   public:
48
49   PropertyButtonsController( Application& application )
50   {
51     // Connect to the Application's Init signal
52     application.InitSignal().Connect( this, &PropertyButtonsController::Create );
53   }
54
55   ~PropertyButtonsController()
56   {
57   }
58
59   void Create( Application& application )
60   {
61     // Setup precalculations for button size and start positions.
62     Toolkit::PushButton button;
63     int index = 0;
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;
67
68     // Create a grid of buttons.
69     for( int y = 0; y < BUTTON_ROWS; ++y )
70     {
71       for( int x = 0; x < BUTTON_COLUMNS; ++x )
72       {
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 ) );
81
82         // Label the button with a unique value.
83         std::stringstream label;
84         label << index;
85         button.SetLabel( label.str() );
86
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 );
93
94         // Hook a callback when the button is clicked.
95         button.ClickedSignal().Connect( this, &PropertyButtonsController::OnButtonClicked );
96
97         // Add the button to the stage.
98         Stage::GetCurrent().Add( button );
99         index++;
100       }
101     }
102
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 );
109   }
110
111   /**
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
115    */
116   bool OnButtonClicked( Toolkit::Button button )
117   {
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 );
123
124     mTagText.SetProperty( TextLabel::Property::TEXT, valueText.str() );
125
126     return true;
127   }
128
129   private:
130
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.
133 };
134
135 // Entry point for applications.
136 int main( int argc, char **argv )
137 {
138   Application application = Application::New( &argc, &argv );
139
140   PropertyButtonsController test( application );
141   application.MainLoop();
142
143   return 0;
144 }