Update the AnimatedImageVisual demo
[platform/core/uifw/dali-demo.git] / examples / animated-images / animated-images-example.cpp
1 /*
2  * Copyright (c) 2017 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 <dali-toolkit/dali-toolkit.h>
19 #include <dali-toolkit/devel-api/controls/buttons/button-devel.h>
20
21 #include "shared/utility.h"
22
23 using namespace Dali;
24 using namespace Dali::Toolkit;
25
26 namespace
27 {
28 const char * const PLAY_ICON( DEMO_IMAGE_DIR "icon-play.png" );
29 const char * const PLAY_ICON_SELECTED( DEMO_IMAGE_DIR "icon-play-selected.png" );
30
31 const char* const STATIC_GIF_DOG( DEMO_IMAGE_DIR "dog-static.gif" );
32 const char* const ANIMATE_GIF_DOG( DEMO_IMAGE_DIR "dog-anim.gif" );
33
34 const char* const STATIC_GIF_LOGO( DEMO_IMAGE_DIR "dali-logo-static.gif" );
35 const char* const ANIMATE_GIF_LOGO( DEMO_IMAGE_DIR "dali-logo-anim.gif" );
36
37 const char* const ANIMATE_PIXEL_AREA( "Animate PixelArea" );
38 const char* const ANIMATE_PIXEL_AREA_AND_SCALE( "Animate PixelArea & Scale" );
39
40 const Vector4 DIM_COLOR( 0.85f, 0.85f, 0.85f, 0.85f );
41 }
42
43 /* This example shows how to display a GIF image.
44  * First a static GIF image is loaded and then when the user presses on the "Play" icon,
45  * the static image is replaced by an animated one
46  */
47
48 class AnimatedImageController : public ConnectionTracker
49 {
50 public:
51
52   AnimatedImageController( Application& application )
53   : mApplication( application )
54   {
55     // Connect to the Application's Init signal
56     mApplication.InitSignal().Connect( this, &AnimatedImageController::Create );
57   }
58
59   ~AnimatedImageController()
60   {
61     // Nothing to do here;
62   }
63
64   // The Init signal is received once (only) during the Application lifetime
65   void Create( Application& application )
66   {
67     // Get a handle to the stage
68     Stage stage = Stage::GetCurrent();
69     stage.SetBackgroundColor( Color::WHITE );
70     // Tie-in input event handlers:
71     stage.KeyEventSignal().Connect( this, &AnimatedImageController::OnKeyEvent );
72
73     mActorDog = CreateGifViewWithOverlayPlayButton( STATIC_GIF_DOG );
74     mActorDog.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
75     mActorDog.SetY( -100.f );
76     stage.Add( mActorDog );
77
78     mActorLogo = CreateGifViewWithOverlayPlayButton( STATIC_GIF_LOGO );
79     mActorLogo.SetAnchorPoint( AnchorPoint::TOP_CENTER );
80     mActorLogo.SetY( 100.f );
81     stage.Add( mActorLogo );
82
83     mTapDetector = TapGestureDetector::New();
84     mTapDetector.DetectedSignal().Connect( this, &AnimatedImageController::OnTap );
85   }
86
87   /**
88    * Create the gif image view with an overlay play button.
89    */
90   Toolkit::ImageView CreateGifViewWithOverlayPlayButton( const std::string& gifUrl  )
91   {
92     Toolkit::ImageView imageView = Toolkit::ImageView::New( gifUrl );
93     imageView.SetParentOrigin( ParentOrigin::CENTER );
94
95     // Create a push button, and add it as child of the image view
96     Toolkit::PushButton animateButton = Toolkit::PushButton::New();
97     animateButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, PLAY_ICON );
98     animateButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, PLAY_ICON_SELECTED );
99     animateButton.SetParentOrigin( ParentOrigin::CENTER );
100     animateButton.SetAnchorPoint( AnchorPoint::CENTER );
101     animateButton.ClickedSignal().Connect( this, &AnimatedImageController::OnPlayButtonClicked );
102     imageView.Add( animateButton );
103
104     // Apply dim color on the gif view and the play button
105     imageView.SetColor( DIM_COLOR );
106
107     return imageView;
108   }
109
110   Toolkit::ImageView CreateGifViewWithAnimatePixelAreaButton( const std::string& gifUrl, WrapMode::Type wrapModeU, WrapMode::Type wrapModeV, const std::string& buttonLabel )
111   {
112     Toolkit::ImageView imageView = Toolkit::ImageView::New();
113     imageView.SetProperty( Toolkit::ImageView::Property::IMAGE,
114                            Property::Map().Add( Toolkit::ImageVisual::Property::URL, gifUrl )
115                                           .Add( Toolkit::ImageVisual::Property::WRAP_MODE_U, wrapModeU )
116                                           .Add( Toolkit::ImageVisual::Property::WRAP_MODE_V, wrapModeV ));
117     imageView.SetParentOrigin( ParentOrigin::CENTER );
118
119     // Create a push button, and add it as child of the image view
120     Toolkit::PushButton animateButton = Toolkit::PushButton::New();
121     animateButton.SetProperty( Toolkit::Button::Property::LABEL, buttonLabel );
122     animateButton.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
123     animateButton.SetAnchorPoint( AnchorPoint::TOP_CENTER );
124     animateButton.SetY( 20.f );
125
126     animateButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
127     animateButton.SetProperty( Actor::Property::INHERIT_SCALE, false );
128     imageView.Add( animateButton );
129
130     mTapDetector.Attach( animateButton );
131     mTapDetector.Attach( imageView );
132
133     return imageView;
134   }
135
136   bool OnPlayButtonClicked( Toolkit::Button button )
137   {
138     Stage stage = Stage::GetCurrent();
139
140     // With play button clicked, the static gif is replaced with animated gif.
141     if( button.GetParent() ==  mActorDog )
142     {
143       // remove the static gif view, the play button is also removed as its child.
144       stage.Remove( mActorDog );
145
146       mActorDog = CreateGifViewWithAnimatePixelAreaButton( ANIMATE_GIF_DOG, WrapMode::REPEAT, WrapMode::DEFAULT, ANIMATE_PIXEL_AREA_AND_SCALE );
147       mActorDog.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
148       mActorDog.SetY( -100.f );
149       stage.Add( mActorDog );
150     }
151     else // button.GetParent() ==  mActorLogo
152     {
153       // remove the static gif view, the play button is also removed as its child.
154       stage.Remove( mActorLogo );
155
156       mActorLogo = CreateGifViewWithAnimatePixelAreaButton( ANIMATE_GIF_LOGO, WrapMode::DEFAULT, WrapMode::MIRRORED_REPEAT, ANIMATE_PIXEL_AREA );
157       mActorLogo.SetAnchorPoint( AnchorPoint::TOP_CENTER );
158       mActorLogo.SetY( 100.f );
159       stage.Add( mActorLogo );
160     }
161     return true;
162   }
163
164   void OnTap(Dali::Actor actor, const Dali::TapGesture& tap)
165   {
166     if( actor.GetParent() ==  mActorDog ) // "Animate Pixel Area" button is clicked
167     {
168       Animation animation = Animation::New( 3.f );
169       animation.AnimateTo( Property( mActorDog, ImageView::Property::PIXEL_AREA ), Vector4( -1.0, 0.0, 3.f, 1.f ), AlphaFunction::SIN );
170       animation.AnimateTo( Property( mActorDog, Actor::Property::SCALE_X ), 3.f, AlphaFunction::SIN );
171       animation.Play();
172     }
173     else if( actor.GetParent() ==  mActorLogo ) // "Animate Pixel Area" button is clicked
174     {
175       Animation animation = Animation::New( 3.f );
176       animation.AnimateTo( Property( mActorLogo, ImageView::Property::PIXEL_AREA ), Vector4( 0.0, 1.0, 1.f, 1.f ), AlphaFunction::SIN );
177       animation.Play();
178     }
179     else if( actor == mActorDog ) // stop the animated gif, switch to static view
180     {
181       Stage stage = Stage::GetCurrent();
182       stage.Remove( mActorDog );
183
184       mActorDog = CreateGifViewWithOverlayPlayButton( STATIC_GIF_DOG );
185       mActorDog.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
186       mActorDog.SetY( -100.f );
187       stage.Add( mActorDog );
188     }
189     else if( actor == mActorLogo ) // stop the animated gif, switch to static view
190     {
191       Stage stage = Stage::GetCurrent();
192       stage.Remove( mActorLogo );
193
194       mActorLogo = CreateGifViewWithOverlayPlayButton( STATIC_GIF_LOGO );
195       mActorLogo.SetAnchorPoint( AnchorPoint::TOP_CENTER );
196       mActorLogo.SetY( 100.f );
197       stage.Add( mActorLogo );
198     }
199   }
200
201   void OnKeyEvent(const KeyEvent& event)
202   {
203     if(event.state == KeyEvent::Down)
204     {
205       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
206       {
207         mApplication.Quit();
208       }
209     }
210   }
211
212 private:
213   Application&  mApplication;
214   Toolkit::ImageView mActorDog;
215   Toolkit::ImageView mActorLogo;
216   TapGestureDetector mTapDetector;
217 };
218
219 // Entry point for Linux & Tizen applications
220 //
221 int DALI_EXPORT_API main( int argc, char **argv )
222 {
223   Application application = Application::New( &argc, &argv );
224
225   AnimatedImageController test( application );
226
227   application.MainLoop();
228
229   return 0;
230 }