Add GuiImage
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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<T extends AbstractGuiImage<T>>
|
||||
extends AbstractGuiElement<T> implements IGuiImage<T> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<GuiImage> {
|
||||
public GuiImage() {
|
||||
}
|
||||
|
||||
public GuiImage(GuiContainer container) {
|
||||
super(container);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GuiImage getThis() {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -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<T extends AbstractGuiImage<T>> extends GuiElement<T> {
|
||||
T setTexture(BufferedImage img);
|
||||
}
|
||||
Reference in New Issue
Block a user