2 * Copyright (c) 2021 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-toolkit/dali-toolkit.h>
19 #include <dali/dali.h>
23 using namespace Dali::Toolkit;
27 // The name we will use to register our custom property by.
28 const char* const TAG_PROPERTY_NAME = "tagIdentifier";
30 // The image for our image view
31 const char* const IMAGE_CARDS = "images/cards.jpg";
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.
39 class PropertyController : public ConnectionTracker
42 PropertyController(Application& application)
44 mTagPropertyIndex(Property::INVALID_INDEX)
46 // Connect to the Application's Init signal
47 application.InitSignal().Connect(this, &PropertyController::Create);
55 void Create(Application& application)
57 // Get the window handle
58 Window window = application.GetWindow();
60 mImageView = ImageView::New();
62 // Set the property to move to the center
63 mImageView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
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);
73 // Add the image view to the window
74 window.Add(mImageView);
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 */);
79 // Connect to the touch-event
80 mImageView.TouchedSignal().Connect(this, &PropertyController::OnTouched);
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");
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
96 bool OnTouched(Actor actor, const TouchEvent& touch)
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);
105 // Increment and set back again
107 actor.SetProperty(mTagPropertyIndex, touchedCount);
109 // Set the text in the text-label
110 std::stringstream valueText;
111 valueText << touchedCount;
112 mTagText.SetProperty(TextLabel::Property::TEXT, valueText.str());
114 return true; // Consumed meaning any gestures will be cancelled
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.
124 // Entry point for applications.
125 int main(int argc, char** argv)
127 Application application = Application::New(&argc, &argv);
129 PropertyController test(application);
130 application.MainLoop();