2 * Copyright (c) 2021 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 Rive");
37 const char* IMAGE_PATH[] = {
38 DEMO_IMAGE_DIR "juice.riv",
41 const unsigned int NUMBER_OF_IMAGES = 1;
51 unsigned int GetControlIndex(Control control)
53 std::string controlName = control.GetProperty<std::string>(Dali::Actor::Property::NAME);
54 unsigned int index = 0;
58 index = std::stoul(controlName);
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
71 AnimatedVectorImageViewController(Application& application)
72 : mApplication(application)
74 // Connect to the Application's Init signal
75 mApplication.InitSignal().Connect(this, &AnimatedVectorImageViewController::Create);
78 void Create(Application& application)
80 // The Init signal is received once (only) during the Application lifetime
82 // Creates a default view with a default tool bar.
83 // The view is added to the window.
84 mContentLayer = DemoHelper::CreateView(application,
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);
102 for(unsigned int x = 0; x < NUMBER_OF_IMAGES; x++)
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));
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));
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);
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);
137 DevelControl::VisualEventSignal(mImageViews[x]).Connect(this, &AnimatedVectorImageViewController::OnVisualEvent);
139 mTable.AddChild(mImageViews[x], TableView::CellPosition(CellPlacement::IMAGE, x));
142 application.GetWindow().KeyEventSignal().Connect(this, &AnimatedVectorImageViewController::OnKeyEvent);
146 bool OnPlayButtonClicked(Button button)
148 unsigned int controlIndex = GetControlIndex(button);
150 ImageView imageView = mImageViews[controlIndex];
152 Property::Map map = imageView.GetProperty<Property::Map>(ImageView::Property::IMAGE);
153 Property::Value* value = map.Find(DevelImageVisual::Property::PLAY_STATE);
157 if(value->Get<int>() != static_cast<int>(DevelImageVisual::PlayState::PLAYING))
159 mPlayButtons[controlIndex].SetProperty(Button::Property::LABEL, "Pause");
161 DevelControl::DoAction(imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::PLAY, Property::Value());
165 mPlayButtons[controlIndex].SetProperty(Button::Property::LABEL, "Play");
167 DevelControl::DoAction(imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::PAUSE, Property::Value());
174 bool OnStopButtonClicked(Button button)
176 unsigned int controlIndex = GetControlIndex(button);
177 ImageView imageView = mImageViews[controlIndex];
178 DevelControl::DoAction(imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::STOP, Property::Value());
183 void OnVisualEvent(Control control, Dali::Property::Index visualIndex, Dali::Property::Index signalId)
185 unsigned int controlIndex = GetControlIndex(control);
187 if(visualIndex == ImageView::Property::IMAGE && signalId == DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED)
189 mPlayButtons[controlIndex].SetProperty(Button::Property::LABEL, "Play");
194 * Main key event handler
196 void OnKeyEvent(const KeyEvent& event)
198 if(event.GetState() == KeyEvent::DOWN)
200 if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
208 Application& mApplication;
210 Control mView; ///< The View instance.
211 ToolBar mToolBar; ///< The View's Toolbar.
212 Layer mContentLayer; ///< Content layer
214 ImageView mImageViews[NUMBER_OF_IMAGES];
215 PushButton mPlayButtons[NUMBER_OF_IMAGES];
216 PushButton mStopButtons[NUMBER_OF_IMAGES];
219 int DALI_EXPORT_API main(int argc, char** argv)
221 Application application = Application::New(&argc, &argv, DEMO_THEME_PATH);
222 AnimatedVectorImageViewController test(application);
223 application.MainLoop();