Difference between revisions of "Controlling Gifs and SPA's"

From PowerUI
Jump to: navigation, search
(Getting hold of the SPA instance)
 
Line 75: Line 75:
 
 
 
});
 
});
 +
 +
</syntaxhighlight>
 +
 +
 +
== Using them outside PowerUI ==
 +
 +
If you'd like to use a GIF or SPA outside of PowerUI (i.e. obtain it as a material) the first thing you'll want to do is request the file. You'd use an ImagePackage for that:
 +
 +
 +
<syntaxhighlight lang="csharp">
 +
 +
// Create a package (using an absolute URL):
 +
PowerUI.ImagePackage package=new PowerUI.ImagePackage("resources://hello.gif");
 +
 +
package.onload=delegate(PowerUI.UIEvent e){
 +
  // Ok!
 +
  // package.Contents is e.g. a GifFormat:
 +
  GifFormat gif=package.Contents as GifFormat;
 +
 
 +
  // Start it up directly (using the default, unlit shader - Start is overloaded):
 +
  Material materialToUse=gif.GifFile.Start();
 +
 
 +
  // Display materialToUse somewhere!
 +
 +
};
 +
 +
package.onerror=delegate(){
 +
// Failed to load.
 +
};
 +
 +
// Request it now!
 +
package.send();
  
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 19:45, 24 February 2017

When you display a GIF with PowerUI, it runs as a Sprite Animaton (SPA). So, both a GIF and an SPA can be controlled in the same way.

Getting hold of the SPA instance

If you're loading an SPA or GIF via src, keep in mind that they can be loaded over the internet so the best way to know when your animation is available is via the spritestart event.

To get to the GIF or SPA from any given element, the instance itself is always held within the background image displayable property:

// Get the background data:
var backgroundData=anElement.RenderData.BGImage;

// The original image package:
var imagePackage=backgroundData.Image;

// The contents are a specific type of image (GifFormat, SpaFormat):
// GifFormat gifContents=imagePackage.Contents as GifFormat;
// SpaFormat spaContents=imagePackage.Contents as SpaFormat;

// Either of the above hold the SPAInstance as 'Animation', if it has started playing:
// gifContents.Animation
// spaContents.Animation

// Otherwise, gifContents.GifFile and spaContents.SpaFile are the originating Gif/Spa.

Events

Name Event object type Purpose
spritestart PowerUI.SpriteEvent Occurs when an SPA or GIF goes on screen and has started playing.
spriteend PowerUI.SpriteEvent Occurs when an SPA or GIF goes off screen or when it is set to not loop and has finished.
spriteiteration PowerUI.SpriteEvent Occurs when an SPA or GIF loops.

Here's an example:

	
// The element to add the event handlers to (typically an img, or whichever element has background-image on it):
var nyanCat=document.getElementById("nyan");

nyanCat.addEventListener("spritestart",delegate(SpriteEvent se){

	Debug.Log("Sprite start!");
	
	// Shortcut to stop it from looping (This will make it stop on the last frame, and spriteend is fired):
	// se.loop=false;
		
});

nyanCat.addEventListener("spriteend",delegate(SpriteEvent se){
		
	Debug.Log("Sprite end!");
	
});
	
nyanCat.addEventListener("spriteiteration",delegate(SpriteEvent se){
	
	Debug.Log("Sprite looped!");
		
});


Using them outside PowerUI

If you'd like to use a GIF or SPA outside of PowerUI (i.e. obtain it as a material) the first thing you'll want to do is request the file. You'd use an ImagePackage for that:


// Create a package (using an absolute URL):
PowerUI.ImagePackage package=new PowerUI.ImagePackage("resources://hello.gif");

package.onload=delegate(PowerUI.UIEvent e){
  // Ok!
  // package.Contents is e.g. a GifFormat:
  GifFormat gif=package.Contents as GifFormat;
  
  // Start it up directly (using the default, unlit shader - Start is overloaded):
  Material materialToUse=gif.GifFile.Start();
  
  // Display materialToUse somewhere!

};

package.onerror=delegate(){
 // Failed to load.
};

// Request it now!
package.send();