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-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>
26 #include "shared/view.h"
29 using namespace Dali::Toolkit;
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");
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"};
42 const unsigned int NUMBER_OF_IMAGES = 3;
52 unsigned int GetControlIndex(Control control)
54 std::string controlName = control.GetProperty<std::string>(Dali::Actor::Property::NAME);
55 unsigned int index = 0;
59 index = std::stoul(controlName);
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
72 AnimatedVectorImageViewController(Application& application)
73 : mApplication(application)
75 // Connect to the Application's Init signal
76 mApplication.InitSignal().Connect(this, &AnimatedVectorImageViewController::Create);
79 void Create(Application& application)
81 // The Init signal is received once (only) during the Application lifetime
83 // Creates a default view with a default tool bar.
84 // The view is added to the window.
85 mContentLayer = DemoHelper::CreateView(application,
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);
103 for(unsigned int x = 0; x < NUMBER_OF_IMAGES; x++)
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));
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));
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);
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);
138 DevelControl::VisualEventSignal(mImageViews[x]).Connect(this, &AnimatedVectorImageViewController::OnVisualEvent);
140 mTable.AddChild(mImageViews[x], TableView::CellPosition(CellPlacement::IMAGE, x));
143 application.GetWindow().KeyEventSignal().Connect(this, &AnimatedVectorImageViewController::OnKeyEvent);
147 bool OnPlayButtonClicked(Button button)
149 unsigned int controlIndex = GetControlIndex(button);
151 ImageView imageView = mImageViews[controlIndex];
153 Property::Map map = imageView.GetProperty<Property::Map>(ImageView::Property::IMAGE);
154 Property::Value* value = map.Find(DevelImageVisual::Property::PLAY_STATE);
158 if(value->Get<int>() != static_cast<int>(DevelImageVisual::PlayState::PLAYING))
160 mPlayButtons[controlIndex].SetProperty(Button::Property::LABEL, "Pause");
162 DevelControl::DoAction(imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::PLAY, Property::Value());
166 mPlayButtons[controlIndex].SetProperty(Button::Property::LABEL, "Play");
168 DevelControl::DoAction(imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::PAUSE, Property::Value());
175 bool OnStopButtonClicked(Button button)
177 unsigned int controlIndex = GetControlIndex(button);
178 ImageView imageView = mImageViews[controlIndex];
179 DevelControl::DoAction(imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::STOP, Property::Value());
184 void OnVisualEvent(Control control, Dali::Property::Index visualIndex, Dali::Property::Index signalId)
186 unsigned int controlIndex = GetControlIndex(control);
188 if(visualIndex == ImageView::Property::IMAGE && signalId == DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED)
190 mPlayButtons[controlIndex].SetProperty(Button::Property::LABEL, "Play");
195 * Main key event handler
197 void OnKeyEvent(const KeyEvent& event)
199 if(event.GetState() == KeyEvent::DOWN)
201 if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
209 Application& mApplication;
211 Control mView; ///< The View instance.
212 ToolBar mToolBar; ///< The View's Toolbar.
213 Layer mContentLayer; ///< Content layer
215 ImageView mImageViews[NUMBER_OF_IMAGES];
216 PushButton mPlayButtons[NUMBER_OF_IMAGES];
217 PushButton mStopButtons[NUMBER_OF_IMAGES];
220 int DALI_EXPORT_API main(int argc, char** argv)
222 Application application = Application::New(&argc, &argv, DEMO_THEME_PATH);
223 AnimatedVectorImageViewController test(application);
224 application.MainLoop();