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.
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/controls/table-view/table-view.h>
24 #include <dali-toolkit/devel-api/visuals/animated-vector-image-visual-actions-devel.h>
25 #include <dali-toolkit/devel-api/visuals/animated-vector-image-visual-signals-devel.h>
26 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
29 using namespace Dali::Toolkit;
34 const char* BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-gradient.jpg" );
35 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
36 const char* APPLICATION_TITLE( "Animated Vector Images" );
38 const char* IMAGE_PATH[] = {
39 DEMO_IMAGE_DIR "insta_camera.json",
40 DEMO_IMAGE_DIR "you're_in!.json",
41 DEMO_IMAGE_DIR "jolly_walker.json"
44 const unsigned int NUMBER_OF_IMAGES = 3;
54 unsigned int GetControlIndex( Control control )
56 std::string controlName = control.GetProperty< std::string >( Dali::Actor::Property::NAME );
57 unsigned int index = 0;
59 if ( controlName != "")
61 index = std::stoul( controlName );
69 // This example shows the usage of AnimatedVectorImageVisual.
70 // It doesn't work on Ubuntu because the visual uses the external library to render frames.
71 class AnimatedVectorImageViewController: public ConnectionTracker
75 AnimatedVectorImageViewController( Application& application )
76 : mApplication( application )
78 // Connect to the Application's Init signal
79 mApplication.InitSignal().Connect( this, &AnimatedVectorImageViewController::Create );
82 void Create( Application& application )
84 // The Init signal is received once (only) during the Application lifetime
86 // Creates a default view with a default tool bar.
87 // The view is added to the window.
88 mContentLayer = DemoHelper::CreateView( application,
96 // Create a table view to show a pair of buttons above each image.
97 mTable = TableView::New( CellPlacement::NUMBER_OF_ROWS, NUMBER_OF_IMAGES );
98 mTable.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
99 mTable.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
100 mTable.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
101 Vector3 offset( 0.9f, 0.70f, 0.0f );
102 mTable.SetProperty( Actor::Property::SIZE_MODE_FACTOR, offset );
103 mTable.SetFitHeight(CellPlacement::TOP_BUTTON);
104 mTable.SetFitHeight(CellPlacement::LOWER_BUTTON);
105 mContentLayer.Add( mTable );
107 for( unsigned int x = 0; x < NUMBER_OF_IMAGES; x++ )
109 mPlayButtons[x] = PushButton::New();
110 mPlayButtons[x].SetProperty( Button::Property::LABEL, "Play" );
111 mPlayButtons[x].SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER );
112 mPlayButtons[x].SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER );
113 mPlayButtons[x].ClickedSignal().Connect( this, &AnimatedVectorImageViewController::OnPlayButtonClicked );
114 mPlayButtons[x].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
115 mPlayButtons[x].SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
116 std::string s = std::to_string(x);
117 mPlayButtons[x].SetProperty( Dali::Actor::Property::NAME, s );
118 mTable.AddChild( mPlayButtons[x], TableView::CellPosition( CellPlacement::TOP_BUTTON, x ) );
120 mStopButtons[x] = PushButton::New();
121 mStopButtons[x].SetProperty( Button::Property::LABEL, "Stop" );
122 mStopButtons[x].SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER );
123 mStopButtons[x].SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER );
124 mStopButtons[x].ClickedSignal().Connect( this, &AnimatedVectorImageViewController::OnStopButtonClicked );
125 mStopButtons[x].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
126 mStopButtons[x].SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
127 mStopButtons[x].SetProperty( Dali::Actor::Property::NAME, s );
128 mTable.AddChild( mStopButtons[x], TableView::CellPosition( CellPlacement::LOWER_BUTTON, x ) );
130 mImageViews[x] = ImageView::New( );
131 Property::Map imagePropertyMap;
132 imagePropertyMap.Insert( Visual::Property::TYPE, Visual::IMAGE );
133 imagePropertyMap.Insert( ImageVisual::Property::URL, IMAGE_PATH[ x ] );
134 imagePropertyMap.Insert( DevelImageVisual::Property::LOOP_COUNT, 3 );
135 mImageViews[x].SetProperty( ImageView::Property::IMAGE, imagePropertyMap );
137 mImageViews[x].SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
138 mImageViews[x].SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
139 mImageViews[x].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
140 mImageViews[x].SetProperty( Dali::Actor::Property::NAME, s );
142 DevelControl::VisualEventSignal( mImageViews[x] ).Connect( this, &AnimatedVectorImageViewController::OnVisualEvent );
144 mTable.AddChild( mImageViews[x], TableView::CellPosition( CellPlacement::IMAGE, x ) );
147 application.GetWindow().KeyEventSignal().Connect(this, &AnimatedVectorImageViewController::OnKeyEvent);
152 bool OnPlayButtonClicked( Button button )
154 unsigned int controlIndex = GetControlIndex( button );
156 ImageView imageView = mImageViews[controlIndex];
158 Property::Map map = imageView.GetProperty< Property::Map >( ImageView::Property::IMAGE );
159 Property::Value* value = map.Find( DevelImageVisual::Property::PLAY_STATE );
163 if( value->Get< int >() != static_cast< int >( DevelImageVisual::PlayState::PLAYING ) )
165 mPlayButtons[controlIndex].SetProperty( Button::Property::LABEL, "Pause" );
167 DevelControl::DoAction( imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::PLAY, Property::Value() );
171 mPlayButtons[controlIndex].SetProperty( Button::Property::LABEL, "Play" );
173 DevelControl::DoAction( imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::PAUSE, Property::Value() );
180 bool OnStopButtonClicked( Button button )
182 unsigned int controlIndex = GetControlIndex( button );
183 ImageView imageView = mImageViews[controlIndex];
184 DevelControl::DoAction( imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::STOP, Property::Value() );
189 void OnVisualEvent( Control control, Dali::Property::Index visualIndex, Dali::Property::Index signalId )
191 unsigned int controlIndex = GetControlIndex( control );
193 if( visualIndex == ImageView::Property::IMAGE && signalId == DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED )
195 mPlayButtons[controlIndex].SetProperty( Button::Property::LABEL, "Play" );
200 * Main key event handler
202 void OnKeyEvent( const KeyEvent& event )
204 if( event.GetState() == KeyEvent::DOWN )
206 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
214 Application& mApplication;
216 Control mView; ///< The View instance.
217 ToolBar mToolBar; ///< The View's Toolbar.
218 Layer mContentLayer; ///< Content layer
220 ImageView mImageViews[ NUMBER_OF_IMAGES ];
221 PushButton mPlayButtons[ NUMBER_OF_IMAGES ];
222 PushButton mStopButtons[ NUMBER_OF_IMAGES ];
226 int DALI_EXPORT_API main( int argc, char **argv )
228 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
229 AnimatedVectorImageViewController test( application );
230 application.MainLoop();