Fixed dialog image size issue. Dialog images can also use URLs now.

This commit is contained in:
Luis C.M committed 2022-11-09 21:33:04 -06:00
1 parent cc39e62bd4
commit 4b1fb37366
2 files changed
+100 -24

No files matched your search

@@ -5,6 +5,7 @@ import net.minecraft.client.gui.Gui;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.texture.ITextureObject;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.client.resources.IResource;
import net.minecraft.util.ResourceLocation;
import noppes.npcs.client.ImageDownloadAlt;
import noppes.npcs.client.gui.customoverlay.OverlayCustom;
@@ -12,6 +13,11 @@ import noppes.npcs.client.renderer.ImageBufferDownloadAlt;
import noppes.npcs.controllers.data.DialogImage;
import org.lwjgl.opengl.GL11;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
public class GuiDialogImage extends Gui {
GuiDialogInteract parent;
ResourceLocation location;
@@ -34,6 +40,11 @@ public class GuiDialogImage extends Gui {
public int imageType = 0; //0 - Default, 1 - Text, 2 - Option
public int alignment = 0;
private int totalWidth, totalHeight;
private ImageDownloadAlt imageDownloadAlt = null;
private boolean isUrl = false;
private boolean gotWidthHeight = false;
public GuiDialogImage(DialogImage dialogImage) {
this.location = new ResourceLocation(dialogImage.texture);
@@ -56,9 +67,16 @@ public class GuiDialogImage extends Gui {
this.alignment = dialogImage.alignment;
if(texture.startsWith("https://")){
this.isUrl = true;
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);
this.imageDownloadAlt = new ImageDownloadAlt(null, texture, 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();
}
}
}
@@ -75,6 +93,15 @@ public class GuiDialogImage extends Gui {
mc.getTextureManager().bindTexture(this.location);
} catch (Exception ignored) { return; }
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(imageDownloadAlt != null && isUrl && !gotWidthHeight){
getURLWidthHeight();
}
GL11.glPushMatrix();
float red = (color >> 16 & 255) / 255f;
float green = (color >> 8 & 255) / 255f;
@@ -84,24 +111,51 @@ public class GuiDialogImage extends Gui {
GL11.glRotatef(this.rotation,0.0F,0.0F,1.0F);
GL11.glScalef(this.scale, this.scale, this.scale);
int p_73729_1_ = (int) (this.x/(this.scale));
int p_73729_2_ = (int) (this.y/(this.scale));
int p_73729_3_ = this.textureX;
int p_73729_4_ = this.textureY;
int p_73729_5_ = (int)(this.width);
int p_73729_6_ = (int)(this.height);
float f = 0.00390625F;
float f1 = 0.00390625F;
Tessellator tessellator = Tessellator.instance;
tessellator.startDrawingQuads();
tessellator.setColorOpaque_F(1, 1, 1);
tessellator.setColorRGBA_F(red, green, blue, alpha);
tessellator.addVertexWithUV((double)(p_73729_1_ + 0), (double)(p_73729_2_ + p_73729_6_), (double)this.zLevel, (double)((float)(p_73729_3_ + 0) * f), (double)((float)(p_73729_4_ + p_73729_6_) * f1));
tessellator.addVertexWithUV((double)(p_73729_1_ + p_73729_5_), (double)(p_73729_2_ + p_73729_6_), (double)this.zLevel, (double)((float)(p_73729_3_ + p_73729_5_) * f), (double)((float)(p_73729_4_ + p_73729_6_) * f1));
tessellator.addVertexWithUV((double)(p_73729_1_ + p_73729_5_), (double)(p_73729_2_ + 0), (double)this.zLevel, (double)((float)(p_73729_3_ + p_73729_5_) * f), (double)((float)(p_73729_4_ + 0) * f1));
tessellator.addVertexWithUV((double)(p_73729_1_ + 0), (double)(p_73729_2_ + 0), (double)this.zLevel, (double)((float)(p_73729_3_ + 0) * f), (double)((float)(p_73729_4_ + 0) * f1));
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();
}
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;
}
}
@@ -1,6 +1,7 @@
package noppes.npcs.client.gui.player;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import net.minecraft.client.Minecraft;
@@ -66,6 +67,8 @@ public class GuiDialogInteract extends GuiNPCInterface implements IGuiClose
private int textSoundTime = 0;
private int textPauseTime = 0;
private HashMap<Integer,GuiDialogImage> dialogImages = new HashMap<>();
public GuiDialogInteract(EntityNPCInterface npc, Dialog dialog){
super(npc);
this.dialog = dialog;
@@ -171,11 +174,17 @@ public class GuiDialogInteract extends GuiNPCInterface implements IGuiClose
}
for (IDialogImage dialogImage : dialog.dialogImages.values()) {
GuiDialogImage image = new GuiDialogImage((DialogImage) dialogImage);
if (image.imageType != 0)
if (dialogImage.getImageType() != 0)
continue;
GuiDialogImage image;
if (dialogImages.containsKey(dialogImage.getId())) {
image = dialogImages.get(dialogImage.getId());
} else {
image = new GuiDialogImage((DialogImage) dialogImage);
dialogImages.put(dialogImage.getId(),image);
}
GL11.glEnable(GL11.GL_BLEND);
OpenGlHelper.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0);
GL11.glDisable(GL11.GL_ALPHA_TEST);
@@ -190,11 +199,17 @@ public class GuiDialogInteract extends GuiNPCInterface implements IGuiClose
GL11.glPushMatrix();
for (IDialogImage dialogImage : dialog.dialogImages.values()) {
GuiDialogImage image = new GuiDialogImage((DialogImage) dialogImage);
if (image.imageType != 1)
if (dialogImage.getImageType() != 1)
continue;
GuiDialogImage image;
if (dialogImages.containsKey(dialogImage.getId())) {
image = dialogImages.get(dialogImage.getId());
} else {
image = new GuiDialogImage((DialogImage) dialogImage);
dialogImages.put(dialogImage.getId(),image);
}
GL11.glEnable(GL11.GL_BLEND);
OpenGlHelper.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0);
GL11.glDisable(GL11.GL_ALPHA_TEST);
@@ -453,11 +468,17 @@ public class GuiDialogInteract extends GuiNPCInterface implements IGuiClose
GL11.glPushMatrix();
for (IDialogImage dialogImage : dialog.dialogImages.values()) {
GuiDialogImage image = new GuiDialogImage((DialogImage) dialogImage);
if (image.imageType != 2)
if (dialogImage.getImageType() != 2)
continue;
GuiDialogImage image;
if (dialogImages.containsKey(dialogImage.getId())) {
image = dialogImages.get(dialogImage.getId());
} else {
image = new GuiDialogImage((DialogImage) dialogImage);
dialogImages.put(dialogImage.getId(),image);
}
GL11.glEnable(GL11.GL_BLEND);
OpenGlHelper.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0);
GL11.glDisable(GL11.GL_ALPHA_TEST);
@@ -618,6 +639,7 @@ public class GuiDialogInteract extends GuiNPCInterface implements IGuiClose
public void appendDialog(Dialog dialog) {
closeOnEsc = !dialog.disableEsc;
this.dialogImages.clear();
this.dialog = dialog;
this.options = new ArrayList<Integer>();