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 char* const ANIMATE_PIXEL_AREA( "Animate PixelArea" );
38 const char* const ANIMATE_PIXEL_AREA_AND_SCALE( "Animate PixelArea & Scale" );
40 const Vector4 DIM_COLOR( 0.85f, 0.85f, 0.85f, 0.85f );
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
48 class AnimatedImageController : public ConnectionTracker
52 AnimatedImageController( Application& application )
53 : mApplication( application )
55 // Connect to the Application's Init signal
56 mApplication.InitSignal().Connect( this, &AnimatedImageController::Create );
59 ~AnimatedImageController()
61 // Nothing to do here;
64 // The Init signal is received once (only) during the Application lifetime
65 void Create( Application& application )
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 );
73 mActorDog = CreateGifViewWithOverlayPlayButton( STATIC_GIF_DOG );
74 mActorDog.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
75 mActorDog.SetY( -100.f );
76 stage.Add( mActorDog );
78 mActorLogo = CreateGifViewWithOverlayPlayButton( STATIC_GIF_LOGO );
79 mActorLogo.SetAnchorPoint( AnchorPoint::TOP_CENTER );
80 mActorLogo.SetY( 100.f );
81 stage.Add( mActorLogo );
83 mTapDetector = TapGestureDetector::New();
84 mTapDetector.DetectedSignal().Connect( this, &AnimatedImageController::OnTap );
88 * Create the gif image view with an overlay play button.
90 Toolkit::ImageView CreateGifViewWithOverlayPlayButton( const std::string& gifUrl )
92 Toolkit::ImageView imageView = Toolkit::ImageView::New( gifUrl );
93 imageView.SetParentOrigin( ParentOrigin::CENTER );
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 );
104 // Apply dim color on the gif view and the play button
105 imageView.SetColor( DIM_COLOR );
110 Toolkit::ImageView CreateGifViewWithAnimatePixelAreaButton( const std::string& gifUrl, WrapMode::Type wrapModeU, WrapMode::Type wrapModeV, const std::string& buttonLabel )
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 );
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 );
126 animateButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
127 animateButton.SetProperty( Actor::Property::INHERIT_SCALE, false );
128 imageView.Add( animateButton );
130 mTapDetector.Attach( animateButton );
131 mTapDetector.Attach( imageView );
136 bool OnPlayButtonClicked( Toolkit::Button button )
138 Stage stage = Stage::GetCurrent();
140 // With play button clicked, the static gif is replaced with animated gif.
141 if( button.GetParent() == mActorDog )
143 // remove the static gif view, the play button is also removed as its child.
144 stage.Remove( mActorDog );
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 );
151 else // button.GetParent() == mActorLogo
153 // remove the static gif view, the play button is also removed as its child.
154 stage.Remove( mActorLogo );
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 );
164 void OnTap(Dali::Actor actor, const Dali::TapGesture& tap)
166 if( actor.GetParent() == mActorDog ) // "Animate Pixel Area" button is clicked
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 );
173 else if( actor.GetParent() == mActorLogo ) // "Animate Pixel Area" button is clicked
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 );
179 else if( actor == mActorDog ) // stop the animated gif, switch to static view
181 Stage stage = Stage::GetCurrent();
182 stage.Remove( mActorDog );
184 mActorDog = CreateGifViewWithOverlayPlayButton( STATIC_GIF_DOG );
185 mActorDog.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
186 mActorDog.SetY( -100.f );
187 stage.Add( mActorDog );
189 else if( actor == mActorLogo ) // stop the animated gif, switch to static view
191 Stage stage = Stage::GetCurrent();
192 stage.Remove( mActorLogo );
194 mActorLogo = CreateGifViewWithOverlayPlayButton( STATIC_GIF_LOGO );
195 mActorLogo.SetAnchorPoint( AnchorPoint::TOP_CENTER );
196 mActorLogo.SetY( 100.f );
197 stage.Add( mActorLogo );
201 void OnKeyEvent(const KeyEvent& event)
203 if(event.state == KeyEvent::Down)
205 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
213 Application& mApplication;
214 Toolkit::ImageView mActorDog;
215 Toolkit::ImageView mActorLogo;
216 TapGestureDetector mTapDetector;
219 // Entry point for Linux & Tizen applications
221 int DALI_EXPORT_API main( int argc, char **argv )
223 Application application = Application::New( &argc, &argv );
225 AnimatedImageController test( application );
227 application.MainLoop();