/// <summary>
 ///     Initializes a new instance of this class.
 /// </summary>
 /// <param name="node">Node that was transformed.</param>
 /// <param name="originalBoundingBox">Nodes original bounding box.</param>
 /// <param name="originalTransformation">Nodes original transformation.</param>
 /// <param name="newBoundingBox">Nodes new bounding box.</param>
 /// <param name="newTransformation">Nodes new transformation.</param>
 public ResizeNodeUndoOperation(EntityNode node, Rectangle originalBoundingBox, Transformation originalTransformation, Rectangle newBoundingBox, Transformation newTransformation)
 {
     _node = node;
     _newBoundingBox = newBoundingBox;
     _newTransformation = newTransformation;
     _originalBoundingBox = originalBoundingBox;
     _originalTransformation = originalTransformation;
 }
        /// <summary>
        ///     Resets this node to the state it was in when it was created.
        /// </summary>
        public virtual void Reset()
        {
            if (_parent != null) _parent.RemoveChild(this);

            _transformation = new Transformation(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f);
            _transformationOffset = new Transformation(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f);
        }
        /// <summary>
        ///		Sets the rendering state so that all children of this 
        ///		camera are drawn correctly.
        /// </summary>
        /// <param name="position">Position where this entity's parent node was rendered.</param>
        public override void Render(Transformation transformation, CameraNode camera, int layer)
        {
            //Statistics.StoreInt("Nodes Rendered", Statistics.ReadInt("Nodes Rendered") + 1);

            Transformation relativeTransformation = CalculateRelativeTransformation(transformation);
            SetupRenderingState(relativeTransformation);

            GraphicsManager.ClearColor	= _clearColor;
            GraphicsManager.Viewport	= _viewport;
            GraphicsManager.ClearScene();

            if (_backgroundImage != null)
            {
                GraphicsManager.ScaleFactor = new float[] { _zoom, _zoom, 1.0f };
                GraphicsManager.TileImage(_backgroundImage, relativeTransformation.X, relativeTransformation.Y, relativeTransformation.Z);

                // Clear up the depth buffer so this image is always at the back.
                GraphicsManager.ClearDepthBuffer();
            }

            // Set the audio listener position at this cameras current position.
            Audio.AudioManager.ListenerPosition = new Vector((-relativeTransformation.X) + (_viewport.Width / 2), -(relativeTransformation.Y) + (_viewport.Height / 2), 0.0f);

            RenderChildren(relativeTransformation, camera, layer);
        }
        /// <summary>
        ///		Gets the transformation of this entity relative to all the nodes above it.
        /// </summary>
        public Transformation CalculateTransformation()
        {
            ArrayList parentNodes = new ArrayList();
            SceneNode parent = this.Parent;
            while (parent != null)
            {
                if ((parent as SceneNode) != null) parentNodes.Add(parent);
                parent = parent.Parent;
            }
            parentNodes.Reverse();

            Transformation relativeTransformation = new Transformation(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f);
            foreach (SceneNode node in parentNodes)
                relativeTransformation = node.CalculateRelativeTransformation(relativeTransformation);

            return CalculateRelativeTransformation(relativeTransformation);
        }
 /// <summary>
 ///		Called when this node needs to be rendered.
 /// </summary>
 public virtual void Render(Transformation transformation, CameraNode camera, int layer)
 {
     RenderChildren(transformation, camera, layer);
 }
        /// <summary>
        ///		Sets the rendering state so that all children of this 
        ///		camera are drawn correctly.
        /// </summary>
        /// <param name="position">Position where this entity's parent node was rendered.</param>
        public override void Render(Transformation transformation, CameraNode camera, int layer)
        {
            Transformation relativeTransformation = CalculateRelativeTransformation(transformation);
            SetupRenderingState(relativeTransformation);

            // Are we currnetly rendering the layer this entity is on?
            if (layer != _depthLayer)
            {
                if (_visible == true) RenderChildren(relativeTransformation, camera, layer);
                return;
            }

            //Statistics.StoreInt("Nodes Rendered", Statistics.ReadInt("Nodes Rendered") + 1);

            // Render all the particles attached to this emitter.
            if (_visible == true || _forceVisibility == true)
            {
                GraphicsManager.PushRenderState();
                foreach (ParticleNode particleNode in _particleList)
                    particleNode.Render(transformation, camera, layer);
                GraphicsManager.PopRenderState();
            }

            // Render the bounding box and sizing points if we have been asked to do so.
            if (_renderBoundingBox == true || _forceBoundingBoxVisibility == true) RenderBoundingBox(relativeTransformation, camera);
            if (_renderSizingPoints == true) RenderSizingPoints(relativeTransformation, camera);
            if (_renderEventLines == true) RenderEventLines(relativeTransformation, camera);
            if (_forceGlobalDebugVisibility == true) RenderDebug(relativeTransformation, camera);

            // Render all the children of this entity.
            RenderChildren(relativeTransformation, camera, layer);
        }
 /// <summary>
 ///		Renders this entity.
 /// </summary>
 /// <param name="position">Position where this entity's parent node was rendered.</param>
 public override void Render(Transformation transformation, CameraNode camera, int layer)
 {
     if (_image == null) return;
     //Statistics.StoreInt("Nodes Rendered", Statistics.ReadInt("Nodes Rendered") + 1);
     Transformation relativeTransformation = CalculateRelativeTransformation(transformation);
     relativeTransformation.Z = transformation.Z;
     SetupRenderingState(relativeTransformation);
     GraphicsManager.RenderImage(_image, relativeTransformation.X, relativeTransformation.Y, relativeTransformation.Z, _frame);
 }
        /// <summary>
        ///		Renders the event lines of this entity.
        /// </summary>
        /// <param name="transformation">Transformation to render event lines at.</param>
        protected virtual void RenderEventLines(Transformation transformation, CameraNode camera)
        {
            GraphicsManager.PushRenderState();
            GraphicsManager.ClearRenderState();
            GraphicsManager.DepthBufferEnabled = false;
            GraphicsManager.VertexColors.AllVertexs = _eventLineColor;

            foreach (SceneNode node in _eventNodes)
            {
                EntityNode entityNode = node as EntityNode;
                if (entityNode == null) continue;

                Transformation relativeTransformation = entityNode.CalculateTransformation(camera);
                int x1 = (int)(transformation.X + ((_boundingRectangle.Width * transformation.ScaleX) / 2));
                int y1 = (int)(transformation.Y + ((_boundingRectangle.Height * transformation.ScaleY) / 2));
                int x2 = (int)(relativeTransformation.X + ((entityNode._boundingRectangle.Width * relativeTransformation.ScaleX) / 2));
                int y2 = (int)(relativeTransformation.Y + ((entityNode._boundingRectangle.Height * relativeTransformation.ScaleY) / 2));
                GraphicsManager.RenderLine(x1, y1, transformation.Z, x2, y2, relativeTransformation.Z);
            }

            GraphicsManager.PopRenderState();
        }
        /// <summary>
        ///		Renders the sizing points of this entity.
        /// </summary>
        /// <param name="transformation">Transformation to render sizing points at.</param>
        protected void RenderSizingPoints(Transformation transformation, CameraNode camera)
        {
            GraphicsManager.PushRenderState();
            GraphicsManager.ClearRenderState();
            GraphicsManager.DepthBufferEnabled = false;
            GraphicsManager.VertexColors.AllVertexs = _sizingPointsColor;

            int halfSize = _sizingPointsSize / 2;
            int x = (int)(transformation.X - halfSize);
            int y = (int)(transformation.Y - halfSize);
            int w = (int)(_boundingRectangle.Width * Math.Abs(transformation.ScaleX));
            int h = (int)(_boundingRectangle.Height * Math.Abs(transformation.ScaleY));
            GraphicsManager.RenderRectangle(x, y, transformation.Z, _sizingPointsSize, _sizingPointsSize, false);
            GraphicsManager.RenderRectangle(x + w, y, transformation.Z, _sizingPointsSize, _sizingPointsSize, false);
            GraphicsManager.RenderRectangle(x + (w / 2), y, transformation.Z, _sizingPointsSize, _sizingPointsSize, false);
            GraphicsManager.RenderRectangle(x + w, y + h, transformation.Z, _sizingPointsSize, _sizingPointsSize, false);
            GraphicsManager.RenderRectangle(x + w, y + (h / 2), transformation.Z, _sizingPointsSize, _sizingPointsSize, false);
            GraphicsManager.RenderRectangle(x, y + h, transformation.Z, _sizingPointsSize, _sizingPointsSize, false);
            GraphicsManager.RenderRectangle(x, y + (h / 2), transformation.Z, _sizingPointsSize, _sizingPointsSize, false);
            GraphicsManager.RenderRectangle(x + (w / 2), y + h, transformation.Z, _sizingPointsSize, _sizingPointsSize, false);

            GraphicsManager.PopRenderState();
        }
        /// <summary>
        ///		Renders the collision box of this entity.
        /// </summary>
        /// <param name="transformation">Transformation to render collisionbox at.</param>
        protected void RenderCollisionBox(Transformation transformation, CameraNode camera)
        {
            if (_solid == false)
                return;

            GraphicsManager.PushRenderState();
            GraphicsManager.ClearRenderState();
            GraphicsManager.DepthBufferEnabled = false;
            GraphicsManager.VertexColors.AllVertexs = _collisionBoxColor;
            GraphicsManager.RenderRectangle(transformation.X + (_collisionRectangle.X * transformation.ScaleX), transformation.Y + (_collisionRectangle.Y * transformation.ScaleY), transformation.Z, Math.Abs(_collisionRectangle.Width * transformation.ScaleX), _collisionRectangle.Height * Math.Abs(transformation.ScaleY), false);
            GraphicsManager.PopRenderState();
        }
        /// <summary>
        ///		Renders the debug information of this entity.
        /// </summary>
        /// <param name="transformation">Transformation to render debug information at.</param>
        protected void RenderDebug(Transformation transformation, CameraNode camera)
        {
            GraphicsManager.PushRenderState();
            GraphicsManager.ClearRenderState();
            GraphicsManager.DepthBufferEnabled = false;

            GraphicsManager.ScaleFactor = new float[3] { 1.0f, 1.0f, 1.0f };
            float[] resolutionScale = GraphicsManager.ResolutionScale;
            GraphicsManager.ResolutionScale = new float[2] { 1.0f, 1.0f };

            string debugText = "(" + transformation.X + "," + transformation.Y + "," + transformation.Z + ") (" + transformation.ScaleX + "," + transformation.ScaleY + "," + transformation.ScaleZ + ") (" + transformation.AngleX + "," + transformation.AngleY + "," + transformation.AngleZ + ")";
            GraphicsManager.VertexColors.AllVertexs = unchecked((int)0xFF000000);
            GraphicsManager.RenderRectangle((transformation.X * resolutionScale[0]), (transformation.Y * resolutionScale[1]) - 30, transformation.Z, GraphicsManager.BitmapFont.TextWidth(debugText, false), GraphicsManager.BitmapFont.TextHeight(debugText, false));
            GraphicsManager.VertexColors.AllVertexs = _debugInfoColor;
            GraphicsManager.RenderText(debugText, (transformation.X * resolutionScale[0]), (transformation.Y * resolutionScale[1]) - 30, transformation.Z);

            debugText = "[Solid:\"" + _solid + "\" Visible:\""+_visible+"\" Frame:\""+_frame+"\" Event:\""+_event+"\" Name:\""+_name+"\"]";
            GraphicsManager.VertexColors.AllVertexs = unchecked((int)0xFF000000);
            GraphicsManager.RenderRectangle((transformation.X * resolutionScale[0]), (transformation.Y * resolutionScale[1]) - 15, transformation.Z, GraphicsManager.BitmapFont.TextWidth(debugText, false), GraphicsManager.BitmapFont.TextHeight(debugText, false));
            GraphicsManager.VertexColors.AllVertexs = _debugInfoColor;
            GraphicsManager.RenderText(debugText, (transformation.X * resolutionScale[0]), (transformation.Y * resolutionScale[1]) - 15, transformation.Z);

            GraphicsManager.PopRenderState();
        }
 /// <summary>
 ///		Renders the bounding box of this entity.
 /// </summary>
 /// <param name="transformation">Transformation to render bounding box at.</param>
 protected void RenderBoundingBox(Transformation transformation, CameraNode camera)
 {
     GraphicsManager.PushRenderState();
     GraphicsManager.ClearRenderState();
     GraphicsManager.DepthBufferEnabled = false;
     GraphicsManager.VertexColors.AllVertexs = _boundingBoxColor;
     GraphicsManager.RenderRectangle(transformation.X, transformation.Y, transformation.Z, Math.Abs(_boundingRectangle.Width * transformation.ScaleX), _boundingRectangle.Height * Math.Abs(transformation.ScaleY), false);
     GraphicsManager.PopRenderState();
 }
 /// <summary>
 ///     Ininitalizes a new instance of this class with the given data.
 /// </summary>
 /// <param name="transformation">Transformation of this rectangle.</param>
 /// <param name="width">Width of this rectangle.</param>
 /// <param name="height">Height of this rectangle.</param>
 public CollisionRectangle(Transformation transformation, float width, float height)
 {
     _transformation = transformation;
     _width = width;
     _height = height;
 }
 /// <summary>
 ///     Called when the reset camera menu item is clicked.
 /// </summary>
 /// <param name="sender">Object that caused this event to be triggered.</param>
 /// <param name="e">Arguments explaining why this event occured.</param>
 private void resetCameraToolStripMenuItem_Click(object sender, EventArgs e)
 {
     _cameraTransformation = new Transformation(0, 0, 0, 0, 0, 0, 1, 1, 1);
 }
 /// <summary>
 ///     Initializes a new instance of this class.
 /// </summary>
 /// <param name="node">Node that was transformed.</param>
 /// <param name="originalTransformation">Nodes original transformation.</param>
 /// <param name="newTransformation">Nodes new transformation.</param>
 public TransformNodeUndoOperation(SceneNode node, Transformation originalTransformation, Transformation newTransformation)
 {
     _node = node;
     _newTransformation = newTransformation;
     _originalTransformation = originalTransformation;
 }
        /// <summary>
        ///     Sets up the rendering state so this entity is rendered correctly.
        /// </summary>
        /// <param name="relativeTransformation">Transformation used to set scale and rotation factors.</param>
        protected void SetupRenderingState(Transformation relativeTransformation)
        {
            GraphicsManager.VertexColors.AllVertexs = _color;
            if (GraphicsManager.BlendMode != _blendMode) GraphicsManager.BlendMode = _blendMode;
            if (GraphicsManager.ScaleFactor[0] != relativeTransformation.ScaleX ||
                GraphicsManager.ScaleFactor[1] != relativeTransformation.ScaleY ||
                GraphicsManager.ScaleFactor[2] != relativeTransformation.ScaleZ)
            {
                GraphicsManager.ScaleFactor[0] = relativeTransformation.ScaleX;
                GraphicsManager.ScaleFactor[1] = relativeTransformation.ScaleY;
                GraphicsManager.ScaleFactor[2] = relativeTransformation.ScaleZ;
            }

            if (GraphicsManager.RotationAngle[0] != relativeTransformation.AngleX ||
                GraphicsManager.RotationAngle[1] != relativeTransformation.AngleY ||
                GraphicsManager.RotationAngle[2] != relativeTransformation.AngleZ)
            {
                GraphicsManager.RotationAngle[0] = relativeTransformation.AngleX;
                GraphicsManager.RotationAngle[1] = relativeTransformation.AngleY;
                GraphicsManager.RotationAngle[2] = relativeTransformation.AngleZ;
            }

            if (_bitmapFont != null) GraphicsManager.BitmapFont = _bitmapFont;
        }
        /// <summary>
        ///		Updates the following entity associated with this process.
        /// </summary>
        /// <param name="deltaTime">Elapsed time since last frame.</param>
        public override void Run(float deltaTime)
        {
            //if (_entity.IsEnabled == false) return;

            // Work out the central points.
            Transformation entityTransform = _entity.CalculateTransformation();

            // If its a camera then we need to invert the coordinates as it uses
            // slightly different ones from normal entities.
            if (_entity is CameraNode)
            {
                entityTransform.X = -entityTransform.X;
                entityTransform.Y = -entityTransform.Y;
            }

            // Are we at the correct place? If so why bother with the below code :P.
            if (entityTransform.X == _x && entityTransform.Y == _y)
            {
                Finish(ProcessResult.Success);
                return;
            }

            // If we can't move then finish.
            if (_gotPreviousTransformation == true)
            {
                if (_previousTransformation.X == entityTransform.X && _previousTransformation.Y == entityTransform.Y && _previousTransformation.Z == entityTransform.Z)
                {
                    if (_previousTransformationTimer.DurationMillisecond > 500) // Half a second and no movement O_o, oh shiz we must have something in out way.
                    {
                        Finish(ProcessResult.Failed);
                        return;
                    }
                }
                else
                {
                    _previousTransformationTimer.Restart();
                    _previousTransformation = entityTransform;
                }
            }
            else
            {
                _previousTransformation = entityTransform;
                _previousTransformationTimer.Restart();
                _gotPreviousTransformation = true;
            }

            float entityTransformCenterX = entityTransform.X + ((_entity.BoundingRectangle.Width / 2) * entityTransform.ScaleX);
            float entityTransformCenterY = entityTransform.Y + ((_entity.BoundingRectangle.Height / 2) * entityTransform.ScaleY);
            float vectorX = entityTransformCenterX - _x;
            float vectorY = entityTransformCenterY - _y;
            float distance = (float)Math.Sqrt(vectorX * vectorX + vectorY * vectorY);
            vectorX /= distance;
            vectorY /= distance;

            float followSpeed = _followSpeed.Start + (((_followSpeed.Finish - _followSpeed.Start) / _originalDistance) * (_originalDistance - distance));

            // Work out vector towards entity.
            float movementX = (vectorX * followSpeed) * deltaTime;
            float movementY = (vectorY * followSpeed) * deltaTime;
            _entity.Move(-movementX,-movementY, 0.0f);

            // Are we done yet?
            if (Math.Abs(Math.Round(distance)) <= Math.Max(1,_followSpeed.Finish) * 2)// || followSpeed == _followSpeed.Finish)
            {
                Finish(ProcessResult.Success);
                return;
            }
        }
        /// <summary>
        ///		Calculates this entitys relative transformation to another transformation.
        /// </summary>
        /// <param name="transformation">Transformation to calculate relative transformation from.</param>
        /// <returns>Transfromation relative to the given transfromation.</returns>
        public override Transformation CalculateRelativeTransformation(Transformation transformation)
        {
            //CollectiveTimer.Restart();

            Transformation newTransform = new Transformation();
            newTransform.AngleX = transformation.AngleX + _transformation.AngleX;
            newTransform.AngleY = transformation.AngleY + _transformation.AngleY;
            newTransform.AngleZ = transformation.AngleZ + _transformation.AngleZ;
            newTransform.ScaleX = transformation.ScaleX + (_transformation.ScaleX < 0.0f ? -_transformation.ScaleX : _transformation.ScaleX) - 1.0f;
            newTransform.ScaleY = transformation.ScaleY + (_transformation.ScaleY < 0.0f ? -_transformation.ScaleY : _transformation.ScaleY) - 1.0f;
            newTransform.ScaleZ = transformation.ScaleZ + (_transformation.ScaleZ < 0.0f ? -_transformation.ScaleZ : _transformation.ScaleZ) - 1.0f;

            if (_transformation.ScaleX < 0) newTransform.ScaleX = -newTransform.ScaleX;
            if (_transformation.ScaleY < 0) newTransform.ScaleY = -newTransform.ScaleY;
            if (_transformation.ScaleZ < 0) newTransform.ScaleZ = -newTransform.ScaleZ;

            newTransform.X = (float)Math.Truncate(transformation.X + ((_transformation.X + _transformationOffset.X) * transformation.ScaleX));
            newTransform.Y = (float)Math.Truncate(transformation.Y + ((_transformation.Y + _transformationOffset.Y) * transformation.ScaleY));

            // Work out the depth offset.
            float depthOffset = 0;
            switch (_depthMode)
            {
                case EntityDepthMode.AddXCoordinate: depthOffset = newTransform.X; break;
                case EntityDepthMode.AddYCoordinate: depthOffset = newTransform.Y; break;
                case EntityDepthMode.SubtractXCoordinate: depthOffset = -newTransform.X; break;
                case EntityDepthMode.SubtractYCoordinate: depthOffset = -newTransform.Y; break;
                case EntityDepthMode.AddCollisionBoxTop: depthOffset = (newTransform.Y + _collisionRectangle.Top); break;
                case EntityDepthMode.AddCollisionBoxBottom: depthOffset = (newTransform.Y + _collisionRectangle.Bottom); break;
                case EntityDepthMode.AddCollisionBoxLeft: depthOffset = (newTransform.X + _collisionRectangle.Left); break;
                case EntityDepthMode.AddCollisionBoxRight: depthOffset = (newTransform.X + _collisionRectangle.Right); break;
                case EntityDepthMode.SubtractCollisionBoxTop: depthOffset = -(newTransform.Y + _collisionRectangle.Top); break;
                case EntityDepthMode.SubtractCollisionBoxBottom: depthOffset = -(newTransform.Y + _collisionRectangle.Bottom); break;
                case EntityDepthMode.SubtractCollisionBoxLeft: depthOffset = -(newTransform.X + _collisionRectangle.Left); break;
                case EntityDepthMode.SubtractCollisionBoxRight: depthOffset = -(newTransform.X + _collisionRectangle.Right); break;
                case EntityDepthMode.AddBoundingBoxTop: depthOffset = (newTransform.Y + _boundingRectangle.Top); break;
                case EntityDepthMode.AddBoundingBoxBottom: depthOffset = (newTransform.Y + _boundingRectangle.Bottom); break;
                case EntityDepthMode.AddBoundingBoxLeft: depthOffset = (newTransform.X + _boundingRectangle.Left); break;
                case EntityDepthMode.AddBoundingBoxRight: depthOffset = (newTransform.X + _boundingRectangle.Right); break;
                case EntityDepthMode.SubtractBoundingBoxTop: depthOffset = -(newTransform.Y + _boundingRectangle.Top); break;
                case EntityDepthMode.SubtractBoundingBoxBottom: depthOffset = -(newTransform.Y + _boundingRectangle.Bottom); break;
                case EntityDepthMode.SubtractBoundingBoxLeft: depthOffset = -(newTransform.X + _boundingRectangle.Left); break;
                case EntityDepthMode.SubtractBoundingBoxRight: depthOffset = -(newTransform.X + _boundingRectangle.Right); break;
            }

            newTransform.Z = (transformation.Z) - (depthOffset + _transformation.Z + _transformationOffset.Z);

            //CollectiveCalls++;
            //CollectiveTime += CollectiveTimer.DurationMillisecond;

            return newTransform;
        }
        /// <summary>
        ///		Renders this tilemap segment at a relative position to its parent node.
        /// </summary>
        /// <param name="transformation">Transformation of parent node.</param>
        public override void Render(Transformation transformation, CameraNode camera, int layer)
        {
            if (_tileData == null) return;

            // Work out where and how we should be rendered.
            Transformation relativeTransformation = CalculateRelativeTransformation(transformation);
            SetupRenderingState(relativeTransformation);

            // Are we currnetly rendering the layer this entity is on? Or are we not visible.
            if (layer != _depthLayer || CanBeSeen(relativeTransformation) == false)
            {
                if (_visible == true) RenderChildren(relativeTransformation, camera, layer);
                return;
            }

            //Statistics.StoreInt("Nodes Rendered", Statistics.ReadInt("Nodes Rendered") + 1);

            // Work out if all the tiles are visible and what are not.
            int segmentLeft = (int)(-((relativeTransformation.X / _tileWidth) / relativeTransformation.ScaleX) - 1);
            int segmentTop = (int)(-((relativeTransformation.Y / _tileHeight) / relativeTransformation.ScaleY) - 1);
            int segmentRight = (int)(segmentLeft + ((GraphicsManager.Resolution[0] / _tileWidth) / relativeTransformation.ScaleX) + 2);
            int segmentBottom = (int)(segmentTop + ((GraphicsManager.Resolution[1] / _tileHeight) / relativeTransformation.ScaleY) + 2);

            // Cap all the segments off so we don't try to draw outside
            // the bounds of this array segment.
            segmentLeft = Math.Min(_width - 1, Math.Max(0, segmentLeft));
            segmentTop = Math.Min(_height - 1, Math.Max(0, segmentTop));
            segmentRight = Math.Min(_width, Math.Max(0, segmentRight));
            segmentBottom = Math.Min(_height, Math.Max(0, segmentBottom));

            if (_visible == true || _forceVisibility == true)
            {
                // Render normally if a prerendered version is not available.
                if (_preRenderedImage == null)
                {
                    //System.Console.WriteLine("Rendered tilemap segment of " + (segmentRight - segmentLeft) + "x" + (segmentBottom - segmentTop) + " (real size: " + (_width + "," + _height) + " tiles:" + ((segmentRight - segmentLeft) * (segmentBottom - segmentTop)) + ")");
                    // Go through all the visible tiles and render them.
                    for (int tileX = segmentLeft; tileX < segmentRight; tileX++)
                        for (int tileY = segmentTop; tileY < segmentBottom; tileY++)
                        {
                            if (_tileData[tileX, tileY] == null || _tileData[tileX, tileY].IsVisible == false || _tileData[tileX, tileY].Tileset == null || _tileData[tileX, tileY].Frame < 0 || _tileData[tileX, tileY].Frame >= _tileData[tileX, tileY].Tileset.Image.FrameCount) continue;
                            _tileData[tileX, tileY].Render(relativeTransformation, camera, layer);
                            /*
                            float tileScaleX = relativeTransformation.ScaleX + Math.Abs(_tileData[tileX, tileY].Transformation.ScaleX) - 1.0f;
                            float tileScaleY = relativeTransformation.ScaleY + Math.Abs(_tileData[tileX, tileY].Transformation.ScaleY) - 1.0f;
                            if (tileScaleX < 0) tileScaleX = -tileScaleX;
                            if (tileScaleY < 0) tileScaleY = -tileScaleY;

                            float tx = relativeTransformation.X + ((tileX * _tileWidth) * tileScaleX);
                            float ty = relativeTransformation.Y + ((tileY * _tileHeight) * tileScaleY);

                            GraphicsManager.VertexColors.AllVertexs = _tileData[tileX, tileY].Color;
                            GraphicsManager.BlendMode = _tileData[tileX, tileY].BlendMode;
                            GraphicsManager.ScaleFactor = new float[] { tileScaleX, tileScaleY };
                            GraphicsManager.RenderImage(_tileData[tileX, tileY].Tileset.Image, tx, ty, relativeTransformation.Z, _tileData[tileX, tileY].Frame);
                        */}
                }

                // w00ties a prerender exists, time for a nice big speedup!
                else
                    GraphicsManager.RenderImage(_preRenderedImage, relativeTransformation.X, relativeTransformation.Y, relativeTransformation.Z, 0);

                // If we have been asked to render a grid then do so.
                if (_renderGrid == true)
                {
                    float width = (_width * _tileWidth) * Math.Abs(relativeTransformation.ScaleX);
                    float height = (_height * _tileHeight) * Math.Abs(relativeTransformation.ScaleY);

                    GraphicsManager.VertexColors.AllVertexs = _gridColor;
                    for (int x = 0; x <= _width; x++)
                    {
                        float lX = (relativeTransformation.X + (x * (_tileWidth * Math.Abs(relativeTransformation.ScaleX))));
                        float lY = relativeTransformation.Y;
                        GraphicsManager.RenderLine(lX, lY, relativeTransformation.Z, lX, lY + height, relativeTransformation.Z);
                    }
                    for (int y = 0; y <= _height; y++)
                    {
                        float lX = relativeTransformation.X;
                        float lY = (relativeTransformation.Y + (y * (_tileHeight * Math.Abs(relativeTransformation.ScaleY))));
                        GraphicsManager.RenderLine(lX, lY, relativeTransformation.Z, lX + width, lY, relativeTransformation.Z);
                    }

                }
            }

            // Render bits and pieces that are required.
            if (_renderCollisionBox == true || _forceCollisionBoxVisibility == true || _forceGlobalCollisionBoxVisibility) RenderCollisionBox(relativeTransformation, camera);
            if (_renderBoundingBox == true || _forceBoundingBoxVisibility == true || _forceGlobalBoundingBoxVisibility) RenderBoundingBox(relativeTransformation, camera);
            if (_renderSizingPoints == true) RenderSizingPoints(relativeTransformation, camera);
            if (_renderEventLines == true || _forceGlobalEventLineVisibility) RenderEventLines(relativeTransformation, camera);
            if (_forceGlobalDebugVisibility == true) RenderDebug(relativeTransformation, camera);

            // Render all the children of this entity.
            if (_visible == true) RenderChildren(relativeTransformation, camera, layer);
        }
 /// <summary>
 ///		Returns true if this entity is within the viewport of the current render target.
 /// </summary>
 /// <param name="transformation">Position this entity should be checked from.</param>
 /// <returns>True if this entity can be seen.</returns>
 public bool CanBeSeen(Transformation transformation)
 {
     return MathMethods.RectanglesOverlap((int)transformation.X, (int)transformation.Y, (int)(Width * Math.Abs(transformation.ScaleX)), (int)(Height * Math.Abs(transformation.ScaleY)), 0, 0, GraphicsManager.Resolution[0], GraphicsManager.Resolution[1]);
 }
        /// <summary>
        ///		Calculates this entitys relative transformation to another transformation.
        /// </summary>
        /// <param name="transformation">Transformation to calculate relative transformation from.</param>
        /// <returns>Transfromation relative to the given transfromation.</returns>
        public virtual Transformation CalculateRelativeTransformation(Transformation transformation)
        {
            Transformation newTransform = new Transformation();
            newTransform.AngleX = transformation.AngleX + _transformation.AngleX;
            newTransform.AngleY = transformation.AngleY + _transformation.AngleY;
            newTransform.AngleZ = transformation.AngleZ + _transformation.AngleZ;
            newTransform.ScaleX = transformation.ScaleX + Math.Abs(_transformation.ScaleX) - 1.0f;
            newTransform.ScaleY = transformation.ScaleY + Math.Abs(_transformation.ScaleY) - 1.0f;
            newTransform.ScaleZ = transformation.ScaleY + Math.Abs(_transformation.ScaleZ) - 1.0f;
            if (_transformation.ScaleX < 0) newTransform.ScaleX = -newTransform.ScaleX;
            if (_transformation.ScaleY < 0) newTransform.ScaleY = -newTransform.ScaleY;
            if (_transformation.ScaleZ < 0) newTransform.ScaleY = -newTransform.ScaleZ;

            newTransform.X = (float)Math.Truncate(transformation.X + ((_transformation.X + _transformationOffset.X) * transformation.ScaleX));
            newTransform.Y = (float)Math.Truncate(transformation.Y + ((_transformation.Y + _transformationOffset.Y) * transformation.ScaleY));

            // Work out the depth offset.
            float depthOffset = 0;
            switch (_depthMode)
            {
                case EntityDepthMode.AddXCoordinate: depthOffset = newTransform.X; break;
                case EntityDepthMode.AddYCoordinate: depthOffset = newTransform.Y; break;
                case EntityDepthMode.SubtractXCoordinate: depthOffset = -newTransform.X; break;
                case EntityDepthMode.SubtractYCoordinate: depthOffset = -newTransform.Y; break;
            }

            //newTransform.Z = (transformation.Z + _transformation.Z + _transformationOffset.Z) - ((_depthLayer * 1000) + depthOffset);
            newTransform.Z = (transformation.Z + _transformation.Z + _transformationOffset.Z) - depthOffset;

            return newTransform;
        }
        /// <summary>
        ///     Called just before this polygon is put through collision processing.
        /// </summary>
        public void PreCollisionProcessing()
        {
            Transformation parentTransformation = _parent != null ? _parent.CalculateTransformation() : new Transformation(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f);
            Transformation transformation = new Transformation(parentTransformation.X + _transformation.X, parentTransformation.Y + _transformation.Y, parentTransformation.Z + _transformation.Z, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f);

            transformation.X += _collisionRectangle.X;
            transformation.Y += _collisionRectangle.Y;

            _collisionPolygon.Transformation = transformation;
            _collisionPolygon.Solid = _solid;
            if (_collisionPolygon is CollisionRectangle)
            {
                ((CollisionRectangle)_collisionPolygon).BoundingWidth = _collisionRectangle.Width;
                ((CollisionRectangle)_collisionPolygon).BoundingHeight = _collisionRectangle.Height;
            }
        }
        /// <summary>
        ///     Gets a bounding box that encircles both this node and its child nodes.
        /// </summary>
        /// <param name="transformation">Transformation of parent node.</param>
        public virtual RectangleF GetGlobalBoundingBox(Transformation transformation)
        {
            if (_persistent == true)
               return new RectangleF(0,0,0,0);

            Transformation relativeTransformation = CalculateRelativeTransformation(transformation);
            RectangleF box = new RectangleF(relativeTransformation.X, relativeTransformation.Y, Width * relativeTransformation.ScaleX, Height * relativeTransformation.ScaleY);
            foreach (SceneNode node in _childList)
            {
                RectangleF childBox = node.GetGlobalBoundingBox(relativeTransformation);
                if (childBox.X == 0 && childBox.Y == 0 && childBox.Width == 0 && childBox.Height == 0) continue;
                if (childBox.X < box.X)
                {
                    box.Width += (box.X - childBox.X);
                    box.X = childBox.X;
                }
                if (childBox.Y < box.Y)
                {
                    box.Height += (box.Y - childBox.Y);
                    box.Y = childBox.Y;
                }
                if (childBox.Right > box.Right) box.Width += (childBox.Right - box.Right);
                if (childBox.Bottom > box.Bottom) box.Height += (childBox.Bottom - box.Bottom);
            }

            return box;
        }
        /// <summary>
        ///		Renders this entity at a position releative to its parent.
        /// </summary>
        /// <param name="position">Position where this entity's parent node was rendered.</param>
        public override void Render(Transformation transformation, CameraNode camera, int layer)
        {
            // Setup rendering mode.
            //HighPreformanceTimer timer = new HighPreformanceTimer();
            Transformation relativeTransformation = CalculateRelativeTransformation(transformation);
            SetupRenderingState(relativeTransformation);
            //if (timer.DurationMillisecond > 1.0f)
            //    System.Console.WriteLine((_image != null ? _image.URL : this.ToString()) + " worked out transformation in " + timer.DurationMillisecond);
            //timer.Restart();

            // Are we currnetly rendering the layer this entity is on?
            if (layer != _depthLayer)
            {
                if (_visible == true) RenderChildren(relativeTransformation, camera, layer);
                return;
            }

            if ((_visible == true || _forceVisibility == true) && (_renderMode == EntityRenderMode.TiledImage || CanBeSeen(relativeTransformation) == true))
            {
                //Statistics.StoreInt("Nodes Rendered", Statistics.ReadInt("Nodes Rendered") + 1);

                Shader previousShader = null;
                if (_shader != null)
                {
                    previousShader = GraphicsManager.Shader;
                    GraphicsManager.Shader = _shader;
                }

                switch (_renderMode)
                {
                    case EntityRenderMode.Image:
                        if (_image == null) break;
                        GraphicsManager.RenderImage(_image, relativeTransformation.X + (((_boundingRectangle.Width - _image.Width) * transformation.ScaleX) / 2), relativeTransformation.Y + (((_boundingRectangle.Height - _image.Height) * transformation.ScaleY) / 2), relativeTransformation.Z, _frame);
                        break;
                    case EntityRenderMode.ContainedImage:
                        {
                            if (_image == null) break;
                            Rectangle originalViewport = GraphicsManager.Viewport;
                            GraphicsManager.Viewport = new Rectangle((int)relativeTransformation.X,
                                                                     (int)relativeTransformation.Y,
                                                                     (int)(_boundingRectangle.Width * Math.Abs(relativeTransformation.ScaleX)),
                                                                     (int)(_boundingRectangle.Height * Math.Abs(relativeTransformation.ScaleY)));
                            GraphicsManager.RenderImage(_image, relativeTransformation.X + (((_boundingRectangle.Width - _image.Width) * transformation.ScaleX) / 2), relativeTransformation.Y + (((_boundingRectangle.Height - _image.Height) * transformation.ScaleY) / 2), relativeTransformation.Z, _frame);
                            GraphicsManager.Viewport = originalViewport;
                        }
                        break;
                    case EntityRenderMode.TiledImage:
                        if (_image == null) break;
                        GraphicsManager.TileImage(_image, relativeTransformation.X, relativeTransformation.Y, relativeTransformation.Z, _frame);
                        break;
                    case EntityRenderMode.ContainedTiledImage:
                        {
                            if (_image == null) break;
                            Rectangle originalViewport = GraphicsManager.Viewport;
                            GraphicsManager.Viewport = new Rectangle((int)relativeTransformation.X,
                                                                     (int)relativeTransformation.Y,
                                                                     (int)(_boundingRectangle.Width * Math.Abs(relativeTransformation.ScaleX)),
                                                                     (int)(_boundingRectangle.Height * Math.Abs(relativeTransformation.ScaleY)));
                            GraphicsManager.TileImage(_image, relativeTransformation.X, relativeTransformation.Y, relativeTransformation.Z, _frame);
                            GraphicsManager.Viewport = originalViewport;
                        }
                        break;
                    case EntityRenderMode.Oval:
                        GraphicsManager.RenderOval(relativeTransformation.X + (_boundingRectangle.X * transformation.ScaleX), relativeTransformation.Y + (_boundingRectangle.Y * transformation.ScaleY), relativeTransformation.Z, _boundingRectangle.Width, _boundingRectangle.Height);
                        break;
                    case EntityRenderMode.HollowOval:
                        GraphicsManager.RenderOval(relativeTransformation.X + (_boundingRectangle.X * transformation.ScaleX), relativeTransformation.Y + (_boundingRectangle.Y * transformation.ScaleY), relativeTransformation.Z, _boundingRectangle.Width, _boundingRectangle.Height, false);
                        break;
                    case EntityRenderMode.Pixel:
                        GraphicsManager.RenderPixel(relativeTransformation.X + ((_boundingRectangle.Width * transformation.ScaleX) / 2), relativeTransformation.Y + ((_boundingRectangle.Height * transformation.ScaleX) / 2), relativeTransformation.Z);
                        break;
                    case EntityRenderMode.Rectangle:
                        GraphicsManager.RenderRectangle(relativeTransformation.X + (_boundingRectangle.X * transformation.ScaleX), relativeTransformation.Y + (_boundingRectangle.Y * transformation.ScaleY), relativeTransformation.Z, _boundingRectangle.Width, _boundingRectangle.Height);
                        break;
                    case EntityRenderMode.HollowRectangle:
                        GraphicsManager.RenderRectangle(relativeTransformation.X + (_boundingRectangle.X * transformation.ScaleX), relativeTransformation.Y + (_boundingRectangle.Y * transformation.ScaleY), relativeTransformation.Z, _boundingRectangle.Width, _boundingRectangle.Height, false);
                        break;
                    case EntityRenderMode.Text:
                        GraphicsManager.RenderText(_text, relativeTransformation.X + (((_boundingRectangle.Width - GraphicsManager.TextWidth(_text, true)) * transformation.ScaleX) / 2), relativeTransformation.Y + (((_boundingRectangle.Height - GraphicsManager.TextHeight(_text, true)) * transformation.ScaleY) / 2), relativeTransformation.Z, true);
                        break;
                    case EntityRenderMode.Mesh:
                        if (_mesh == null) break;
                        GraphicsManager.RenderMesh(_mesh, relativeTransformation.X + (((_boundingRectangle.Width - _mesh.Width) * transformation.ScaleX) / 2), relativeTransformation.Y + (((_boundingRectangle.Height - _mesh.Height) * transformation.ScaleY) / 2), relativeTransformation.Z);
                        break;
                }

                if (_shader != null)
                    GraphicsManager.Shader = previousShader;
            }

            // Render bits and pieces that are required.
            if (_renderCollisionBox == true || _forceCollisionBoxVisibility == true || _forceGlobalCollisionBoxVisibility) RenderCollisionBox(relativeTransformation, camera);
            if (_renderBoundingBox == true || _forceBoundingBoxVisibility == true || _forceGlobalBoundingBoxVisibility) RenderBoundingBox(relativeTransformation, camera);
            if (_renderSizingPoints == true) RenderSizingPoints(relativeTransformation, camera);
            if (_renderEventLines == true || _forceGlobalEventLineVisibility) RenderEventLines(relativeTransformation, camera);
            if (_forceGlobalDebugVisibility == true) RenderDebug(relativeTransformation, camera);

            //if (timer.DurationMillisecond > 1.0f)
            //    System.Console.WriteLine((_image != null ? _image.URL : this.ToString()) + " rendered in " + timer.DurationMillisecond);

            // Render all the children of this entity.
            if (_visible == true) RenderChildren(relativeTransformation, camera, layer);
        }
        /// <summary>
        ///		Renders all child nodes of this node.
        /// </summary>
        public void RenderChildren(Transformation transformation, CameraNode camera, int layer)
        {
            foreach (SceneNode child in _childList)
            {
                // Not on the same layer? Not got any children? Return.
                if (layer != child.DepthLayer && (child.Children.Count == 0)) continue;

                //HighPreformanceTimer timer = new HighPreformanceTimer();
                GraphicsManager.PushRenderState();
                child.Render(transformation, camera, layer);
                GraphicsManager.PopRenderState();
                //if (timer.DurationMillisecond > 2)
               //     System.Console.WriteLine(child.ToString() + " (" + ((child as EntityNode) != null ? (((EntityNode)child).Image != null ? ((EntityNode)child).Image.URL : "") + "," + ((EntityNode)child).RenderMode : "") + ") - Rendered in " + timer.DurationMillisecond);
            }
        }
        /// <summary>
        ///		Called when the selector tool is selected and an event has been recieved.
        /// </summary>
        /// <param name="firedEvent">Event that caused this update to be called.</param>
        public void UpdateSelector(Event firedEvent)
        {
            if (firedEvent.ID == "mouse_move" && InputManager.KeyDown(KeyCodes.LeftMouse))
            {
                if (_toolMoving == true)
                {
                    _toolMovementDimensions.Width = InputManager.MouseX - _toolMovementDimensions.X;
                    _toolMovementDimensions.Height = InputManager.MouseY - _toolMovementDimensions.Y;
                }
                else if (_movingObject == true)
                {
                    ArrayList groupsMoved = new ArrayList();
                    foreach (EntityNode iterationEntity in _selectedEntityList)
                    {
                        if (iterationEntity.Parent != null && iterationEntity.Parent as GroupNode != null && groupsMoved.Contains(iterationEntity.Parent) == true) continue;
                        EntityNode entity = (iterationEntity.Parent != null && iterationEntity.Parent as GroupNode != null) ? (GroupNode)iterationEntity.Parent : iterationEntity;

                        entity.Move(_xAxisLocked == true ? 0.0f : ((InputManager.MouseX - _moveObjectDimensions.X) / Editor.GlobalInstance.CameraNode.Zoom), _yAxisLocked == true ? 0.0f : ((InputManager.MouseY - _moveObjectDimensions.Y) / Editor.GlobalInstance.CameraNode.Zoom), 0.0f);

                        if (iterationEntity.Parent != null && iterationEntity.Parent as GroupNode != null)
                            groupsMoved.Add(iterationEntity.Parent);
                    }
                    _moveObjectDimensions.X = InputManager.MouseX;
                    _moveObjectDimensions.Y = InputManager.MouseY;
                    _mapChangedSinceSave = true;
                }
                else if (_sizingObject == true)
                {
                    int movementX = _xAxisLocked == true ? 0 : (int)((InputManager.MouseX - _moveObjectDimensions.X) / Editor.GlobalInstance.CameraNode.Zoom), movementY = _yAxisLocked == true ? 0 : (int)((InputManager.MouseY - _moveObjectDimensions.Y) / Editor.GlobalInstance.CameraNode.Zoom);
                    _objectToSize.BoundingRectangle = _objectSizeOriginalBoundingBox;
                    _objectToSize.Transformation = _objectSizeOriginalTransformation;

                    if ((_sizingDirection & SizingDirection.Top) != 0)
                    {
                        _objectToSize.Resize(_objectToSize.BoundingRectangle.Width, Math.Max(_objectToSize.BoundingRectangle.Height + -movementY,_gridHeight));
                        _objectToSize.Position(_objectToSize.Transformation.X, _objectToSize.Transformation.Y + -(_objectToSize.BoundingRectangle.Height - _objectSizeOriginalBoundingBox.Height), _objectToSize.Transformation.Z);
                    }
                    if ((_sizingDirection & SizingDirection.Left) != 0)
                    {
                        _objectToSize.Resize(Math.Max(_objectToSize.BoundingRectangle.Width + -movementX, _gridWidth), _objectToSize.BoundingRectangle.Height);
                        _objectToSize.Position(_objectToSize.Transformation.X + -(_objectToSize.BoundingRectangle.Width - _objectSizeOriginalBoundingBox.Width), _objectToSize.Transformation.Y, _objectToSize.Transformation.Z);
                    }

                    if ((_sizingDirection & SizingDirection.Bottom) != 0)
                        _objectToSize.Resize(_objectToSize.BoundingRectangle.Width, Math.Max(_objectToSize.BoundingRectangle.Height + movementY, _gridHeight));
                    if ((_sizingDirection & SizingDirection.Right) != 0)
                        _objectToSize.Resize(Math.Max(_objectToSize.BoundingRectangle.Width + movementX, _gridWidth), _objectToSize.BoundingRectangle.Height);
                    _mapChangedSinceSave = true;
                }
            }
            else if (firedEvent.ID == "key_pressed" && ((InputEventData)firedEvent.Data).KeyCode == KeyCodes.LeftMouse)
            {
                bool touchingSizingPoint = false;
                Rectangle mouseRectangle = new Rectangle(InputManager.MouseX, InputManager.MouseY, 1, 1);
                foreach (EntityNode node in _selectedEntityList)
                    if (node.RectangleSizingPointsIntersect(mouseRectangle, Editor.GlobalInstance.CameraNode))
                        touchingSizingPoint = true;

                // Was last click in same spot? If so we want to select the new object.
                if (touchingSizingPoint == false && ((_toolMovementDimensions.X == InputManager.MouseX && _toolMovementDimensions.Y == InputManager.MouseY) || (_toolMovementDimensions.Width == 0 || _toolMovementDimensions.Height == 0)))
                {
                    if (InputManager.KeyDown(KeyCodes.LeftControl))
                    {
                        Rectangle intersectRectangle = new Rectangle(InputManager.MouseX, InputManager.MouseY, 1, 1);
                        foreach (SceneNode node in Engine.Engine.GlobalInstance.Map.SceneGraph.EnumerateNodes())
                        {
                            EntityNode entity = node as EntityNode;
                            if (entity == null) continue;

                            if (entity.RectangleBoundingBoxIntersect(intersectRectangle, Editor.GlobalInstance.CameraNode) == true)
                            {
                                if (_selectedEntityList.Contains(entity))
                                    RemoveEntityFromSelection(entity);
                                else
                                    AddEntityToSelection(entity);
                                break;
                            }
                        }

                        SyncronizeWindow();
                    }
                    else
                    {
                        Rectangle intersectRectangle = new Rectangle(InputManager.MouseX, InputManager.MouseY, 1, 1);
                        bool canSelect = (_selectedEntityList.Count == 0);
                        EntityNode selectEntity = null, firstEntity = null;

                        foreach (SceneNode node in Engine.Engine.GlobalInstance.Map.SceneGraph.EnumerateNodes())
                        {
                            EntityNode entity = node as EntityNode;
                            if (entity == null) continue;

                            if (entity.RectangleBoundingBoxIntersect(intersectRectangle, Editor.GlobalInstance.CameraNode) == true)
                            {
                                if (canSelect == true)
                                {
                                    selectEntity = entity;
                                    break;
                                }
                                else if (firstEntity != null)
                                    firstEntity = entity;

                                if (_selectedEntityList.Contains(entity))
                                    canSelect = true;
                            }
                        }
                        if (selectEntity == null)
                            selectEntity = firstEntity;

                        ClearSelection();

                        if (selectEntity != null)
                        {
                            AddEntityToSelection(selectEntity);
                            SyncronizeWindow();
                        }

                        _movingObject = true;
                        _moveObjectDimensions.X = InputManager.MouseX;
                        _moveObjectDimensions.Y = InputManager.MouseY;
                    }
                }
                else
                {
                    ArrayList removeList = new ArrayList();
                    Rectangle intersectRectangle = new Rectangle(InputManager.MouseX, InputManager.MouseY, 1, 1);
                    foreach (SceneNode node in Engine.Engine.GlobalInstance.Map.SceneGraph.EnumerateNodes())
                    {
                        EntityNode entity = node as EntityNode;
                        if (entity == null) continue;
                        if (entity.RectangleBoundingBoxIntersect(intersectRectangle, Editor.GlobalInstance.CameraNode) == false &&
                            entity.RectangleSizingPointsIntersect(intersectRectangle, Editor.GlobalInstance.CameraNode) == false)
                            removeList.Add(entity);
                    }
                    if (!InputManager.KeyDown(KeyCodes.LeftControl))
                    {
                        foreach (EntityNode entity in removeList)
                            RemoveEntityFromSelection(entity);
                    }

                    _movingObject = true;
                    _moveObjectDimensions.X = InputManager.MouseX;
                    _moveObjectDimensions.Y = InputManager.MouseY;
                }

                // Lets select an object or size one.
                foreach (EntityNode entity in _selectedEntityList)
                {
                    Transformation transformation = entity.CalculateTransformation(Editor.GlobalInstance.CameraNode);
                    int x = (int)(transformation.X - (entity.SizingPointsSize / 2)), y = (int)(transformation.Y - (entity.SizingPointsSize / 2));
                    int w = (int)(entity.BoundingRectangle.Width * Editor.GlobalInstance.CameraNode.Zoom), h = (int)(entity.BoundingRectangle.Height * Editor.GlobalInstance.CameraNode.Zoom);

                    bool previousMoving = _movingObject;
                    _movingObject = false;
                    _sizingObject = true;
                    _objectToSize = entity;
                    _objectSizeOriginalBoundingBox = _objectToSize.BoundingRectangle;
                    _objectSizeOriginalTransformation = _objectToSize.Transformation;
                    if (mouseRectangle.IntersectsWith(new Rectangle(x, y, entity.SizingPointsSize, entity.SizingPointsSize)))
                    {
                        _sizingDirection = SizingDirection.Top | SizingDirection.Left;
                        Cursor = Cursors.SizeNWSE;
                    }
                    else if (mouseRectangle.IntersectsWith(new Rectangle(x + w, y, entity.SizingPointsSize, entity.SizingPointsSize)))
                    {
                        _sizingDirection = SizingDirection.Top | SizingDirection.Right;
                        Cursor = Cursors.SizeNESW;
                    }
                    else if (mouseRectangle.IntersectsWith(new Rectangle(x, y + h, entity.SizingPointsSize, entity.SizingPointsSize)))
                    {
                        _sizingDirection = SizingDirection.Bottom | SizingDirection.Left;
                        Cursor = Cursors.SizeNESW;
                    }
                    else if (mouseRectangle.IntersectsWith(new Rectangle(x + w, y + h, entity.SizingPointsSize, entity.SizingPointsSize)))
                    {
                        _sizingDirection = SizingDirection.Bottom | SizingDirection.Right;
                        Cursor = Cursors.SizeNWSE;
                    }
                    else if (mouseRectangle.IntersectsWith(new Rectangle(x + (w / 2), y, entity.SizingPointsSize, entity.SizingPointsSize)))
                    {
                        _sizingDirection = SizingDirection.Top;
                        Cursor = Cursors.SizeNS;
                    }
                    else if (mouseRectangle.IntersectsWith(new Rectangle(x + (w / 2), y + h, entity.SizingPointsSize, entity.SizingPointsSize)))
                    {
                        _sizingDirection = SizingDirection.Bottom;
                        Cursor = Cursors.SizeNS;
                    }
                    else if (mouseRectangle.IntersectsWith(new Rectangle(x, y + (h / 2), entity.SizingPointsSize, entity.SizingPointsSize)))
                    {
                        _sizingDirection = SizingDirection.Left;
                        Cursor = Cursors.SizeWE;
                    }
                    else if (mouseRectangle.IntersectsWith(new Rectangle(x + w, y + (h / 2), entity.SizingPointsSize, entity.SizingPointsSize)))
                    {
                        _sizingDirection = SizingDirection.Right;
                        Cursor = Cursors.SizeWE;
                    }
                    else
                    {
                        _sizingObject = false;
                        _objectToSize = null;
                        _movingObject = previousMoving;
                    }
                }

                // If we are holding shift then we want to select multiple objects.
                if (InputManager.KeyDown(KeyCodes.LeftShift))
                {
                    _movingObject = false;
                    _sizingObject = false;
                    _toolMoving = true;
                }

                // Log the original transformations of the selected nodes.
                _originalEntityTransformations.Clear();
                _originalEntityBoundingBoxs.Clear();
                foreach (EntityNode entity in _selectedEntityList)
                {
                    _originalEntityTransformations.Add(entity, entity.Transformation);
                    _originalEntityBoundingBoxs.Add(entity, entity.BoundingRectangle);
                }

                _toolMovementDimensions.X = InputManager.MouseX;
                _toolMovementDimensions.Y = InputManager.MouseY;
                _toolMovementDimensions.Width = _selectedEntityList.Count > 0 ? 1 : 0;
                _toolMovementDimensions.Height = _selectedEntityList.Count > 0 ? 1 : 0;
            }
            else if (firedEvent.ID == "key_released" && ((InputEventData)firedEvent.Data).KeyCode == KeyCodes.LeftMouse)
            {
                if (_toolMoving == true)
                {
                    _toolMoving = false;

                    // Create a rectangle from which we can check intersection against.
                    Rectangle intersectRectangle = new Rectangle();

                    // If we have a negative width then make it positive.
                    if (_toolMovementDimensions.Width < 0)
                    {
                        intersectRectangle.X = _toolMovementDimensions.X + _toolMovementDimensions.Width;
                        intersectRectangle.Width = Math.Abs(_toolMovementDimensions.Width);
                    }
                    else
                    {
                        intersectRectangle.X = _toolMovementDimensions.X;
                        intersectRectangle.Width = _toolMovementDimensions.Width;
                    }

                    // If we have a negative height then make it positive.
                    if (_toolMovementDimensions.Height < 0)
                    {
                        intersectRectangle.Y = _toolMovementDimensions.Y + _toolMovementDimensions.Height;
                        intersectRectangle.Height = Math.Abs(_toolMovementDimensions.Height);
                    }
                    else
                    {
                        intersectRectangle.Y = _toolMovementDimensions.Y;
                        intersectRectangle.Height = _toolMovementDimensions.Height;
                    }

                    // Unselect all the pervious entitys.
                    ArrayList oldSelectionList = new ArrayList();
                    oldSelectionList.AddRange(_selectedEntityList);

                    // Clear out old selection.
                    if (InputManager.KeyDown(KeyCodes.LeftControl) == false)
                        ClearSelection();

                    // Grab all the entitys within our selection rectangle and select them.
                    bool canSelect = (oldSelectionList.Count == 0 || !(intersectRectangle.Width < 2 && intersectRectangle.Height < 2));
                    EntityNode selectEntity = null;
                    foreach (SceneNode node in Engine.Engine.GlobalInstance.Map.SceneGraph.EnumerateNodes())
                    {
                        EntityNode entity = node as EntityNode;
                        if (entity == null) continue;

                        if (entity.RectangleBoundingBoxIntersect(intersectRectangle, Editor.GlobalInstance.CameraNode) == true)
                        {
                            if (canSelect == true)
                            {
                                AddEntityToSelection(entity);
                                if (intersectRectangle.Width < 2 && intersectRectangle.Height < 2)
                                    break;
                            }
                            else if (selectEntity == null)
                                selectEntity = entity;

                            if (oldSelectionList.Contains(entity))
                                canSelect = true;
                        }
                    }
                    if (canSelect == false && selectEntity != null)
                        AddEntityToSelection(selectEntity);
                }
                else if (_movingObject == true)
                {
                    // Snap the objects to the grid.
                    if (_snapToGrid == true)
                    {
                        foreach (EntityNode entity in _selectedEntityList)
                        {
                            entity.Position(((int)entity.Transformation.X / _gridWidth) * _gridWidth, ((int)entity.Transformation.Y / _gridHeight) * _gridHeight, entity.Transformation.Z);

                            // Create a new transformation undo operation if transformation has changed.
                            Transformation originalTransformation = (Transformation)_originalEntityTransformations[entity];
                            if (originalTransformation != entity.Transformation)
                                PushUndoOperation(new TransformNodeUndoOperation(entity, originalTransformation, entity.Transformation));
                        }
                        _originalEntityTransformations.Clear();
                        if (_entityPropertiesWindow != null && _entityPropertiesWindow.Entity != null)
                            _entityPropertiesWindow.UpdateProperty("location", new Point((int)_entityPropertiesWindow.Entity.Transformation.X, (int)_entityPropertiesWindow.Entity.Transformation.Y));
                    }
                    _movingObject = false;
                    _mapChangedSinceSave = true;
                }
                else if (_sizingObject == true)
                {
                    if (_snapToGrid == true)
                    {
                        _objectToSize.Position(((int)_objectToSize.Transformation.X / _gridWidth) * _gridWidth, ((int)_objectToSize.Transformation.Y / _gridHeight) * _gridHeight, _objectToSize.Transformation.Z);
                        _objectToSize.BoundingRectangle = new Rectangle(_objectToSize.BoundingRectangle.X, _objectToSize.BoundingRectangle.Y, ((int)_objectToSize.BoundingRectangle.Width / _gridWidth) * _gridWidth, ((int)_objectToSize.BoundingRectangle.Height / _gridHeight) * _gridHeight);

                        Rectangle newBoundingRectangle = _objectToSize.BoundingRectangle;
                        if (newBoundingRectangle.Width < _gridWidth) newBoundingRectangle.Width = _gridWidth;
                        if (newBoundingRectangle.Height < _gridHeight) newBoundingRectangle.Height = _gridHeight;
                        _objectToSize.BoundingRectangle = newBoundingRectangle;

                        // Create a new sizing undo operation if size has changed.
                        Rectangle originalBoundingBox = (Rectangle)_originalEntityBoundingBoxs[_objectToSize];
                        if (!_objectToSize.BoundingRectangle.Equals(originalBoundingBox))
                            PushUndoOperation(new ResizeNodeUndoOperation(_objectToSize, originalBoundingBox, (Transformation)_originalEntityTransformations[_objectToSize], _objectToSize.BoundingRectangle, _objectToSize.Transformation));
                    }
                    if (_entityPropertiesWindow != null && _entityPropertiesWindow.Entity != null)
                    {
                        _entityPropertiesWindow.UpdateProperty("bounding rectangle", new Rectangle(_entityPropertiesWindow.Entity.BoundingRectangle.X, _entityPropertiesWindow.Entity.BoundingRectangle.Y, _entityPropertiesWindow.Entity.BoundingRectangle.Width, _entityPropertiesWindow.Entity.BoundingRectangle.Height));
                        _entityPropertiesWindow.UpdateProperty("collision rectangle", new Rectangle(_entityPropertiesWindow.Entity.CollisionRectangle.X, _entityPropertiesWindow.Entity.CollisionRectangle.Y, _entityPropertiesWindow.Entity.CollisionRectangle.Width, _entityPropertiesWindow.Entity.CollisionRectangle.Height));
                    }
                    Cursor = Cursors.Arrow;
                    _sizingObject = false;
                    _mapChangedSinceSave = true;
                }

                SyncronizeWindow();
            }
        }
 /// <summary>
 ///		Renders this entity at a position releative to its parent.
 /// </summary>
 /// <param name="position">Position where this entity's parent node was rendered.</param>
 public override void Render(Transformation transformation, CameraNode camera, int layer)
 {
     if (layer == _depthLayer && _process != null && _process.Process != null && _renderFunction != null)
         _process.Process[0].InvokeFunction(_renderFunction, true, true, true);
     base.Render(transformation, camera, layer);
 }
        /// <summary>
        ///		Calculates this entitys relative transformation to another transformation.
        /// </summary>
        /// <param name="transformation">Transformation to calculate relative transformation from.</param>
        /// <returns>Transfromation relative to the given transfromation.</returns>
        public override Transformation CalculateRelativeTransformation(Transformation transformation)
        {
            Transformation newTransform = new Transformation();

            newTransform.AngleX = transformation.AngleX + _transformation.AngleX;
            newTransform.AngleY = transformation.AngleY + _transformation.AngleY;
            newTransform.AngleZ = transformation.AngleZ + _transformation.AngleZ;
            newTransform.ScaleX = (transformation.ScaleX + (_transformation.ScaleX - 1.0f)) + (_zoom - 1.0f);
            newTransform.ScaleY = (transformation.ScaleY + (_transformation.ScaleY - 1.0f)) + (_zoom - 1.0f);
            newTransform.ScaleZ = (transformation.ScaleZ + (_transformation.ScaleZ - 1.0f)) + (_zoom - 1.0f);

            newTransform.X = (float)Math.Ceiling(((transformation.X - (_transformation.X + _transformationOffset.X)) * newTransform.ScaleX) + _viewport.X);
            newTransform.Y = (float)Math.Ceiling(((transformation.Y - (_transformation.Y + _transformationOffset.Y)) * newTransform.ScaleY) + _viewport.Y);
            newTransform.Z = transformation.Z + (_transformation.Z + _transformationOffset.Z);

            return newTransform;
        }