2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali-toolkit/dali-toolkit.h>
20 #include "shared/utility.h"
23 using namespace Dali::Toolkit;
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" );
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" );
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" );
36 const Vector4 DIM_COLOR( 0.85f, 0.85f, 0.85f, 0.85f );
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
44 class AnimatedImageController : public ConnectionTracker
48 AnimatedImageController( Application& application )
49 : mApplication( application )
51 // Connect to the Application's Init signal
52 mApplication.InitSignal().Connect( this, &AnimatedImageController::Create );
55 ~AnimatedImageController()
57 // Nothing to do here;
60 // The Init signal is received once (only) during the Application lifetime
61 void Create( Application& application )
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 );
69 mActorDog = CreateGifViewWithOverlayButton( STATIC_GIF_DOG );
70 mActorDog.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
71 mActorDog.SetY( -100.f );
72 stage.Add( mActorDog );
74 mActorLogo = CreateGifViewWithOverlayButton( STATIC_GIF_LOGO );
75 mActorLogo.SetAnchorPoint( AnchorPoint::TOP_CENTER );
76 mActorLogo.SetY( 100.f );
77 stage.Add( mActorLogo );
81 * Create the gif image view with an overlay play button.
83 Toolkit::ImageView CreateGifViewWithOverlayButton( const std::string& gifUrl )
85 Toolkit::ImageView imageView = Toolkit::ImageView::New( gifUrl );
86 imageView.SetParentOrigin( ParentOrigin::CENTER );
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 );
97 // Apply dim color on the gif view and the play button
98 imageView.SetColor( DIM_COLOR );
103 bool OnPlayButtonClicked( Toolkit::Button button )
105 Stage stage = Stage::GetCurrent();
107 // With play button clicked, the static gif is replaced with animated gif.
108 if( button.GetParent() == mActorDog )
110 // remove the static gif view, the play button is also removed as its child.
111 stage.Remove( mActorDog );
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 );
119 else // button.GetParent() == mActorLogo
121 // remove the static gif view, the play button is also removed as its child.
122 stage.Remove( mActorLogo );
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 );
134 void OnKeyEvent(const KeyEvent& event)
136 if(event.state == KeyEvent::Down)
138 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
146 Application& mApplication;
147 Toolkit::ImageView mActorDog;
148 Toolkit::ImageView mActorLogo;
151 // Entry point for Linux & Tizen applications
153 int DALI_EXPORT_API main( int argc, char **argv )
155 Application application = Application::New( &argc, &argv );
157 AnimatedImageController test( application );
159 application.MainLoop();