Blame |
Last modification |
View Log
| RSS feed
package com.gebauz.bauzoid.graphics.sprite;
import com.badlogic.gdx.graphics.Texture;
/** A region that addresses a single rectangular region inside a sprite's texture. */
public class SpriteRegion
{
// Constants========================================================================================
// Embedded Types===================================================================================
// Fields===========================================================================================
public float left = 0;
public float top = 0;
public float bottom = 0;
public float right = 0;
private Sprite mParentSprite = null;
private int mRegionIndex = -1;
// Methods==========================================================================================
/** 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, boolean 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 final int getRegionIndex()
{
return mRegionIndex;
}
}