2 * Copyright (c) 2017 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>
19 #include <dali-toolkit/devel-api/controls/buttons/button-devel.h>
21 #include "shared/utility.h"
24 using namespace Dali::Toolkit;
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" );
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" );
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" );
37 const Vector4 DIM_COLOR( 0.85f, 0.85f, 0.85f, 0.85f );
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
45 class AnimatedImageController : public ConnectionTracker
49 AnimatedImageController( Application& application )
50 : mApplication( application )
52 // Connect to the Application's Init signal
53 mApplication.InitSignal().Connect( this, &AnimatedImageController::Create );
56 ~AnimatedImageController()
58 // Nothing to do here;
61 // The Init signal is received once (only) during the Application lifetime
62 void Create( Application& application )
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 );
70 mActorDog = CreateGifViewWithOverlayButton( STATIC_GIF_DOG );
71 mActorDog.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
72 mActorDog.SetY( -100.f );
73 stage.Add( mActorDog );
75 mActorLogo = CreateGifViewWithOverlayButton( STATIC_GIF_LOGO );
76 mActorLogo.SetAnchorPoint( AnchorPoint::TOP_CENTER );
77 mActorLogo.SetY( 100.f );
78 stage.Add( mActorLogo );
82 * Create the gif image view with an overlay play button.
84 Toolkit::ImageView CreateGifViewWithOverlayButton( const std::string& gifUrl )
86 Toolkit::ImageView imageView = Toolkit::ImageView::New( gifUrl );
87 imageView.SetParentOrigin( ParentOrigin::CENTER );
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 );
98 // Apply dim color on the gif view and the play button
99 imageView.SetColor( DIM_COLOR );
104 bool OnPlayButtonClicked( Toolkit::Button button )
106 Stage stage = Stage::GetCurrent();
108 // With play button clicked, the static gif is replaced with animated gif.
109 if( button.GetParent() == mActorDog )
111 // remove the static gif view, the play button is also removed as its child.
112 stage.Remove( mActorDog );
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 );
120 else // button.GetParent() == mActorLogo
122 // remove the static gif view, the play button is also removed as its child.
123 stage.Remove( mActorLogo );
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 );
135 void OnKeyEvent(const KeyEvent& event)
137 if(event.state == KeyEvent::Down)
139 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
147 Application& mApplication;
148 Toolkit::ImageView mActorDog;
149 Toolkit::ImageView mActorLogo;
152 // Entry point for Linux & Tizen applications
154 int DALI_EXPORT_API main( int argc, char **argv )
156 Application application = Application::New( &argc, &argv );
158 AnimatedImageController test( application );
160 application.MainLoop();