mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-21 22:02:17 +00:00
CustomOverlayTexturedRect:
-Fixed texture rectangle rendering
This commit is contained in:
1 parent
41a6803e2e
commit
1b5b6cac6d
1 file changed
+76
-21
+76
-21
@@ -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;
|
||||
@@ -14,12 +15,17 @@ import noppes.npcs.scripted.interfaces.overlay.ICustomOverlayComponent;
|
||||
import noppes.npcs.scripted.overlay.ScriptOverlayTexturedRect;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class CustomOverlayTexturedRect extends Gui implements IOverlayComponent {
|
||||
OverlayCustom parent;
|
||||
int alignment;
|
||||
int id;
|
||||
|
||||
ResourceLocation texture;
|
||||
ResourceLocation location;
|
||||
int x;
|
||||
int y;
|
||||
int width;
|
||||
@@ -32,15 +38,20 @@ public class CustomOverlayTexturedRect extends Gui implements IOverlayComponent
|
||||
float alpha;
|
||||
float rotation;
|
||||
|
||||
private int totalWidth, totalHeight;
|
||||
private ImageDownloadAlt imageDownloadAlt = null;
|
||||
private boolean isUrl = false;
|
||||
private boolean gotWidthHeight = false;
|
||||
|
||||
public CustomOverlayTexturedRect(int id, String texture, int x, int y, int width, int height) {
|
||||
this(id, texture, x, y, width, height, 0, 0);
|
||||
}
|
||||
|
||||
public CustomOverlayTexturedRect(int id, String texture, int x, int y, int width, int height, int textureX, int textureY) {
|
||||
public CustomOverlayTexturedRect(int id, String directory, int x, int y, int width, int height, int textureX, int textureY) {
|
||||
this.scale = 1.0F;
|
||||
this.alignment = 0;
|
||||
this.id = id;
|
||||
this.texture = new ResourceLocation(texture);
|
||||
this.location = new ResourceLocation(directory);
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = width;
|
||||
@@ -48,10 +59,17 @@ public class CustomOverlayTexturedRect extends Gui implements IOverlayComponent
|
||||
this.textureX = textureX;
|
||||
this.textureY = textureY;
|
||||
|
||||
if(texture.startsWith("https://")){
|
||||
if(directory.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.texture, object);
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +82,16 @@ public class CustomOverlayTexturedRect extends Gui implements IOverlayComponent
|
||||
}
|
||||
|
||||
public void onRender(Minecraft mc, float partialTicks) {
|
||||
mc.getTextureManager().bindTexture(this.texture);
|
||||
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(imageDownloadAlt != null && isUrl && !gotWidthHeight){
|
||||
getURLWidthHeight();
|
||||
}
|
||||
|
||||
GL11.glPushMatrix();
|
||||
float red = (color >> 16 & 255) / 255f;
|
||||
@@ -72,34 +99,26 @@ public class CustomOverlayTexturedRect extends Gui implements IOverlayComponent
|
||||
float blue = (color & 255) / 255f;
|
||||
|
||||
GL11.glTranslatef(this.alignment%3*((float)(OverlayCustom.scaledWidth)/2), (float) (Math.floor((float)(alignment/3))*((float)(OverlayCustom.scaledHeight)/2)),0.0F);
|
||||
GL11.glTranslatef( this.x - u1 * totalWidth, this.y - v1 * totalHeight,(float)this.id);
|
||||
|
||||
GL11.glScalef(this.scale, this.scale, this.scale);
|
||||
|
||||
GL11.glRotatef(this.rotation,0.0F,0.0F,1.0F);
|
||||
|
||||
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 ICustomOverlayComponent toComponent() {
|
||||
ScriptOverlayTexturedRect component = new ScriptOverlayTexturedRect(this.id, this.texture.toString(), this.x, this.y, this.width, this.height, this.textureX, this.textureY);
|
||||
ScriptOverlayTexturedRect component = new ScriptOverlayTexturedRect(this.id, this.location.toString(), this.x, this.y, this.width, this.height, this.textureX, this.textureY);
|
||||
component.setScale(scale);
|
||||
component.setAlignment(alignment);
|
||||
component.setAlpha(alpha);
|
||||
@@ -124,4 +143,40 @@ public class CustomOverlayTexturedRect extends Gui implements IOverlayComponent
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user