Add AnimatedVectorImageVisual example
[platform/core/uifw/dali-demo.git] / examples / animated-vector-images / animated-vector-images-example.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <string>
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
25 using namespace Dali;
26 using namespace Dali::Toolkit;
27
28 namespace
29 {
30
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" );
34
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"
39 };
40
41 const unsigned int NUMBER_OF_IMAGES = 3;
42
43 enum CellPlacement
44 {
45   TOP_BUTTON,
46   LOWER_BUTTON,
47   IMAGE,
48   NUMBER_OF_ROWS
49 };
50
51 unsigned int GetButtonIndex( Button button )
52 {
53   std::string buttonName = button.GetName();
54   unsigned int index = 0;
55
56   if ( buttonName != "")
57   {
58     index = std::stoul( buttonName );
59   }
60
61   return index;
62 }
63
64 }  // namespace
65
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
69 {
70  public:
71
72   AnimatedVectorImageViewController( Application& application )
73     : mApplication( application )
74   {
75     // Connect to the Application's Init signal
76     mApplication.InitSignal().Connect( this, &AnimatedVectorImageViewController::Create );
77   }
78
79   void Create( Application& application )
80   {
81     // The Init signal is received once (only) during the Application lifetime
82
83     // Creates a default view with a default tool bar.
84     // The view is added to the stage.
85     mContentLayer = DemoHelper::CreateView( application,
86                                             mView,
87                                             mToolBar,
88                                             BACKGROUND_IMAGE,
89                                             TOOLBAR_IMAGE,
90                                             APPLICATION_TITLE );
91
92
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 );
103
104     for( unsigned int x = 0; x < NUMBER_OF_IMAGES; x++ )
105     {
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 )  );
116
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 )  );
126
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 );
132
133       mImageViews[x].SetParentOrigin( ParentOrigin::CENTER );
134       mImageViews[x].SetAnchorPoint( AnchorPoint::CENTER );
135       mImageViews[x].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
136
137       mTable.AddChild( mImageViews[x], TableView::CellPosition( CellPlacement::IMAGE, x ) );
138
139       // Set changeable counter and toggle for each ImageView
140       mImageViewPlayStatus[x] = false;
141       mImageViewPauseStatus[x] = false;
142     }
143
144     Stage::GetCurrent().KeyEventSignal().Connect(this, &AnimatedVectorImageViewController::OnKeyEvent);
145   }
146
147 private:
148
149   bool OnPlayButtonClicked( Button button )
150   {
151     unsigned int buttonIndex = GetButtonIndex( button );
152
153     ImageView imageView =  mImageViews[buttonIndex];
154     if( !mImageViewPlayStatus[buttonIndex] )
155     {
156       mPlayButtons[buttonIndex].SetProperty( Button::Property::LABEL, "Stop" );
157
158       DevelControl::DoAction( imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::PLAY, Property::Value() );
159     }
160     else
161     {
162       mPlayButtons[buttonIndex].SetProperty( Button::Property::LABEL, "Play" );
163
164       DevelControl::DoAction( imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::STOP, Property::Value() );
165     }
166
167     mImageViewPlayStatus[buttonIndex] = !mImageViewPlayStatus[buttonIndex];
168
169     return true;
170   }
171
172   bool OnPauseButtonClicked( Button button )
173   {
174     unsigned int buttonIndex = GetButtonIndex( button );
175
176     ImageView imageView =  mImageViews[buttonIndex];
177     if( !mImageViewPauseStatus[buttonIndex] )
178     {
179       mPauseButtons[buttonIndex].SetProperty( Button::Property::LABEL, "Resume" );
180
181       DevelControl::DoAction( imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::PAUSE, Property::Value() );
182     }
183     else
184     {
185       mPauseButtons[buttonIndex].SetProperty( Button::Property::LABEL, "Pause" );
186
187       DevelControl::DoAction( imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::RESUME, Property::Value() );
188     }
189
190     mImageViewPauseStatus[buttonIndex] = !mImageViewPauseStatus[buttonIndex];
191
192     return true;
193   }
194
195   /**
196    * Main key event handler
197    */
198   void OnKeyEvent( const KeyEvent& event )
199   {
200     if( event.state == KeyEvent::Down )
201     {
202       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
203       {
204         mApplication.Quit();
205       }
206     }
207   }
208
209 private:
210   Application&  mApplication;
211
212   Control           mView;                              ///< The View instance.
213   ToolBar           mToolBar;                           ///< The View's Toolbar.
214   Layer             mContentLayer;                      ///< Content layer
215   TableView         mTable;
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 ];
221
222 };
223
224 int DALI_EXPORT_API main( int argc, char **argv )
225 {
226   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
227   AnimatedVectorImageViewController test( application );
228   application.MainLoop();
229   return 0;
230 }