Allow copying of GuiImages without recreating the texture
This commit is contained in:
@@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
package de.johni0702.minecraft.gui.element;
|
package de.johni0702.minecraft.gui.element;
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
import de.johni0702.minecraft.gui.GuiRenderer;
|
import de.johni0702.minecraft.gui.GuiRenderer;
|
||||||
import de.johni0702.minecraft.gui.RenderInfo;
|
import de.johni0702.minecraft.gui.RenderInfo;
|
||||||
import de.johni0702.minecraft.gui.container.GuiContainer;
|
import de.johni0702.minecraft.gui.container.GuiContainer;
|
||||||
@@ -41,6 +42,12 @@ public abstract class AbstractGuiImage<T extends AbstractGuiImage<T>>
|
|||||||
private int uWidth, vHeight;
|
private int uWidth, vHeight;
|
||||||
private int textureWidth, textureHeight;
|
private int textureWidth, textureHeight;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reference to the copied image to prevent it from being garbage collected
|
||||||
|
* and subsequently releasing the OpenGL texture.
|
||||||
|
*/
|
||||||
|
private AbstractGuiImage<T> copyOf;
|
||||||
|
|
||||||
public AbstractGuiImage() {
|
public AbstractGuiImage() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,6 +55,18 @@ public abstract class AbstractGuiImage<T extends AbstractGuiImage<T>>
|
|||||||
super(container);
|
super(container);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public AbstractGuiImage(AbstractGuiImage<T> copyOf) {
|
||||||
|
this.texture = copyOf.texture;
|
||||||
|
this.resourceLocation = copyOf.resourceLocation;
|
||||||
|
this.u = copyOf.u;
|
||||||
|
this.v = copyOf.v;
|
||||||
|
this.uWidth = copyOf.uWidth;
|
||||||
|
this.vHeight = copyOf.vHeight;
|
||||||
|
this.textureWidth = copyOf.textureWidth;
|
||||||
|
this.textureHeight = copyOf.textureHeight;
|
||||||
|
this.copyOf = copyOf;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) {
|
public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) {
|
||||||
if (texture != null) {
|
if (texture != null) {
|
||||||
@@ -63,7 +82,7 @@ public abstract class AbstractGuiImage<T extends AbstractGuiImage<T>>
|
|||||||
@Override
|
@Override
|
||||||
protected void finalize() throws Throwable {
|
protected void finalize() throws Throwable {
|
||||||
super.finalize();
|
super.finalize();
|
||||||
if (texture != null) {
|
if (texture != null && copyOf == null) {
|
||||||
getMinecraft().addScheduledTask(new Finalizer(texture));
|
getMinecraft().addScheduledTask(new Finalizer(texture));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -75,6 +94,7 @@ public abstract class AbstractGuiImage<T extends AbstractGuiImage<T>>
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T setTexture(BufferedImage img) {
|
public T setTexture(BufferedImage img) {
|
||||||
|
Preconditions.checkState(copyOf == null, "Cannot change texture of copy.");
|
||||||
resourceLocation = null;
|
resourceLocation = null;
|
||||||
if (texture != null) {
|
if (texture != null) {
|
||||||
texture.deleteGlTexture();
|
texture.deleteGlTexture();
|
||||||
@@ -87,6 +107,7 @@ public abstract class AbstractGuiImage<T extends AbstractGuiImage<T>>
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T setTexture(ResourceLocation resourceLocation) {
|
public T setTexture(ResourceLocation resourceLocation) {
|
||||||
|
Preconditions.checkState(copyOf == null, "Cannot change texture of copy.");
|
||||||
if (texture != null) {
|
if (texture != null) {
|
||||||
texture.deleteGlTexture();
|
texture.deleteGlTexture();
|
||||||
texture = null;
|
texture = null;
|
||||||
|
|||||||
@@ -32,6 +32,10 @@ public class GuiImage extends AbstractGuiImage<GuiImage> {
|
|||||||
super(container);
|
super(container);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public GuiImage(GuiImage copyOf) {
|
||||||
|
super(copyOf);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected GuiImage getThis() {
|
protected GuiImage getThis() {
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
Reference in New Issue
Block a user