ViewHelper in Actionscript 3.0

April 28th, 2008

I always found the MovieClipHelper.as of the Pixlib framework useful for rolling out applications in a quick and straightforward manner.  Although the newest iteration of the framework  LowRA is a useful and powerful xml driven environment to roll out RIAs, I still find the need for a manager to hold references to all my views.  The power of a centralized repository of all views and standard methods for x, y manipulation is great to have around for any project.  I've also been looking through the Vegas framework and was unable to find a View Management tool along the same lines.  So I decided to roll my own using Pixlib as a basis and Vegas as my environment.   This little class does most of the things you'll find in the Pixlib version like holding all view references in a hash map.  This design principle makes managing views incredibly easy.  By just defining a single enum file of static strings, you can uniquely name all the views of your application and then subsequently access those views from anywhere in the application.  I find this useful when managing sizing and positioning of multiple views that are dependent upon one another.  So herein follows a quick and dirty ViewManager designed for Sprites in AS3. 

 
package com.extralongfingers.definitionalliterature.client.util
{
	import flash.display.DisplayObject;
 
	import vegas.data.map.HashMap;
 
	import flash.geom.Point;
	import flash.events.EventDispatcher;
	import flash.display.Sprite;
 
	/**
	 * @author Gregory Sogorka
	 */
	public class ViewHelper
	{
		public var view 			: Sprite;
		public var _dispatcher  	: EventDispatcher = new EventDispatcher();
		private static var _a 		: HashMap = new HashMap();
		private var _sName			: String;
 
		function ViewHelper( s : String, spr : Sprite )
		{
			if( spr == null ) trace( " You must pass a Sprite to the constructor.");
 
			else
			{
				view = spr;
				_setName( s );
			}
		}
 
		public function setVisible( b : Boolean ) : void
		{
			if ( b )
			{
				show();
			}
			else
			{
				hide();
			}
		}
 
		public function hide() : void
		{
			view.visible = false;
		}
 
		public function show() : void
		{
			view.visible = true;
		}
 
		public function move( x : Number, y : Number ) : void
		{
			view.x = x;
			view.y = y;
		}
 
		public function getPosition() : Point
		{
			return new Point( view.x, view.y );
		}
 
		public function setSize( w : Number, h : Number ) : void
		{
			view.width = w;
			view.height = h;
		}
 
		public function getSize() : Point
		{
			return new Point( view.width, view.height );
		}
 
		public function getName() : String
		{
			return _sName;
		}
 
		public function release() : void
		{
			ViewHelper._unregister( _sName );
			_sName = null;
		}	
 
		public function isVisible() : Boolean
		{
			return view.visible;
		}
 
		public static function getMovieClipHelper( sName:String ) : ViewHelper
		{
			if (!ViewHelper._a.containsKey( sName ) )
			{
				trace( "Can't find ViewHelper instance with '" + sName + "' name." );
			}
			return _a.get( sName );
		}
 
		public static function isRegistered( sName:String ) : Boolean
		{
			return ViewHelper._a.containsKey( sName );
		}
 
		//Private methods.
 
		private function _setName( name:String ) : void
		{
			if ( ViewHelper._register( name, this ) ) _sName = name;
		}
 
		private static function _register( sName:String, oHelper:ViewHelper ) : Boolean
		{
			if ( ViewHelper._a.containsKey( sName ) )
			{
				trace( "ViewHelper instance is already registered with '" + sName + "' name." );
				return false;
			}
			else
			{
				ViewHelper._a.put( sName, oHelper );
				return true;
			}
		}
 
		private static function _unregister( sName:String ) : void
		{
			ViewHelper._a.remove( sName );
		}
 
	}
}
 

    If you have any suggestions or improvements that might make this type of thing work even better drop me a line.

§ Leave a Reply

What's this?

You are currently reading ViewHelper in Actionscript 3.0 at Meandering Thought.

meta