Subversion Repositories AndroidProjects

Rev

Rev 238 | 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
{
        private SpriteRegion[] mRegions = null;
       
        /** Constructor. */
        public AtlasSprite(Graphics graphics, Texture texture, SpriteRegion[] regions)
        {
                super(graphics, texture);
               
                mRegions = regions;
        }
       
        public void dispose()
        {
                super.dispose();
                mRegions = null;
        }
       
        public final AtlasSpriteInstance createSpriteInstance(int regionIndex)
        {
                return new AtlasSpriteInstance(this, regionIndex);
        }
               
        public void update(float deltaTime)
        {
               
        }
       
        public void render()
        {
        }
       
        public void render(int regionIndex)
        {
                render(regionIndex, x, y, w, h);
        }
       
        public void render(int regionIndex, float _x, float _y)
        {
                render(regionIndex, _x, _y, w, h);
        }
       
        public void render(int regionIndex, float _x, float _y, float _w, float _h)
        {
                if (mRegions == null)
                        return;
               
                SpriteRegion 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(_x, _y, _w, _h);
        }

        /** 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 SpriteRegion(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 SpriteRegion 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();
        }
       
}