Beispiel #1
0
        /// <summary>
        /// Function to add a node as a child to this node.
        /// </summary>
        /// <param name="dimensions">Dimensions that the node will occupy.</param>
        public SpriteNode AddNode(DX.Size2 dimensions)
        {
            if (!IsLeaf)
            {
                return(Left.IsLeaf ? Left.AddNode(dimensions) ?? Right.AddNode(dimensions)
                                   : Right.AddNode(dimensions) ?? Left.AddNode(dimensions));
            }

            // Do nothing if there's no more room.
            if (_noMoreRoom)
            {
                return(null);
            }

            // Ensure we can fit this node.
            if ((dimensions.Width > Region.Width) || (dimensions.Height > Region.Height))
            {
                return(null);
            }

            // We have an exact fit, so there will be no more room for other nodes.
            if ((dimensions.Width == Region.Width) && (dimensions.Height == Region.Height))
            {
                _noMoreRoom = true;
                return(this);
            }

            // The region occupied by the requested dimensions.
            Left  = new SpriteNode(this);
            Right = new SpriteNode(this);

            // Subdivide.
            var delta = new DX.Size2(Region.Width - dimensions.Width, Region.Height - dimensions.Height);

            if (delta.Height <= 0) //(delta.Width > delta.Height)
            {
                Left.Region  = new DX.Rectangle(Region.Left, Region.Top, dimensions.Width, Region.Height);
                Right.Region = new DX.Rectangle(Region.Left + dimensions.Width, Region.Top, delta.Width, Region.Height);
            }
            else
            {
                Left.Region  = new DX.Rectangle(Region.Left, Region.Top, Region.Width, dimensions.Height);
                Right.Region = new DX.Rectangle(Region.Left, Region.Top + dimensions.Height, Region.Width, delta.Height);
            }

            return(Left.AddNode(dimensions) ?? Right.AddNode(dimensions));
        }
Beispiel #2
0
        /// <summary>
        /// Function to add a node to the
        /// </summary>
        /// <param name="dimensions">The sprite dimensions.</param>
        /// <returns>A rectangle for the area on the image that the sprite will be located at, or <b>null</b> if there's no room.</returns>
        public static DX.Rectangle?Add(DX.Size2 dimensions)
        {
            if ((dimensions.Width > Root.Region.Width) || (dimensions.Height > Root.Region.Height))
            {
                return(null);
            }

            // Do nothing here.
            if ((dimensions.Width == 0) || (dimensions.Height == 0))
            {
                return(DX.Rectangle.Empty);
            }

            SpriteNode newNode = Root.AddNode(dimensions);

            return(newNode?.Region);
        }
Beispiel #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SpriteNode"/> class.
 /// </summary>
 /// <param name="parentNode">The parent node.</param>
 public SpriteNode(SpriteNode parentNode)
 {
     Region = DX.Rectangle.Empty;
     Parent = parentNode;
 }
Beispiel #4
0
 /// <summary>
 /// Function to create the root node.
 /// </summary>
 /// <param name="textureWidth">The width of the texture.</param>
 /// <param name="textureHeight">The height of the texture.</param>
 public static void CreateRoot(int textureWidth, int textureHeight) => Root = new SpriteNode(null)
 {
     Region = new DX.Rectangle(0, 0, textureWidth, textureHeight)
 };