From a61c0eb4e53be9d47911eede0c1fc96277cb7713 Mon Sep 17 00:00:00 2001 From: johni0702 Date: Sat, 11 Jul 2015 01:35:04 +0200 Subject: [PATCH] Add GuiImage --- .../johni0702/minecraft/gui/GuiRenderer.java | 5 + .../minecraft/gui/MinecraftGuiRenderer.java | 13 +++ .../minecraft/gui/OffsetGuiRenderer.java | 11 +++ .../gui/element/AbstractGuiImage.java | 91 +++++++++++++++++++ .../minecraft/gui/element/GuiImage.java | 39 ++++++++ .../minecraft/gui/element/IGuiImage.java | 29 ++++++ 6 files changed, 188 insertions(+) create mode 100644 src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiImage.java create mode 100644 src/main/java/de/johni0702/minecraft/gui/element/GuiImage.java create mode 100644 src/main/java/de/johni0702/minecraft/gui/element/IGuiImage.java diff --git a/src/main/java/de/johni0702/minecraft/gui/GuiRenderer.java b/src/main/java/de/johni0702/minecraft/gui/GuiRenderer.java index e69b9e7e..796cbc0b 100644 --- a/src/main/java/de/johni0702/minecraft/gui/GuiRenderer.java +++ b/src/main/java/de/johni0702/minecraft/gui/GuiRenderer.java @@ -22,6 +22,7 @@ package de.johni0702.minecraft.gui; +import net.minecraft.client.renderer.texture.ITextureObject; import net.minecraft.util.ResourceLocation; import org.lwjgl.util.ReadableColor; import org.lwjgl.util.ReadableDimension; @@ -35,8 +36,12 @@ public interface GuiRenderer { void bindTexture(ResourceLocation location); + void bindTexture(ITextureObject texture); + void drawTexturedRect(int x, int y, int u, int v, int width, int height); + void drawTexturedRect(int x, int y, int u, int v, int width, int height, int uWidth, int vHeight, int textureWidth, int textureHeight); + void drawRect(int x, int y, int width, int height, int color); void drawRect(int x, int y, int width, int height, ReadableColor color); diff --git a/src/main/java/de/johni0702/minecraft/gui/MinecraftGuiRenderer.java b/src/main/java/de/johni0702/minecraft/gui/MinecraftGuiRenderer.java index de20509d..effe4cb2 100644 --- a/src/main/java/de/johni0702/minecraft/gui/MinecraftGuiRenderer.java +++ b/src/main/java/de/johni0702/minecraft/gui/MinecraftGuiRenderer.java @@ -26,8 +26,10 @@ import lombok.NonNull; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.Gui; +import net.minecraft.client.renderer.GlStateManager; import net.minecraft.client.renderer.Tessellator; import net.minecraft.client.renderer.WorldRenderer; +import net.minecraft.client.renderer.texture.ITextureObject; import net.minecraft.util.ResourceLocation; import org.lwjgl.util.Color; import org.lwjgl.util.*; @@ -61,11 +63,22 @@ public class MinecraftGuiRenderer implements GuiRenderer { Minecraft.getMinecraft().getTextureManager().bindTexture(location); } + @Override + public void bindTexture(ITextureObject texture) { + GlStateManager.bindTexture(texture.getGlTextureId()); + } + @Override public void drawTexturedRect(int x, int y, int u, int v, int width, int height) { gui.drawTexturedModalRect(x, y, u, v, width, height); } + @Override + public void drawTexturedRect(int x, int y, int u, int v, int width, int height, int uWidth, int vHeight, int textureWidth, int textureHeight) { + GlStateManager.color(1, 1, 1); + Gui.drawScaledCustomSizeModalRect(x, y, u, v, uWidth, vHeight, width, height, textureWidth, textureHeight); + } + @Override public void drawRect(int x, int y, int width, int height, int color) { Gui.drawRect(x, y, x + width, y + height, color); diff --git a/src/main/java/de/johni0702/minecraft/gui/OffsetGuiRenderer.java b/src/main/java/de/johni0702/minecraft/gui/OffsetGuiRenderer.java index 90f835c4..65c44302 100644 --- a/src/main/java/de/johni0702/minecraft/gui/OffsetGuiRenderer.java +++ b/src/main/java/de/johni0702/minecraft/gui/OffsetGuiRenderer.java @@ -23,6 +23,7 @@ package de.johni0702.minecraft.gui; import lombok.NonNull; +import net.minecraft.client.renderer.texture.ITextureObject; import net.minecraft.util.ResourceLocation; import org.lwjgl.util.Point; import org.lwjgl.util.ReadableColor; @@ -66,11 +67,21 @@ public class OffsetGuiRenderer implements GuiRenderer { renderer.bindTexture(location); } + @Override + public void bindTexture(ITextureObject texture) { + renderer.bindTexture(texture); + } + @Override public void drawTexturedRect(int x, int y, int u, int v, int width, int height) { renderer.drawTexturedRect(x + position.getX(), y + position.getY(), u, v, width, height); } + @Override + public void drawTexturedRect(int x, int y, int u, int v, int width, int height, int uWidth, int vHeight, int textureWidth, int textureHeight) { + renderer.drawTexturedRect(x + position.getX(), y + position.getY(), u, v, width, height, uWidth, vHeight, textureWidth, textureHeight); + } + @Override public void drawRect(int x, int y, int width, int height, int color) { renderer.drawRect(x + position.getX(), y + position.getY(), width, height, color); diff --git a/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiImage.java b/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiImage.java new file mode 100644 index 00000000..4cb3d12c --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiImage.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.element; + +import de.johni0702.minecraft.gui.GuiRenderer; +import de.johni0702.minecraft.gui.RenderInfo; +import de.johni0702.minecraft.gui.container.GuiContainer; +import lombok.RequiredArgsConstructor; +import net.minecraft.client.renderer.texture.DynamicTexture; +import org.lwjgl.util.ReadableDimension; + +import java.awt.image.BufferedImage; + +public abstract class AbstractGuiImage> + extends AbstractGuiElement implements IGuiImage { + private DynamicTexture texture; + private int textureWidth, textureHeight; + + public AbstractGuiImage() { + } + + public AbstractGuiImage(GuiContainer container) { + super(container); + } + + @Override + public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) { + renderer.bindTexture(texture); + int w = size.getWidth(); + int h = size.getHeight(); + renderer.drawTexturedRect(0, 0, 0, 0, w, h, textureWidth, textureHeight, textureWidth, textureHeight); + } + + @Override + protected void finalize() throws Throwable { + super.finalize(); + if (texture != null) { + getMinecraft().addScheduledTask(new Finalizer(texture)); + } + } + + @Override + public ReadableDimension getMinSize() { + return getPreferredSize(); + } + + @Override + public T setTexture(BufferedImage img) { + if (texture != null) { + texture.deleteGlTexture(); + } + texture = new DynamicTexture(img); + textureWidth = img.getWidth(); + textureHeight = img.getHeight(); + return getThis(); + } + + /** + * We use a static class here in order to prevent the inner class from keeping the outer class + * alive after finalization when still unloading the texture. + */ + @RequiredArgsConstructor + private static final class Finalizer implements Runnable { + private final DynamicTexture texture; + + @Override + public void run() { + texture.deleteGlTexture(); + } + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/element/GuiImage.java b/src/main/java/de/johni0702/minecraft/gui/element/GuiImage.java new file mode 100644 index 00000000..107520f0 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/element/GuiImage.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.element; + +import de.johni0702.minecraft.gui.container.GuiContainer; + +public class GuiImage extends AbstractGuiImage { + public GuiImage() { + } + + public GuiImage(GuiContainer container) { + super(container); + } + + @Override + protected GuiImage getThis() { + return this; + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/element/IGuiImage.java b/src/main/java/de/johni0702/minecraft/gui/element/IGuiImage.java new file mode 100644 index 00000000..6f50f70d --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/element/IGuiImage.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.element; + +import java.awt.image.BufferedImage; + +public interface IGuiImage> extends GuiElement { + T setTexture(BufferedImage img); +}