From b49402f8bdafa340afd46906e65a322030b25668 Mon Sep 17 00:00:00 2001 From: "Luis C.M" <42545778+c0508383@users.noreply.github.com> Date: Sat, 20 May 2023 19:23:54 -0500 Subject: [PATCH] Implemented ImageData object caching for custom GUI button & rect elements --- src/api | 2 +- .../custom/components/CustomGuiButton.java | 135 ++++++------------ .../components/CustomGuiTexturedRect.java | 102 ++++--------- .../client/renderer/customitem/ImageData.java | 13 +- 4 files changed, 77 insertions(+), 175 deletions(-) diff --git a/src/api b/src/api index 5685f9a0f..f07e76b56 160000 --- a/src/api +++ b/src/api @@ -1 +1 @@ -Subproject commit 5685f9a0f856879edc7e1f722da2fee98be187e2 +Subproject commit f07e76b567671a5f8bfc12d0160cfe857aefea29 diff --git a/src/main/java/noppes/npcs/client/gui/custom/components/CustomGuiButton.java b/src/main/java/noppes/npcs/client/gui/custom/components/CustomGuiButton.java index 38a3794e1..29b918291 100644 --- a/src/main/java/noppes/npcs/client/gui/custom/components/CustomGuiButton.java +++ b/src/main/java/noppes/npcs/client/gui/custom/components/CustomGuiButton.java @@ -14,10 +14,12 @@ import net.minecraft.client.renderer.texture.TextureManager; import net.minecraft.client.resources.IResource; import net.minecraft.util.ResourceLocation; import noppes.npcs.api.gui.ICustomGuiComponent; +import noppes.npcs.client.Client; import noppes.npcs.client.ImageDownloadAlt; import noppes.npcs.client.gui.custom.GuiCustom; import noppes.npcs.client.gui.custom.interfaces.IClickListener; import noppes.npcs.client.renderer.ImageBufferDownloadAlt; +import noppes.npcs.client.renderer.customitem.ImageData; import noppes.npcs.scripted.gui.ScriptGuiButton; import org.lwjgl.opengl.GL11; @@ -28,7 +30,9 @@ import java.io.InputStream; public class CustomGuiButton extends GuiButton implements IClickListener { GuiCustom parent; - ResourceLocation location = null; + + ImageData imageData = null; + String texture; public int textureX; public int textureY; boolean field_146123_n; @@ -39,11 +43,6 @@ public class CustomGuiButton extends GuiButton implements IClickListener { float alpha; float scale = 1.0F; - private int totalWidth, totalHeight; - private ImageDownloadAlt imageDownloadAlt = null; - private boolean isUrl = false; - private boolean gotWidthHeight = false; - public CustomGuiButton(int id, String buttonText, int x, int y) { super(id, x, y, buttonText); } @@ -58,24 +57,12 @@ public class CustomGuiButton extends GuiButton implements IClickListener { public CustomGuiButton(int buttonId, String buttonText, int x, int y, int width, int height, String texture, int textureX, int textureY) { this(buttonId, buttonText, x, y, width, height); + this.texture = texture; this.textureX = textureX; this.textureY = textureY; this.label = buttonText; if (texture != null && !texture.isEmpty()) { - this.location = new ResourceLocation(texture); - if(texture.startsWith("https://")){ - TextureManager texturemanager = Minecraft.getMinecraft().getTextureManager(); - ITextureObject object = new ImageDownloadAlt(null, texture, new ResourceLocation("customnpcs:textures/gui/invisible.png"), new ImageBufferDownloadAlt(true,false)); - texturemanager.loadTexture(this.location, object); - } else { - try { - this.getWidthHeight(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } else { - this.location = null; + this.imageData = Client.getImageData(texture); } } @@ -115,42 +102,36 @@ public class CustomGuiButton extends GuiButton implements IClickListener { public void onRender(Minecraft mc, int mouseX, int mouseY, int mouseWheel, float partialTicks) { this.field_146123_n = mouseX >= this.xPosition + GuiCustom.guiLeft && mouseY >= this.yPosition + GuiCustom.guiTop && mouseX < this.xPosition + GuiCustom.guiLeft + this.width * this.scale && mouseY < this.yPosition + GuiCustom.guiTop + this.height * this.scale; - - if (this.location == null) { - totalWidth = totalHeight = 256; - textureX = textureY = 0; - } - - float u1 = (float)textureX/(float)totalWidth; - float u2 = u1 + (float)width/(float)totalWidth; - float v1 = (float)textureY/(float)totalHeight; - float v2 = v1 + (float)height/(float)totalHeight; - - if (this.location != null && this.enabled) { - if (this.field_146123_n) { - v1 = (float)(textureY + 2 * this.height)/(float)totalHeight; - } else { - v1 = (float)(textureY + this.height)/(float)totalHeight; - } - v2 = v1 + (float)height/(float)totalHeight; - } - - if(imageDownloadAlt != null && isUrl && !gotWidthHeight){ - getURLWidthHeight(); - } - FontRenderer fontRenderer = mc.fontRenderer; - GL11.glPushMatrix(); - float red = (color >> 16 & 255) / 255f; - float green = (color >> 8 & 255) / 255f; - float blue = (color & 255) / 255f; - GL11.glColor4f(red,green,blue,this.alpha); - GL11.glTranslatef(GuiCustom.guiLeft + this.xPosition - u1 * totalWidth * this.scale, GuiCustom.guiTop + this.yPosition - v1 * totalHeight * this.scale,(float)this.id); - GL11.glScalef(this.scale,this.scale,1.0F); + if (this.imageData != null && this.imageData.imageLoaded()) { + int totalWidth = this.imageData.getTotalWidth(); + int totalHeight = this.imageData.getTotalHeight(); - if (this.location != null) { - mc.getTextureManager().bindTexture(this.location); + float u1 = (float)textureX/(float)totalWidth; + float u2 = u1 + (float)width/(float)totalWidth; + float v1 = (float)textureY/(float)totalHeight; + float v2 = v1 + (float)height/(float)totalHeight; + + if (this.enabled) { + if (this.field_146123_n) { + v1 = (float)(textureY + 2 * this.height)/(float)totalHeight; + } else { + v1 = (float)(textureY + this.height)/(float)totalHeight; + } + v2 = v1 + (float)height/(float)totalHeight; + } + + GL11.glPushMatrix(); + float red = (color >> 16 & 255) / 255f; + float green = (color >> 8 & 255) / 255f; + float blue = (color & 255) / 255f; + GL11.glColor4f(red,green,blue,this.alpha); + + GL11.glTranslatef(GuiCustom.guiLeft + this.xPosition - u1 * totalWidth * this.scale, GuiCustom.guiTop + this.yPosition - v1 * totalHeight * this.scale,(float)this.id); + GL11.glScalef(this.scale,this.scale,1.0F); + + this.imageData.bindTexture(); GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); Tessellator tessellator = Tessellator.instance; tessellator.startDrawingQuads(); @@ -163,7 +144,9 @@ public class CustomGuiButton extends GuiButton implements IClickListener { tessellator.draw(); GL11.glTranslated(0.0D, 0.0D, 0.1D); this.drawCenteredString(fontRenderer, this.label, this.width / 2, (this.height - 8) / 2, this.color); - } else { + GL11.glPopMatrix(); + } else { + GL11.glPushMatrix(); mc.getTextureManager().bindTexture(buttonTextures); int i = this.getHoverState(this.field_146123_n); GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); @@ -179,9 +162,9 @@ public class CustomGuiButton extends GuiButton implements IClickListener { j = 16777120; } GL11.glTranslated(0.0D, 0.0D, 0.1D); - this.drawCenteredString(fontRenderer, this.displayString, this.width / 2, (this.height-8)/2, j); - } - GL11.glPopMatrix(); + this.drawCenteredString(fontRenderer, this.displayString, this.width / 2, (this.height - 8) / 2, j); + GL11.glPopMatrix(); + } if (this.field_146123_n && this.hoverText != null && this.hoverText.length > 0) { this.parent.hoverText = this.hoverText; @@ -189,7 +172,7 @@ public class CustomGuiButton extends GuiButton implements IClickListener { } public ICustomGuiComponent toComponent() { - ScriptGuiButton component = new ScriptGuiButton(this.id, this.label, this.xPosition, this.yPosition, this.width, this.height, this.location.toString(), this.textureX, this.textureY); + ScriptGuiButton component = new ScriptGuiButton(this.id, this.label, this.xPosition, this.yPosition, this.width, this.height, this.texture, this.textureX, this.textureY); component.setHoverText(this.hoverText); component.setColor(this.color); component.setAlpha(this.alpha); @@ -204,40 +187,4 @@ public class CustomGuiButton extends GuiButton implements IClickListener { return false; } } - - public void getWidthHeight() throws IOException { - InputStream inputstream = null; - - try { - IResource iresource = Minecraft.getMinecraft().getResourceManager().getResource(this.location); - inputstream = iresource.getInputStream(); - BufferedImage bufferedimage = ImageIO.read(inputstream); - gotWidthHeight = true; - this.totalWidth = bufferedimage.getWidth(); - this.totalHeight = bufferedimage.getHeight(); - correctWidthHeight(); - } catch (Exception e) { - e.printStackTrace(); - } finally { - if (inputstream != null) { - inputstream.close(); - } - } - } - - public void getURLWidthHeight(){ - if(imageDownloadAlt.getBufferedImage() != null) { - gotWidthHeight = true; - this.totalWidth = imageDownloadAlt.getBufferedImage().getWidth(); - this.totalHeight = imageDownloadAlt.getBufferedImage().getHeight(); - correctWidthHeight(); - } - } - - public void correctWidthHeight(){ - totalWidth = Math.max(totalWidth, 1); - totalHeight = Math.max(totalHeight, 1); - this.width = width < 0 ? totalWidth : width; - this.height = height < 0 ? totalHeight : height; - } } diff --git a/src/main/java/noppes/npcs/client/gui/custom/components/CustomGuiTexturedRect.java b/src/main/java/noppes/npcs/client/gui/custom/components/CustomGuiTexturedRect.java index dcf3c5db7..241a6e2f5 100644 --- a/src/main/java/noppes/npcs/client/gui/custom/components/CustomGuiTexturedRect.java +++ b/src/main/java/noppes/npcs/client/gui/custom/components/CustomGuiTexturedRect.java @@ -6,16 +6,19 @@ package noppes.npcs.client.gui.custom.components; import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.Gui; import net.minecraft.client.renderer.Tessellator; import net.minecraft.client.renderer.texture.TextureManager; import net.minecraft.client.resources.IResource; import net.minecraft.util.ResourceLocation; import noppes.npcs.api.gui.ICustomGuiComponent; +import noppes.npcs.client.Client; import noppes.npcs.client.ImageDownloadAlt; import noppes.npcs.client.gui.custom.GuiCustom; import noppes.npcs.client.gui.custom.interfaces.IGuiComponent; import noppes.npcs.client.renderer.ImageBufferDownloadAlt; +import noppes.npcs.client.renderer.customitem.ImageData; import noppes.npcs.scripted.gui.ScriptGuiTexturedRect; import org.lwjgl.opengl.GL11; @@ -26,7 +29,9 @@ import java.io.InputStream; public class CustomGuiTexturedRect extends Gui implements IGuiComponent { GuiCustom parent; - ResourceLocation location; + + ImageData imageData = null; + String texture; int id; int x; int y; @@ -41,11 +46,6 @@ public class CustomGuiTexturedRect extends Gui implements IGuiComponent { float alpha; float rotation; - private int totalWidth, totalHeight; - private ImageDownloadAlt imageDownloadAlt = null; - private boolean isUrl = false; - private boolean gotWidthHeight = false; - public CustomGuiTexturedRect(int id, String texture, int x, int y, int width, int height) { this(id, texture, x, y, width, height, 0, 0); } @@ -53,25 +53,15 @@ public class CustomGuiTexturedRect extends Gui implements IGuiComponent { public CustomGuiTexturedRect(int id, String directory, int x, int y, int width, int height, int textureX, int textureY) { this.scale = 1.0F; this.id = id; - this.location = new ResourceLocation(directory); + this.texture = directory; this.x = x; this.y = y; this.width = width; this.height = height; this.textureX = textureX; this.textureY = textureY; - - if(directory.startsWith("https://")){ - this.isUrl = true; - TextureManager texturemanager = Minecraft.getMinecraft().getTextureManager(); - this.imageDownloadAlt = new ImageDownloadAlt(null, directory, new ResourceLocation("customnpcs:textures/gui/invisible.png"), new ImageBufferDownloadAlt(true,false)); - texturemanager.loadTexture(this.location, this.imageDownloadAlt); - } else { - try { - this.getWidthHeight(); - } catch (IOException e) { - e.printStackTrace(); - } + if (texture != null && !texture.isEmpty()) { + this.imageData = Client.getImageData(texture); } } @@ -85,37 +75,37 @@ public class CustomGuiTexturedRect extends Gui implements IGuiComponent { public void onRender(Minecraft mc, int mouseX, int mouseY, int mouseWheel, float partialTicks) { boolean hovered = mouseX >= this.x + GuiCustom.guiLeft && mouseY >= this.y + GuiCustom.guiTop && mouseX < this.x + GuiCustom.guiLeft + this.width && mouseY < this.y + GuiCustom.guiTop + this.height; - mc.getTextureManager().bindTexture(this.location); - float u1 = (float)textureX/(float)totalWidth; - float u2 = u1 + (float)width/(float)totalWidth; - float v1 = (float)textureY/(float)totalHeight; - float v2 = v1 + (float)height/(float)totalHeight; + if (this.imageData != null && this.imageData.imageLoaded()) { + int totalWidth = this.imageData.getTotalWidth(); + int totalHeight = this.imageData.getTotalHeight(); - if(imageDownloadAlt != null && isUrl && !gotWidthHeight){ - getURLWidthHeight(); - } + float u1 = (float) textureX / (float) totalWidth; + float u2 = u1 + (float) width / (float) totalWidth; + float v1 = (float) textureY / (float) totalHeight; + float v2 = v1 + (float) height / (float) totalHeight; - GL11.glPushMatrix(); + GL11.glPushMatrix(); float red = (color >> 16 & 255) / 255f; - float green = (color >> 8 & 255) / 255f; + float green = (color >> 8 & 255) / 255f; float blue = (color & 255) / 255f; - GL11.glColor4f(red,green,blue,this.alpha); + GL11.glColor4f(red, green, blue, this.alpha); - GL11.glTranslatef(GuiCustom.guiLeft + this.x - u1 * totalWidth, GuiCustom.guiTop + this.y - v1 * totalHeight,(float)this.id); - GL11.glRotatef(this.rotation,0.0F,0.0F,1.0F); + GL11.glTranslatef(GuiCustom.guiLeft + this.x - u1 * totalWidth, GuiCustom.guiTop + this.y - v1 * totalHeight, (float) this.id); + GL11.glRotatef(this.rotation, 0.0F, 0.0F, 1.0F); GL11.glScalef(this.scale, this.scale, this.scale); Tessellator tessellator = Tessellator.instance; tessellator.startDrawingQuads(); tessellator.setColorOpaque_F(1, 1, 1); tessellator.setColorRGBA_F(red, green, blue, alpha); - tessellator.addVertexWithUV( u2 * totalWidth, v2 * totalHeight, 0, u2, v2); - tessellator.addVertexWithUV( u2 * totalWidth, v1 * totalHeight, 0, u2, v1); - tessellator.addVertexWithUV( u1 * totalWidth, v1 * totalHeight, 0, u1, v1); - tessellator.addVertexWithUV( u1 * totalWidth, v2 * totalHeight, 0, u1, v2); + tessellator.addVertexWithUV(u2 * totalWidth, v2 * totalHeight, 0, u2, v2); + tessellator.addVertexWithUV(u2 * totalWidth, v1 * totalHeight, 0, u2, v1); + tessellator.addVertexWithUV(u1 * totalWidth, v1 * totalHeight, 0, u1, v1); + tessellator.addVertexWithUV(u1 * totalWidth, v2 * totalHeight, 0, u1, v2); tessellator.draw(); - GL11.glPopMatrix(); + GL11.glPopMatrix(); + } if (hovered && this.hoverText != null && this.hoverText.length > 0) { this.parent.hoverText = this.hoverText; @@ -123,7 +113,7 @@ public class CustomGuiTexturedRect extends Gui implements IGuiComponent { } public ICustomGuiComponent toComponent() { - ScriptGuiTexturedRect component = new ScriptGuiTexturedRect(this.id, this.location.toString(), this.x, this.y, this.width, this.height, this.textureX, this.textureY); + ScriptGuiTexturedRect component = new ScriptGuiTexturedRect(this.id, this.texture, this.x, this.y, this.width, this.height, this.textureX, this.textureY); component.setHoverText(this.hoverText); component.setScale(this.scale); component.setColor(color); @@ -151,40 +141,4 @@ public class CustomGuiTexturedRect extends Gui implements IGuiComponent { return rect; } - - public void getWidthHeight() throws IOException { - InputStream inputstream = null; - - try { - IResource iresource = Minecraft.getMinecraft().getResourceManager().getResource(this.location); - inputstream = iresource.getInputStream(); - BufferedImage bufferedimage = ImageIO.read(inputstream); - gotWidthHeight = true; - this.totalWidth = bufferedimage.getWidth(); - this.totalHeight = bufferedimage.getHeight(); - correctWidthHeight(); - } catch (Exception e) { - e.printStackTrace(); - } finally { - if (inputstream != null) { - inputstream.close(); - } - } - } - - public void getURLWidthHeight(){ - if(imageDownloadAlt.getBufferedImage() != null) { - gotWidthHeight = true; - this.totalWidth = imageDownloadAlt.getBufferedImage().getWidth(); - this.totalHeight = imageDownloadAlt.getBufferedImage().getHeight(); - correctWidthHeight(); - } - } - - public void correctWidthHeight(){ - totalWidth = Math.max(totalWidth, 1); - totalHeight = Math.max(totalHeight, 1); - this.width = width < 0 ? totalWidth : width; - this.height = height < 0 ? totalHeight : height; - } } diff --git a/src/main/java/noppes/npcs/client/renderer/customitem/ImageData.java b/src/main/java/noppes/npcs/client/renderer/customitem/ImageData.java index 1af9204e6..1be88063a 100644 --- a/src/main/java/noppes/npcs/client/renderer/customitem/ImageData.java +++ b/src/main/java/noppes/npcs/client/renderer/customitem/ImageData.java @@ -13,12 +13,13 @@ import java.io.IOException; import java.io.InputStream; public class ImageData { - private ResourceLocation location; + private final ResourceLocation location; private final boolean isUrl; + private ImageDownloadAlt imageDownloadAlt = null; private BufferedImage bufferedImage = null; - private int totalWidth, totalHeight; + private int totalWidth, totalHeight; private boolean gotWidthHeight; private boolean invalid; @@ -28,14 +29,14 @@ public class ImageData { this.isUrl = true; TextureManager texturemanager = Minecraft.getMinecraft().getTextureManager(); this.imageDownloadAlt = new ImageDownloadAlt(null, directory, new ResourceLocation("customnpcs:textures/gui/invisible.png"), new ImageBufferDownloadAlt(true, false)); - texturemanager.loadTexture(location, this.imageDownloadAlt); + texturemanager.loadTexture(this.location, this.imageDownloadAlt); } else { this.isUrl = false; } } - public boolean invalidTexture() { - return this.invalid; + public boolean imageLoaded() { + return !this.invalid && this.location != null && this.gotWidthHeight; } public void bindTexture() { @@ -49,7 +50,7 @@ public class ImageData { } } - public ResourceLocation getLocation() { + private ResourceLocation getLocation() { return this.isUrl && this.imageDownloadAlt.getBufferedImage() == null ? null : this.location; }