(Touch) Updated programming guide, Automated Tests & KeyboardFocusManager to use...
[platform/core/uifw/dali-toolkit.git] / docs / content / example-code / properties.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 using namespace Dali::Toolkit;
24
25 namespace
26 {
27
28 // The name we will use to register our custom property by.
29 const char* const TAG_PROPERTY_NAME = "tagIdentifier";
30
31 // The image for our image view
32 const char* const IMAGE_CARDS = "images/cards.jpg";
33 }  // namespace
34
35 /**
36  * This example shows how to set properties in C++ and how to register and look-up custom properties.
37  * An image is added to the screen which changes and a custom property is added to the image-view.
38  * This value is incremented every time the image is touched and the text-label is updated.
39  */
40 class PropertyController: public ConnectionTracker
41 {
42 public:
43
44   PropertyController( Application& application )
45   : mTagText(),
46     mTagPropertyIndex( Property::INVALID_INDEX )
47   {
48     // Connect to the Application's Init signal
49     application.InitSignal().Connect( this, &PropertyController::Create );
50   }
51
52   ~PropertyController()
53   {
54   }
55
56   // C++ EXAMPLE
57   void Create( Application& application )
58   {
59     // Get the stage handle
60     Stage stage = Stage::GetCurrent();
61
62     mImageView = ImageView::New();
63
64     // Set the property to move to the center
65     mImageView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
66
67     // Set another property to set the image-map
68     Property::Map imageMap;
69     imageMap[ "rendererType" ] = "image";
70     imageMap[ "url" ]          = IMAGE_CARDS;
71     imageMap[ "desiredWidth" ]        = 100;
72     imageMap[ "desiredHeight" ]       = 100;
73     mImageView.SetProperty( ImageView::Property::IMAGE, imageMap );
74
75     // Add the image view to the stage
76     stage.Add( mImageView );
77
78     // Register a custom float property on mImageView and use it to store the number of times we are tapped
79     mTagPropertyIndex = mImageView.RegisterProperty( TAG_PROPERTY_NAME, 0, Property::READ_WRITE /* Event-side only, i.e. not animatable */ );
80
81     // Connect to the touch-event
82     mImageView.TouchSignal().Connect( this, &PropertyController::OnTouched );
83
84     // Create text label
85     mTagText = Toolkit::TextLabel::New( "0" );
86     mTagText.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
87     mTagText.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
88     mTagText.SetProperty( TextLabel::Property::TEXT_COLOR, Color::WHITE );
89     mTagText.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
90     stage.Add( mTagText );
91   }
92
93   /**
94    * Called when the image view is touched
95    * param[in] touch The touch-event
96    * return Set to true if the signal was consumed correctly
97    */
98   bool OnTouched( Actor actor, const TouchData& touch )
99   {
100     int touchedCount = 0;
101
102     // Look up the tag property by the cached property index.
103     // 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:
104     // Property::Index index = actor.GetPropertyIndex( TAG_PROPERTY_NAME );
105     actor.GetProperty( mTagPropertyIndex ).Get( touchedCount );
106
107     // Increment and set back again
108     ++touchedCount;
109     actor.SetProperty( mTagPropertyIndex, touchedCount );
110
111     // Set the text in the text-label
112     std::stringstream valueText;
113     valueText << touchedCount;
114     mTagText.SetProperty( TextLabel::Property::TEXT, valueText.str() );
115
116     return true; // Consumed
117   }
118   // C++ EXAMPLE END
119
120 private:
121
122   ImageView mImageView;              ///< An image view to show an image
123   TextLabel mTagText;                 ///< A text label used to show the last button pressed.
124   Property::Index mTagPropertyIndex;  ///< A cached property index of our custom tag property.
125 };
126
127 // Entry point for applications.
128 int main( int argc, char **argv )
129 {
130   Application application = Application::New( &argc, &argv );
131
132   PropertyController test( application );
133   application.MainLoop();
134
135   return 0;
136 }