Commit a59b6846f18f67e3ae5a47999b59a1986c3993d6

Authored by David Steele
1 parent d7a61b4c

Updated animated-images example to also handle image arrays

Change-Id: I191a2181c42c882c050bb4e9ba35a54a656283a9
examples/animated-images/animated-images-example.cpp
... ... @@ -17,7 +17,7 @@
17 17  
18 18 #include <dali-toolkit/dali-toolkit.h>
19 19 #include <dali-toolkit/devel-api/controls/buttons/button-devel.h>
20   -
  20 +#include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
21 21 #include "shared/utility.h"
22 22  
23 23 using namespace Dali;
... ... @@ -37,6 +37,13 @@ const char* const ANIMATE_GIF_LOGO( DEMO_IMAGE_DIR &quot;dali-logo-anim.gif&quot; );
37 37 const char* const ANIMATE_PIXEL_AREA( "Animate PixelArea" );
38 38 const char* const ANIMATE_PIXEL_AREA_AND_SCALE( "Animate PixelArea & Scale" );
39 39  
  40 +const char* const STATIC_IMAGE_ARRAY_DOG( DEMO_IMAGE_DIR "dog-anim-001.png" );
  41 +const char* const ANIMATE_IMAGE_ARRAY_DOG( DEMO_IMAGE_DIR "dog-anim-%03d.png" );
  42 +
  43 +const char* const STATIC_IMAGE_ARRAY_LOGO( DEMO_IMAGE_DIR "dali-logo-anim-001.png" );
  44 +const char* const ANIMATE_IMAGE_ARRAY_LOGO( DEMO_IMAGE_DIR "dali-logo-anim-%03d.png" );
  45 +
  46 +
40 47 const Vector4 DIM_COLOR( 0.85f, 0.85f, 0.85f, 0.85f );
41 48 }
42 49  
... ... @@ -48,9 +55,20 @@ const Vector4 DIM_COLOR( 0.85f, 0.85f, 0.85f, 0.85f );
48 55 class AnimatedImageController : public ConnectionTracker
49 56 {
50 57 public:
  58 + enum ImageType
  59 + {
  60 + GIF,
  61 + IMAGE_ARRAY
  62 + };
  63 + enum StateType
  64 + {
  65 + STATIC,
  66 + ANIMATED
  67 + };
51 68  
52 69 AnimatedImageController( Application& application )
53   - : mApplication( application )
  70 + : mApplication( application ),
  71 + mImageType(GIF)
54 72 {
55 73 // Connect to the Application's Init signal
56 74 mApplication.InitSignal().Connect( this, &AnimatedImageController::Create );
... ... @@ -70,26 +88,91 @@ public:
70 88 // Tie-in input event handlers:
71 89 stage.KeyEventSignal().Connect( this, &AnimatedImageController::OnKeyEvent );
72 90  
73   - mActorDog = CreateGifViewWithOverlayPlayButton( STATIC_GIF_DOG );
74   - mActorDog.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
75   - mActorDog.SetY( -100.f );
76   - stage.Add( mActorDog );
77   -
78   - mActorLogo = CreateGifViewWithOverlayPlayButton( STATIC_GIF_LOGO );
79   - mActorLogo.SetAnchorPoint( AnchorPoint::TOP_CENTER );
80   - mActorLogo.SetY( 100.f );
81   - stage.Add( mActorLogo );
  91 + CreateStaticImageView( 0 );
  92 + CreateStaticImageView( 1 );
  93 +
  94 + mGifButton = Toolkit::RadioButton::New("Gif");
  95 + mGifButton.SetProperty( Button::Property::SELECTED, true );
  96 + mArrayButton = Toolkit::RadioButton::New("Array");
  97 + mGifButton.ClickedSignal().Connect( this, &AnimatedImageController::OnTypeButtonClicked );
  98 + mArrayButton.ClickedSignal().Connect( this, &AnimatedImageController::OnTypeButtonClicked );
  99 +
  100 + Toolkit::TableView radioButtonLayout = Toolkit::TableView::New(1, 2);
  101 + radioButtonLayout.SetName("RadioButtonsLayout");
  102 + radioButtonLayout.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::HEIGHT );
  103 + radioButtonLayout.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
  104 + radioButtonLayout.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
  105 + radioButtonLayout.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
  106 + radioButtonLayout.SetFitHeight(0);
  107 + radioButtonLayout.AddChild( mGifButton, TableView::CellPosition(0,0) );
  108 + radioButtonLayout.AddChild( mArrayButton, TableView::CellPosition(0,1) );
  109 + radioButtonLayout.SetCellAlignment( TableView::CellPosition( 0, 0 ), HorizontalAlignment::CENTER, VerticalAlignment::CENTER );
  110 + radioButtonLayout.SetCellAlignment( TableView::CellPosition( 0, 1 ), HorizontalAlignment::CENTER, VerticalAlignment::CENTER );
  111 + radioButtonLayout.SetY( -10.0f );
  112 +
  113 + stage.Add( radioButtonLayout );
82 114  
83 115 mTapDetector = TapGestureDetector::New();
84 116 mTapDetector.DetectedSignal().Connect( this, &AnimatedImageController::OnTap );
85 117 }
86 118  
  119 + void CreateStaticImageView( int index )
  120 + {
  121 + Actor& actor = (index==0) ? mActorDog : mActorLogo;
  122 +
  123 + Stage stage = Stage::GetCurrent();
  124 + if( actor )
  125 + {
  126 + stage.Remove( actor );
  127 + }
  128 +
  129 + Property::Value viewSetup = SetupViewProperties( mImageType, STATIC, index, false );
  130 + actor = CreateImageViewWithPlayButton( viewSetup );
  131 + SetLayout(actor, index);
  132 + stage.Add( actor );
  133 + }
  134 +
  135 +
  136 + void CreateAnimImageView( int index )
  137 + {
  138 + Actor& actor = (index==0) ? mActorDog : mActorLogo;
  139 +
  140 + Stage stage = Stage::GetCurrent();
  141 + if( actor )
  142 + {
  143 + stage.Remove( actor );
  144 + }
  145 +
  146 + const char* label = (index==0) ? ANIMATE_PIXEL_AREA_AND_SCALE : ANIMATE_PIXEL_AREA;
  147 +
  148 + Property::Value viewSetup = SetupViewProperties( mImageType, ANIMATED, index, true );
  149 + actor = CreateImageViewWithAnimatePixelAreaButton( viewSetup, label);
  150 + SetLayout(actor, index);
  151 +
  152 + stage.Add( actor );
  153 + }
  154 +
  155 + void SetLayout( Actor actor, int index )
  156 + {
  157 + if( index == 0 )
  158 + {
  159 + actor.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
  160 + actor.SetY( -80.f );
  161 + }
  162 + else
  163 + {
  164 + actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
  165 + actor.SetY( 80.f );
  166 + }
  167 + }
  168 +
87 169 /**
88 170 * Create the gif image view with an overlay play button.
89 171 */
90   - Toolkit::ImageView CreateGifViewWithOverlayPlayButton( const std::string& gifUrl )
  172 + Toolkit::ImageView CreateImageViewWithPlayButton( Property::Value& viewSetup )
91 173 {
92   - Toolkit::ImageView imageView = Toolkit::ImageView::New( gifUrl );
  174 + Toolkit::ImageView imageView = Toolkit::ImageView::New();
  175 + imageView.SetProperty( ImageView::Property::IMAGE, viewSetup );
93 176 imageView.SetParentOrigin( ParentOrigin::CENTER );
94 177  
95 178 // Create a push button, and add it as child of the image view
... ... @@ -107,13 +190,10 @@ public:
107 190 return imageView;
108 191 }
109 192  
110   - Toolkit::ImageView CreateGifViewWithAnimatePixelAreaButton( const std::string& gifUrl, WrapMode::Type wrapModeU, WrapMode::Type wrapModeV, const std::string& buttonLabel )
  193 + Toolkit::ImageView CreateImageViewWithAnimatePixelAreaButton( Property::Value& viewSetup, const std::string& buttonLabel )
111 194 {
112 195 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 ));
  196 + imageView.SetProperty( Toolkit::ImageView::Property::IMAGE, viewSetup );
117 197 imageView.SetParentOrigin( ParentOrigin::CENTER );
118 198  
119 199 // Create a push button, and add it as child of the image view
... ... @@ -141,22 +221,12 @@ public:
141 221 if( button.GetParent() == mActorDog )
142 222 {
143 223 // remove the static gif view, the play button is also removed as its child.
144   - stage.Remove( mActorDog );
145   -
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 );
  224 + CreateAnimImageView( 0 );
150 225 }
151 226 else // button.GetParent() == mActorLogo
152 227 {
153 228 // remove the static gif view, the play button is also removed as its child.
154   - stage.Remove( mActorLogo );
155   -
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 );
  229 + CreateAnimImageView( 1 );
160 230 }
161 231 return true;
162 232 }
... ... @@ -178,24 +248,28 @@ public:
178 248 }
179 249 else if( actor == mActorDog ) // stop the animated gif, switch to static view
180 250 {
181   - Stage stage = Stage::GetCurrent();
182   - stage.Remove( mActorDog );
183   -
184   - mActorDog = CreateGifViewWithOverlayPlayButton( STATIC_GIF_DOG );
185   - mActorDog.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
186   - mActorDog.SetY( -100.f );
187   - stage.Add( mActorDog );
  251 + CreateStaticImageView( 0 );
188 252 }
189 253 else if( actor == mActorLogo ) // stop the animated gif, switch to static view
190 254 {
191   - Stage stage = Stage::GetCurrent();
192   - stage.Remove( mActorLogo );
  255 + CreateStaticImageView( 1 );
  256 + }
  257 + }
193 258  
194   - mActorLogo = CreateGifViewWithOverlayPlayButton( STATIC_GIF_LOGO );
195   - mActorLogo.SetAnchorPoint( AnchorPoint::TOP_CENTER );
196   - mActorLogo.SetY( 100.f );
197   - stage.Add( mActorLogo );
  259 + bool OnTypeButtonClicked( Toolkit::Button button )
  260 + {
  261 + if( button == mGifButton )
  262 + {
  263 + mImageType = GIF;
  264 + }
  265 + else
  266 + {
  267 + mImageType = IMAGE_ARRAY;
198 268 }
  269 + Stage stage = Stage::GetCurrent();
  270 + CreateStaticImageView( 0 );
  271 + CreateStaticImageView( 1 );
  272 + return true;
199 273 }
200 274  
201 275 void OnKeyEvent(const KeyEvent& event)
... ... @@ -209,11 +283,98 @@ public:
209 283 }
210 284 }
211 285  
  286 + Property::Value SetupViewProperties( ImageType type, StateType state, int index, bool wrap )
  287 + {
  288 + Property::Map map;
  289 +
  290 + AddUrl( map, type, state, index );
  291 + AddWrap( map, wrap && state != 0, index );
  292 + AddCache( map, type, index );
  293 + return Property::Value(map);
  294 + }
  295 +
  296 + void AddUrl( Property::Map& map, ImageType type, StateType state, int index )
  297 + {
  298 + const char* urls[2][2] =
  299 + { { STATIC_GIF_DOG, STATIC_GIF_LOGO },
  300 + { ANIMATE_GIF_DOG, ANIMATE_GIF_LOGO }
  301 + };
  302 + const char* urlFormats[2][2] =
  303 + { { STATIC_IMAGE_ARRAY_DOG, STATIC_IMAGE_ARRAY_LOGO } ,
  304 + { ANIMATE_IMAGE_ARRAY_DOG, ANIMATE_IMAGE_ARRAY_LOGO } };
  305 +
  306 + int numFrames[2] = { 8, 15 };
  307 +
  308 + if( type == GIF )
  309 + {
  310 + map.Add( Toolkit::ImageVisual::Property::URL, Property::Value( urls[state][index] ) );
  311 + }
  312 + else
  313 + {
  314 + if( state == STATIC )
  315 + {
  316 + Property::Array frameUrls;
  317 + frameUrls.Add(Property::Value( urlFormats[0][index] ));
  318 + map.Add( Toolkit::ImageVisual::Property::URL, frameUrls );
  319 + }
  320 + else
  321 + {
  322 + Property::Array frameUrls;
  323 + for( int i=1; i<= numFrames[index]; ++i )
  324 + {
  325 + char* buffer;
  326 + int len = asprintf( &buffer, urlFormats[1][index], i);
  327 + if( len > 0 )
  328 + {
  329 + std::string frameUrl(buffer);
  330 + free(buffer);
  331 + frameUrls.Add( Property::Value( frameUrl ) );
  332 + }
  333 + }
  334 + map.Add( Toolkit::ImageVisual::Property::URL, Property::Value( frameUrls ) );
  335 + }
  336 + }
  337 + }
  338 +
  339 + void AddWrap( Property::Map& map, bool wrap, int index )
  340 + {
  341 + WrapMode::Type wrapModes[2][2] = {
  342 + { WrapMode::REPEAT, WrapMode::DEFAULT },
  343 + { WrapMode::DEFAULT, WrapMode::MIRRORED_REPEAT } };
  344 +
  345 + if( wrap )
  346 + {
  347 + map
  348 + .Add( Toolkit::ImageVisual::Property::WRAP_MODE_U, wrapModes[index][0] )
  349 + .Add( Toolkit::ImageVisual::Property::WRAP_MODE_V, wrapModes[index][1] );
  350 + }
  351 + else
  352 + {
  353 + map
  354 + .Add( Toolkit::ImageVisual::Property::WRAP_MODE_U, WrapMode::DEFAULT )
  355 + .Add( Toolkit::ImageVisual::Property::WRAP_MODE_V, WrapMode::DEFAULT );
  356 + }
  357 + }
  358 +
  359 + void AddCache( Property::Map& map, ImageType type, int index )
  360 + {
  361 + if( type == IMAGE_ARRAY )
  362 + {
  363 + map
  364 + .Add( Toolkit::DevelImageVisual::Property::BATCH_SIZE, 4 )
  365 + .Add( Toolkit::DevelImageVisual::Property::CACHE_SIZE, 10 )
  366 + .Add( Toolkit::DevelImageVisual::Property::FRAME_DELAY, 150 );
  367 + }
  368 + }
  369 +
212 370 private:
213 371 Application& mApplication;
214 372 Toolkit::ImageView mActorDog;
215 373 Toolkit::ImageView mActorLogo;
  374 + Toolkit::RadioButton mGifButton;
  375 + Toolkit::RadioButton mArrayButton;
216 376 TapGestureDetector mTapDetector;
  377 + ImageType mImageType;
217 378 };
218 379  
219 380 // Entry point for Linux & Tizen applications
... ...
resources/images/dali-logo-anim-001.png 0 → 100644

13.5 KB

resources/images/dali-logo-anim-002.png 0 → 100644

742 Bytes

resources/images/dali-logo-anim-003.png 0 → 100644

6.49 KB

resources/images/dali-logo-anim-004.png 0 → 100644

25.5 KB

resources/images/dali-logo-anim-005.png 0 → 100644

27.9 KB

resources/images/dali-logo-anim-006.png 0 → 100644

32 KB

resources/images/dali-logo-anim-007.png 0 → 100644

31.9 KB

resources/images/dali-logo-anim-008.png 0 → 100644

32 KB

resources/images/dali-logo-anim-009.png 0 → 100644

31.9 KB

resources/images/dali-logo-anim-010.png 0 → 100644

31.8 KB

resources/images/dali-logo-anim-011.png 0 → 100644

13.5 KB

resources/images/dali-logo-anim-012.png 0 → 100644

14 KB

resources/images/dali-logo-anim-013.png 0 → 100644

33.5 KB

resources/images/dali-logo-anim-014.png 0 → 100644

33.1 KB

resources/images/dali-logo-anim-015.png 0 → 100644

33.4 KB

resources/images/dog-anim-001.png 0 → 100644

7.38 KB

resources/images/dog-anim-002.png 0 → 100644

7.94 KB

resources/images/dog-anim-003.png 0 → 100644

6.01 KB

resources/images/dog-anim-004.png 0 → 100644

7.65 KB

resources/images/dog-anim-005.png 0 → 100644

7.58 KB

resources/images/dog-anim-006.png 0 → 100644

7.35 KB

resources/images/dog-anim-007.png 0 → 100644

7.63 KB

resources/images/dog-anim-008.png 0 → 100644

7.66 KB