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