Rev 899 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 835 | chris | 1 | package com.gebauz.bauzoid.menu; |
| 2 | |||
| 3 | import com.gebauz.bauzoid.game.Game; |
||
| 4 | import com.gebauz.bauzoid.game.GameObject; |
||
| 5 | import com.gebauz.bauzoid.graphics.sprite.AtlasSprite; |
||
| 915 | chris | 6 | import com.gebauz.bauzoid.graphics.sprite.AtlasSpriteFrame; |
| 835 | chris | 7 | import com.gebauz.bauzoid.graphics.sprite.SpriteRegion; |
| 8 | import com.gebauz.bauzoid.graphics.sprite.TileBatch; |
||
| 9 | import com.gebauz.bauzoid.math.Rect; |
||
| 10 | import com.gebauz.bauzoid.math.Vector4; |
||
| 11 | |||
| 12 | public class FrameElement extends GameObject |
||
| 13 | { |
||
| 14 | // TODO: make customizeable cell size |
||
| 15 | public static final float CELL_SIZE = 32.0f; |
||
| 16 | |||
| 17 | public static final float TOP_OFFSET_ = 18.0f; |
||
| 18 | public static final float LEFT_OFFSET_ = 8.0f; |
||
| 19 | public static final float RIGHT_OFFSET_ = 8.0f; |
||
| 20 | public static final float BOTTOM_OFFSET_ = 12.0f; |
||
| 21 | |||
| 22 | /* public static final float TOP_OFFSET = CELL_SIZE-18.0f; |
||
| 23 | public static final float LEFT_OFFSET = CELL_SIZE-8.0f; |
||
| 24 | public static final float RIGHT_OFFSET = CELL_SIZE-8.0f; |
||
| 25 | public static final float BOTTOM_OFFSET = CELL_SIZE-12.0f;*/ |
||
| 26 | |||
| 27 | public enum FrameRegion |
||
| 28 | { |
||
| 29 | TOP_LEFT, |
||
| 30 | TOP_CENTER, |
||
| 31 | TOP_RIGHT, |
||
| 32 | MID_LEFT, |
||
| 33 | MID_CENTER, |
||
| 34 | MID_RIGHT, |
||
| 35 | BOTTOM_LEFT, |
||
| 36 | BOTTOM_CENTER, |
||
| 37 | BOTTOM_RIGHT, |
||
| 38 | } |
||
| 39 | |||
| 40 | public float x = 0.0f; |
||
| 41 | public float y = 0.0f; |
||
| 42 | public float w = 0.0f; |
||
| 43 | public float h = 0.0f; |
||
| 44 | |||
| 45 | public Vector4 color = new Vector4(1, 1, 1, 1); |
||
| 46 | |||
| 47 | private AtlasSprite mSprite = null; |
||
| 915 | chris | 48 | private AtlasSpriteFrame[] mSprites = new AtlasSpriteFrame[FrameRegion.values().length]; |
| 835 | chris | 49 | |
| 50 | /** Offsets for the frame, i.e. amount of pixels the actual inside-area is in from left/top/right/bottom. */ |
||
| 51 | private Rect mInnerOffset = null; |
||
| 52 | |||
| 53 | private boolean mIncludeMidCenter = false; |
||
| 54 | |||
| 55 | public FrameElement(Game game) |
||
| 56 | { |
||
| 57 | super(game); |
||
| 58 | } |
||
| 59 | |||
| 60 | public void init(String textureFile, boolean includeMidCenter) |
||
| 61 | { |
||
| 62 | mIncludeMidCenter = includeMidCenter; |
||
| 63 | |||
| 64 | mSprite = new AtlasSprite(getGame().getGraphics(), textureFile); |
||
| 65 | mSprite.init(); |
||
| 66 | |||
| 67 | SpriteRegion[] regions = new SpriteRegion[FrameRegion.values().length]; |
||
| 68 | for (int i = 0; i < regions.length; i++) |
||
| 69 | { |
||
| 70 | // offset by 1 to account for the margin between regions |
||
| 71 | Vector4 r = getRegion(FrameRegion.values()[i]); |
||
| 72 | regions[i] = new SpriteRegion(mSprite.getTexture(), r.x, r.y, r.z, r.w); |
||
| 73 | } |
||
| 74 | mSprite.setRegions(regions); |
||
| 75 | //mSprite = new AtlasSprite(getGame().getGraphics(), tex, regions); |
||
| 76 | |||
| 77 | for (int i = 0; i < regions.length; i++) |
||
| 78 | { |
||
| 79 | mSprites[i] = mSprite.createSpriteInstance(i); |
||
| 80 | } |
||
| 81 | |||
| 82 | if (mInnerOffset == null) |
||
| 83 | { |
||
| 84 | mInnerOffset = new Rect(LEFT_OFFSET_, TOP_OFFSET_, RIGHT_OFFSET_, BOTTOM_OFFSET_); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | public void exit() |
||
| 89 | { |
||
| 90 | |||
| 91 | } |
||
| 92 | |||
| 93 | public void update(float deltaTime) |
||
| 94 | { |
||
| 95 | |||
| 96 | } |
||
| 97 | |||
| 98 | public void render() |
||
| 99 | { |
||
| 100 | TileBatch batch = getGraphics().getBatch(); |
||
| 101 | |||
| 102 | //batch.begin(); |
||
| 103 | |||
| 104 | if (mIncludeMidCenter) |
||
| 105 | renderMidCenter(batch); |
||
| 106 | |||
| 107 | renderTopLeft(batch); |
||
| 108 | renderTopCenter(batch); |
||
| 109 | renderTopRight(batch); |
||
| 110 | renderMidLeft(batch); |
||
| 111 | renderMidRight(batch); |
||
| 112 | renderBottomLeft(batch); |
||
| 113 | renderBottomCenter(batch); |
||
| 114 | renderBottomRight(batch); |
||
| 115 | |||
| 116 | //batch.end(); |
||
| 117 | } |
||
| 118 | |||
| 119 | public void renderTopLeft(TileBatch batch) |
||
| 120 | { |
||
| 915 | chris | 121 | AtlasSpriteFrame s = mSprites[FrameRegion.TOP_LEFT.ordinal()]; |
| 899 | chris | 122 | s.param.x = x + mInnerOffset.left; |
| 123 | s.param.y = y + mInnerOffset.top; |
||
| 124 | s.param.w = CELL_SIZE; |
||
| 125 | s.param.h = CELL_SIZE; |
||
| 126 | s.param.pivotX = s.param.w; |
||
| 127 | s.param.pivotY = s.param.h; |
||
| 128 | s.param.color = color; |
||
| 835 | chris | 129 | //s.render(); |
| 130 | batch.drawSprite(s); |
||
| 131 | } |
||
| 132 | |||
| 133 | public void renderTopCenter(TileBatch batch) |
||
| 134 | { |
||
| 915 | chris | 135 | AtlasSpriteFrame s = mSprites[FrameRegion.TOP_CENTER.ordinal()]; |
| 899 | chris | 136 | s.param.x = x + mInnerOffset.left; |
| 137 | s.param.y = y + mInnerOffset.top; |
||
| 138 | s.param.w = w - mInnerOffset.left - mInnerOffset.right; |
||
| 139 | s.param.h = CELL_SIZE; |
||
| 140 | s.param.pivotX = 0; |
||
| 141 | s.param.pivotY = s.param.h; |
||
| 142 | s.param.color = color; |
||
| 835 | chris | 143 | //s.render(); |
| 144 | batch.drawSprite(s); |
||
| 145 | } |
||
| 146 | |||
| 147 | public void renderTopRight(TileBatch batch) |
||
| 148 | { |
||
| 915 | chris | 149 | AtlasSpriteFrame s = mSprites[FrameRegion.TOP_RIGHT.ordinal()]; |
| 899 | chris | 150 | s.param.x = x + w - mInnerOffset.right; |
| 151 | s.param.y = y + mInnerOffset.top; |
||
| 152 | s.param.w = CELL_SIZE; |
||
| 153 | s.param.h = CELL_SIZE; |
||
| 154 | s.param.pivotX = 0; |
||
| 155 | s.param.pivotY = s.param.h; |
||
| 156 | s.param.color = color; |
||
| 835 | chris | 157 | //s.render(); |
| 158 | batch.drawSprite(s); |
||
| 159 | } |
||
| 160 | |||
| 161 | public void renderMidLeft(TileBatch batch) |
||
| 162 | { |
||
| 915 | chris | 163 | AtlasSpriteFrame s = mSprites[FrameRegion.MID_LEFT.ordinal()]; |
| 899 | chris | 164 | s.param.x = x + mInnerOffset.left; |
| 165 | s.param.y = y + mInnerOffset.top; |
||
| 166 | s.param.w = CELL_SIZE; |
||
| 167 | s.param.h = h - mInnerOffset.top - mInnerOffset.bottom; |
||
| 168 | s.param.pivotX = s.param.w; |
||
| 169 | s.param.pivotY = 0; |
||
| 170 | s.param.color = color; |
||
| 835 | chris | 171 | //s.render(); |
| 172 | batch.drawSprite(s); |
||
| 173 | } |
||
| 174 | |||
| 175 | public void renderMidCenter(TileBatch batch) |
||
| 176 | { |
||
| 915 | chris | 177 | AtlasSpriteFrame s = mSprites[FrameRegion.MID_CENTER.ordinal()]; |
| 899 | chris | 178 | s.param.x = x + mInnerOffset.left; |
| 179 | s.param.y = y + mInnerOffset.top; |
||
| 180 | s.param.w = w - mInnerOffset.left - mInnerOffset.right; |
||
| 181 | s.param.h = h - mInnerOffset.top - mInnerOffset.bottom; |
||
| 182 | s.param.pivotX = 0; |
||
| 183 | s.param.pivotY = 0; |
||
| 184 | s.param.color = color; |
||
| 835 | chris | 185 | //s.render(); |
| 186 | batch.drawSprite(s); |
||
| 187 | } |
||
| 188 | |||
| 189 | public void renderMidRight(TileBatch batch) |
||
| 190 | { |
||
| 915 | chris | 191 | AtlasSpriteFrame s = mSprites[FrameRegion.MID_RIGHT.ordinal()]; |
| 899 | chris | 192 | s.param.x = x + w - mInnerOffset.right; |
| 193 | s.param.y = y + mInnerOffset.top; |
||
| 194 | s.param.w = CELL_SIZE; |
||
| 195 | s.param.h = h - mInnerOffset.top - mInnerOffset.bottom; |
||
| 196 | s.param.pivotX = 0; |
||
| 197 | s.param.pivotY = 0; |
||
| 198 | s.param.color = color; |
||
| 835 | chris | 199 | //s.render(); |
| 200 | batch.drawSprite(s); |
||
| 201 | } |
||
| 202 | |||
| 203 | public void renderBottomLeft(TileBatch batch) |
||
| 204 | { |
||
| 915 | chris | 205 | AtlasSpriteFrame s = mSprites[FrameRegion.BOTTOM_LEFT.ordinal()]; |
| 899 | chris | 206 | s.param.x = x + mInnerOffset.left; |
| 207 | s.param.y = y + h - mInnerOffset.bottom; |
||
| 208 | s.param.w = CELL_SIZE; |
||
| 209 | s.param.h = CELL_SIZE; |
||
| 210 | s.param.pivotX = s.param.w; |
||
| 211 | s.param.pivotY = 0; |
||
| 212 | s.param.color = color; |
||
| 835 | chris | 213 | //s.render(); |
| 214 | batch.drawSprite(s); |
||
| 215 | } |
||
| 216 | |||
| 217 | public void renderBottomCenter(TileBatch batch) |
||
| 218 | { |
||
| 915 | chris | 219 | AtlasSpriteFrame s = mSprites[FrameRegion.BOTTOM_CENTER.ordinal()]; |
| 899 | chris | 220 | s.param.x = x + mInnerOffset.left; |
| 221 | s.param.y = y + h - mInnerOffset.bottom; |
||
| 222 | s.param.w = w - mInnerOffset.left - mInnerOffset.right; |
||
| 223 | s.param.h = CELL_SIZE; |
||
| 224 | s.param.pivotX = 0; |
||
| 225 | s.param.pivotY = 0; |
||
| 226 | s.param.color = color; |
||
| 835 | chris | 227 | //s.render(); |
| 228 | batch.drawSprite(s); |
||
| 229 | } |
||
| 230 | |||
| 231 | public void renderBottomRight(TileBatch batch) |
||
| 232 | { |
||
| 915 | chris | 233 | AtlasSpriteFrame s = mSprites[FrameRegion.BOTTOM_RIGHT.ordinal()]; |
| 899 | chris | 234 | s.param.x = x + w - mInnerOffset.right; |
| 235 | s.param.y = y + h - mInnerOffset.bottom; |
||
| 236 | s.param.w = CELL_SIZE; |
||
| 237 | s.param.h = CELL_SIZE; |
||
| 238 | s.param.pivotX = 0; |
||
| 239 | s.param.pivotY = 0; |
||
| 240 | s.param.color = color; |
||
| 835 | chris | 241 | //s.render(); |
| 242 | batch.drawSprite(s); |
||
| 243 | } |
||
| 244 | |||
| 245 | public Vector4 getRegion(FrameRegion region) |
||
| 246 | { |
||
| 247 | return new Vector4((region.ordinal() % 3) * 16, (region.ordinal() / 3) * 16, 16, 16); |
||
| 248 | } |
||
| 249 | |||
| 250 | public Rect getInnerOffset() { return mInnerOffset; } |
||
| 251 | public void setInnerOffset(Rect offset) { mInnerOffset = offset; } |
||
| 252 | } |
||
| 253 | |||
| 254 |