2 * Copyright (c) 2020 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>
23 using namespace Dali::Toolkit;
28 // The name we will use to register our custom property by.
29 const char* const TAG_PROPERTY_NAME = "tagIdentifier";
31 // The image for our image view
32 const char* const IMAGE_CARDS = "images/cards.jpg";
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.
40 class PropertyController: public ConnectionTracker
44 PropertyController( Application& application )
46 mTagPropertyIndex( Property::INVALID_INDEX )
48 // Connect to the Application's Init signal
49 application.InitSignal().Connect( this, &PropertyController::Create );
57 void Create( Application& application )
59 // Get the window handle
60 Window window = application.GetWindow();
62 mImageView = ImageView::New();
64 // Set the property to move to the center
65 mImageView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
67 // Set another property to set the image-map
68 Property::Map imageMap;
69 imageMap[ Visual::Property::TYPE ] = Visual::IMAGE;
70 imageMap[ ImageVisual::Property::URL ] = IMAGE_CARDS;
71 imageMap[ ImageVisual::Property::DESIRED_WIDTH ] = 100;
72 imageMap[ ImageVisual::Property::DESIRED_HEIGHT ] = 100;
73 mImageView.SetProperty( ImageView::Property::IMAGE, imageMap );
75 // Add the image view to the window
76 window.Add( mImageView );
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 */ );
81 // Connect to the touch-event
82 mImageView.TouchSignal().Connect( this, &PropertyController::OnTouched );
85 mTagText = Toolkit::TextLabel::New( "0" );
86 mTagText.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER );
87 mTagText.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER );
88 mTagText.SetProperty( TextLabel::Property::TEXT_COLOR, Color::WHITE );
89 mTagText.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
90 window.Add( mTagText );
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
98 bool OnTouched( Actor actor, const TouchEvent& touch )
100 int touchedCount = 0;
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 );
107 // Increment and set back again
109 actor.SetProperty( mTagPropertyIndex, touchedCount );
111 // Set the text in the text-label
112 std::stringstream valueText;
113 valueText << touchedCount;
114 mTagText.SetProperty( TextLabel::Property::TEXT, valueText.str() );
116 return true; // Consumed meaning any gestures will be cancelled
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.
127 // Entry point for applications.
128 int main( int argc, char **argv )
130 Application application = Application::New( &argc, &argv );
132 PropertyController test( application );
133 application.MainLoop();