diff options
Diffstat (limited to 'src/main/java/io')
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java | 10 | ||||
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java | 8 | ||||
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WDynamicLabel.java | 6 | ||||
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java | 59 | ||||
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java | 8 | ||||
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java | 8 | ||||
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/data/HorizontalAlignment.java (renamed from src/main/java/io/github/cottonmc/cotton/gui/widget/data/Alignment.java) | 2 | ||||
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/data/VerticalAlignment.java | 7 |
8 files changed, 78 insertions, 30 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 cde778a..df66c1d 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 @@ -11,7 +11,7 @@ import net.minecraft.text.StringRenderable; import net.minecraft.util.Identifier; import org.lwjgl.opengl.GL11; -import io.github.cottonmc.cotton.gui.widget.data.Alignment; +import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; /** * {@code ScreenDrawing} contains utility methods for drawing contents on a screen. @@ -331,7 +331,7 @@ public class ScreenDrawing { * @param width the width of the string, used for aligning * @param color the text color */ - public static void drawString(MatrixStack matrices, String s, Alignment align, int x, int y, int width, int color) { + public static void drawString(MatrixStack matrices, String s, HorizontalAlignment align, int x, int y, int width, int color) { switch(align) { case LEFT: { MinecraftClient.getInstance().textRenderer.draw(matrices, s, x, y, color); @@ -364,7 +364,7 @@ public class ScreenDrawing { * @param color the text color * @since 1.9.0 */ - public static void drawString(MatrixStack matrices, StringRenderable text, Alignment align, int x, int y, int width, int color) { + public static void drawString(MatrixStack matrices, StringRenderable text, HorizontalAlignment align, int x, int y, int width, int color) { switch(align) { case LEFT: { MinecraftClient.getInstance().textRenderer.draw(matrices, text, x, y, color); @@ -396,7 +396,7 @@ public class ScreenDrawing { * @param width the width of the string, used for aligning * @param color the text color */ - public static void drawStringWithShadow(MatrixStack matrices, String s, Alignment align, int x, int y, int width, int color) { + public static void drawStringWithShadow(MatrixStack matrices, String s, HorizontalAlignment align, int x, int y, int width, int color) { switch(align) { case LEFT: { MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, s, x, y, color); @@ -428,7 +428,7 @@ public class ScreenDrawing { * @param width the width of the string, used for aligning * @param color the text color */ - public static void drawStringWithShadow(MatrixStack matrices, StringRenderable text, Alignment align, int x, int y, int width, int color) { + public static void drawStringWithShadow(MatrixStack matrices, StringRenderable text, HorizontalAlignment align, int x, int y, int width, int color) { switch(align) { case LEFT: { MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, text, x, y, color); 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 b308483..957725e 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 @@ -10,14 +10,14 @@ import net.minecraft.sound.SoundEvents; import net.minecraft.text.StringRenderable; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; -import io.github.cottonmc.cotton.gui.widget.data.Alignment; +import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; public class WButton extends WWidget { private StringRenderable label; protected int color = WLabel.DEFAULT_TEXT_COLOR; protected int darkmodeColor = WLabel.DEFAULT_TEXT_COLOR; private boolean enabled = true; - protected Alignment alignment = Alignment.CENTER; + protected HorizontalAlignment alignment = HorizontalAlignment.CENTER; private Runnable onClick; @@ -119,11 +119,11 @@ public class WButton extends WWidget { return this; } - public Alignment getAlignment() { + public HorizontalAlignment getAlignment() { return alignment; } - public WButton setAlignment(Alignment alignment) { + public WButton setAlignment(HorizontalAlignment alignment) { this.alignment = alignment; return this; } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WDynamicLabel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WDynamicLabel.java index 9e87174..a3adac9 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WDynamicLabel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WDynamicLabel.java @@ -4,7 +4,7 @@ import net.minecraft.client.util.math.MatrixStack; 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 io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; import java.util.function.Supplier; @@ -17,7 +17,7 @@ import java.util.function.Supplier; */ public class WDynamicLabel extends WWidget { protected Supplier<String> text; - protected Alignment alignment = Alignment.LEFT; + protected HorizontalAlignment alignment = HorizontalAlignment.LEFT; protected int color; protected int darkmodeColor; @@ -71,7 +71,7 @@ public class WDynamicLabel extends WWidget { return this; } - public WDynamicLabel setAlignment(Alignment align) { + public WDynamicLabel setAlignment(HorizontalAlignment align) { this.alignment = align; return this; } 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 ab1764f..ffd8433 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 @@ -1,8 +1,10 @@ package io.github.cottonmc.cotton.gui.widget; +import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.LiteralText; @@ -12,7 +14,7 @@ import net.minecraft.text.Style; import io.github.cottonmc.cotton.gui.client.LibGuiClient; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.client.TextHoverRendererScreen; -import io.github.cottonmc.cotton.gui.widget.data.Alignment; +import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; import javax.annotation.Nullable; @@ -21,7 +23,8 @@ import javax.annotation.Nullable; */ public class WLabel extends WWidget { protected StringRenderable text; - protected Alignment alignment = Alignment.LEFT; + protected HorizontalAlignment horizontalAlignment = HorizontalAlignment.LEFT; + protected VerticalAlignment verticalAlignment = VerticalAlignment.TOP; protected int color; protected int darkmodeColor; @@ -78,7 +81,24 @@ public class WLabel extends WWidget { @Override public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { - ScreenDrawing.drawString(matrices, text, alignment, x, y, this.getWidth(), LibGuiClient.config.darkMode ? darkmodeColor : color); + MinecraftClient mc = MinecraftClient.getInstance(); + TextRenderer renderer = mc.textRenderer; + int yOffset; + + switch (verticalAlignment) { + case CENTER: + yOffset = height / 2 - renderer.fontHeight / 2; + break; + case BOTTOM: + yOffset = height - renderer.fontHeight; + break; + case TOP: + default: + yOffset = 0; + break; + } + + ScreenDrawing.drawString(matrices, text, horizontalAlignment, x, y + yOffset, this.getWidth(), LibGuiClient.config.darkMode ? darkmodeColor : color); Style hoveredTextStyle = getTextStyleAt(mouseX, mouseY); if (hoveredTextStyle != null) { @@ -212,23 +232,44 @@ public class WLabel extends WWidget { } /** - * Gets the text alignment of this label. + * Gets the horizontal text alignment of this label. + * + * @return the alignment + * @since 2.0.0 + */ + public HorizontalAlignment getHorizontalAlignment() { + return horizontalAlignment; + } + + /** + * Sets the horizontal text alignment of this label. + * + * @param align the new text alignment + * @return this label + */ + public WLabel setHorizontalAlignment(HorizontalAlignment align) { + this.horizontalAlignment = align; + return this; + } + + /** + * Gets the vertical text alignment of this label. * * @return the alignment * @since 2.0.0 */ - public Alignment getAlignment() { - return alignment; + public VerticalAlignment getVerticalAlignment() { + return verticalAlignment; } /** - * Sets the text alignment of this label. + * Sets the vertical text alignment of this label. * * @param align the new text alignment * @return this label */ - public WLabel setAlignment(Alignment align) { - this.alignment = align; + public WLabel setVerticalAlignment(VerticalAlignment align) { + this.verticalAlignment = 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 4cce813..200b191 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 @@ -9,7 +9,7 @@ import net.minecraft.text.StringRenderable; import net.minecraft.util.math.Quaternion; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; -import io.github.cottonmc.cotton.gui.widget.data.Alignment; +import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; import io.github.cottonmc.cotton.gui.widget.data.Axis; import javax.annotation.Nullable; @@ -28,7 +28,7 @@ public class WLabeledSlider extends WAbstractSlider { @Nullable private StringRenderable label = null; @Nullable private LabelUpdater labelUpdater = null; - private Alignment labelAlignment = Alignment.CENTER; + private HorizontalAlignment labelAlignment = HorizontalAlignment.CENTER; /** * Constructs a horizontal slider with no default label. @@ -117,7 +117,7 @@ public class WLabeledSlider extends WAbstractSlider { * * @return the alignment */ - public Alignment getLabelAlignment() { + public HorizontalAlignment getLabelAlignment() { return labelAlignment; } @@ -126,7 +126,7 @@ public class WLabeledSlider extends WAbstractSlider { * * @param labelAlignment the new alignment */ - public void setLabelAlignment(Alignment labelAlignment) { + public void setLabelAlignment(HorizontalAlignment labelAlignment) { this.labelAlignment = labelAlignment; } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java index 8e4d826..ff83d6d 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java @@ -12,7 +12,7 @@ import net.minecraft.text.Style; import io.github.cottonmc.cotton.gui.client.LibGuiClient; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.client.TextHoverRendererScreen; -import io.github.cottonmc.cotton.gui.widget.data.Alignment; +import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; import javax.annotation.Nullable; import java.util.List; @@ -27,7 +27,7 @@ public class WText extends WWidget { protected StringRenderable text; protected int color; protected int darkmodeColor; - protected Alignment alignment = Alignment.LEFT; + protected HorizontalAlignment alignment = HorizontalAlignment.LEFT; private List<StringRenderable> wrappedLines; private boolean wrappingScheduled = false; @@ -208,7 +208,7 @@ public class WText extends WWidget { * @return the alignment * @since 1.9.0 */ - public Alignment getAlignment() { + public HorizontalAlignment getAlignment() { return alignment; } @@ -219,7 +219,7 @@ public class WText extends WWidget { * @return this widget * @since 1.9.0 */ - public WText setAlignment(Alignment alignment) { + public WText setAlignment(HorizontalAlignment alignment) { this.alignment = alignment; return this; } 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/HorizontalAlignment.java index 3e3420a..61c914d 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Alignment.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/HorizontalAlignment.java @@ -1,6 +1,6 @@ package io.github.cottonmc.cotton.gui.widget.data; -public enum Alignment { +public enum HorizontalAlignment { LEFT, CENTER, RIGHT; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/data/VerticalAlignment.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/VerticalAlignment.java new file mode 100644 index 0000000..8123ad3 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/VerticalAlignment.java @@ -0,0 +1,7 @@ +package io.github.cottonmc.cotton.gui.widget.data; + +public enum VerticalAlignment { + TOP, + CENTER, + BOTTOM; +} |