splinklibrary feature demo

October 18th, 2008

here is a quick demo class showcasing a few splinklibrary features. Please note that this example focuses on showing some splinklibrary features and it is not considered to be a best practice approach for loading and tweening an image. Also note that the demo is just a teaser, splinklibrary has a lot more useful concepts to offer. I recommend you to checkout the source and see for yourself.

 
package
{
import org.splink.library.loading.QLoader;
import org.splink.library.loading.QUrlLoader;
import org.splink.library.logging.ILogger;
import org.splink.library.logging.ILoggerFactory;
import org.splink.library.logging.LogLevel;
import org.splink.library.logging.LogRange;
import org.splink.library.logging.LoggerFactory;
import org.splink.library.logging.LoggerProvider;
import org.splink.library.logging.logoutput.DefaultOutputFormatter;
import org.splink.library.logging.logoutput.FirebugOutput;
import org.splink.library.logging.logoutput.QLogOutput;
import org.splink.library.queue.Queue;
import org.splink.library.queue.QueueEvent;
import org.splink.library.queue.QueueResultProvider;
import org.splink.library.queue.ResultQueue;
import org.splink.library.tween.TweenAction;
import org.splink.library.tween.TweenPool;
import org.splink.library.tween.sprop.FilterProp;
 
import mx.effects.easing.Sine;
 
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.filters.BlurFilter;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
/**
 * This class demos the usage of some of the splinklibrary classes.
 *
 * @author Max Kugland
 */
public class Demo extends Sprite
{
private var _logger:ILogger;
 
/**
 * Configure the logger once for the project
 */
private function configureLogger():void
{
	// we need an ILoggerFactory, which will create our ILogger instances
	var factory:ILoggerFactory = new LoggerFactory();
	// set a factory id
	factory.setId("demo");
	// set the range of LogLevels which will be logged
	factory.setRange(new LogRange(LogLevel.TRACE, LogLevel.FATAL));
	// set an IOutputFormatter to format our logmessages
	factory.setOuputFormatter(new DefaultOutputFormatter("demo-app"));
	// add ILogOuputs which will send the logs to their destination,
	// in this case we let the logs appear in QLog and in Firebug
	factory.addLogOutput(new QLogOutput());
	factory.addLogOutput(new FirebugOutput());
	// add the factory to the LoggerProvider
	LoggerProvider.addLoggerFactory(factory);
 
	// get an ILogger from the LoggerProvider using the ILoggerFactory
	// with the id "demo"
	_logger = LoggerProvider.getLogger("demo", Demo);
}
 
public function Demo()
{
	configureLogger();
 
	// log a message with the LogLevel INFO, as INFO is within the
	// specified LogRange, the message will get logged
	_logger.log(LogLevel.INFO, "starting Demo");
 
	// Create a ResultQueue and register listeners for error and
	// completion events as ResultQueue is capable of distributing
	// events because it extends Distributor
	var queue:ResultQueue = new ResultQueue;
	queue.register(QueueEvent.ON_COMPLETE, onComplete);
	queue.register(QueueEvent.ON_ERROR, onError);
 
	// add a loader to the queue which loads xml and assign it an id
	//(feedResult)
	queue.add(new QUrlLoader(
	new URLRequest("http://splink.org/?feed=rss2"),
	URLLoaderDataFormat.TEXT, "feedResult"));
 
	// add a loader to the queue which loads an image and register a
	// listener for its completion event, also pass the queue instance
	// (q) to enable it's usage within the method which is called on
	// it's completion, (QLoader also extends Distributor and therefore
	// can fire events)
	queue.add(new QLoader(
	new URLRequest(
	"http://splink.org/wp-content/themes/splink/img/header.jpg"))).
	register(QueueEvent.ON_COMPLETE, onImage, queue);
 
	// start the queue
	queue.start();
}
 
/**
 * Called when the image loader completes
 *
 * @param e the event sent by the image loader
 * @param q an optional object, in this case the queue
 */
private function onImage(e:QueueEvent, q:Queue):void
{
	// unregister from the event source (the image loader)
	e.getSource().unregister(QueueEvent.ON_COMPLETE, onImage);
 
	// cast the event source (IDistributor) to QLoader
	var loader:QLoader = (e.getSource() as QLoader);
 
	// add the loaded image data to the stage, and set it's alpha
	// value to 0
	var bmp:DisplayObject = loader.getContent();
	addChild(bmp).alpha = 0;
 
	_logger.log(LogLevel.TRACE, "image present: " + bmp);
 
	// create a bitmapfilter which is used for tweening
	var fprop:ISpecialProp = new FilterProp(new BlurFilter(0, 0, 1));
 
	// create a TweenPool and add some TweenActions targeting the
	// loaded image (bmp). tween the alpha from it's current value
	// (0) to 1 and tween the blurX  and blurY properties of the image
	// from 100 to 0
	var t:TweenPool = new TweenPool;
	t.add(new TweenAction(bmp, Sine.easeOut, 500,
	TweenAction.ALPHA, bmp.alpha, 1));
	t.add(new TweenAction(bmp, Sine.easeOut, 500,
	TweenAction.BLUR_X, 100, 0, 0, fprop));
	t.add(new TweenAction(bmp, Sine.easeOut, 500,
	TweenAction.BLUR_Y, 100, 0, 0, fprop));
	// add the TweenPool to the end of the queue
	q.add(t);
}
 
/**
 * If one of the queued operations fails, we get notified here
 */
private function onError(e:QueueEvent):void
{
	_logger.log(LogLevel.ERROR, "Demo error " + e.getErrorMessage());
}
 
/**
 * As the queue continues even if a queued operation fails onComplete
 * gets called in any case.
 */
private function onComplete(e:QueueEvent):void
{
	// as we used a ResultQueue we can retrieve a QueueResultProvider
	// which carries the results of the IResultQueueable items within
	// the ResultQueue. Note that the TweenPool is not an
	// IResultQueueable but an IQueueable, as it doesn't compute any
	// results, so the QueueResultProvider only holds the  results of
	// both loaders (the image loader and the xml loader)
	var p:QueueResultProvider =
	(e.getSource() as ResultQueue).getResultProvider();
 
	// here we retrieve the result with the id "feedResult" from the
	// QueueResultProvider and as we know its content is xml we cast
	// it to xml
	var xml:XML = XML(p.getResultById("feedResult").getResult());
 
	// we log the title of the loaded xml document which is a rss feed
	// at the TRACE LogLevel and ouput that the demo is complete at
	//INFO level
	_logger.log(LogLevel.TRACE, "xml feed title: "+xml["channel"].title);
	_logger.log(LogLevel.INFO, "Demo complete.");
 
	// Eventually we invoke finalize on the event source which results
	// in removement of all the registred listeners
	e.getSource().finalize();
}
}
}
 

No Comments »

No Comments

No comments yet.

Leave a comment




Powered by WordPress