[Tizen] Add AnimatedVectorImageVisualRive example
[platform/core/uifw/dali-demo.git] / examples / animated-vector-images-rive / animated-vector-images-rive-example.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-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 <string>
26 #include "shared/view.h"
27
28 using namespace Dali;
29 using namespace Dali::Toolkit;
30
31 namespace
32 {
33 const char* BACKGROUND_IMAGE(DEMO_IMAGE_DIR "background-gradient.jpg");
34 const char* TOOLBAR_IMAGE(DEMO_IMAGE_DIR "top-bar.png");
35 const char* APPLICATION_TITLE("Animated Vector Images Rive");
36
37 const char* IMAGE_PATH[] = {
38   DEMO_IMAGE_DIR "juice.riv",
39   };
40
41 const unsigned int NUMBER_OF_IMAGES = 1;
42
43 enum CellPlacement
44 {
45   TOP_BUTTON,
46   LOWER_BUTTON,
47   IMAGE,
48   NUMBER_OF_ROWS
49 };
50
51 unsigned int GetControlIndex(Control control)
52 {
53   std::string  controlName = control.GetProperty<std::string>(Dali::Actor::Property::NAME);
54   unsigned int index       = 0;
55
56   if(controlName != "")
57   {
58     index = std::stoul(controlName);
59   }
60
61   return index;
62 }
63
64 } // namespace
65
66 // This example shows the usage of AnimatedVectorImageVisual(Rive).
67 // It doesn't work on Ubuntu because the visual uses the external library to render frames.
68 class AnimatedVectorImageViewController : public ConnectionTracker
69 {
70 public:
71   AnimatedVectorImageViewController(Application& application)
72   : mApplication(application)
73   {
74     // Connect to the Application's Init signal
75     mApplication.InitSignal().Connect(this, &AnimatedVectorImageViewController::Create);
76   }
77
78   void Create(Application& application)
79   {
80     // The Init signal is received once (only) during the Application lifetime
81
82     // Creates a default view with a default tool bar.
83     // The view is added to the window.
84     mContentLayer = DemoHelper::CreateView(application,
85                                            mView,
86                                            mToolBar,
87                                            BACKGROUND_IMAGE,
88                                            TOOLBAR_IMAGE,
89                                            APPLICATION_TITLE);
90
91     // Create a table view to show a pair of buttons above each image.
92     mTable = TableView::New(CellPlacement::NUMBER_OF_ROWS, NUMBER_OF_IMAGES);
93     mTable.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
94     mTable.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
95     mTable.SetResizePolicy(ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS);
96     Vector3 offset(0.9f, 0.70f, 0.0f);
97     mTable.SetProperty(Actor::Property::SIZE_MODE_FACTOR, offset);
98     mTable.SetFitHeight(CellPlacement::TOP_BUTTON);
99     mTable.SetFitHeight(CellPlacement::LOWER_BUTTON);
100     mContentLayer.Add(mTable);
101
102     for(unsigned int x = 0; x < NUMBER_OF_IMAGES; x++)
103     {
104       mPlayButtons[x] = PushButton::New();
105       mPlayButtons[x].SetProperty(Button::Property::LABEL, "Play");
106       mPlayButtons[x].SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER);
107       mPlayButtons[x].SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER);
108       mPlayButtons[x].ClickedSignal().Connect(this, &AnimatedVectorImageViewController::OnPlayButtonClicked);
109       mPlayButtons[x].SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
110       mPlayButtons[x].SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
111       std::string s = std::to_string(x);
112       mPlayButtons[x].SetProperty(Dali::Actor::Property::NAME, s);
113       mTable.AddChild(mPlayButtons[x], TableView::CellPosition(CellPlacement::TOP_BUTTON, x));
114
115       mStopButtons[x] = PushButton::New();
116       mStopButtons[x].SetProperty(Button::Property::LABEL, "Stop");
117       mStopButtons[x].SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER);
118       mStopButtons[x].SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER);
119       mStopButtons[x].ClickedSignal().Connect(this, &AnimatedVectorImageViewController::OnStopButtonClicked);
120       mStopButtons[x].SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
121       mStopButtons[x].SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
122       mStopButtons[x].SetProperty(Dali::Actor::Property::NAME, s);
123       mTable.AddChild(mStopButtons[x], TableView::CellPosition(CellPlacement::LOWER_BUTTON, x));
124
125       mImageViews[x] = ImageView::New();
126       Property::Map imagePropertyMap;
127       imagePropertyMap.Insert(Visual::Property::TYPE, Visual::IMAGE);
128       imagePropertyMap.Insert(ImageVisual::Property::URL, IMAGE_PATH[x]);
129       imagePropertyMap.Insert(DevelImageVisual::Property::LOOP_COUNT, 3);
130       mImageViews[x].SetProperty(ImageView::Property::IMAGE, imagePropertyMap);
131
132       mImageViews[x].SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
133       mImageViews[x].SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
134       mImageViews[x].SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
135       mImageViews[x].SetProperty(Dali::Actor::Property::NAME, s);
136
137       DevelControl::VisualEventSignal(mImageViews[x]).Connect(this, &AnimatedVectorImageViewController::OnVisualEvent);
138
139       mTable.AddChild(mImageViews[x], TableView::CellPosition(CellPlacement::IMAGE, x));
140     }
141
142     application.GetWindow().KeyEventSignal().Connect(this, &AnimatedVectorImageViewController::OnKeyEvent);
143   }
144
145 private:
146   bool OnPlayButtonClicked(Button button)
147   {
148     unsigned int controlIndex = GetControlIndex(button);
149
150     ImageView imageView = mImageViews[controlIndex];
151
152     Property::Map    map   = imageView.GetProperty<Property::Map>(ImageView::Property::IMAGE);
153     Property::Value* value = map.Find(DevelImageVisual::Property::PLAY_STATE);
154
155     if(value)
156     {
157       if(value->Get<int>() != static_cast<int>(DevelImageVisual::PlayState::PLAYING))
158       {
159         mPlayButtons[controlIndex].SetProperty(Button::Property::LABEL, "Pause");
160
161         DevelControl::DoAction(imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::PLAY, Property::Value());
162       }
163       else
164       {
165         mPlayButtons[controlIndex].SetProperty(Button::Property::LABEL, "Play");
166
167         DevelControl::DoAction(imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::PAUSE, Property::Value());
168       }
169     }
170
171     return true;
172   }
173
174   bool OnStopButtonClicked(Button button)
175   {
176     unsigned int controlIndex = GetControlIndex(button);
177     ImageView    imageView    = mImageViews[controlIndex];
178     DevelControl::DoAction(imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::STOP, Property::Value());
179
180     return true;
181   }
182
183   void OnVisualEvent(Control control, Dali::Property::Index visualIndex, Dali::Property::Index signalId)
184   {
185     unsigned int controlIndex = GetControlIndex(control);
186
187     if(visualIndex == ImageView::Property::IMAGE && signalId == DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED)
188     {
189       mPlayButtons[controlIndex].SetProperty(Button::Property::LABEL, "Play");
190     }
191   }
192
193   /**
194    * Main key event handler
195    */
196   void OnKeyEvent(const KeyEvent& event)
197   {
198     if(event.GetState() == KeyEvent::DOWN)
199     {
200       if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
201       {
202         mApplication.Quit();
203       }
204     }
205   }
206
207 private:
208   Application& mApplication;
209
210   Control    mView;         ///< The View instance.
211   ToolBar    mToolBar;      ///< The View's Toolbar.
212   Layer      mContentLayer; ///< Content layer
213   TableView  mTable;
214   ImageView  mImageViews[NUMBER_OF_IMAGES];
215   PushButton mPlayButtons[NUMBER_OF_IMAGES];
216   PushButton mStopButtons[NUMBER_OF_IMAGES];
217 };
218
219 int DALI_EXPORT_API main(int argc, char** argv)
220 {
221   Application                       application = Application::New(&argc, &argv, DEMO_THEME_PATH);
222   AnimatedVectorImageViewController test(application);
223   application.MainLoop();
224   return 0;
225 }