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