diff --git a/src/main/java/noppes/npcs/client/gui/util/GuiUtil.java b/src/main/java/noppes/npcs/client/gui/util/GuiUtil.java new file mode 100644 index 000000000..10c86a40f --- /dev/null +++ b/src/main/java/noppes/npcs/client/gui/util/GuiUtil.java @@ -0,0 +1,169 @@ +package noppes.npcs.client.gui.util; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.ScaledResolution; +import net.minecraft.client.renderer.OpenGlHelper; +import net.minecraft.client.renderer.Tessellator; +import org.lwjgl.input.Mouse; +import org.lwjgl.opengl.GL11; + +public class GuiUtil { + + public static void setScissorClip(int x, int y, int width, int height) { + Minecraft mc = Minecraft.getMinecraft(); + ScaledResolution res = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); + int scale = res.getScaleFactor(); + + + // Adjust position to Top-Left origin (OpenGL window/screen space uses bottom-left origin) + y = mc.displayHeight - y * scale; + height *= scale; + + // Set clip + GL11.glScissor(x * scale, y - height, width * scale, height); + } + + public static void drawHorizontalLine(double y, double startX, double endX, int color) { + if (endX < startX) { + double i1 = startX; + startX = endX; + endX = i1; + } + + drawRectD(startX, y, endX + 1, y + 1, color); + } + + public static void drawVerticalLine(double x, double startY, double endY, int color) { + if (endY < startY) { + double i1 = startY; + startY = endY; + endY = i1; + } + + drawRectD(x, startY + 1, x + 1, endY, color); + } + + public static void drawRectD(double left, double top, double right, double bottom) { + double j1; + + if (left < right) { + j1 = left; + left = right; + right = j1; + } + + if (top < bottom) { + j1 = top; + top = bottom; + bottom = j1; + } + + Tessellator tessellator = Tessellator.instance; + GL11.glEnable(GL11.GL_BLEND); + GL11.glDisable(GL11.GL_TEXTURE_2D); + OpenGlHelper.glBlendFunc(770, 771, 1, 0); + tessellator.startDrawingQuads(); + tessellator.addVertex(left, bottom, 0.0D); + tessellator.addVertex(right, bottom, 0.0D); + tessellator.addVertex(right, top, 0.0D); + tessellator.addVertex(left, top, 0.0D); + tessellator.draw(); + GL11.glEnable(GL11.GL_TEXTURE_2D); + GL11.glDisable(GL11.GL_BLEND); + } + + + public static void drawRectD(double left, double top, double right, double bottom, int color) { + float f3 = (float) (color >> 24 & 255) / 255.0F; + float f = (float) (color >> 16 & 255) / 255.0F; + float f1 = (float) (color >> 8 & 255) / 255.0F; + float f2 = (float) (color & 255) / 255.0F; + GL11.glColor4f(f, f1, f2, f3); + drawRectD(left, top, right, bottom); + } + + public static void drawGradientRect(int left, int top, int right, int bottom, int startColor, int endColor) { + double zLevel = 0; + + float f = (float) (startColor >> 24 & 255) / 255.0F; + float f1 = (float) (startColor >> 16 & 255) / 255.0F; + float f2 = (float) (startColor >> 8 & 255) / 255.0F; + float f3 = (float) (startColor & 255) / 255.0F; + float f4 = (float) (endColor >> 24 & 255) / 255.0F; + float f5 = (float) (endColor >> 16 & 255) / 255.0F; + float f6 = (float) (endColor >> 8 & 255) / 255.0F; + float f7 = (float) (endColor & 255) / 255.0F; + GL11.glDisable(GL11.GL_TEXTURE_2D); + GL11.glEnable(GL11.GL_BLEND); + GL11.glDisable(GL11.GL_ALPHA_TEST); + OpenGlHelper.glBlendFunc(770, 771, 1, 0); + GL11.glShadeModel(GL11.GL_SMOOTH); + Tessellator tessellator = Tessellator.instance; + tessellator.startDrawingQuads(); + tessellator.setColorRGBA_F(f1, f2, f3, f); + tessellator.addVertex(right, top, zLevel); + tessellator.addVertex(left, top, zLevel); + tessellator.setColorRGBA_F(f5, f6, f7, f4); + tessellator.addVertex(left, bottom, zLevel); + tessellator.addVertex(right, bottom, zLevel); + tessellator.draw(); + GL11.glShadeModel(GL11.GL_FLAT); + GL11.glDisable(GL11.GL_BLEND); + GL11.glEnable(GL11.GL_ALPHA_TEST); + GL11.glEnable(GL11.GL_TEXTURE_2D); + } + + public static void drawTexturedModalRect(double x, double y, double width, double height, int textureX, int textureY) { + double zLevel = 0; + float f = 1 / 256f; + float f1 = 1 / 256f; + Tessellator tessellator = Tessellator.instance; + tessellator.startDrawingQuads(); + tessellator.addVertexWithUV(x + 0, y + height, zLevel, (float) (textureX) * f, (float) (textureY + height) * f1); + tessellator.addVertexWithUV(x + width, y + height, zLevel, (float) (textureX + width) * f, (float) (textureY + height) * f1); + tessellator.addVertexWithUV(x + width, y + 0, zLevel, (float) (textureX + width) * f, (float) (textureY) * f1); + tessellator.addVertexWithUV(x + 0, y + 0, zLevel, (float) (textureX) * f, (float) (textureY) * f1); + tessellator.draw(); + } + + public static void setMouse(int guiX, int guiY) { + Minecraft mc = Minecraft.getMinecraft(); + ScaledResolution scaledResolution = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); + + int factor = scaledResolution.getScaleFactor(); + + Mouse.setCursorPosition(guiX * factor, mc.displayHeight - (guiY * factor)); + } + + public static void setMouseX(int guiX) { + Minecraft mc = Minecraft.getMinecraft(); + ScaledResolution scaledResolution = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); + + int factor = scaledResolution.getScaleFactor(); + Mouse.setCursorPosition(guiX * factor, Mouse.getY()); + } + + public static void setMouseY(int guiY) { + Minecraft mc = Minecraft.getMinecraft(); + ScaledResolution scaledResolution = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); + + int factor = scaledResolution.getScaleFactor(); + Mouse.setCursorPosition(Mouse.getX(), mc.displayHeight - (guiY * factor)); + } + + public static double preciseMouseX() { + Minecraft mc = Minecraft.getMinecraft(); + ScaledResolution scaledResolution = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); + + double width = scaledResolution.getScaledWidth(); + return Mouse.getX() * width / mc.displayWidth; + } + + public static double preciseMouseY() { + Minecraft mc = Minecraft.getMinecraft(); + ScaledResolution scaledResolution = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); + + double height = scaledResolution.getScaledHeight(); + return height - Mouse.getY() * height / mc.displayHeight - 1; + } +} diff --git a/src/main/java/noppes/npcs/client/util/Color.java b/src/main/java/noppes/npcs/client/util/Color.java new file mode 100644 index 000000000..75450aeee --- /dev/null +++ b/src/main/java/noppes/npcs/client/util/Color.java @@ -0,0 +1,114 @@ +package noppes.npcs.client.util; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import net.minecraft.nbt.NBTTagCompound; +import org.lwjgl.opengl.GL11; + +public class Color { + public int color; + public float alpha; + + public Color(int color) { + setColor(color, 1); + } + + public Color(int color, float alpha) { + setColor(color, alpha); + } + + public Color(int red, int green, int blue, float alpha) { + setColor(red, green, blue, alpha); + } + + public void setColor(int color, float alpha) { + this.color = color; + this.alpha = alpha; + } + + public void setColor(int red, int green, int blue, float alpha) { + this.color = (red << 16) + (green << 8) + blue; + this.alpha = alpha; + } + + public static Color lerpRGBA(Color color1, Color color2, float fraction) { + fraction = Math.min(fraction, 1f); + int red = (int) (color1.getRed() + ((color2.getRed() - color1.getRed()) * fraction)); + int green = (int) (color1.getGreen() + ((color2.getGreen() - color1.getGreen()) * fraction)); + int blue = (int) (color1.getBlue() + ((color2.getBlue() - color1.getBlue()) * fraction)); + float newAlpha = color1.alpha + ((color2.alpha - color1.alpha) * fraction); + int newColor = (red << 16) + (green << 8) + blue; + return new Color(newColor, newAlpha); + } + + public Color lerpRGBA(Color color2, float fraction) { + return Color.lerpRGBA(this, color2, fraction); + } + + public Color multiply(float multi) { + if (multi == 1) + return this.clone(); + + int r = (int) ((getRed() * multi)); + int g = (int) ((getGreen() * multi)); + int b = (int) ((getBlue() * multi)); + float a = alpha * multi; + return new Color((r << 16) + (g << 8) + b, a); + } + + @SideOnly(Side.CLIENT) + public void glColor() { + GL11.glColor4f(getRedF(), getGreenF(), getBlueF(), alpha); + } + + public NBTTagCompound writeToNBT(NBTTagCompound compound, String name) { + compound.setInteger(name + "Color", color); + compound.setFloat(name + "Alpha", alpha); + + return compound; + } + + public void readFromNBT(NBTTagCompound compound, String name) { + setColor(compound.getInteger(name + "Color"), compound.getFloat(name + "Alpha")); + } + + public int getRed() { + return (color >> 16 & 0xFF); + } + + public float getRedF() { + return (float) (color >> 16 & 0xFF) / 255f; + } + + public int getGreen() { + return (color >> 8 & 0xFF); + } + + public float getGreenF() { + return (float) (color >> 8 & 0xFF) / 255f; + } + + public int getBlue() { + return (color & 0xFF); + } + + public float getBlueF() { + return (float) (color & 0xFF) / 255f; + } + + public static String getColor(int color) { + String str; + for (str = Integer.toHexString(color); str.length() < 6; str = "0" + str) { + } + return str; + } + + public static String getColor(int color, float alpha) { + String str = getColor(color); + return str + " | " + (int) (alpha * 255); + } + + public Color clone() { + return new Color(color, alpha); + } +} diff --git a/src/main/java/noppes/npcs/util/ValueUtil.java b/src/main/java/noppes/npcs/util/ValueUtil.java index 48cd0b1a9..a81fb9fef 100644 --- a/src/main/java/noppes/npcs/util/ValueUtil.java +++ b/src/main/java/noppes/npcs/util/ValueUtil.java @@ -1,6 +1,15 @@ package noppes.npcs.util; public class ValueUtil { + + public static float lerp(float start, float end, float delta) { + return start + delta * (end - start); + } + + public static double lerp(double start, double end, double delta) { + return start + delta * (end - start); + } + public static float clamp(float given, float min, float max) { if (given < min) return min; @@ -48,4 +57,16 @@ public class ValueUtil { return max; return given; } + + public static String format(double value) { + return (value % 1 == 0) ? String.format("%.0f", value) : String.format("%.2f", value); + } + + public static String format(String precision, float value) { + return (value % 1 == 0) ? String.format("%.0f", value) : String.format(precision, value); + } + + public static String format(String precision, double value) { + return (value % 1 == 0) ? String.format("%.0f", value) : String.format(precision, value); + } }