Subversion Repositories AndroidProjects

Rev

Blame | Last modification | View Log | RSS feed

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using BauzoidNET.graphics.texture;

namespace BauzoidNET.graphics.sprite
{
    public class SpriteRegion
    {
        public float left = 0;
        public float top = 0;
        public float bottom = 0;
        public float right = 0;

        private Sprite mParentSprite = null;
        private int mRegionIndex = -1;


        /** Constructor. */
            public SpriteRegion(Sprite parentSprite, int index)
            {
                    mParentSprite = parentSprite;
                    mRegionIndex = index;
            }
       
            /** Constructor - sets region by relative texture coordinates. */
            public SpriteRegion(Sprite parentSprite, int index, float relativeTop, float relativeLeft, float relativeRight, float relativeBottom) :
            this(parentSprite, index, relativeTop, relativeLeft, relativeRight - relativeTop, relativeBottom - relativeTop, false)
            {
            }
       
            /** Constructor - sets region by absolute texture coordinates in texels. Requires reference texture to calculate relative texture coordinates. */
            public SpriteRegion(Sprite parentSprite, int index, float x, float y, float w, float h, bool absolute) :
            this(parentSprite, index)
            {
                    if (absolute)
                            setAbsolute(parentSprite.getTexture(), x, y, w, h);
                    else
                            setRelative(x, y, w, h);
            }
       
            // Getters/Setters==================================================================================
       
            /** Set region by relative texel coordinates.
                * Coordinates are in relative space [0..1].
                */

            public void setRelative(float x, float y, float w, float h)
            {
                    left = x;
                    top = y;
                    right = left + w;
                    bottom = top + h;
            }
       
            /** Set region by absolute pixel coordinates relative to the specified texture.
                * Coordinates are in absolute texel space.
                */

            public void setAbsolute(Texture referenceTexture, float x, float y, float w, float h)
            {
                    left = x / referenceTexture.getWidth();
                    top = y / referenceTexture.getHeight();
                    right = left + (w / referenceTexture.getWidth());
                    bottom = top + (h / referenceTexture.getHeight());
            }
       
            /** Get the region's width in relative texel coordinates. */
            public float getWidth()
            {
                    return (right-left);
            }
       
            /** Get the region's height in relative texel coordinates. */
            public float getHeight()
            {
                    return (bottom-top);
            }
       
            /** Get the region's parent sprite. */
            public Sprite getSprite()
            {
                    return mParentSprite;
            }
       
            /** Get the region's index. */
            public int getRegionIndex()
            {
                    return mRegionIndex;
            }
    }
}