From 126fbe5c3e5b4b4eb9c459e9b1047bad5be34ca4 Mon Sep 17 00:00:00 2001 From: Falkreon Date: Sat, 31 Aug 2019 13:07:54 -0500 Subject: Improve ScreenDrawing, move Axis/Alignment into data pkg, add server-safe Color --- .../cottonmc/cotton/gui/client/ScreenDrawing.java | 84 +++++++++-- .../io/github/cottonmc/cotton/gui/widget/Axis.java | 6 - .../cotton/gui/widget/WAbstractSlider.java | 2 + .../github/cottonmc/cotton/gui/widget/WButton.java | 9 +- .../github/cottonmc/cotton/gui/widget/WLabel.java | 7 + .../cottonmc/cotton/gui/widget/WLabeledSlider.java | 1 + .../cottonmc/cotton/gui/widget/WListPanel.java | 1 + .../cottonmc/cotton/gui/widget/WScrollBar.java | 1 + .../github/cottonmc/cotton/gui/widget/WSlider.java | 1 + .../cottonmc/cotton/gui/widget/data/Alignment.java | 7 + .../cottonmc/cotton/gui/widget/data/Axis.java | 6 + .../cottonmc/cotton/gui/widget/data/Color.java | 164 +++++++++++++++++++++ 12 files changed, 267 insertions(+), 22 deletions(-) delete mode 100644 src/main/java/io/github/cottonmc/cotton/gui/widget/Axis.java create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/widget/data/Alignment.java create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/widget/data/Axis.java create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/widget/data/Color.java (limited to 'src/main') diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java b/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java index 1973999..243c885 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java @@ -4,6 +4,7 @@ import org.lwjgl.opengl.GL11; import com.mojang.blaze3d.systems.RenderSystem; +import io.github.cottonmc.cotton.gui.widget.data.Alignment; import net.minecraft.class_4493.class_4534; import net.minecraft.class_4493.class_4535; import net.minecraft.client.MinecraftClient; @@ -16,11 +17,11 @@ import net.minecraft.util.Identifier; public class ScreenDrawing { private ScreenDrawing() {} - public static void texturedRect(int left, int top, int width, int height, Identifier texture, int color) { - texturedRect(left, top, width, height, texture, 0, 0, 1, 1, color); + public static void texturedRect(int x, int y, int width, int height, Identifier texture, int color) { + texturedRect(x, y, width, height, texture, 0, 0, 1, 1, color); } - - public static void texturedRect(int left, int top, int width, int height, Identifier texture, float u1, float v1, float u2, float v2, int color) { + + public static void texturedRect(int x, int y, int width, int height, Identifier texture, float u1, float v1, float u2, float v2, int color) { MinecraftClient.getInstance().getTextureManager().bindTexture(texture); //float scale = 0.00390625F; @@ -38,15 +39,35 @@ public class ScreenDrawing { RenderSystem.blendFuncSeparate(class_4535.SRC_ALPHA, class_4534.ONE_MINUS_SRC_ALPHA, class_4535.ONE, class_4534.ZERO); RenderSystem.color4f(r, g, b, 1.0f); buffer.begin(GL11.GL_QUADS, VertexFormats.POSITION_UV); //I thought GL_QUADS was deprecated but okay, sure. - buffer.vertex(left, top + height, 0).texture(u1, v2).next(); - buffer.vertex(left + width, top + height, 0).texture(u2, v2).next(); - buffer.vertex(left + width, top, 0).texture(u2, v1).next(); - buffer.vertex(left, top, 0).texture(u1, v1).next(); + buffer.vertex(x, y + height, 0).texture(u1, v2).next(); + buffer.vertex(x + width, y + height, 0).texture(u2, v2).next(); + buffer.vertex(x + width, y, 0).texture(u2, v1).next(); + buffer.vertex(x, y, 0).texture(u1, v1).next(); tessellator.draw(); //GlStateManager.enableTexture2D(); RenderSystem.disableBlend(); } + /** + * If the texture is 256x256, this draws the texture at one pixel per texel. + * @param x the x coordinate of the box on-screen + * @param y the y coordinate of the box on-screen + * @param width the width of the box on-screen + * @param height the height of the box on-screen + * @param texture the Identifier for the texture + * @param textureX the x offset into the texture + * @param textureY the y offset into the texture + * @param color a color to tint the texture. This can be transparent! Use 0xFF_FFFFFF if you don't want a color tint + */ + public static void texturedGuiRect(int x, int y, int width, int height, Identifier texture, int textureX, int textureY, int color) { + float px = 1/256f; + texturedRect(x, y, width, height, texture, textureX*px, textureY*px, (textureX+width)*px, (textureY+height)*px, color); + } + + public static void texturedGuiRect(int left, int top, int width, int height, Identifier texture, int color) { + texturedGuiRect(left, top, width, height, texture, 0, 0, color); + } + /** * Draws an untextured rectangle of the specified RGB color. */ @@ -195,11 +216,56 @@ public class ScreenDrawing { coloredRect(x + 1, y + height - 1, width - 1, 1, bottomright); //Bottom hilight } + public static void drawString(String s, Alignment align, int x, int y, int width, int color) { + switch(align) { + case LEFT: { + MinecraftClient.getInstance().textRenderer.draw(s, x, y, color); + } + break; + case CENTER: { + int wid = MinecraftClient.getInstance().textRenderer.getStringWidth(s); + int l = (width-2) - (wid/2); + MinecraftClient.getInstance().textRenderer.draw(s, l, y, color); + } + break; + case RIGHT: { + int wid = MinecraftClient.getInstance().textRenderer.getStringWidth(s); + int l = width - wid; + MinecraftClient.getInstance().textRenderer.draw(s, l, y, color); + } + break; + } + } + + public static void drawStringWithShadow(String s, Alignment align, int x, int y, int width, int color) { + switch(align) { + case LEFT: { + MinecraftClient.getInstance().textRenderer.drawWithShadow(s, x, y, color); + } + break; + case CENTER: { + int wid = MinecraftClient.getInstance().textRenderer.getStringWidth(s); + int l = (width-2) - (wid/2); + MinecraftClient.getInstance().textRenderer.drawWithShadow(s, l, y, color); + } + break; + case RIGHT: { + int wid = MinecraftClient.getInstance().textRenderer.getStringWidth(s); + int l = width - wid; + MinecraftClient.getInstance().textRenderer.drawWithShadow(s, l, y, color); + } + break; + } + } + public static void drawString(String s, int x, int y, int color) { MinecraftClient.getInstance().textRenderer.draw(s, x, y, color); - //MinecraftClient.getInstance().getFontManager().getTextRenderer(MinecraftClient.DEFAULT_TEXT_RENDERER_ID).draw(s, x, y, color); } + /** + * @deprecated for removal; please use {@link #drawStringWithShadow(String, Alignment, int, int, int, int)} + */ + @Deprecated public static void drawCenteredWithShadow(String s, int x, int y, int color) { TextRenderer render = MinecraftClient.getInstance().getFontManager().getTextRenderer(MinecraftClient.DEFAULT_TEXT_RENDERER_ID); render.drawWithShadow(s, (float)(x - render.getStringWidth(s) / 2), (float)y, color); diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/Axis.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/Axis.java deleted file mode 100644 index fd97f20..0000000 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/Axis.java +++ /dev/null @@ -1,6 +0,0 @@ -package io.github.cottonmc.cotton.gui.widget; - -public enum Axis { - HORIZONTAL, - VERTICAL; -} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java index 074371f..641b396 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java @@ -3,6 +3,8 @@ package io.github.cottonmc.cotton.gui.widget; import net.minecraft.util.math.MathHelper; import org.lwjgl.glfw.GLFW; +import io.github.cottonmc.cotton.gui.widget.data.Axis; + import javax.annotation.Nullable; import java.util.function.IntConsumer; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java index 7c3a3b5..f491238 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java @@ -30,10 +30,8 @@ public class WButton extends WWidget { @Override - public void paintForeground(int x, int y, int mouseX, int mouseY) { - //System.out.println("Mouse: { "+mouseX+", "+mouseY+" }"); - - boolean hovered = (mouseX>=x && mouseY>=y && mouseX=0 && mouseY>=0 && mouseX> 24) & 0xFF; + } + + public int getR() { + return (value >> 16) & 0xFF; + } + + public int getG() { + return (value >> 8) & 0xFF; + } + + public int getB() { + return value & 0xFF; + } + + /** Gets the chroma value, which is related to the length of the vector in projected (hexagonal) space. */ + public int getChroma() { + int r = getR(); + int g = getG(); + int b = getB(); + + int max = Math.max(Math.max(r, g), b); + int min = Math.min(Math.min(r, g), b); + return max-min; + } + + /** Gets the HSV/HSL Hue, which is the angle around the color hexagon (or circle) */ + public int getHue() { + float r = getR()/255f; + float g = getG()/255f; + float b = getB()/255f; + + float max = Math.max(Math.max(r, g), b); + float min = Math.min(Math.min(r, g), b); + float chroma = max-min; + + if (chroma==0) return 0; + + if (max>=r) return + (int)((((g-b)/chroma) % 6 ) * 60); + if (max>=g) return + (int)((((b-r)/chroma) + 2) * 60); + if (max>=b) return + (int)((((r-g)/chroma) + 4) * 60); + + //Mathematically, we shouldn't ever reach here + return 0; + } + + /** Gets the HSL Lightness, or average light intensity, of this color */ + public int getLightness() { + int r = getR(); + int g = getG(); + int b = getB(); + + int max = Math.max(Math.max(r, g), b); + int min = Math.min(Math.min(r, g), b); + return (max+min)/2; + } + + /** Gets the HSL Luma, or perceptual brightness, of this color */ + public int getLuma() { + float r = getR()/255f; + float g = getG()/255f; + float b = getB()/255f; + + return (int)(((0.2126f * r) + (0.7152f * g) + (0.0722f * b)) * 255); + } + + /** Gets the HSV Value, which is just the largest component in the color */ + public int getValue() { + int r = getR(); + int g = getG(); + int b = getB(); + + return Math.max(Math.max(r, g), b); + } + + /** Gets the saturation for this color based on chrominance and HSV Value */ + public float getHSVSaturation() { + float v = getValue(); //I don't rescale these to 0..1 because it's just the ratio between them + if (v==0) return 0; + float c = getChroma(); + return c/v; + } + + /** Gets the saturation for this color based on chrominance and HSL luma. */ + public float getHSLSaturation() { + float l = getLuma()/255f; //rescaled here because there's more than just a ratio going on + if (l==0 || l==1) return 0; + float c = getChroma()/255f; + return c / (1 - Math.abs(2*l - 1)); + } + } + + public static class HSL implements Color { + /** HSL Hue, from 0..1 */ + private float hue; + /** HSL Saturation, from 0..1 */ + private float sat; + /** HSL Luma, from 0..1 */ + private float luma; + + public int toRgb() { + float chroma = 1 - (Math.abs(2*luma - 1)); + //TODO: Finish implementing + return 0; + } + + public float getHue() { + return hue; + } + + public float getSaturation() { + return sat; + } + + public float getLuma() { + return luma; + } + } +} -- cgit