Revert "Revert "KeyEvent class pimpling""
[platform/core/uifw/dali-demo.git] / examples / animated-vector-images / animated-vector-images-example.cpp
1 /*
2  * Copyright (c) 2020 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/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>
27
28 using namespace Dali;
29 using namespace Dali::Toolkit;
30
31 namespace
32 {
33
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" );
37
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"
42 };
43
44 const unsigned int NUMBER_OF_IMAGES = 3;
45
46 enum CellPlacement
47 {
48   TOP_BUTTON,
49   LOWER_BUTTON,
50   IMAGE,
51   NUMBER_OF_ROWS
52 };
53
54 unsigned int GetControlIndex( Control control )
55 {
56   std::string controlName = control.GetProperty< std::string >( Dali::Actor::Property::NAME );
57   unsigned int index = 0;
58
59   if ( controlName != "")
60   {
61     index = std::stoul( controlName );
62   }
63
64   return index;
65 }
66
67 }  // namespace
68
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
72 {
73  public:
74
75   AnimatedVectorImageViewController( Application& application )
76     : mApplication( application )
77   {
78     // Connect to the Application's Init signal
79     mApplication.InitSignal().Connect( this, &AnimatedVectorImageViewController::Create );
80   }
81
82   void Create( Application& application )
83   {
84     // The Init signal is received once (only) during the Application lifetime
85
86     // Creates a default view with a default tool bar.
87     // The view is added to the window.
88     mContentLayer = DemoHelper::CreateView( application,
89                                             mView,
90                                             mToolBar,
91                                             BACKGROUND_IMAGE,
92                                             TOOLBAR_IMAGE,
93                                             APPLICATION_TITLE );
94
95
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 );
106
107     for( unsigned int x = 0; x < NUMBER_OF_IMAGES; x++ )
108     {
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 )  );
119
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 )  );
129
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 );
136
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 );
141
142       DevelControl::VisualEventSignal( mImageViews[x] ).Connect( this, &AnimatedVectorImageViewController::OnVisualEvent );
143
144       mTable.AddChild( mImageViews[x], TableView::CellPosition( CellPlacement::IMAGE, x ) );
145     }
146
147     application.GetWindow().KeyEventSignal().Connect(this, &AnimatedVectorImageViewController::OnKeyEvent);
148   }
149
150 private:
151
152   bool OnPlayButtonClicked( Button button )
153   {
154     unsigned int controlIndex = GetControlIndex( button );
155
156     ImageView imageView =  mImageViews[controlIndex];
157
158     Property::Map map = imageView.GetProperty< Property::Map >( ImageView::Property::IMAGE );
159     Property::Value* value = map.Find( DevelImageVisual::Property::PLAY_STATE );
160
161     if( value )
162     {
163       if( value->Get< int >() != static_cast< int >( DevelImageVisual::PlayState::PLAYING ) )
164       {
165         mPlayButtons[controlIndex].SetProperty( Button::Property::LABEL, "Pause" );
166
167         DevelControl::DoAction( imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::PLAY, Property::Value() );
168       }
169       else
170       {
171         mPlayButtons[controlIndex].SetProperty( Button::Property::LABEL, "Play" );
172
173         DevelControl::DoAction( imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::PAUSE, Property::Value() );
174       }
175     }
176
177     return true;
178   }
179
180   bool OnStopButtonClicked( Button button )
181   {
182     unsigned int controlIndex = GetControlIndex( button );
183     ImageView imageView =  mImageViews[controlIndex];
184     DevelControl::DoAction( imageView, ImageView::Property::IMAGE, DevelAnimatedVectorImageVisual::Action::STOP, Property::Value() );
185
186     return true;
187   }
188
189   void OnVisualEvent( Control control, Dali::Property::Index visualIndex, Dali::Property::Index signalId )
190   {
191     unsigned int controlIndex = GetControlIndex( control );
192
193     if( visualIndex == ImageView::Property::IMAGE && signalId == DevelAnimatedVectorImageVisual::Signal::ANIMATION_FINISHED )
194     {
195       mPlayButtons[controlIndex].SetProperty( Button::Property::LABEL, "Play" );
196     }
197   }
198
199   /**
200    * Main key event handler
201    */
202   void OnKeyEvent( const KeyEvent& event )
203   {
204     if( event.GetState() == KeyEvent::Down )
205     {
206       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
207       {
208         mApplication.Quit();
209       }
210     }
211   }
212
213 private:
214   Application&  mApplication;
215
216   Control           mView;                              ///< The View instance.
217   ToolBar           mToolBar;                           ///< The View's Toolbar.
218   Layer             mContentLayer;                      ///< Content layer
219   TableView         mTable;
220   ImageView         mImageViews[ NUMBER_OF_IMAGES ];
221   PushButton        mPlayButtons[ NUMBER_OF_IMAGES ];
222   PushButton        mStopButtons[ NUMBER_OF_IMAGES ];
223
224 };
225
226 int DALI_EXPORT_API main( int argc, char **argv )
227 {
228   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
229   AnimatedVectorImageViewController test( application );
230   application.MainLoop();
231   return 0;
232 }