2 * Copyright (c) 2019 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>
24 #include <dali-toolkit/devel-api/visuals/animated-vector-image-visual-signals-devel.h>
25 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
28 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"
43 const unsigned int NUMBER_OF_IMAGES = 3;
53 unsigned int GetControlIndex( Control control )
55 std::string controlName = control.GetName();
56 unsigned int index = 0;
58 if ( controlName != "")
60 index = std::stoul( controlName );
68 // This example shows the usage of AnimatedVectorImageVisual.
69 // It doesn't work on Ubuntu because the visual uses the external library to render frames.
70 class AnimatedVectorImageViewController: public ConnectionTracker
74 AnimatedVectorImageViewController( Application& application )
75 : mApplication( application )
77 // Connect to the Application's Init signal
78 mApplication.InitSignal().Connect( this, &AnimatedVectorImageViewController::Create );
81 void Create( Application& application )
83 // The Init signal is received once (only) during the Application lifetime
85 // Creates a default view with a default tool bar.
86 // The view is added to the stage.
87 mContentLayer = DemoHelper::CreateView( application,
95 // Create a table view to show a pair of buttons above each image.
96 mTable = TableView::New( CellPlacement::NUMBER_OF_ROWS, NUMBER_OF_IMAGES );
97 mTable.SetAnchorPoint( AnchorPoint::CENTER );
98 mTable.SetParentOrigin( ParentOrigin::CENTER );
99 mTable.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
100 Vector3 offset( 0.9f, 0.70f, 0.0f );
101 mTable.SetSizeModeFactor( offset );
102 mTable.SetFitHeight(CellPlacement::TOP_BUTTON);
103 mTable.SetFitHeight(CellPlacement::LOWER_BUTTON);
104 mContentLayer.Add( mTable );
106 for( unsigned int x = 0; x < NUMBER_OF_IMAGES; x++ )
108 mPlayButtons[x] = PushButton::New();
109 mPlayButtons[x].SetProperty( Button::Property::LABEL, "Play" );
110 mPlayButtons[x].SetParentOrigin( ParentOrigin::TOP_CENTER );
111 mPlayButtons[x].SetAnchorPoint( AnchorPoint::TOP_CENTER );
112 mPlayButtons[x].ClickedSignal().Connect( this, &AnimatedVectorImageViewController::OnPlayButtonClicked );
113 mPlayButtons[x].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
114 mPlayButtons[x].SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
115 std::string s = std::to_string(x);
116 mPlayButtons[x].SetName( s );
117 mTable.AddChild( mPlayButtons[x], TableView::CellPosition( CellPlacement::TOP_BUTTON, x ) );
119 mStopButtons[x] = PushButton::New();
120 mStopButtons[x].SetProperty( Button::Property::LABEL, "Stop" );
121 mStopButtons[x].SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
122 mStopButtons[x].SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
123 mStopButtons[x].ClickedSignal().Connect( this, &AnimatedVectorImageViewController::OnStopButtonClicked );
124 mStopButtons[x].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
125 mStopButtons[x].SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
126 mStopButtons[x].SetName( s );
127 mTable.AddChild( mStopButtons[x], TableView::CellPosition( CellPlacement::LOWER_BUTTON, x ) );
129 mImageViews[x] = ImageView::New( );
130 Property::Map imagePropertyMap;
131 imagePropertyMap.Insert( Visual::Property::TYPE, Visual::IMAGE );
132 imagePropertyMap.Insert( ImageVisual::Property::URL, IMAGE_PATH[ x ] );
133 imagePropertyMap.Insert( DevelImageVisual::Property::LOOP_COUNT, 3 );
134 mImageViews[x].SetProperty( ImageView::Property::IMAGE, imagePropertyMap );
136 mImageViews[x].SetParentOrigin( ParentOrigin::CENTER );
137 mImageViews[x].SetAnchorPoint( AnchorPoint::CENTER );
138 mImageViews[x].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
139 mImageViews[x].SetName( s );
141 DevelControl::VisualEventSignal( mImageViews[x] ).Connect( this, &AnimatedVectorImageViewController::OnVisualEvent );
143 mTable.AddChild( mImageViews[x], TableView::CellPosition( CellPlacement::IMAGE, x ) );
146 Stage::GetCurrent().KeyEventSignal().Connect(this, &AnimatedVectorImageViewController::OnKeyEvent);
151 bool OnPlayButtonClicked( Button button )
153 unsigned int controlIndex = GetControlIndex( button );
155 ImageView imageView = mImageViews[controlIndex];
157 Property::Map map = imageView.GetProperty< Property::Map >( ImageView::Property::IMAGE );
158 Property::Value* value = map.Find( DevelImageVisual::Property::PLAY_STATE );
162 if( value->Get< int >() != static_cast< int >( DevelImageVisual::PlayState::PLAYING ) )
164 mPlayButtons[controlIndex].SetProperty( Button::Property::LABEL, "Pause" );
166 DevelControl::DoAction( imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::PLAY, Property::Value() );
170 mPlayButtons[controlIndex].SetProperty( Button::Property::LABEL, "Play" );
172 DevelControl::DoAction( imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::PAUSE, Property::Value() );
179 bool OnStopButtonClicked( Button button )
181 unsigned int controlIndex = GetControlIndex( button );
182 ImageView imageView = mImageViews[controlIndex];
183 DevelControl::DoAction( imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::STOP, Property::Value() );
188 void OnVisualEvent( Control control, Dali::Property::Index visualIndex, Dali::Property::Index signalId )
190 unsigned int controlIndex = GetControlIndex( control );
192 if( visualIndex == ImageView::Property::IMAGE && signalId == DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED )
194 mPlayButtons[controlIndex].SetProperty( Button::Property::LABEL, "Play" );
199 * Main key event handler
201 void OnKeyEvent( const KeyEvent& event )
203 if( event.state == KeyEvent::Down )
205 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
213 Application& mApplication;
215 Control mView; ///< The View instance.
216 ToolBar mToolBar; ///< The View's Toolbar.
217 Layer mContentLayer; ///< Content layer
219 ImageView mImageViews[ NUMBER_OF_IMAGES ];
220 PushButton mPlayButtons[ NUMBER_OF_IMAGES ];
221 PushButton mStopButtons[ NUMBER_OF_IMAGES ];
225 int DALI_EXPORT_API main( int argc, char **argv )
227 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
228 AnimatedVectorImageViewController test( application );
229 application.MainLoop();