diff --git a/src/main/java/com/falsepattern/lib/internal/render/ClampedIcon.java b/src/main/java/com/falsepattern/lib/internal/render/ClampedIcon.java new file mode 100644 index 0000000..dc80c24 --- /dev/null +++ b/src/main/java/com/falsepattern/lib/internal/render/ClampedIcon.java @@ -0,0 +1,59 @@ +package com.falsepattern.lib.internal.render; + +import com.falsepattern.lib.util.MathUtil; +import lombok.AllArgsConstructor; +import net.minecraft.util.IIcon; + +@AllArgsConstructor +public final class ClampedIcon implements IIcon { + private final IIcon delegate; + + @Override + public int getIconWidth() { + return delegate.getIconWidth(); + } + + @Override + public int getIconHeight() { + return delegate.getIconHeight(); + } + + @Override + public float getMinU() { + return delegate.getMinU(); + } + + @Override + public float getMaxU() { + return delegate.getMaxU(); + } + + @Override + public float getMinV() { + return delegate.getMinV(); + } + + @Override + public float getMaxV() { + return delegate.getMaxV(); + } + + @Override + public float getInterpolatedU(double textureU) { + return delegate.getInterpolatedU(clampTextureCoordinate(textureU)); + } + + @Override + public float getInterpolatedV(double textureV) { + return delegate.getInterpolatedV(clampTextureCoordinate(textureV)); + } + + @Override + public String getIconName() { + return delegate.getIconName(); + } + + private double clampTextureCoordinate(double textureCoordinate) { + return MathUtil.clamp(textureCoordinate, 0D, 16D); + } +} diff --git a/src/main/java/com/falsepattern/lib/util/RenderUtil.java b/src/main/java/com/falsepattern/lib/util/RenderUtil.java index ff75a3f..6c3f043 100644 --- a/src/main/java/com/falsepattern/lib/util/RenderUtil.java +++ b/src/main/java/com/falsepattern/lib/util/RenderUtil.java @@ -21,18 +21,22 @@ package com.falsepattern.lib.util; import com.falsepattern.lib.StableAPI; +import com.falsepattern.lib.internal.render.ClampedIcon; import com.falsepattern.lib.internal.render.FullTextureIcon; import lombok.SneakyThrows; import lombok.experimental.UtilityClass; import lombok.val; import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.RenderBlocks; import net.minecraft.util.IIcon; import net.minecraft.util.Timer; import cpw.mods.fml.relauncher.ReflectionHelper; import cpw.mods.fml.relauncher.SideOnly; import org.lwjgl.opengl.GL11; +import java.util.Collections; + import static cpw.mods.fml.relauncher.Side.CLIENT; import static net.minecraft.client.Minecraft.getMinecraft; @@ -76,6 +80,20 @@ public final class RenderUtil { return new FullTextureIcon(iconName, width, height); } + /** + * Wraps the given icon as a clamped icon. + *

+ * A clamped icon will clamp the coordinates given to {@link IIcon#getInterpolatedU(double)} and {@link IIcon#getInterpolatedV(double)} to the range of 0 to 16. + *

+ * This is helpful when using {@link RenderBlocks} but having different bounds. + * + * @param icon The icon to clamp + */ + @StableAPI.Expose(since = "0.10.0") + public static IIcon wrapAsClampedIcon(IIcon icon) { + return new ClampedIcon(icon); + } + /** * Provides the partial tick between the last and next client tick, in the range of 0 to 1. *