diff options
author | Falkreon <falkreon@gmail.com> | 2019-08-31 13:07:54 -0500 |
---|---|---|
committer | Falkreon <falkreon@gmail.com> | 2019-08-31 13:07:54 -0500 |
commit | 126fbe5c3e5b4b4eb9c459e9b1047bad5be34ca4 (patch) | |
tree | 4b49feda4c7fe964708b487daddb238ed88cfd3f | |
parent | a018db6c699488aa9b9e392cd4515c97fade095a (diff) | |
download | LibGui-126fbe5c3e5b4b4eb9c459e9b1047bad5be34ca4.tar.gz LibGui-126fbe5c3e5b4b4eb9c459e9b1047bad5be34ca4.tar.bz2 LibGui-126fbe5c3e5b4b4eb9c459e9b1047bad5be34ca4.zip |
Improve ScreenDrawing, move Axis/Alignment into data pkg, add server-safe Color
12 files changed, 267 insertions, 22 deletions
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,16 +39,36 @@ 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. */ public static void coloredRect(int left, int top, int width, int height, int 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<x+getWidth() && mouseY<y+getHeight()); + public void paintBackground(int x, int y, int mouseX, int mouseY) { + boolean hovered = (mouseX>=0 && mouseY>=0 && mouseX<getWidth() && mouseY<getHeight()); int state = 1; //1=regular. 2=hovered. 0=disabled. if (!enabled) state = 0; else if (hovered) state = 2; @@ -61,9 +59,6 @@ public class WButton extends WWidget { ScreenDrawing.drawCenteredWithShadow(label.asFormattedString(), x+(getWidth()/2), y + ((20 - 8) / 2), color); //LibGuiClient.config.darkMode ? darkmodeColor : color); } - - - super.paintForeground(x, y, mouseX, mouseY); } @Override diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java index d17729f..0966a20 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java @@ -2,10 +2,12 @@ package io.github.cottonmc.cotton.gui.widget; import io.github.cottonmc.cotton.gui.client.LibGuiClient; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; +import io.github.cottonmc.cotton.gui.widget.data.Alignment; import net.minecraft.text.LiteralText; import net.minecraft.text.Text; public class WLabel extends WWidget { protected Text text; + protected Alignment alignment = Alignment.LEFT; protected int color; protected int darkmodeColor; @@ -62,4 +64,9 @@ public class WLabel extends WWidget { this.text = text; return this; } + + public WLabel setAlignment(Alignment align) { + this.alignment = align; + return this; + } }
\ No newline at end of file diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java index 16a5f60..a959485 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java @@ -1,6 +1,7 @@ package io.github.cottonmc.cotton.gui.widget; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; +import io.github.cottonmc.cotton.gui.widget.data.Axis; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.gui.widget.AbstractButtonWidget; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java index 75e337d..e86e1da 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java @@ -7,6 +7,7 @@ import java.util.function.BiConsumer; import java.util.function.Supplier; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; +import io.github.cottonmc.cotton.gui.widget.data.Axis; /** * Similar to the RecyclerView in Android, this widget represents a scrollable list of items. diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java index 26caa8b..e493152 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java @@ -1,6 +1,7 @@ package io.github.cottonmc.cotton.gui.widget; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; +import io.github.cottonmc.cotton.gui.widget.data.Axis; public class WScrollBar extends WWidget { protected Axis axis = Axis.HORIZONTAL; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java index e9c862f..3bd7c20 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java @@ -2,6 +2,7 @@ package io.github.cottonmc.cotton.gui.widget; import io.github.cottonmc.cotton.gui.client.BackgroundPainter; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; +import io.github.cottonmc.cotton.gui.widget.data.Axis; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.util.Identifier; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Alignment.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Alignment.java new file mode 100644 index 0000000..3e3420a --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Alignment.java @@ -0,0 +1,7 @@ +package io.github.cottonmc.cotton.gui.widget.data; + +public enum Alignment { + LEFT, + CENTER, + RIGHT; +} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Axis.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Axis.java new file mode 100644 index 0000000..c94624a --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Axis.java @@ -0,0 +1,6 @@ +package io.github.cottonmc.cotton.gui.widget.data; + +public enum Axis { + HORIZONTAL, + VERTICAL; +} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Color.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Color.java new file mode 100644 index 0000000..bf6806a --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Color.java @@ -0,0 +1,164 @@ +package io.github.cottonmc.cotton.gui.widget.data; + +public interface Color { + /** + * Gets an ARGB integer representing this color in the sRGB colorspace. + */ + public int toRgb(); + + + public static Color rgb(int value) { + return new RGB(value); + } + + public static Color rgb(int a, int r, int g, int b) { + return new RGB(a, r, g, b); + } + + public static Color opaqueRgb(int value) { + return new RGB(value | 0xFF_000000); + } + + public static class RGB implements Color { + private final int value; + + public RGB(int value) { + this.value = value; + } + + public RGB(int a, int r, int g, int b) { + value = + ((a & 0xFF) << 24) | + ((r & 0xFF) << 16) | + ((g & 0xFF) << 8) | + (b & 0xFF); + } + + @Override + public int toRgb() { + return value; + } + + public int getA() { + return (value >> 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 <em>luma</em>. */ + 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; + } + } +} |