Rev 233 |
Rev 238 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
package com.gebauz.Bauzoid.graphics.sprite;
import com.badlogic.gdx.graphics.Texture;
import com.gebauz.Bauzoid.graphics.Graphics;
/** Sprite atlas.
* Implements a sprite that defines multiple regions that can be used
* as Sprite Atlas or as Animated Sprite.
*
*/
public class AtlasSprite
extends Sprite
{
public static class Region
{
public float left
;
public float top
;
public float bottom
;
public float right
;
/** Constructor. */
public Region(Texture texture,
float x,
float y,
float w,
float h
)
{
setAbsolute
(texture, x, y, w, h
);
}
/** Constructor - sets region by relative texture coordinates. */
public Region(float _top,
float _left,
float _right,
float _bottom
)
{
top = _top
;
left = _left
;
right = _right
;
bottom = _bottom
;
}
/** Set region by absolute pixel coordinates relative to the specified texture.
* Coordinates are in absolute texel space.
*/
public void setAbsolute
(Texture texture,
float x,
float y,
float w,
float h
)
{
left = x / texture.
getWidth();
top = y / texture.
getHeight();
right = left +
(w / texture.
getWidth());
bottom = top +
(h / texture.
getHeight());
}
};
private Region[] mRegions =
null;
/** Constructor. */
public AtlasSprite
(Graphics graphics, Texture texture,
Region[] regions
)
{
super(graphics, texture
);
mRegions = regions
;
}
public void dispose
()
{
super.
dispose();
mRegions =
null;
}
public AtlasSpriteInstance createSpriteInstance
(int regionIndex
)
{
return new AtlasSpriteInstance
(this, regionIndex
);
}
public void update
(float deltaTime
)
{
}
public void render
()
{
}
public void render
(int regionIndex
)
{
if (mRegions ==
null)
return;
Region r = mRegions
[regionIndex
];
float[] texCoords =
{
r.
left, r.
top,
r.
right, r.
top,
r.
right, r.
bottom,
r.
left, r.
bottom
};
mMesh.
setTexCoords(texCoords
);
super.
render();
}
/** Set absolute coordinates for given region. */
public void setAbsolute
(int regionIndex,
float x,
float y,
float w,
float h
)
{
if ((mTexture ==
null) ||
(mRegions ==
null) ||
(regionIndex
>= mRegions.
length))
return;
if (mRegions
[regionIndex
] ==
null)
{
// create region
mRegions
[regionIndex
] =
new Region(x, y, w, h
);
return;
}
mRegions
[regionIndex
].
setAbsolute(mTexture, x, y, w, h
);
}
/** Get number of defined regions. */
public final int getRegionCount
()
{
if (mRegions ==
null)
return 0;
return mRegions.
length;
}
/** Get region. */
public final Region getRegion
(int i
)
{
if (mRegions ==
null)
return null;
return mRegions
[i
];
}
/** Get a region's width. */
public final float getRegionWidth
(int i
)
{
if (mRegions ==
null)
return 0.0f
;
return (mRegions
[i
].
right - mRegions
[i
].
left) * mTexture.
getWidth();
}
/** Get a region's height. */
public final float getRegionHeight
(int i
)
{
if (mRegions ==
null)
return 0.0f
;
return (mRegions
[i
].
bottom - mRegions
[i
].
top) * mTexture.
getHeight();
}
}