package com.blarg.gdx.graphics.atlas;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;

public class CustomGridTextureAtlas extends TextureAtlas {
	public CustomGridTextureAtlas(Texture texture) {
		this(texture, TEXCOORD_EDGE_BLEED_OFFSET);
	}

	public CustomGridTextureAtlas(Texture texture, float edgeCoordOffset) {
		super(texture, edgeCoordOffset);
	}

	public int add(Rectangle rect) {
		return add((int)rect.x, (int)rect.y, (int)rect.getWidth(), (int)rect.getHeight());
	}

	public int add(int x, int y, int width, int height) {
		// pixel location/dimensions
		int left = x;
		int top = y;
		int right = left + width;
		int bottom = top + height;

		// texture coordinates
		// HACK: subtract edgeCoordOffset from the bottom right edges to
		//       get around floating point rounding errors (adjacent tiles will
		//       slightly bleed in otherwise)
		float u = (left + edgeCoordOffset) / (float)texture.getWidth();
		float v = (top + edgeCoordOffset) / (float)texture.getHeight();
		float u2 = ((float)right - edgeCoordOffset) / (float)texture.getWidth();
		float v2 = ((float)bottom - edgeCoordOffset) / (float)texture.getHeight();

		TextureRegion tile = new TextureRegion();
		tile.setTexture(texture);
		tile.setRegion(u, v, u2, v2);
		tiles.add(tile);

		return tiles.size - 1;
	}

	public void reset() {
		tiles.clear();
	}
}

@@ -1375,24 +1375,1350 @@
 ze - 1;%0A%09%7D%0A%0A
+%09public int addGrid(int startX, int startY, int tileWidth, int tileHeight, int numTilesX, int numTilesY, int tileBorder) %7B%0A%09%09int actualTileWidth = tileWidth + tileBorder;%0A%09%09int actualTileHeight = tileHeight + tileBorder;%0A%0A%09%09int numAdded = 0;%0A%0A%09%09for (int y = 0; y %3C numTilesY; ++y)%0A%09%09%7B%0A%09%09%09for (int x = 0; x %3C numTilesX; ++x)%0A%09%09%09%7B%0A%09%09%09%09// pixel location/dimensions%0A%09%09%09%09int left = startX + tileBorder + x * actualTileWidth;%0A%09%09%09%09int top = startY + tileBorder + y * actualTileHeight;%0A%09%09%09%09int right = left + actualTileWidth - tileBorder;%0A%09%09%09%09int bottom = top + actualTileHeight - tileBorder;%0A%0A%09%09%09%09// texture coordinates%0A%09%09%09%09// HACK: subtract edgeCoordOffset from the bottom right edges to%0A%09%09%09%09//       get around floating point rounding errors (adjacent tiles will%0A%09%09%09%09//       slightly bleed in otherwise)%0A%09%09%09%09float u = (left - tileBorder + edgeCoordOffset) / (float)texture.getWidth();%0A%09%09%09%09float v = (top - tileBorder + edgeCoordOffset) / (float)texture.getHeight();%0A%09%09%09%09float u2 = ((float)right + tileBorder - edgeCoordOffset) / (float)texture.getWidth();%0A%09%09%09%09float v2 = ((float)bottom + tileBorder - edgeCoordOffset) / (float)texture.getHeight();%0A%0A%09%09%09%09TextureRegion tile = new TextureRegion();%0A%09%09%09%09tile.setTexture(texture);%0A%09%09%09%09tile.setRegion(u, v, u2, v2);%0A%09%09%09%09tiles.add(tile);%0A%0A%09%09%09%09++numAdded;%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09return numAdded;%0A%09%7D%0A%0A
 %09public void

add "addGrid" method to CustomGridTextureAtlas to auto-add tiles in a grid layout