2 * Copyright (c) 2022 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-toolkit/devel-api/controls/control-devel.h>
20 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
21 #include <dali-toolkit/devel-api/visuals/animated-vector-image-visual-actions-devel.h>
22 #include <dali-toolkit/devel-api/visuals/animated-vector-image-visual-signals-devel.h>
23 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
24 #include <dali/dali.h>
25 #include <dali/devel-api/adaptor-framework/vector-animation-renderer.h>
27 #include "shared/view.h"
30 using namespace Dali::Toolkit;
34 const char* BACKGROUND_IMAGE(DEMO_IMAGE_DIR "background-gradient.jpg");
35 const char* TOOLBAR_IMAGE(DEMO_IMAGE_DIR "top-bar.png");
36 const char* APPLICATION_TITLE("Animated Vector Images");
38 const char* IMAGE_PATH[] = {
39 DEMO_IMAGE_DIR "done.json",
40 DEMO_IMAGE_DIR "insta_camera.json",
41 DEMO_IMAGE_DIR "jolly_walker.json"};
43 const unsigned int NUMBER_OF_IMAGES = 3;
53 unsigned int GetControlIndex(Control control)
55 std::string controlName = control.GetProperty<std::string>(Dali::Actor::Property::NAME);
56 unsigned int index = 0;
60 index = std::stoul(controlName);
68 // This example shows the usage of AnimatedVectorImageVisual.
69 // It doesn't work on Ubuntu because the visual uses the external library to render frames.
70 class AnimatedVectorImageViewController : public ConnectionTracker
73 AnimatedVectorImageViewController(Application& application)
74 : mApplication(application)
76 // Connect to the Application's Init signal
77 mApplication.InitSignal().Connect(this, &AnimatedVectorImageViewController::Create);
80 void Create(Application& application)
82 // The Init signal is received once (only) during the Application lifetime
84 // Creates a default view with a default tool bar.
85 // The view is added to the window.
86 mContentLayer = DemoHelper::CreateView(application,
93 // Create a table view to show a pair of buttons above each image.
94 mTable = TableView::New(CellPlacement::NUMBER_OF_ROWS, NUMBER_OF_IMAGES);
95 mTable.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
96 mTable.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
97 mTable.SetResizePolicy(ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS);
98 Vector3 offset(0.9f, 0.70f, 0.0f);
99 mTable.SetProperty(Actor::Property::SIZE_MODE_FACTOR, offset);
100 mTable.SetFitHeight(CellPlacement::TOP_BUTTON);
101 mTable.SetFitHeight(CellPlacement::LOWER_BUTTON);
102 mContentLayer.Add(mTable);
104 for(unsigned int x = 0; x < NUMBER_OF_IMAGES; x++)
106 mPlayButtons[x] = PushButton::New();
107 mPlayButtons[x].SetProperty(Button::Property::LABEL, "Play");
108 mPlayButtons[x].SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER);
109 mPlayButtons[x].SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER);
110 mPlayButtons[x].ClickedSignal().Connect(this, &AnimatedVectorImageViewController::OnPlayButtonClicked);
111 mPlayButtons[x].SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
112 mPlayButtons[x].SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
113 std::string s = std::to_string(x);
114 mPlayButtons[x].SetProperty(Dali::Actor::Property::NAME, s);
115 mTable.AddChild(mPlayButtons[x], TableView::CellPosition(CellPlacement::TOP_BUTTON, x));
117 mStopButtons[x] = PushButton::New();
118 mStopButtons[x].SetProperty(Button::Property::LABEL, "Stop");
119 mStopButtons[x].SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER);
120 mStopButtons[x].SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER);
121 mStopButtons[x].ClickedSignal().Connect(this, &AnimatedVectorImageViewController::OnStopButtonClicked);
122 mStopButtons[x].SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
123 mStopButtons[x].SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
124 mStopButtons[x].SetProperty(Dali::Actor::Property::NAME, s);
125 mTable.AddChild(mStopButtons[x], TableView::CellPosition(CellPlacement::LOWER_BUTTON, x));
127 mImageViews[x] = ImageView::New();
128 Property::Map imagePropertyMap;
129 imagePropertyMap.Insert(Visual::Property::TYPE, Visual::IMAGE);
130 imagePropertyMap.Insert(ImageVisual::Property::URL, IMAGE_PATH[x]);
131 imagePropertyMap.Insert(DevelImageVisual::Property::LOOP_COUNT, 3);
132 mImageViews[x].SetProperty(ImageView::Property::IMAGE, imagePropertyMap);
134 mImageViews[x].SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
135 mImageViews[x].SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
136 mImageViews[x].SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
137 mImageViews[x].SetProperty(Dali::Actor::Property::NAME, s);
139 DevelControl::VisualEventSignal(mImageViews[x]).Connect(this, &AnimatedVectorImageViewController::OnVisualEvent);
141 mTable.AddChild(mImageViews[x], TableView::CellPosition(CellPlacement::IMAGE, x));
144 DevelAnimatedVectorImageVisual::DynamicPropertyInfo info;
146 info.keyPath = "Shape Layer 1.Ellipse 1.Fill 1";
147 info.property = static_cast<int>(VectorAnimationRenderer::VectorProperty::FILL_COLOR);
148 info.callback = MakeCallback(this, &AnimatedVectorImageViewController::FillColorCallback);
150 DevelControl::DoActionExtension(mImageViews[0], ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::SET_DYNAMIC_PROPERTY, Any(info));
154 info.property = static_cast<int>(VectorAnimationRenderer::VectorProperty::STROKE_COLOR);
155 info.callback = MakeCallback(this, &AnimatedVectorImageViewController::StrokeColorCallback);
157 DevelControl::DoActionExtension(mImageViews[0], ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::SET_DYNAMIC_PROPERTY, Any(info));
161 info.property = static_cast<int>(VectorAnimationRenderer::VectorProperty::STROKE_WIDTH);
162 info.callback = MakeCallback(this, &AnimatedVectorImageViewController::StrokeWidthCallback);
164 DevelControl::DoActionExtension(mImageViews[0], ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::SET_DYNAMIC_PROPERTY, Any(info));
167 info.keyPath = "Shape Layer 2.Shape 1";
168 info.property = static_cast<int>(VectorAnimationRenderer::VectorProperty::TRANSFORM_ROTATION);
169 info.callback = MakeCallback(this, &AnimatedVectorImageViewController::TransformRotationCallback);
171 DevelControl::DoActionExtension(mImageViews[0], ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::SET_DYNAMIC_PROPERTY, Any(info));
173 application.GetWindow().KeyEventSignal().Connect(this, &AnimatedVectorImageViewController::OnKeyEvent);
177 Property::Value FillColorCallback(int32_t id, VectorAnimationRenderer::VectorProperty property, uint32_t frameNumber)
181 return Vector3(0, 0, 1);
185 return Vector3(1, 0, 0);
189 Property::Value StrokeColorCallback(int32_t id, VectorAnimationRenderer::VectorProperty property, uint32_t frameNumber)
193 return Vector3(1, 0, 1);
197 return Vector3(1, 1, 0);
201 Property::Value StrokeWidthCallback(int32_t id, VectorAnimationRenderer::VectorProperty property, uint32_t frameNumber)
213 Property::Value TransformRotationCallback(int32_t id, VectorAnimationRenderer::VectorProperty property, uint32_t frameNumber)
215 return frameNumber * 20.0f;
218 bool OnPlayButtonClicked(Button button)
220 unsigned int controlIndex = GetControlIndex(button);
222 ImageView imageView = mImageViews[controlIndex];
224 Property::Map map = imageView.GetProperty<Property::Map>(ImageView::Property::IMAGE);
225 Property::Value* value = map.Find(DevelImageVisual::Property::PLAY_STATE);
229 if(value->Get<int>() != static_cast<int>(DevelImageVisual::PlayState::PLAYING))
231 mPlayButtons[controlIndex].SetProperty(Button::Property::LABEL, "Pause");
233 DevelControl::DoAction(imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::PLAY, Property::Value());
237 mPlayButtons[controlIndex].SetProperty(Button::Property::LABEL, "Play");
239 DevelControl::DoAction(imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::PAUSE, Property::Value());
246 bool OnStopButtonClicked(Button button)
248 unsigned int controlIndex = GetControlIndex(button);
249 ImageView imageView = mImageViews[controlIndex];
250 DevelControl::DoAction(imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::STOP, Property::Value());
255 void OnVisualEvent(Control control, Dali::Property::Index visualIndex, Dali::Property::Index signalId)
257 unsigned int controlIndex = GetControlIndex(control);
259 if(visualIndex == ImageView::Property::IMAGE && signalId == DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED)
261 mPlayButtons[controlIndex].SetProperty(Button::Property::LABEL, "Play");
266 * Main key event handler
268 void OnKeyEvent(const KeyEvent& event)
270 if(event.GetState() == KeyEvent::DOWN)
272 if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
280 Application& mApplication;
282 Control mView; ///< The View instance.
283 ToolBar mToolBar; ///< The View's Toolbar.
284 Layer mContentLayer; ///< Content layer
286 ImageView mImageViews[NUMBER_OF_IMAGES];
287 PushButton mPlayButtons[NUMBER_OF_IMAGES];
288 PushButton mStopButtons[NUMBER_OF_IMAGES];
291 int DALI_EXPORT_API main(int argc, char** argv)
293 Application application = Application::New(&argc, &argv, DEMO_THEME_PATH);
294 AnimatedVectorImageViewController test(application);
295 application.MainLoop();