root/src/BRAINSFramework/QuadTree/QuadTreePositionItem.cs

User picture

Author: conkerjo

Revision: 30 («Previous)


File Size: 4.07 KB

(July 01, 2009 23:02 UTC) Almost 3 years ago


  

 
Show/hide line numbers
// *******************************************//
// *********Credits to Kyle Schouviller*******//
// *******for the original implementation*****//
// *******************************************//
using System;
using Microsoft.Xna.Framework;

namespace Brains.Framework.QuadTree
{
    /// <summary>
    /// A position item in a quadtree
    /// </summary>
    /// <typeparam name="T">The type of the QuadTree item's parent</typeparam>
    public class QuadTreePositionItem<T>
    {
        public QuadTreeNode<T> node;
        internal QuadTree<T> quadTree;
        

        /// <summary>
        /// Handles the move event
        /// </summary>
        internal void OnMove()
        {
            // Update rectangles
            rect.TopLeft = position - (size * .5f);
            rect.BottomRight = position + (size * .5f);

            // Call event handler
            // if (Move != null) Move(this);
            hasMoved = true;
            if (node != null) node.ItemMove(this);
        }
        internal bool hasMoved = false;
        /// <summary>
        /// Handles the destroy event
        /// </summary>
        protected void OnDestroy()
        {
            //if (Destroy != null) Destroy(this);
            node.RemoveItem(this);
        }

        

        

        /// <summary>
        /// The center position of this item
        /// </summary>
        private Vector2 position;

        /// <summary>
        /// Gets or sets the center position of this item
        /// </summary>
        public Vector2 Position
        {
            get { return position; }
            set
            {
                position = value;
                OnMove();
                if (node != null)
                {
                    if (node.ContainsRect(this.rect))
                        return;
                  //  node.RemoveItem(this);
                }
               // if (quadTree!= null)
                 //   quadTree.Insert(this);
                
            }
        }

        /// <summary>
        /// The size of this item
        /// </summary>
        private Vector2 size;

        /// <summary>
        /// Gets or sets the size of this item
        /// </summary>
        public Vector2 Size
        {
            get { return size; }
            set
            {
                size = value;
                rect.TopLeft = position - (size / 2f);
                rect.BottomRight = position + (size / 2f);
                OnMove();
            }
        }

        /// <summary>
        /// The rectangle containing this item
        /// </summary>
        private RectangleF rect;

        /// <summary>
        /// Gets a rectangle containing this item
        /// </summary>
        public RectangleF Rect
        {
            get { return rect; }
        }

        /// <summary>
        /// The parent of this item
        /// </summary>
        /// <remarks>The Parent accessor is used to gain access to the item controlling this position item</remarks>
        private T parent;

        /// <summary>
        /// Gets the parent of this item
        /// </summary>
        public T Parent
        {
            get { return parent; }
        }

        

        

        /// <summary>
        /// Creates a position item in a QuadTree
        /// </summary>
        /// <param name="parent">The parent of this item</param>
        /// <param name="position">The position of this item</param>
        /// <param name="size">The size of this item</param>
        public QuadTreePositionItem(T parent, Vector2 position, Vector2 size)
        {
            this.rect = new RectangleF(0f, 0f, 1f, 1f);

            this.parent = parent;
            this.position = position ;
            this.size = size;
            OnMove();
        }

        

        

        /// <summary>
        /// Destroys this item and removes it from the QuadTree
        /// </summary>
        public void Delete()
        {
            OnDestroy();
        }

        
    }

}