Added Animated Gif example
[platform/core/uifw/dali-demo.git] / examples / animated-images / animated-images-example.cpp
1 /*
2  * Copyright (c) 2016 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
20 #include "shared/utility.h"
21
22 using namespace Dali;
23 using namespace Dali::Toolkit;
24
25 namespace
26 {
27 const char * const PLAY_ICON( DEMO_IMAGE_DIR "icon-play.png" );
28 const char * const PLAY_ICON_SELECTED( DEMO_IMAGE_DIR "icon-play-selected.png" );
29
30 const char* const STATIC_GIF_DOG( DEMO_IMAGE_DIR "dog-static.gif" );
31 const char* const ANIMATE_GIF_DOG( DEMO_IMAGE_DIR "dog-anim.gif" );
32
33 const char* const STATIC_GIF_LOGO( DEMO_IMAGE_DIR "dali-logo-static.gif" );
34 const char* const ANIMATE_GIF_LOGO( DEMO_IMAGE_DIR "dali-logo-anim.gif" );
35
36 const Vector4 DIM_COLOR( 0.85f, 0.85f, 0.85f, 0.85f );
37 }
38
39 /* This example shows how to display a GIF image.
40  * First a static GIF image is loaded and then when the user presses on the "Play" icon,
41  * the static image is replaced by an animated one
42  */
43
44 class AnimatedImageController : public ConnectionTracker
45 {
46 public:
47
48   AnimatedImageController( Application& application )
49   : mApplication( application )
50   {
51     // Connect to the Application's Init signal
52     mApplication.InitSignal().Connect( this, &AnimatedImageController::Create );
53   }
54
55   ~AnimatedImageController()
56   {
57     // Nothing to do here;
58   }
59
60   // The Init signal is received once (only) during the Application lifetime
61   void Create( Application& application )
62   {
63     // Get a handle to the stage
64     Stage stage = Stage::GetCurrent();
65     stage.SetBackgroundColor( Color::WHITE );
66     // Tie-in input event handlers:
67     stage.KeyEventSignal().Connect( this, &AnimatedImageController::OnKeyEvent );
68
69     mActorDog = CreateGifViewWithOverlayButton( STATIC_GIF_DOG );
70     mActorDog.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
71     mActorDog.SetY( -100.f );
72     stage.Add( mActorDog );
73
74     mActorLogo = CreateGifViewWithOverlayButton( STATIC_GIF_LOGO );
75     mActorLogo.SetAnchorPoint( AnchorPoint::TOP_CENTER );
76     mActorLogo.SetY( 100.f );
77     stage.Add( mActorLogo );
78   }
79
80   /**
81    * Create the gif image view with an overlay play button.
82    */
83   Toolkit::ImageView CreateGifViewWithOverlayButton( const std::string& gifUrl  )
84   {
85     Toolkit::ImageView imageView = Toolkit::ImageView::New( gifUrl );
86     imageView.SetParentOrigin( ParentOrigin::CENTER );
87
88     // Create a push button, and add it as child of the image view
89     Toolkit::PushButton animateButton = Toolkit::PushButton::New();
90     animateButton.SetUnselectedImage( PLAY_ICON );
91     animateButton.SetSelectedImage( PLAY_ICON_SELECTED );
92     animateButton.SetParentOrigin( ParentOrigin::CENTER );
93     animateButton.SetAnchorPoint( AnchorPoint::CENTER );
94     animateButton.ClickedSignal().Connect( this, &AnimatedImageController::OnPlayButtonClicked );
95     imageView.Add( animateButton );
96
97     // Apply dim color on the gif view and the play button
98     imageView.SetColor( DIM_COLOR );
99
100     return imageView;
101   }
102
103   bool OnPlayButtonClicked( Toolkit::Button button )
104   {
105     Stage stage = Stage::GetCurrent();
106
107     // With play button clicked, the static gif is replaced with animated gif.
108     if( button.GetParent() ==  mActorDog )
109     {
110       // remove the static gif view, the play button is also removed as its child.
111       stage.Remove( mActorDog );
112
113       mActorDog = Toolkit::ImageView::New( ANIMATE_GIF_DOG );
114       mActorDog.SetParentOrigin( ParentOrigin::CENTER );
115       mActorDog.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
116       mActorDog.SetY( -100.f );
117       stage.Add( mActorDog );
118     }
119     else // button.GetParent() ==  mActorLogo
120     {
121       // remove the static gif view, the play button is also removed as its child.
122       stage.Remove( mActorLogo );
123
124       mActorLogo = Toolkit::ImageView::New( ANIMATE_GIF_LOGO );
125       mActorLogo.SetParentOrigin( ParentOrigin::CENTER );
126       mActorLogo.SetAnchorPoint( AnchorPoint::TOP_CENTER );
127       mActorLogo.SetY( 100.f );
128       stage.Add( mActorLogo );
129     }
130     return true;
131   }
132
133
134   void OnKeyEvent(const KeyEvent& event)
135   {
136     if(event.state == KeyEvent::Down)
137     {
138       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
139       {
140         mApplication.Quit();
141       }
142     }
143   }
144
145 private:
146   Application&  mApplication;
147   Toolkit::ImageView mActorDog;
148   Toolkit::ImageView mActorLogo;
149 };
150
151 // Entry point for Linux & Tizen applications
152 //
153 int DALI_EXPORT_API main( int argc, char **argv )
154 {
155   Application application = Application::New( &argc, &argv );
156
157   AnimatedImageController test( application );
158
159   application.MainLoop();
160
161   return 0;
162 }