2 * Copyright (c) 2018 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.
19 #include "shared/view.h"
20 #include <dali/dali.h>
21 #include <dali-toolkit/dali-toolkit.h>
22 #include <dali-toolkit/devel-api/controls/control-devel.h>
23 #include <dali-toolkit/devel-api/visuals/animated-vector-image-visual-actions-devel.h>
26 using namespace Dali::Toolkit;
31 const char* BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-gradient.jpg" );
32 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
33 const char* APPLICATION_TITLE( "Animated Vector Images" );
35 const char* IMAGE_PATH[] = {
36 DEMO_IMAGE_DIR "insta_camera.json",
37 DEMO_IMAGE_DIR "you're_in!.json",
38 DEMO_IMAGE_DIR "jolly_walker.json"
41 const unsigned int NUMBER_OF_IMAGES = 3;
51 unsigned int GetButtonIndex( Button button )
53 std::string buttonName = button.GetName();
54 unsigned int index = 0;
56 if ( buttonName != "")
58 index = std::stoul( buttonName );
66 // This example shows the usage of AnimatedVectorImageVisual.
67 // It doesn't work on Ubuntu because the visual uses the external library to render frames.
68 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 stage.
85 mContentLayer = DemoHelper::CreateView( application,
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.SetAnchorPoint( AnchorPoint::CENTER );
96 mTable.SetParentOrigin( ParentOrigin::CENTER );
97 mTable.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
98 Vector3 offset( 0.9f, 0.70f, 0.0f );
99 mTable.SetSizeModeFactor( offset );
100 mTable.SetFitHeight(CellPlacement::TOP_BUTTON);
101 mTable.SetFitHeight(CellPlacement::LOWER_BUTTON);
102 mContentLayer.Add( mTable );
104 for( unsigned int x = 0; x < NUMBER_OF_IMAGES; x++ )
106 mPlayButtons[x] = PushButton::New();
107 mPlayButtons[x].SetProperty( Button::Property::LABEL, "Play" );
108 mPlayButtons[x].SetParentOrigin( ParentOrigin::TOP_CENTER );
109 mPlayButtons[x].SetAnchorPoint( 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].SetName( s );
115 mTable.AddChild( mPlayButtons[x], TableView::CellPosition( CellPlacement::TOP_BUTTON, x ) );
117 mPauseButtons[x] = PushButton::New();
118 mPauseButtons[x].SetProperty( Button::Property::LABEL, "Pause" );
119 mPauseButtons[x].SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
120 mPauseButtons[x].SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
121 mPauseButtons[x].ClickedSignal().Connect( this, &AnimatedVectorImageViewController::OnPauseButtonClicked );
122 mPauseButtons[x].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
123 mPauseButtons[x].SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
124 mPauseButtons[x].SetName( s );
125 mTable.AddChild( mPauseButtons[x], TableView::CellPosition( CellPlacement::LOWER_BUTTON, x ) );
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 mImageViews[x].SetProperty( ImageView::Property::IMAGE , imagePropertyMap );
133 mImageViews[x].SetParentOrigin( ParentOrigin::CENTER );
134 mImageViews[x].SetAnchorPoint( AnchorPoint::CENTER );
135 mImageViews[x].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
137 mTable.AddChild( mImageViews[x], TableView::CellPosition( CellPlacement::IMAGE, x ) );
139 // Set changeable counter and toggle for each ImageView
140 mImageViewPlayStatus[x] = false;
141 mImageViewPauseStatus[x] = false;
144 Stage::GetCurrent().KeyEventSignal().Connect(this, &AnimatedVectorImageViewController::OnKeyEvent);
149 bool OnPlayButtonClicked( Button button )
151 unsigned int buttonIndex = GetButtonIndex( button );
153 ImageView imageView = mImageViews[buttonIndex];
154 if( !mImageViewPlayStatus[buttonIndex] )
156 mPlayButtons[buttonIndex].SetProperty( Button::Property::LABEL, "Stop" );
158 DevelControl::DoAction( imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::PLAY, Property::Value() );
162 mPlayButtons[buttonIndex].SetProperty( Button::Property::LABEL, "Play" );
164 DevelControl::DoAction( imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::STOP, Property::Value() );
167 mImageViewPlayStatus[buttonIndex] = !mImageViewPlayStatus[buttonIndex];
172 bool OnPauseButtonClicked( Button button )
174 unsigned int buttonIndex = GetButtonIndex( button );
176 ImageView imageView = mImageViews[buttonIndex];
177 if( !mImageViewPauseStatus[buttonIndex] )
179 mPauseButtons[buttonIndex].SetProperty( Button::Property::LABEL, "Resume" );
181 DevelControl::DoAction( imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::PAUSE, Property::Value() );
185 mPauseButtons[buttonIndex].SetProperty( Button::Property::LABEL, "Pause" );
187 DevelControl::DoAction( imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::RESUME, Property::Value() );
190 mImageViewPauseStatus[buttonIndex] = !mImageViewPauseStatus[buttonIndex];
196 * Main key event handler
198 void OnKeyEvent( const KeyEvent& event )
200 if( event.state == KeyEvent::Down )
202 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
210 Application& mApplication;
212 Control mView; ///< The View instance.
213 ToolBar mToolBar; ///< The View's Toolbar.
214 Layer mContentLayer; ///< Content layer
216 ImageView mImageViews[ NUMBER_OF_IMAGES ];
217 PushButton mPlayButtons[ NUMBER_OF_IMAGES ];
218 PushButton mPauseButtons[ NUMBER_OF_IMAGES ];
219 bool mImageViewPlayStatus[ NUMBER_OF_IMAGES ];
220 bool mImageViewPauseStatus[ NUMBER_OF_IMAGES ];
224 int DALI_EXPORT_API main( int argc, char **argv )
226 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
227 AnimatedVectorImageViewController test( application );
228 application.MainLoop();