Completely modularize submap and RenderBlocks managing

- Fix RenderBlocksCTM ignoring the renderMin/Max values
- Add custom handler for carpet to avoid hardcoding
- TODO blocks that use existing blocks as icons, e.g. ice stairs
This commit is contained in:
tterrag1098 committed 2015-04-24 03:38:37 -04:00
1 parent 80149390b6
commit 41877f3543
23 files changed
+661 -493

No files matched your search

@@ -0,0 +1,291 @@
package com.cricketcraft.chisel.api.rendering;
import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.hash.TIntObjectHashMap;
import java.util.EnumMap;
import net.minecraft.block.Block;
import net.minecraft.world.IBlockAccess;
import net.minecraftforge.common.util.ForgeDirection;
import com.cricketcraft.chisel.api.IFacade;
import com.google.common.collect.Maps;
import static com.cricketcraft.chisel.api.rendering.Dir.*;
// @formatter:off
/**
* The CTM renderer will draw the block's FACE using by assembling 4 quadrants from the 5 available block
* textures. The normal Texture.png is the blocks "unconnected" texture, and is used when CTM is disabled or the block
* has nothing to connect to. This texture has all of the outside corner quadrants The texture-ctm.png contains the
* rest of the quadrants.
* <pre><blockquote>
* ┌─────────────────┐ ┌────────────────────────────────┐
* │ texture.png │ │ texture-ctm.png │
* │ ╔══════╤══════╗ │ │ ──────┼────── ║ ─────┼───── ║ │
* │ ║ │ ║ │ │ │ │ │║ │ ║ │
* │ ║ 16 │ 17 ║ │ │ │ 0 │ 1 │║ 2 │ 3 ║ │
* │ ╟──────┼──────╢ │ │ ┼──────┼──────┼╟──────┼──────╢ │
* │ ║ │ ║ │ │ │ │ │║ │ ║ │
* │ ║ 18 │ 19 ║ │ │ │ 4 │ 5 │║ 6 │ 7 ║ │
* │ ╚══════╧══════╝ │ │ ──────┼────── ║ ─────┼───── ║ │
* └─────────────────┘ │ ═══════╤═══════╝ ─────┼───── ╚ │
* │ │ │ ││ │ │ │
* │ │ 8 │ 9 ││ 10 │ 11 │ │
* │ ┼──────┼──────┼┼──────┼──────┼ │
* │ │ │ ││ │ │ │
* │ │ 12 │ 13 ││ 14 │ 15 │ │
* │ ═══════╧═══════╗ ─────┼───── ╔ │
* └────────────────────────────────┘
* </blockquote></pre>
* combining { 18, 13, 9, 16 }, we can generate a texture connected to the right!
* <pre><blockquote>
* ╔══════╤═══════
* ║ │ │
* ║ 16 │ 9 │
* ╟──────┼──────┼
* ║ │ │
* ║ 18 │ 13 │
* ╚══════╧═══════
* </blockquote></pre>
*
* combining { 18, 13, 11, 2 }, we can generate a texture, in the shape of an L (connected to the right, and up
* <pre><blockquote>
* ║ ─────┼───── ╚
* ║ │ │
* ║ 2 │ 11 │
* ╟──────┼──────┼
* ║ │ │
* ║ 18 │ 13 │
* ╚══════╧═══════
* </blockquote></pre>
*
* HAVE FUN!
* -CptRageToaster-
*/
// @formatter:on
public class CTM {
/** Some hardcoded offset values for the different corner indeces */
private static int[] submapOffsets = { 4, 5, 1, 0 };
private static TIntObjectMap<Dir[]> submapMap = new TIntObjectHashMap<Dir[]>();
private static EnumMap<Dir, Boolean> connectionMap = Maps.newEnumMap(Dir.class);
/** For use via the Chisel 2 config only, altering this could cause unintended behavior */
public static boolean disableObscuredFaceCheckConfig = false;
public Boolean disableObscuredFaceCheck = null;
static {
for (Dir dir : Dir.VALUES) {
connectionMap.put(dir, false);
}
// Mapping the different corner indeces to their respective dirs
submapMap.put(0, new Dir[] { BOTTOM, LEFT, BOTTOM_LEFT });
submapMap.put(1, new Dir[] { BOTTOM, RIGHT, BOTTOM_RIGHT });
submapMap.put(2, new Dir[] { TOP, RIGHT, TOP_RIGHT });
submapMap.put(3, new Dir[] { TOP, LEFT, TOP_LEFT });
}
private CTM() {
}
public static CTM getInstance() {
return new CTM();
}
/**
* @return The indeces of the typical 4x4 submap to use for the given face at the given location.
*
* Indeces are in counter-clockwise order starting at bottom left.
*/
public int[] getSubmapIndices(IBlockAccess world, int x, int y, int z, int side) {
int[] ret = new int[] { 18, 19, 17, 16 };
if (world == null) {
return ret;
}
Block block = world.getBlock(x, y, z);
int meta = world.getBlockMetadata(x, y, z);
buildConnectionMap(world, x, y, z, side, block, meta);
// Map connections to submap indeces
for (int i = 0; i < 4; i++) {
fillSubmaps(ret, i);
}
return ret;
}
/**
* Builds the connection map and stores it in this CTM instance. The {@link #connected(Dir)}, {@link #connectedAnd(Dir...)}, and {@link #connectedOr(Dir...)} methods can be used to access it.
*/
public void buildConnectionMap(IBlockAccess world, int x, int y, int z, int side, Block block, int meta) {
for (Dir dir : Dir.VALUES) {
connectionMap.put(dir, dir.isConnected(this, world, x, y, z, side, block, meta));
}
}
private void fillSubmaps(int[] ret, int idx) {
Dir[] dirs = submapMap.get(idx);
if (connectedOr(dirs[0], dirs[1])) {
if (connectedAnd(dirs)) {
// If all dirs are connected, we use the fully connected face,
// the base offset value.
ret[idx] = submapOffsets[idx];
} else {
// This is a bit magic-y, but basically the array is ordered so
// the first dir requires an offset of 2, and the second dir
// requires an offset of 8, plus the initial offset for the
// corner.
ret[idx] = submapOffsets[idx] + (connected(dirs[0]) ? 2 : 0) + (connected(dirs[1]) ? 8 : 0);
}
}
}
/**
* @param dir
* The direction to check connection in.
* @return True if the cached connectionMap holds a connection in this {@link Dir direction}.
*/
public boolean connected(Dir dir) {
return connectionMap.get(dir);
}
/**
* @param dirs
* The directions to check connection in.
* @return True if the cached connectionMap holds a connection in <i><b>all</b></i> the given {@link Dir directions}.
*/
public boolean connectedAnd(Dir... dirs) {
for (Dir dir : dirs) {
if (!connected(dir)) {
return false;
}
}
return true;
}
/**
* @param dirs
* The directions to check connection in.
* @return True if the cached connectionMap holds a connection in <i><b>one of</b></i> the given {@link Dir directions}.
*/
private boolean connectedOr(Dir... dirs) {
for (Dir dir : dirs) {
if (connected(dir)) {
return true;
}
}
return false;
}
/**
* A simple check for if the given block can connect to the given direction on the given side.
*
* @param world
* @param x
* The x coordinate of the block to check <i>against</i>. This is not the position of your block.
* @param y
* The y coordinate of the block to check <i>against</i>. This is not the position of your block.
* @param z
* The z coordinate of the block to check <i>against</i>. This is not the position of your block.
* @param side
* The side of the block to check for connection status. This is <i>not</i> the direction to check in.
* @param block
* The block to check against for connection.
* @param meta
* The metadata to check against for connection.
* @return True if the given block can connect to the given location on the given side.
*/
public boolean isConnected(IBlockAccess world, int x, int y, int z, int side, Block block, int meta) {
ForgeDirection dir = ForgeDirection.VALID_DIRECTIONS[side];
return isConnected(world, x, y, z, dir, block, meta);
}
/**
* A simple check for if the given block can connect to the given direction on the given side.
*
* @param world
* @param x
* The x coordinate of the block to check <i>against</i>. This is not the position of your block.
* @param y
* The y coordinate of the block to check <i>against</i>. This is not the position of your block.
* @param z
* The z coordinate of the block to check <i>against</i>. This is not the position of your block.
* @param dir
* The {@link ForgeDirection side} of the block to check for connection status. This is <i>not</i> the direction to check in.
* @param block
* The block to check against for connection.
* @param meta
* The metadata to check against for connection.
* @return True if the given block can connect to the given location on the given side.
*/
public boolean isConnected(IBlockAccess world, int x, int y, int z, ForgeDirection dir, Block block, int meta) {
int x2 = x + dir.offsetX;
int y2 = y + dir.offsetY;
int z2 = z + dir.offsetZ;
boolean disableObscured = disableObscuredFaceCheck == null ? disableObscuredFaceCheckConfig : disableObscuredFaceCheck;
Block con = getBlockOrFacade(world, x, y, z, dir.ordinal());
Block obscuring = disableObscured ? null : getBlockOrFacade(world, x2, y2, z2, dir.ordinal());
// no block or a bad API user
if (con == null) {
return false;
}
boolean ret = con.equals(block) && getBlockOrFacadeMetadata(world, x, y, z, dir.ordinal()) == meta;
// no block obscuring this face
if (obscuring == null) {
return ret;
}
// check that we aren't already connected outwards from this side
ret &= !(obscuring.equals(block) && getBlockOrFacadeMetadata(world, x2, y2, z2, dir.ordinal()) == meta);
return ret;
}
/**
* A utility method to access metadata from both the world and {@link IFacade} blocks in the world.
*
* @param world
* @param x
* @param y
* @param z
* @param side
* The side to check for. -1 for unknown.
* @return The metadata at this position in the world, or inside an {@link IFacade} block.
*/
public int getBlockOrFacadeMetadata(IBlockAccess world, int x, int y, int z, int side) {
Block blk = world.getBlock(x, y, z);
if (blk instanceof IFacade) {
return ((IFacade) blk).getFacadeMetadata(world, x, y, z, side);
}
return world.getBlockMetadata(x, y, z);
}
/**
* A utility method to access a block from both the world and {@link IFacade} blocks in the world.
*
* @param world
* @param x
* @param y
* @param z
* @param side
* The side to check for. -1 for unknown.
* @return The block at this position in the world, or inside an {@link IFacade} block.
*/
public Block getBlockOrFacade(IBlockAccess world, int x, int y, int z, int side) {
Block blk = world.getBlock(x, y, z);
if (blk instanceof IFacade) {
blk = ((IFacade) blk).getFacade(world, x, y, z, side);
}
return blk;
}
}
@@ -0,0 +1,105 @@
package com.cricketcraft.chisel.api.rendering;
import net.minecraft.block.Block;
import net.minecraft.world.IBlockAccess;
import net.minecraftforge.common.util.ForgeDirection;
import static net.minecraftforge.common.util.ForgeDirection.*;
/**
* Think of this class as a "Two dimensional ForgeDirection, with diagonals".
* <p>
* It represents the eight different directions a face of a block can connect with CTM, and contains the logic for determining if a block is indeed connected in that direction.
* <p>
* Note that, for example, {@link #TOP_RIGHT} does not mean connected to the {@link #TOP} and {@link #RIGHT}, but connected in the diagonal direction represented by {@link #TOP_RIGHT}. This is used
* for inner corner rendering.
*/
public enum Dir {
// @formatter:off
TOP(UP),
TOP_RIGHT(UP, EAST),
RIGHT(EAST),
BOTTOM_RIGHT(DOWN, EAST),
BOTTOM(DOWN),
BOTTOM_LEFT(DOWN, WEST),
LEFT(WEST),
TOP_LEFT(UP, WEST);
// @formatter:on
/**
* All values of this enum, used to prevent unnecessary allocation via {@link #values()}.
*/
public static final Dir[] VALUES = values();
private static final ForgeDirection NORMAL = SOUTH;
private ForgeDirection[] dirs;
private Dir(ForgeDirection... dirs) {
this.dirs = dirs;
}
/**
* Finds if this block is connected for the given side in this Dir.
*
* @param inst
* The CTM instance to use for logic.
* @param world
* The world the block is in.
* @param x
* The x coordinate of your block.
* @param y
* The y coordinate of your block.
* @param z
* The z coordinate of your block.
* @param sideIdx
* The side index of the current face. This equivalent to {@link ForgeDirection#ordinal()}
* @param block
* The block being rendered.
* @param meta
* The metadata of the block.
* @return True if the block is connected in the given Dir, false otherwise.
*/
public boolean isConnected(CTM inst, IBlockAccess world, int x, int y, int z, int sideIdx, Block block, int meta) {
ForgeDirection side = getOrientation(sideIdx);
ForgeDirection[] dirs = getNormalizedDirs(side);
for (ForgeDirection dir : dirs) {
x += dir.offsetX;
y += dir.offsetY;
z += dir.offsetZ;
}
return inst.isConnected(world, x, y, z, side, block, meta);
}
private ForgeDirection[] getNormalizedDirs(ForgeDirection normal) {
if (normal == NORMAL) {
return dirs;
} else if (normal == NORMAL.getOpposite()) {
// If this is the opposite direction of the default normal, we
// need to mirror the dirs
// A mirror version does not affect y+ and y- so we ignore those
ForgeDirection[] ret = new ForgeDirection[dirs.length];
for (int i = 0; i < ret.length; i++) {
ret[i] = dirs[i].offsetY != 0 ? dirs[i] : dirs[i].getOpposite();
}
return ret;
} else {
ForgeDirection axis = null;
// Next, we need different a different rotation axis depending
// on if this is up/down or not
if (normal.offsetY == 0) {
// If it is not up/down, pick either the left or right-hand
// rotation
axis = normal == NORMAL.getRotation(UP) ? UP : DOWN;
} else {
// If it is up/down, pick either the up or down rotation.
axis = normal == UP ? NORMAL.getRotation(DOWN) : NORMAL.getRotation(UP);
}
ForgeDirection[] ret = new ForgeDirection[dirs.length];
// Finally apply all the rotations
for (int i = 0; i < ret.length; i++) {
ret[i] = dirs[i].getRotation(axis);
}
return ret;
}
}
}
@@ -0,0 +1,40 @@
package com.cricketcraft.chisel.api.rendering;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.util.IIcon;
import net.minecraft.world.IBlockAccess;
public interface ISubmapManager {
/**
* Gets the icon for item rendering, based on side and item metadata.
*
* @param side
* Side of the ItemBlock.
* @param meta
* Metadata of the ItemBlock.
* @return The IIcon for rendering.
*/
IIcon getIcon(int side, int meta);
/**
* Gets the icon for world rendering, based on world position and side.
*/
IIcon getIcon(IBlockAccess world, int x, int y, int z, int side);
/**
* Hook for registering any custom icons you might need in this class. Will be called during the normal block method.
*
* @param modName
* The mod ID this submap was registered to.
* @param block
* The block being registered for.
* @param register
* The IIconRegister instance.
*/
void registerIcons(String modName, Block block, IIconRegister register);
RenderBlocks createRenderContext(RenderBlocks rendererOld, IBlockAccess world);
}
@@ -0,0 +1,166 @@
package com.cricketcraft.chisel.api.rendering;
import java.util.List;
import net.minecraft.util.IIcon;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.common.MinecraftForge;
import com.google.common.collect.Lists;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
/**
* This class is used to split up a large IIcon into smaller "submapped" icons used for CTM and other texture manipulation.
*/
public class TextureSubmap {
private static List<TextureSubmap> submaps = Lists.newArrayList();
private static TextureSubmap dummy = new TextureSubmap(null, 0, 0);
static {
MinecraftForge.EVENT_BUS.register(dummy);
}
private int width, height;
private IIcon baseIcon;
private IIcon[][] icons;
/**
* Construct a new submap. A submap is not required to be square, but it is required to be rectangular.
*
* @param baseIcon
* The IIcon to submap.
* @param width
* The width of the map, in icons.
* @param height
* The height of the map, in icons.
*/
public TextureSubmap(IIcon baseIcon, int width, int height) {
this.baseIcon = baseIcon;
this.width = width;
this.height = height;
this.icons = new IIcon[width][height];
submaps.add(this);
}
/**
* The width, in icons, of the submap.
*/
public int getWidth() {
return width;
}
/**
* The height, in icons, of the submap.
*/
public int getHeight() {
return height;
}
/**
* The large "base" icon used for the submap.
*/
public IIcon getBaseIcon() {
return baseIcon;
}
/**
* Finds and returns the sub-icon at the given coordinates. For example, if you have a 3 by 3 submap, calling {@code getSubIcon(1, 1)} will return the center subicon.
*/
public IIcon getSubIcon(int x, int y) {
x = x % icons.length;
return icons[x][y % icons[x].length];
}
/**
* For internal use only, this is used to create the virtual "subicons" used in the map.
*/
@SubscribeEvent
public final void TexturesStitched(TextureStitchEvent.Post event) {
for (TextureSubmap ts : submaps) {
for (int x = 0; x < ts.width; x++) {
for (int y = 0; y < ts.height; y++) {
ts.icons[x][y] = new TextureVirtual(ts.getBaseIcon(), ts.width, ts.height, x, y);
}
}
}
}
private class TextureVirtual implements IIcon {
private int width, height;
private float umin, umax, vmin, vmax;
private String name;
private IIcon parentIcon;
private TextureVirtual(IIcon parent, int w, int h, int x, int y) {
parentIcon = parent;
umin = parentIcon.getInterpolatedU(16.0 * (x) / w);
umax = parentIcon.getInterpolatedU(16.0 * (x + 1) / w);
vmin = parentIcon.getInterpolatedV(16.0 * (y) / h);
vmax = parentIcon.getInterpolatedV(16.0 * (y + 1) / h);
name = parentIcon.getIconName() + "|" + x + "." + y;
width = parentIcon.getIconWidth();
height = parentIcon.getIconHeight();
}
@Override
@SideOnly(Side.CLIENT)
public float getMinU() {
return umin;
}
@Override
@SideOnly(Side.CLIENT)
public float getMaxU() {
return umax;
}
@Override
@SideOnly(Side.CLIENT)
public float getInterpolatedU(double d0) {
return (float) (umin + (umax - umin) * d0 / 16.0);
}
@Override
@SideOnly(Side.CLIENT)
public float getMinV() {
return vmin;
}
@Override
@SideOnly(Side.CLIENT)
public float getMaxV() {
return vmax;
}
@Override
@SideOnly(Side.CLIENT)
public float getInterpolatedV(double d0) {
return (float) (vmin + (vmax - vmin) * d0 / 16.0);
}
@Override
@SideOnly(Side.CLIENT)
public String getIconName() {
return name;
}
@Override
@SideOnly(Side.CLIENT)
public int getIconWidth() {
return width;
}
@Override
@SideOnly(Side.CLIENT)
public int getIconHeight() {
return height;
}
}
}
@@ -0,0 +1,391 @@
package com.cricketcraft.chisel.api.rendering;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.util.IIcon;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.IBlockAccess;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
import com.cricketcraft.chisel.api.carving.ICarvingVariation;
import com.cricketcraft.chisel.carving.CarvableHelper;
import com.cricketcraft.chisel.client.render.RenderBlocksCTM;
import com.cricketcraft.chisel.client.render.RenderBlocksColumn;
/**
* Handles all default {@link ISubmapManager} behavior
*/
@SuppressWarnings("unchecked")
public enum TextureType {
// @formatter:off
TOPSIDE("top", "side") {
@Override
protected Object registerIcons(ICarvingVariation variation, String modName, String texturePath, IIconRegister register) {
return new IIcon[]{
register.registerIcon(modName + ":" + texturePath + "-side"),
register.registerIcon(modName + ":" + texturePath + "-top")
};
}
@Override
protected IIcon getIcon(ICarvingVariation variation, Object cachedObject, int side, int meta) {
IIcon[] icons = (IIcon[]) cachedObject;
return side > 1 ? icons[0] : icons[1];
}
},
TOPBOTSIDE("top", "bottom", "side"){
@Override
protected Object registerIcons(ICarvingVariation variation, String modName, String texturePath, IIconRegister register) {
return new IIcon[]{
register.registerIcon(modName + ":" + texturePath + "-side"),
register.registerIcon(modName + ":" + texturePath + "-bottom"),
register.registerIcon(modName + ":" + texturePath + "-top")
};
}
@Override
protected IIcon getIcon(ICarvingVariation variation, Object cachedObject, int side, int meta) {
IIcon[] icons = (IIcon[]) cachedObject;
return side > 1 ? icons[0] : icons[side + 1];
}
},
CTMV("ctmv", "top"){
@Override
protected Object registerIcons(ICarvingVariation variation, String modName, String texturePath, IIconRegister register) {
return Pair.of(new TextureSubmap(register.registerIcon(modName + ":" + texturePath + "-ctmv"), 2, 2), register.registerIcon(modName + ":" + texturePath + "-top"));
}
@Override
protected IIcon getIcon(ICarvingVariation variation, Object cachedObject, int side, int meta) {
Pair<TextureSubmap, IIcon> data = (Pair<TextureSubmap, IIcon>) cachedObject;
return side < 2 ? data.getRight() : data.getLeft().getSubIcon(0, 0);
}
@Override
protected IIcon getIcon(ICarvingVariation variation, Object cachedObject, IBlockAccess world, int x, int y, int z, int side, int meta) {
Pair<IIcon, TextureSubmap> data = (Pair<IIcon, TextureSubmap>) cachedObject;
if (side < 2)
return data.getLeft();
Block block = world.getBlock(x, y, z);
boolean topConnected = ctm.isConnected(world, x, y + 1, z, side, block, meta);
boolean botConnected = ctm.isConnected(world, x, y - 1, z, side, block, meta);
TextureSubmap map = data.getRight();
if (topConnected && botConnected)
return map.getSubIcon(0, 1);
if (topConnected && !botConnected)
return map.getSubIcon(1, 1);
if (!topConnected && botConnected)
return map.getSubIcon(1, 0);
return map.getSubIcon(0, 0);
}
@Override
protected RenderBlocks createRenderContext(RenderBlocks rendererOld, IBlockAccess world, Object cachedObject) {
RenderBlocksColumn ret = theRenderBlocksColumn;
Pair<IIcon, TextureSubmap> data = (Pair<IIcon, TextureSubmap>) cachedObject;
ret.blockAccess = world;
ret.renderMaxX = 1.0;
ret.renderMaxY = 1.0;
ret.renderMaxZ = 1.0;
ret.submap = data.getRight();
ret.iconTop = data.getLeft();
return ret;
}
},
CTMH("ctmh", "top"){
@Override
protected Object registerIcons(ICarvingVariation variation, String modName, String texturePath, IIconRegister register) {
return Pair.of(new TextureSubmap(register.registerIcon(modName + ":" + texturePath + "-ctmh"), 2, 2), register.registerIcon(modName + ":" + texturePath + "-top"));
}
@Override
protected IIcon getIcon(ICarvingVariation variation, Object cachedObject, int side, int meta) {
return CTMV.getIcon(variation, cachedObject, side, meta);
}
@Override
protected IIcon getIcon(ICarvingVariation variation, Object cachedObject, IBlockAccess world, int x, int y, int z, int side, int meta) {
Pair<IIcon, TextureSubmap> data = (Pair<IIcon, TextureSubmap>) cachedObject;
if (side < 2)
return data.getLeft();
Block block = ctm.getBlockOrFacade(world, x, y, z, side);
boolean p;
boolean n;
boolean reverse = side == 2 || side == 5;
if (side < 4) {
p = ctm.isConnected(world, x - 1, y, z, side, block, meta);
n = ctm.isConnected(world, x + 1, y, z, side, block, meta);
} else {
p = ctm.isConnected(world, x, y, z - 1, side, block, meta);
n = ctm.isConnected(world, x, y, z + 1, side, block, meta);
}
TextureSubmap map = data.getRight();
if (p && n)
return map.getSubIcon(1, 0);
else if (p)
return map.getSubIcon(reverse ? 0 : 1, 1);
else if (n)
return map.getSubIcon(reverse ? 0 : 1, 1);
return map.getSubIcon(0, 0);
}
},
V9("v9"){
@Override
protected Object registerIcons(ICarvingVariation variation, String modName, String texturePath, IIconRegister register) {
return new TextureSubmap(register.registerIcon(modName + ":" + texturePath + "-v9"), 3, 3);
}
@Override
protected IIcon getIcon(ICarvingVariation variation, Object cachedObject, int side, int meta) {
return ((TextureSubmap)cachedObject).getSubIcon(1, 1);
}
@Override
protected IIcon getIcon(ICarvingVariation variation, Object cachedObject, IBlockAccess world, int x, int y, int z, int side, int meta) {
int variationSize = (this == TextureType.V9) ? 3 : 2;
TextureSubmap map = (TextureSubmap) cachedObject;
int xModulus = x % variationSize;
int zModulus = z % variationSize;
//This ensures that blocks placed near 0,0 or it's axis' do not misbehave
int textureX = (xModulus < 0) ? (xModulus + variationSize) : xModulus;
int textureZ = (zModulus < 0) ? (zModulus + variationSize) : zModulus;
//Always invert the y index
int textureY = (variationSize - (y % variationSize) - 1);
if (side == 2 || side == 5) {
//For WEST, SOUTH reverse the indexes for both X and Z
textureX = (variationSize - textureX - 1);
textureZ = (variationSize - textureZ - 1);
} /*else if (side == 0) {
//For DOWN, reverse the indexes for only Z
textureZ = (variationSize - textureZ - 1);
}*/
int index;
if (side == 0 || side == 1) {
// DOWN || UP
index = textureX + textureZ * variationSize;
} else if (side == 2 || side == 3) {
// NORTH || SOUTH
index = textureX + textureY * variationSize;
} else {
// WEST || EAST
index = textureZ + textureY * variationSize;
}
return map.getSubIcon(index % variationSize, index / variationSize);
}
},
V4("v4"){
@Override
protected Object registerIcons(ICarvingVariation variation, String modName, String texturePath, IIconRegister register) {
return new TextureSubmap(register.registerIcon(modName + ":" + texturePath + "-v4"), 3, 3);
}
@Override
protected IIcon getIcon(ICarvingVariation variation, Object cachedObject, int side, int meta) {
return ((TextureSubmap)cachedObject).getSubIcon(0, 0);
}
@Override
protected IIcon getIcon(ICarvingVariation variation, Object cachedObject, IBlockAccess world, int x, int y, int z, int side, int meta) {
return V9.getIcon(variation, cachedObject, world, x, y, z, side, meta);
}
},
CTMX("", "ctm"){
@Override
protected Object registerIcons(ICarvingVariation variation, String modName, String texturePath, IIconRegister register) {
IIcon baseIcon = register.registerIcon(modName + ":" + texturePath);
return Triple.of(
baseIcon,
new TextureSubmap(register.registerIcon(modName + ":" + texturePath + "-ctm"), 4, 4),
new TextureSubmap(baseIcon, 2, 2)
);
}
@Override
protected IIcon getIcon(ICarvingVariation variation, Object cachedObject, int side, int meta) {
return ((Triple<IIcon, ?, ?>)cachedObject).getLeft();
}
@Override
protected RenderBlocks createRenderContext(RenderBlocks rendererOld, IBlockAccess world, Object cachedObject) {
RenderBlocksCTM ret = theRenderBlocksCTM;
Triple<?, TextureSubmap, TextureSubmap> data = (Triple<?, TextureSubmap, TextureSubmap>) cachedObject;
ret.blockAccess = world;
ret.renderMaxX = 1.0;
ret.renderMaxY = 1.0;
ret.renderMaxZ = 1.0;
ret.submap = data.getMiddle();
ret.submapSmall = data.getRight();
ret.rendererOld = rendererOld;
return ret;
}
},
R16("r16"){
@Override
protected Object registerIcons(ICarvingVariation variation, String modName, String texturePath, IIconRegister register) {
return new TextureSubmap(register.registerIcon(modName + ":" + texturePath + "-r16"), 4, 4);
}
@Override
protected IIcon getIcon(ICarvingVariation variation, Object cachedObject, int side, int meta) {
return V9.getIcon(variation, cachedObject, side, meta);
}
},
R9("r9"){
@Override
protected Object registerIcons(ICarvingVariation variation, String modName, String texturePath, IIconRegister register) {
return new TextureSubmap(register.registerIcon(modName + ":" + texturePath + "-r9"), 3, 3);
}
@Override
protected IIcon getIcon(ICarvingVariation variation, Object cachedObject, int side, int meta) {
return V9.getIcon(variation, cachedObject, side, meta);
}
@Override
protected IIcon getIcon(ICarvingVariation variation, Object cachedObject, IBlockAccess world, int x, int y, int z, int side, int meta) {
return R4.getIcon(variation, cachedObject, side, meta);
}
},
R4("r4"){
@Override
protected Object registerIcons(ICarvingVariation variation, String modName, String texturePath, IIconRegister register) {
return new TextureSubmap(register.registerIcon(modName + ":" + texturePath + "-r4"), 2, 2);
}
@Override
protected IIcon getIcon(ICarvingVariation variation, Object cachedObject, int side, int meta) {
return V4.getIcon(variation, cachedObject, side, meta);
}
@Override
protected IIcon getIcon(ICarvingVariation variation, Object cachedObject, IBlockAccess world, int x, int y, int z, int side, int meta) {
rand.setSeed(x + y + z + side);
rand.nextBoolean();
int size = this == TextureType.R4 ? 2 : 3;
return ((TextureSubmap)cachedObject).getSubIcon(rand.nextInt(size), rand.nextInt(size));
}
},
NORMAL,
CUSTOM;
private static final TextureType[] VALUES;
private static final CTM ctm = CTM.getInstance();
private static final Random rand = new Random();
private static final RenderBlocksCTM theRenderBlocksCTM = new RenderBlocksCTM();
private static final RenderBlocksColumn theRenderBlocksColumn = new RenderBlocksColumn();
private String[] suffixes;
static {
VALUES = ArrayUtils.subarray(values(), 0, values().length - 1);
}
private TextureType(String... suffixes) {
this.suffixes = suffixes.length == 0 ? new String[] { "" } : suffixes;
}
public ISubmapManager createManagerFor(ICarvingVariation variation, String texturePath) {
return new SubmapManagerDefault(this, variation, texturePath);
}
protected Object registerIcons(ICarvingVariation variation, String modName, String texturePath, IIconRegister register) {
return register.registerIcon(modName + ":" + texturePath);
}
protected IIcon getIcon(ICarvingVariation variation, Object cachedObject, int side, int meta) {
return (IIcon) cachedObject;
}
protected IIcon getIcon(ICarvingVariation variation, Object cachedObject, IBlockAccess world, int x, int y, int z, int side, int meta) {
return getIcon(variation, cachedObject, side, meta);
}
protected RenderBlocks createRenderContext(RenderBlocks rendererOld, IBlockAccess world, Object cachedObject) {
return null;
}
public static TextureType getTypeFor(CarvableHelper inst, String modid, String path) {
if (path == null) {
return CUSTOM;
}
for (TextureType t : VALUES) {
boolean matches = true;
for (String s : t.suffixes) {
if (!exists(modid, path, s.isEmpty() ? s : "-" + s)) {
matches = false;
}
}
if (matches) {
return t;
}
}
return CUSTOM;
}
// This is ugly, but faster than class.getResource
private static boolean exists(String modid, String path, String postfix) {
ResourceLocation rl = new ResourceLocation(modid, "textures/blocks/" + path + postfix + ".png");
try {
Minecraft.getMinecraft().getResourceManager().getAllResources(rl);
return true;
} catch (Throwable t) {
return false;
}
}
private static class SubmapManagerDefault implements ISubmapManager {
private TextureType type;
private ICarvingVariation variation;
private Object cachedObject;
private String texturePath;
private SubmapManagerDefault(TextureType type, ICarvingVariation variation, String texturePath) {
this.type = type;
this.variation = variation;
this.texturePath = texturePath;
}
@Override
public IIcon getIcon(int side, int meta) {
return type.getIcon(variation, cachedObject, side, meta);
}
@Override
public IIcon getIcon(IBlockAccess world, int x, int y, int z, int side) {
return type.getIcon(variation, cachedObject, world, x, y, z, side, world.getBlockMetadata(x, y, z));
}
@Override
public void registerIcons(String modName, Block block, IIconRegister register) {
cachedObject = type.registerIcons(variation, modName, texturePath, register);
}
@Override
public RenderBlocks createRenderContext(RenderBlocks rendererOld, IBlockAccess world) {
return type.createRenderContext(rendererOld, world, cachedObject);
}
}
}
@@ -0,0 +1,7 @@
@API(apiVersion = ChiselAPIProps.VERSION, owner = "Chisel", provides = "ChiselAPI|Rendering")
package com.cricketcraft.chisel.api.rendering;
import com.cricketcraft.chisel.api.ChiselAPIProps;
import cpw.mods.fml.common.API;