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>
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 char* const ANIMATE_PIXEL_AREA( "Animate PixelArea" );
37 const char* const ANIMATE_PIXEL_AREA_AND_SCALE( "Animate PixelArea & Scale" );
39 const char* const STATIC_IMAGE_ARRAY_DOG( DEMO_IMAGE_DIR "dog-anim-001.png" );
40 const char* const ANIMATE_IMAGE_ARRAY_DOG( DEMO_IMAGE_DIR "dog-anim-%03d.png" );
42 const char* const STATIC_IMAGE_ARRAY_LOGO( DEMO_IMAGE_DIR "dali-logo-anim-001.png" );
43 const char* const ANIMATE_IMAGE_ARRAY_LOGO( DEMO_IMAGE_DIR "dali-logo-anim-%03d.png" );
46 const Vector4 DIM_COLOR( 0.85f, 0.85f, 0.85f, 0.85f );
49 /* This example shows how to display a GIF image.
50 * First a static GIF image is loaded and then when the user presses on the "Play" icon,
51 * the static image is replaced by an animated one
54 class AnimatedImageController : public ConnectionTracker
68 AnimatedImageController( Application& application )
69 : mApplication( application ),
72 // Connect to the Application's Init signal
73 mApplication.InitSignal().Connect( this, &AnimatedImageController::Create );
76 ~AnimatedImageController()
78 // Nothing to do here;
81 // The Init signal is received once (only) during the Application lifetime
82 void Create( Application& application )
84 // Get a handle to the stage
85 Stage stage = Stage::GetCurrent();
86 stage.SetBackgroundColor( Color::WHITE );
87 // Tie-in input event handlers:
88 stage.KeyEventSignal().Connect( this, &AnimatedImageController::OnKeyEvent );
90 CreateStaticImageView( 0 );
91 CreateStaticImageView( 1 );
93 mGifButton = Toolkit::RadioButton::New("Gif");
94 mGifButton.SetProperty( Button::Property::SELECTED, true );
95 mArrayButton = Toolkit::RadioButton::New("Array");
96 mGifButton.ClickedSignal().Connect( this, &AnimatedImageController::OnTypeButtonClicked );
97 mArrayButton.ClickedSignal().Connect( this, &AnimatedImageController::OnTypeButtonClicked );
99 Toolkit::TableView radioButtonLayout = Toolkit::TableView::New(1, 2);
100 radioButtonLayout.SetName("RadioButtonsLayout");
101 radioButtonLayout.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::HEIGHT );
102 radioButtonLayout.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
103 radioButtonLayout.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
104 radioButtonLayout.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
105 radioButtonLayout.SetFitHeight(0);
106 radioButtonLayout.AddChild( mGifButton, TableView::CellPosition(0,0) );
107 radioButtonLayout.AddChild( mArrayButton, TableView::CellPosition(0,1) );
108 radioButtonLayout.SetCellAlignment( TableView::CellPosition( 0, 0 ), HorizontalAlignment::CENTER, VerticalAlignment::CENTER );
109 radioButtonLayout.SetCellAlignment( TableView::CellPosition( 0, 1 ), HorizontalAlignment::CENTER, VerticalAlignment::CENTER );
110 radioButtonLayout.SetY( -10.0f );
112 stage.Add( radioButtonLayout );
114 mTapDetector = TapGestureDetector::New();
115 mTapDetector.DetectedSignal().Connect( this, &AnimatedImageController::OnTap );
118 void CreateStaticImageView( int index )
120 Actor& actor = (index==0) ? mActorDog : mActorLogo;
122 Stage stage = Stage::GetCurrent();
125 stage.Remove( actor );
128 Property::Value viewSetup = SetupViewProperties( mImageType, STATIC, index, false );
129 actor = CreateImageViewWithPlayButton( viewSetup );
130 SetLayout(actor, index);
135 void CreateAnimImageView( int index )
137 Actor& actor = (index==0) ? mActorDog : mActorLogo;
139 Stage stage = Stage::GetCurrent();
142 stage.Remove( actor );
145 const char* label = (index==0) ? ANIMATE_PIXEL_AREA_AND_SCALE : ANIMATE_PIXEL_AREA;
147 Property::Value viewSetup = SetupViewProperties( mImageType, ANIMATED, index, true );
148 actor = CreateImageViewWithAnimatePixelAreaButton( viewSetup, label);
149 SetLayout(actor, index);
154 void SetLayout( Actor actor, int index )
158 actor.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
163 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
169 * Create the gif image view with an overlay play button.
171 Toolkit::ImageView CreateImageViewWithPlayButton( Property::Value& viewSetup )
173 Toolkit::ImageView imageView = Toolkit::ImageView::New();
174 imageView.SetProperty( ImageView::Property::IMAGE, viewSetup );
175 imageView.SetParentOrigin( ParentOrigin::CENTER );
177 // Create a push button, and add it as child of the image view
178 Toolkit::PushButton animateButton = Toolkit::PushButton::New();
179 animateButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, PLAY_ICON );
180 animateButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, PLAY_ICON_SELECTED );
181 animateButton.SetParentOrigin( ParentOrigin::CENTER );
182 animateButton.SetAnchorPoint( AnchorPoint::CENTER );
183 animateButton.ClickedSignal().Connect( this, &AnimatedImageController::OnPlayButtonClicked );
184 imageView.Add( animateButton );
186 // Apply dim color on the gif view and the play button
187 imageView.SetColor( DIM_COLOR );
192 Toolkit::ImageView CreateImageViewWithAnimatePixelAreaButton( Property::Value& viewSetup, const std::string& buttonLabel )
194 Toolkit::ImageView imageView = Toolkit::ImageView::New();
195 imageView.SetProperty( Toolkit::ImageView::Property::IMAGE, viewSetup );
196 imageView.SetParentOrigin( ParentOrigin::CENTER );
198 // Create a push button, and add it as child of the image view
199 Toolkit::PushButton animateButton = Toolkit::PushButton::New();
200 animateButton.SetProperty( Toolkit::Button::Property::LABEL, buttonLabel );
201 animateButton.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
202 animateButton.SetAnchorPoint( AnchorPoint::TOP_CENTER );
203 animateButton.SetY( 20.f );
205 animateButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
206 animateButton.SetProperty( Actor::Property::INHERIT_SCALE, false );
207 imageView.Add( animateButton );
209 mTapDetector.Attach( animateButton );
210 mTapDetector.Attach( imageView );
215 bool OnPlayButtonClicked( Toolkit::Button button )
217 Stage stage = Stage::GetCurrent();
219 // With play button clicked, the static gif is replaced with animated gif.
220 if( button.GetParent() == mActorDog )
222 // remove the static gif view, the play button is also removed as its child.
223 CreateAnimImageView( 0 );
225 else // button.GetParent() == mActorLogo
227 // remove the static gif view, the play button is also removed as its child.
228 CreateAnimImageView( 1 );
233 void OnTap(Dali::Actor actor, const Dali::TapGesture& tap)
235 if( actor.GetParent() == mActorDog ) // "Animate Pixel Area" button is clicked
237 Animation animation = Animation::New( 3.f );
238 animation.AnimateTo( Property( mActorDog, ImageView::Property::PIXEL_AREA ), Vector4( -1.0, 0.0, 3.f, 1.f ), AlphaFunction::SIN );
239 animation.AnimateTo( Property( mActorDog, Actor::Property::SCALE_X ), 3.f, AlphaFunction::SIN );
242 else if( actor.GetParent() == mActorLogo ) // "Animate Pixel Area" button is clicked
244 Animation animation = Animation::New( 3.f );
245 animation.AnimateTo( Property( mActorLogo, ImageView::Property::PIXEL_AREA ), Vector4( 0.0, 1.0, 1.f, 1.f ), AlphaFunction::SIN );
248 else if( actor == mActorDog ) // stop the animated gif, switch to static view
250 CreateStaticImageView( 0 );
252 else if( actor == mActorLogo ) // stop the animated gif, switch to static view
254 CreateStaticImageView( 1 );
258 bool OnTypeButtonClicked( Toolkit::Button button )
260 if( button == mGifButton )
266 mImageType = IMAGE_ARRAY;
268 Stage stage = Stage::GetCurrent();
269 CreateStaticImageView( 0 );
270 CreateStaticImageView( 1 );
274 void OnKeyEvent(const KeyEvent& event)
276 if(event.state == KeyEvent::Down)
278 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
285 Property::Value SetupViewProperties( ImageType type, StateType state, int index, bool wrap )
289 AddUrl( map, type, state, index );
290 AddWrap( map, wrap && state != 0, index );
291 AddCache( map, type, index );
292 return Property::Value(map);
295 void AddUrl( Property::Map& map, ImageType type, StateType state, int index )
297 const char* urls[2][2] =
298 { { STATIC_GIF_DOG, STATIC_GIF_LOGO },
299 { ANIMATE_GIF_DOG, ANIMATE_GIF_LOGO }
301 const char* urlFormats[2][2] =
302 { { STATIC_IMAGE_ARRAY_DOG, STATIC_IMAGE_ARRAY_LOGO } ,
303 { ANIMATE_IMAGE_ARRAY_DOG, ANIMATE_IMAGE_ARRAY_LOGO } };
305 int numFrames[2] = { 8, 15 };
309 map.Add( Toolkit::ImageVisual::Property::URL, Property::Value( urls[state][index] ) );
313 if( state == STATIC )
315 Property::Array frameUrls;
316 frameUrls.Add(Property::Value( urlFormats[0][index] ));
317 map.Add( Toolkit::ImageVisual::Property::URL, frameUrls );
321 Property::Array frameUrls;
322 for( int i=1; i<= numFrames[index]; ++i )
325 int len = asprintf( &buffer, urlFormats[1][index], i);
328 std::string frameUrl(buffer);
330 frameUrls.Add( Property::Value( frameUrl ) );
333 map.Add( Toolkit::ImageVisual::Property::URL, Property::Value( frameUrls ) );
338 void AddWrap( Property::Map& map, bool wrap, int index )
340 WrapMode::Type wrapModes[2][2] = {
341 { WrapMode::REPEAT, WrapMode::DEFAULT },
342 { WrapMode::DEFAULT, WrapMode::MIRRORED_REPEAT } };
347 .Add( Toolkit::ImageVisual::Property::WRAP_MODE_U, wrapModes[index][0] )
348 .Add( Toolkit::ImageVisual::Property::WRAP_MODE_V, wrapModes[index][1] );
353 .Add( Toolkit::ImageVisual::Property::WRAP_MODE_U, WrapMode::DEFAULT )
354 .Add( Toolkit::ImageVisual::Property::WRAP_MODE_V, WrapMode::DEFAULT );
358 void AddCache( Property::Map& map, ImageType type, int index )
360 if( type == IMAGE_ARRAY )
363 .Add( Toolkit::ImageVisual::Property::BATCH_SIZE, 4 )
364 .Add( Toolkit::ImageVisual::Property::CACHE_SIZE, 10 )
365 .Add( Toolkit::ImageVisual::Property::FRAME_DELAY, 150 );
370 Application& mApplication;
371 Toolkit::ImageView mActorDog;
372 Toolkit::ImageView mActorLogo;
373 Toolkit::RadioButton mGifButton;
374 Toolkit::RadioButton mArrayButton;
375 TapGestureDetector mTapDetector;
376 ImageType mImageType;
380 int DALI_EXPORT_API main( int argc, char **argv )
382 Application application = Application::New( &argc, &argv );
384 AnimatedImageController test( application );
386 application.MainLoop();