Newer
Older
exporter / RectangleNode.as
package {
	import flash.geom.Rectangle;

	public class RectangleNode
	{
		public var Left:RectangleNode = null;
		public var Right:RectangleNode = null;
		public var Rect:Rectangle = null;
		public var InUse:Boolean = false;
		public var Removed:Boolean = false;
		public var Parent:RectangleNode = null;
		public var AllocationID = 0;
		public var Host:RectanglePacker= null;
		public var MinAllocationID = 0;
		public var NodeID;
		public static var NextNodeID = 0;
		
		public var DontRemove = false;
		
		public var LastAccessTime = 0;
		
		public var Flags = [];
		
		public var UpdateTo:RectangleNode = null;
		
		/*
		public function get Removed()
		{
			return _Removed;
		}
		
		public function set Removed(r)
		{
			this._Removed = r;
		}
		
		public function get Host():RectanglePacker
		{
			return _Host;
		}
		public function set Host(p:RectanglePacker)
		{
			this._Host = p;

			if (_Left && _Left.Host != p)
			{
				throw new Error("bad left host");
			}
			if (_Right && _Right.Host != p)
			{
				throw new Error("bad right host");
			}
			if (_Parent && _Parent.Host != p)
			{
				throw new Error("bad parent host");
			}

		}
		
		public function get Left():RectangleNode
		{
			return _Left;
		}
		public function set Left(n:RectangleNode)
		{
			if (n && n.Host != Host)
			{
				throw new Error("bad left host");
			}
			_Left = n;
		}
		public function get Right():RectangleNode
		{
			return _Right;
		}
		public function set Right(n:RectangleNode)
		{
			if (n && n.Host != Host)
			{
				throw new Error("bad right host");
			}
			_Right = n;
		}
		
		public function get Parent():RectangleNode
		{
			return _Parent;
		}
		public function set Parent(n:RectangleNode)
		{
			if (n && n.Host != Host)
			{
				throw new Error("bad parent host");
			}
			_Parent = n;
		}
		*/
		public function get IsFreeSpace()
		{
			return Left == null && Right == null && !InUse;
		}
		public function UpdateLinksFromNode(node:RectangleNode)
		{
			if (node.Parent)
			{
				if (node.Parent.Left == node)
				{
					node.Parent.Left = this;
				}
				if (node.Parent.Right == node)
				{
					node.Parent.Right = this;
				}
			}
			if (node.Left)
			{
				node.Left.Parent = this;
			}
			if (node.Right)
			{
				node.Right.Parent = this;
			}
		}
		public function MakeFromNode(node:RectangleNode)
		{
			//_Host = node.Host;
			Host = node.Host;
			Left = node.Left;
			Right = node.Right;
			Parent = node.Parent;
			AllocationID = node.AllocationID;
			InUse = node.InUse;
			MinAllocationID = node.MinAllocationID;
			NodeID = node.NodeID;
			Rect = node.Rect.clone();
			Removed = node.Removed;
			DontRemove = node.DontRemove;
		}
		public function RectangleNode(x, y, w, h, host:RectanglePacker)
		{
			this.NodeID = NextNodeID++;
			this.Host = host;
			this.Rect = new Rectangle(x, y, w, h);
		}
		public function OutputJSON()
		{
			var node = {};
			node.rect = [this.Rect.left, this.Rect.top, this.Rect.width, this.Rect.height];
			node.in_use = InUse;
			node.id = NodeID;
			if (this.Left) node.left = this.Left.OutputJSON();
			if (this.Right) node.right = this.Right.OutputJSON();
			return node;
		}
		
		public function ReadJSON(details)
		{
			details.new_node = this;
			this.Rect = new Rectangle(details.rect[0], details.rect[1], details.rect[2], details.rect[3]);
			this.Removed = false;
			
			this.InUse = details.in_use;
			
			if (details.hasOwnProperty("right"))
			{
				this.Right = new RectangleNode(0, 0, 0, 0, this.Host);
				Right.ReadJSON(details.right);
				Right.Parent = this;
			}
			if (details.hasOwnProperty("left"))
			{
				this.Left = new RectangleNode(0, 0, 0, 0, this.Host);
				Left.ReadJSON(details.left);
				Left.Parent = this;
			}
		}
	}
}