(Vector) Use dynamic property
[platform/core/uifw/dali-demo.git] / examples / animated-vector-images / animated-vector-images-example.cpp
1 /*
2  * Copyright (c) 2022 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-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>
26 #include <string>
27 #include "shared/view.h"
28
29 using namespace Dali;
30 using namespace Dali::Toolkit;
31
32 namespace
33 {
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");
37
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"};
42
43 const unsigned int NUMBER_OF_IMAGES = 3;
44
45 enum CellPlacement
46 {
47   TOP_BUTTON,
48   LOWER_BUTTON,
49   IMAGE,
50   NUMBER_OF_ROWS
51 };
52
53 unsigned int GetControlIndex(Control control)
54 {
55   std::string  controlName = control.GetProperty<std::string>(Dali::Actor::Property::NAME);
56   unsigned int index       = 0;
57
58   if(controlName != "")
59   {
60     index = std::stoul(controlName);
61   }
62
63   return index;
64 }
65
66 } // namespace
67
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
71 {
72 public:
73   AnimatedVectorImageViewController(Application& application)
74   : mApplication(application)
75   {
76     // Connect to the Application's Init signal
77     mApplication.InitSignal().Connect(this, &AnimatedVectorImageViewController::Create);
78   }
79
80   void Create(Application& application)
81   {
82     // The Init signal is received once (only) during the Application lifetime
83
84     // Creates a default view with a default tool bar.
85     // The view is added to the window.
86     mContentLayer = DemoHelper::CreateView(application,
87                                            mView,
88                                            mToolBar,
89                                            BACKGROUND_IMAGE,
90                                            TOOLBAR_IMAGE,
91                                            APPLICATION_TITLE);
92
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);
103
104     for(unsigned int x = 0; x < NUMBER_OF_IMAGES; x++)
105     {
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));
116
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));
126
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);
133
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);
138
139       DevelControl::VisualEventSignal(mImageViews[x]).Connect(this, &AnimatedVectorImageViewController::OnVisualEvent);
140
141       mTable.AddChild(mImageViews[x], TableView::CellPosition(CellPlacement::IMAGE, x));
142     }
143
144     DevelAnimatedVectorImageVisual::DynamicPropertyInfo info;
145     info.id       = 1;
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);
149
150     DevelControl::DoActionExtension(mImageViews[0], ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::SET_DYNAMIC_PROPERTY, Any(info));
151
152     info.id       = 2;
153     info.keyPath  = "**";
154     info.property = static_cast<int>(VectorAnimationRenderer::VectorProperty::STROKE_COLOR);
155     info.callback = MakeCallback(this, &AnimatedVectorImageViewController::StrokeColorCallback);
156
157     DevelControl::DoActionExtension(mImageViews[0], ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::SET_DYNAMIC_PROPERTY, Any(info));
158
159     info.id       = 3;
160     info.keyPath  = "**";
161     info.property = static_cast<int>(VectorAnimationRenderer::VectorProperty::STROKE_WIDTH);
162     info.callback = MakeCallback(this, &AnimatedVectorImageViewController::StrokeWidthCallback);
163
164     DevelControl::DoActionExtension(mImageViews[0], ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::SET_DYNAMIC_PROPERTY, Any(info));
165
166     info.id       = 4;
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);
170
171     DevelControl::DoActionExtension(mImageViews[0], ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::SET_DYNAMIC_PROPERTY, Any(info));
172
173     application.GetWindow().KeyEventSignal().Connect(this, &AnimatedVectorImageViewController::OnKeyEvent);
174   }
175
176 private:
177   Property::Value FillColorCallback(int32_t id, VectorAnimationRenderer::VectorProperty property, uint32_t frameNumber)
178   {
179     if(frameNumber < 60)
180     {
181       return Vector3(0, 0, 1);
182     }
183     else
184     {
185       return Vector3(1, 0, 0);
186     }
187   }
188
189   Property::Value StrokeColorCallback(int32_t id, VectorAnimationRenderer::VectorProperty property, uint32_t frameNumber)
190   {
191     if(frameNumber < 60)
192     {
193       return Vector3(1, 0, 1);
194     }
195     else
196     {
197       return Vector3(1, 1, 0);
198     }
199   }
200
201   Property::Value StrokeWidthCallback(int32_t id, VectorAnimationRenderer::VectorProperty property, uint32_t frameNumber)
202   {
203     if(frameNumber < 60)
204     {
205       return 2.0f;
206     }
207     else
208     {
209       return 5.0f;
210     }
211   }
212
213   Property::Value TransformRotationCallback(int32_t id, VectorAnimationRenderer::VectorProperty property, uint32_t frameNumber)
214   {
215     return frameNumber * 20.0f;
216   }
217
218   bool OnPlayButtonClicked(Button button)
219   {
220     unsigned int controlIndex = GetControlIndex(button);
221
222     ImageView imageView = mImageViews[controlIndex];
223
224     Property::Map    map   = imageView.GetProperty<Property::Map>(ImageView::Property::IMAGE);
225     Property::Value* value = map.Find(DevelImageVisual::Property::PLAY_STATE);
226
227     if(value)
228     {
229       if(value->Get<int>() != static_cast<int>(DevelImageVisual::PlayState::PLAYING))
230       {
231         mPlayButtons[controlIndex].SetProperty(Button::Property::LABEL, "Pause");
232
233         DevelControl::DoAction(imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::PLAY, Property::Value());
234       }
235       else
236       {
237         mPlayButtons[controlIndex].SetProperty(Button::Property::LABEL, "Play");
238
239         DevelControl::DoAction(imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::PAUSE, Property::Value());
240       }
241     }
242
243     return true;
244   }
245
246   bool OnStopButtonClicked(Button button)
247   {
248     unsigned int controlIndex = GetControlIndex(button);
249     ImageView    imageView    = mImageViews[controlIndex];
250     DevelControl::DoAction(imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::STOP, Property::Value());
251
252     return true;
253   }
254
255   void OnVisualEvent(Control control, Dali::Property::Index visualIndex, Dali::Property::Index signalId)
256   {
257     unsigned int controlIndex = GetControlIndex(control);
258
259     if(visualIndex == ImageView::Property::IMAGE && signalId == DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED)
260     {
261       mPlayButtons[controlIndex].SetProperty(Button::Property::LABEL, "Play");
262     }
263   }
264
265   /**
266    * Main key event handler
267    */
268   void OnKeyEvent(const KeyEvent& event)
269   {
270     if(event.GetState() == KeyEvent::DOWN)
271     {
272       if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
273       {
274         mApplication.Quit();
275       }
276     }
277   }
278
279 private:
280   Application& mApplication;
281
282   Control    mView;         ///< The View instance.
283   ToolBar    mToolBar;      ///< The View's Toolbar.
284   Layer      mContentLayer; ///< Content layer
285   TableView  mTable;
286   ImageView  mImageViews[NUMBER_OF_IMAGES];
287   PushButton mPlayButtons[NUMBER_OF_IMAGES];
288   PushButton mStopButtons[NUMBER_OF_IMAGES];
289 };
290
291 int DALI_EXPORT_API main(int argc, char** argv)
292 {
293   Application                       application = Application::New(&argc, &argv, DEMO_THEME_PATH);
294   AnimatedVectorImageViewController test(application);
295   application.MainLoop();
296   return 0;
297 }