Rev 238 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 226 | chris | 1 | package com.gebauz.Bauzoid.graphics.sprite; |
| 2 | |||
| 3 | import com.badlogic.gdx.graphics.Texture; |
||
| 4 | import com.gebauz.Bauzoid.graphics.Graphics; |
||
| 5 | |||
| 6 | /** Sprite atlas. |
||
| 7 | * Implements a sprite that defines multiple regions that can be used |
||
| 8 | * as Sprite Atlas or as Animated Sprite. |
||
| 9 | * |
||
| 10 | */ |
||
| 11 | public class AtlasSprite extends Sprite |
||
| 12 | { |
||
| 238 | chris | 13 | private SpriteRegion[] mRegions = null; |
| 226 | chris | 14 | |
| 233 | chris | 15 | /** Constructor. */ |
| 238 | chris | 16 | public AtlasSprite(Graphics graphics, Texture texture, SpriteRegion[] regions) |
| 226 | chris | 17 | { |
| 18 | super(graphics, texture); |
||
| 19 | |||
| 20 | mRegions = regions; |
||
| 21 | } |
||
| 22 | |||
| 23 | public void dispose() |
||
| 24 | { |
||
| 25 | super.dispose(); |
||
| 26 | mRegions = null; |
||
| 27 | } |
||
| 236 | chris | 28 | |
| 238 | chris | 29 | public final AtlasSpriteInstance createSpriteInstance(int regionIndex) |
| 236 | chris | 30 | { |
| 31 | return new AtlasSpriteInstance(this, regionIndex); |
||
| 32 | } |
||
| 233 | chris | 33 | |
| 226 | chris | 34 | public void update(float deltaTime) |
| 35 | { |
||
| 36 | |||
| 37 | } |
||
| 38 | |||
| 39 | public void render() |
||
| 40 | { |
||
| 233 | chris | 41 | } |
| 42 | |||
| 43 | public void render(int regionIndex) |
||
| 44 | { |
||
| 239 | chris | 45 | render(regionIndex, x, y, w, h); |
| 46 | } |
||
| 47 | |||
| 48 | public void render(int regionIndex, float _x, float _y) |
||
| 49 | { |
||
| 50 | render(regionIndex, _x, _y, w, h); |
||
| 51 | } |
||
| 52 | |||
| 53 | public void render(int regionIndex, float _x, float _y, float _w, float _h) |
||
| 54 | { |
||
| 233 | chris | 55 | if (mRegions == null) |
| 56 | return; |
||
| 226 | chris | 57 | |
| 238 | chris | 58 | SpriteRegion r = mRegions[regionIndex]; |
| 233 | chris | 59 | |
| 60 | float[] texCoords = { |
||
| 61 | r.left, r.top, |
||
| 62 | r.right, r.top, |
||
| 63 | r.right, r.bottom, |
||
| 64 | r.left, r.bottom |
||
| 65 | }; |
||
| 66 | |||
| 67 | mMesh.setTexCoords(texCoords); |
||
| 68 | |||
| 239 | chris | 69 | super.render(_x, _y, _w, _h); |
| 226 | chris | 70 | } |
| 71 | |||
| 233 | chris | 72 | /** Set absolute coordinates for given region. */ |
| 73 | public void setAbsolute(int regionIndex, float x, float y, float w, float h) |
||
| 74 | { |
||
| 75 | if ((mTexture == null) || (mRegions == null) || (regionIndex >= mRegions.length)) |
||
| 76 | return; |
||
| 77 | |||
| 78 | if (mRegions[regionIndex] == null) |
||
| 79 | { |
||
| 80 | // create region |
||
| 238 | chris | 81 | mRegions[regionIndex] = new SpriteRegion(x, y, w, h); |
| 233 | chris | 82 | return; |
| 83 | } |
||
| 84 | |||
| 85 | mRegions[regionIndex].setAbsolute(mTexture, x, y, w, h); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** Get number of defined regions. */ |
||
| 89 | public final int getRegionCount() |
||
| 90 | { |
||
| 91 | if (mRegions == null) |
||
| 92 | return 0; |
||
| 93 | |||
| 94 | return mRegions.length; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** Get region. */ |
||
| 238 | chris | 98 | public final SpriteRegion getRegion(int i) |
| 233 | chris | 99 | { |
| 100 | if (mRegions == null) |
||
| 101 | return null; |
||
| 102 | |||
| 103 | return mRegions[i]; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** Get a region's width. */ |
||
| 107 | public final float getRegionWidth(int i) |
||
| 108 | { |
||
| 109 | if (mRegions == null) |
||
| 110 | return 0.0f; |
||
| 111 | |||
| 112 | return (mRegions[i].right - mRegions[i].left) * mTexture.getWidth(); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** Get a region's height. */ |
||
| 116 | public final float getRegionHeight(int i) |
||
| 117 | { |
||
| 118 | if (mRegions == null) |
||
| 119 | return 0.0f; |
||
| 120 | |||
| 121 | return (mRegions[i].bottom - mRegions[i].top) * mTexture.getHeight(); |
||
| 122 | } |
||
| 123 | |||
| 226 | chris | 124 | } |
| 233 | chris | 125 | |
| 126 | |||
| 127 |