The rebirth of DisplayEditor class

February 18, 2011 by fMajakovskij

Ok, the previous DisplayEditor class has been discontinued.

I started to write it without make any search on what has already been done out there.

It turns out the outstanding TransformTool by Senocular, which makes any further development of DisplayEditor useless.

So, since I don’t want to reinvent the wheel, the updated DisplayEditor class simply will handle only the object selection and the properly displaylist change of TranformTool, and a couple of other useful tasks like object deletion and undo delete.

All the dirty transformation are done by the TransformTool package by Senocular.

Here the example:

This movie requires Flash Player 9

Simply done using the following actionscript code:

import maja.behaviors.DisplayEditor;
var ed:DisplayEditor = new DisplayEditor(mc);

Keep in mind that you need the Senocular TransformTool classes to use this class properly.
Here the class source code:

package maja.behaviors
{
	import com.senocular.display.transform.*;

	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.ui.Keyboard;

	public class DisplayEditor
	{

		public static const VERSION:String = "002";

		private var currStage:Stage
		private var mc:DisplayObject;
		private var selected:DisplayObject

		/**
		 * Init the Class
		 * @param _mc
		 *
		 */
		public function DisplayEditor(_mc:DisplayObject)
		{
			mc=_mc;
			if(mc.stage)
			{
				init();
			}else{
				mc.addEventListener(Event.ADDED_TO_STAGE, init);
			}
		}

		private var tool:TransformTool;

		/**
		 * Ensure the mc is added to stage and add some listeners for dragging
		 * @param e
		 *
		 */
		private function init(e:Event=null):void
		{
			currStage = mc.stage;

			tool = new TransformTool(new ControlSetFull());

			mc.removeEventListener(Event.ADDED_TO_STAGE, init);

			mc.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			currStage.addEventListener(KeyboardEvent.KEY_UP, onKeys);
		}

		private var undo:DisplayObject;
		private var undoParent:DisplayObjectContainer
		private var undoIndex:int;
		private function onKeys(e:KeyboardEvent):void
		{
			// delete object
			if(e.keyCode == Keyboard.BACKSPACE)
			{
				if(selected)
				{
					if(tool.stage)
					{
						tool.parent.removeChild(tool);

						undo=selected;
						undoParent=selected.parent;
						undoIndex=undoParent.getChildIndex(undo);

						undoParent.removeChild(undo);

						selected=null
					}
				}
			}

			if(e.keyCode == Keyboard.DOWN)
			{
				if(undo)
				{
					undoParent.addChildAt(undo,undoIndex);

					toolSelect(undo)
				}
			}
		}

		private function onDown(e:MouseEvent):void
		{
			toolSelect(e.target as DisplayObject)
		}

		private function toolSelect(target:DisplayObject):void
		{
			var me:DisplayObject = target
			var parent:DisplayObjectContainer = me.parent;

			if(parent)
			{
				if( checkIfIsTool(me) ) return;
				parent.addChild(tool);
				tool.target = me
				selected=me;
			}
		}

		private function checkIfIsTool(item:DisplayObject):Boolean
		{
			try
			{
				var p:DisplayObject = item.parent;
				var lastP:DisplayObject = p;

				//Iterate until the parent value is null (we've reached the end of this displayobject chain).
				while((p = p.parent) != null)
				{
					if(lastP == tool) return true;
					lastP = p;
				}
			}catch(err:Error){
				trace("Error catch: " + err);
			}
			return false;
		}

	}
}

Download [download id="25"] class.

3 thoughts on “The rebirth of DisplayEditor class

  1. PJ Lee says:

    Hi, I am PJ form china, the DisplayEditor class is very good.

    I have a request, whether given testDisplayEditor002.swf complete source code, I used the Flex 4 development tools.

    Thaks very much.

    • fMajakovskij says:

      You can find the complete source code of that swf at the top of the post, for instance, are two line of code! The ‘mc’ is the MovieClip entirely drawn by hand in Flash authoring tool.

      Regards

  2. Kelley says:

    I’ve been looking for a delete function for senocular’s transform tool for a long time now. I would love to be able to incorporate this display editor class to accomplish that, but I don’t quite understand how this could function with my project.

    I have dynamically loaded thumbnails which, when clicked, load the larger image to the stage with the transform tool ability attached.

    Do you have any idea how to incorporate your delete function with something like this?

    Thanks in advance,
    Kelley

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>