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