as3 simple history framework

When I was working on a project for my studies I found myself trying to implement a undo and redu functionality. It made me some problems because I was a beginner in the as3.
I think I’m much better now an I think that my experience could be useful to any one of You.

Grab the source from github:

https://github.com/mateuszmackowiak/as3UndoRedoHistory

Usage:

package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import pl.mateuszmackowiak.history.UndoRedoHistoryManager;

	/**
	 * @author Mateusz Maćkowiak
	 * @see https://mateuszmackowiak.wordpress.com
	 * @since 2011
	 */
	public class HistoryFrameworkTest extends Sprite
	{
		public var history:UndoRedoHistoryManager;
		public var box:Sprite;
		
		private var boxPostion:Point;
		private var boxPoint:Point;
		
		public function HistoryFrameworkTest()
		{
			history = new UndoRedoHistoryManager();
			
			box = new Sprite();
			box.graphics.beginFill(0x000000);
			box.graphics.drawRect(0,0,100,100);
			box.graphics.endFill();
			box.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
			addChild(box);
			
			addEventListener(Event.ADDED_TO_STAGE,addedToStage);
		}
		private function addedToStage(event:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,addedToStage);
			history.addBasicKeySupport(stage);
		}
		
		protected function mouseDownHandler(event:MouseEvent):void
		{
			stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
			
			const target:Object = event.target;
			boxPostion = new Point(target.x,target.y);
			boxPoint = new Point(target.mouseX,target.mouseY);
		}
		
		protected function mouseUpHandler(event:MouseEvent):void
		{
			stage.removeEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
			history.addToHistory(moveTo,[box,box.x,box.y],moveTo,[box,boxPostion.x,boxPostion.y]);
		}
		
		protected function mouseMoveHandler(event:MouseEvent):void
		{
			box.x = event.stageX - boxPoint.x;
			box.y = event.stageY - boxPoint.y;
		}
		
		
		public static function moveTo(arguments:Array):void
		{
			arguments[0].x = arguments[1];
			arguments[0].y = arguments[2];
		}
	}	
}
Advertisements