d7e4f8a4ff070ded3220dc46639dfb8b9c17d936
[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 #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>
26
27 using namespace Dali;
28 using namespace Dali::Toolkit;
29
30 namespace
31 {
32
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" );
36
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"
41 };
42
43 const unsigned int NUMBER_OF_IMAGES = 3;
44
45 enum CellPlacement
46 {
47   TOP_BUTTON,
48   LOWER_BUTTON,
49   IMAGE,
50   NUMBER_OF_ROWS
51 };
52
53 unsigned int GetControlIndex( Control control )
54 {
55   std::string controlName = control.GetName();
56   unsigned int index = 0;
57
58   if ( controlName != "")
59   {
60     index = std::stoul( controlName );
61   }
62
63   return index;
64 }
65
66 }  // namespace
67
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
71 {
72  public:
73
74   AnimatedVectorImageViewController( Application& application )
75     : mApplication( application )
76   {
77     // Connect to the Application's Init signal
78     mApplication.InitSignal().Connect( this, &AnimatedVectorImageViewController::Create );
79   }
80
81   void Create( Application& application )
82   {
83     // The Init signal is received once (only) during the Application lifetime
84
85     // Creates a default view with a default tool bar.
86     // The view is added to the stage.
87     mContentLayer = DemoHelper::CreateView( application,
88                                             mView,
89                                             mToolBar,
90                                             BACKGROUND_IMAGE,
91                                             TOOLBAR_IMAGE,
92                                             APPLICATION_TITLE );
93
94
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 );
105
106     for( unsigned int x = 0; x < NUMBER_OF_IMAGES; x++ )
107     {
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 )  );
118
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 )  );
128
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 );
135
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 );
140
141       DevelControl::VisualEventSignal( mImageViews[x] ).Connect( this, &AnimatedVectorImageViewController::OnVisualEvent );
142
143       mTable.AddChild( mImageViews[x], TableView::CellPosition( CellPlacement::IMAGE, x ) );
144     }
145
146     Stage::GetCurrent().KeyEventSignal().Connect(this, &AnimatedVectorImageViewController::OnKeyEvent);
147   }
148
149 private:
150
151   bool OnPlayButtonClicked( Button button )
152   {
153     unsigned int controlIndex = GetControlIndex( button );
154
155     ImageView imageView =  mImageViews[controlIndex];
156
157     Property::Map map = imageView.GetProperty< Property::Map >( ImageView::Property::IMAGE );
158     Property::Value* value = map.Find( DevelImageVisual::Property::PLAY_STATE );
159
160     if( value->Get< int >() != static_cast< int >( DevelImageVisual::PlayState::PLAYING ) )
161     {
162       mPlayButtons[controlIndex].SetProperty( Button::Property::LABEL, "Pause" );
163
164       DevelControl::DoAction( imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::PLAY, Property::Value() );
165     }
166     else
167     {
168       mPlayButtons[controlIndex].SetProperty( Button::Property::LABEL, "Play" );
169
170       DevelControl::DoAction( imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::PAUSE, Property::Value() );
171     }
172
173     return true;
174   }
175
176   bool OnStopButtonClicked( Button button )
177   {
178     unsigned int controlIndex = GetControlIndex( button );
179     ImageView imageView =  mImageViews[controlIndex];
180     DevelControl::DoAction( imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::STOP, Property::Value() );
181
182     return true;
183   }
184
185   void OnVisualEvent( Control control, Dali::Property::Index visualIndex, Dali::Property::Index signalId )
186   {
187     unsigned int controlIndex = GetControlIndex( control );
188
189     if( visualIndex == ImageView::Property::IMAGE && signalId == DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED )
190     {
191       mPlayButtons[controlIndex].SetProperty( Button::Property::LABEL, "Play" );
192     }
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        mStopButtons[ NUMBER_OF_IMAGES ];
219
220 };
221
222 int DALI_EXPORT_API main( int argc, char **argv )
223 {
224   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
225   AnimatedVectorImageViewController test( application );
226   application.MainLoop();
227   return 0;
228 }