diff options
Diffstat (limited to 'src/main/java/io')
9 files changed, 185 insertions, 16 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/SyncedGuiDescription.java b/src/main/java/io/github/cottonmc/cotton/gui/SyncedGuiDescription.java index fae723e..cee1c69 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/SyncedGuiDescription.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/SyncedGuiDescription.java @@ -34,6 +34,7 @@ import io.github.cottonmc.cotton.gui.widget.WPanel; import io.github.cottonmc.cotton.gui.widget.WPlayerInvPanel; import io.github.cottonmc.cotton.gui.widget.WWidget; import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; +import io.github.cottonmc.cotton.gui.widget.data.Insets; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; @@ -49,7 +50,7 @@ public class SyncedGuiDescription extends ScreenHandler implements GuiDescriptio protected World world; protected PropertyDelegate propertyDelegate; - protected WPanel rootPanel = new WGridPanel(); + protected WPanel rootPanel = new WGridPanel().setInsets(Insets.ROOT_PANEL); protected int titleColor = WLabel.DEFAULT_TEXT_COLOR; protected int darkTitleColor = WLabel.DEFAULT_DARKMODE_TEXT_COLOR; protected boolean fullscreen = false; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java b/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java index 13ca534..f66b7bf 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java @@ -27,16 +27,14 @@ public interface BackgroundPainter { * <p>This background painter uses {@code libgui:textures/widget/panel_light.png} as the light texture and * {@code libgui:textures/widget/panel_dark.png} as the dark texture. * - * <p>This background painter applies a padding of 8 pixels to all sides around the widget. - * * <p>This background painter is the default painter for root panels. * * You can override {@link io.github.cottonmc.cotton.gui.GuiDescription#addPainters()} to customize the painter yourself. * * @since 1.5.0 */ public static BackgroundPainter VANILLA = createLightDarkVariants( - createNinePatch(new Identifier("libgui", "textures/widget/panel_light.png"), 8), - createNinePatch(new Identifier("libgui", "textures/widget/panel_dark.png"), 8) + createNinePatch(new Identifier("libgui", "textures/widget/panel_light.png")), + createNinePatch(new Identifier("libgui", "textures/widget/panel_dark.png")) ); /** diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/LightweightGuiDescription.java b/src/main/java/io/github/cottonmc/cotton/gui/client/LightweightGuiDescription.java index 54ae02f..55b62a4 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/LightweightGuiDescription.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/LightweightGuiDescription.java @@ -9,13 +9,14 @@ import io.github.cottonmc.cotton.gui.widget.WLabel; import io.github.cottonmc.cotton.gui.widget.WPanel; import io.github.cottonmc.cotton.gui.widget.WWidget; import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; +import io.github.cottonmc.cotton.gui.widget.data.Insets; import org.jetbrains.annotations.Nullable; /** * A GuiDescription without any associated Minecraft classes */ public class LightweightGuiDescription implements GuiDescription { - protected WPanel rootPanel = new WGridPanel(); + protected WPanel rootPanel = new WGridPanel().setInsets(Insets.ROOT_PANEL); protected PropertyDelegate propertyDelegate; protected WWidget focus; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/client/modmenu/ConfigGui.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/modmenu/ConfigGui.java index 5f3d4fa..ccb2a37 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/impl/client/modmenu/ConfigGui.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/modmenu/ConfigGui.java @@ -1,5 +1,7 @@ package io.github.cottonmc.cotton.gui.impl.client.modmenu; +import io.github.cottonmc.cotton.gui.widget.data.Insets; + import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; import net.minecraft.text.TranslatableText; @@ -16,6 +18,7 @@ public class ConfigGui extends LightweightGuiDescription { public ConfigGui(Screen previous) { WGridPanel root = new WGridPanel(); + root.setInsets(Insets.ROOT_PANEL); setRootPanel(root); WToggleButton darkmodeButton = new WToggleButton(new TranslatableText("option.libgui.darkmode")) { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WGridPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WGridPanel.java index 69aa036..f78cc08 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WGridPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WGridPanel.java @@ -1,5 +1,9 @@ package io.github.cottonmc.cotton.gui.widget; +import io.github.cottonmc.cotton.gui.widget.data.Insets; + +import java.util.Objects; + /** * A panel that positions children in a grid. */ @@ -10,6 +14,8 @@ public class WGridPanel extends WPanel { */ protected int grid = 18; + private Insets insets = Insets.NONE; + /** * Constructs a grid panel with the default grid size. */ @@ -35,12 +41,12 @@ public class WGridPanel extends WPanel { public void add(WWidget w, int x, int y) { children.add(w); w.parent = this; - w.setLocation(x * grid, y * grid); + w.setLocation(x * grid + insets.left, y * grid + insets.top); if (w.canResize()) { w.setSize(grid, grid); } - expandToFit(w); + expandToFit(w, insets); } /** @@ -55,11 +61,33 @@ public class WGridPanel extends WPanel { public void add(WWidget w, int x, int y, int width, int height) { children.add(w); w.parent = this; - w.setLocation(x * grid, y * grid); + w.setLocation(x * grid + insets.left, y * grid + insets.top); if (w.canResize()) { w.setSize(width * grid, height * grid); } - expandToFit(w); + expandToFit(w, insets); + } + + /** + * Gets the layout insets of this panel. + * + * @return the insets + * @since 4.0.0 + */ + public Insets getInsets() { + return insets; + } + + /** + * Sets the layout insets of this panel. + * + * @param insets the insets, should not be null + * @return this panel + * @since 4.0.0 + */ + public WGridPanel setInsets(Insets insets) { + this.insets = Objects.requireNonNull(insets, "insets"); + return this; } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java index 51a10ce..0e666c8 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java @@ -1,5 +1,7 @@ package io.github.cottonmc.cotton.gui.widget; +import io.github.cottonmc.cotton.gui.widget.data.Insets; + import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.util.math.MatrixStack; @@ -78,8 +80,19 @@ public abstract class WPanel extends WWidget { * @param w the widget */ protected void expandToFit(WWidget w) { - int pushRight = w.getX()+w.getWidth(); - int pushDown = w.getY()+w.getHeight(); + expandToFit(w, Insets.NONE); + } + + /** + * Expands this panel be at least as large as the widget. + * + * @param w the widget + * @param insets the layout insets + * @since 4.0.0 + */ + protected void expandToFit(WWidget w, Insets insets) { + int pushRight = w.getX()+w.getWidth()+insets.right; + int pushDown = w.getY()+w.getHeight()+insets.bottom; this.setSize(Math.max(this.getWidth(), pushRight), Math.max(this.getHeight(), pushDown)); } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlainPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlainPanel.java index d658d1f..89a9891 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlainPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlainPanel.java @@ -1,9 +1,15 @@ package io.github.cottonmc.cotton.gui.widget; +import io.github.cottonmc.cotton.gui.widget.data.Insets; + +import java.util.Objects; + /** * A panel that positions children by pixel-perfect positions. */ public class WPlainPanel extends WPanel { + private Insets insets = Insets.NONE; + /** * Adds a new widget to this panel. * @@ -17,12 +23,12 @@ public class WPlainPanel extends WPanel { public void add(WWidget w, int x, int y) { children.add(w); w.parent = this; - w.setLocation(x, y); + w.setLocation(insets.left + x, insets.top + y); if (w.canResize()) { w.setSize(18, 18); } - expandToFit(w); + expandToFit(w, insets); //valid = false; } @@ -38,12 +44,34 @@ public class WPlainPanel extends WPanel { public void add(WWidget w, int x, int y, int width, int height) { children.add(w); w.parent = this; - w.setLocation(x, y); + w.setLocation(insets.left + x, insets.top + y); if (w.canResize()) { w.setSize(width, height); } - expandToFit(w); + expandToFit(w, insets); //valid = false; } + + /** + * Gets the layout insets of this panel. + * + * @return the insets + * @since 4.0.0 + */ + public Insets getInsets() { + return insets; + } + + /** + * Sets the layout insets of this panel. + * + * @param insets the insets, should not be null + * @return this panel + * @since 4.0.0 + */ + public WPlainPanel setInsets(Insets insets) { + this.insets = Objects.requireNonNull(insets, "insets"); + return this; + } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java index 2bc7d15..c105924 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java @@ -59,6 +59,11 @@ public class WPlayerInvPanel extends WPlainPanel { this.add(hotbar, 0, y + 58); } + @Override + public boolean canResize() { + return false; + } + /** * Creates a vanilla-style inventory label for a player inventory. * diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Insets.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Insets.java new file mode 100644 index 0000000..85c32ed --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Insets.java @@ -0,0 +1,92 @@ +package io.github.cottonmc.cotton.gui.widget.data; + +import java.util.Objects; + +/** + * The layout insets of a panel. The insets describe how many pixels should be around the panel's contents. + * For example, root panels have 7 pixels around their contents, and that is set via {@link #ROOT_PANEL}. + * + * @since 4.0.0 + */ +public final class Insets { + /** + * Empty layout insets that do not provide any borders around content. + */ + public static final Insets NONE = new Insets(0); + + /** + * The default insets of a root panel, providing 7 pixels around the content on all sides. + */ + public static final Insets ROOT_PANEL = new Insets(7); + + /** The top (-Y) inset size. */ + public final int top; + /** The left (-X) inset size. */ + public final int left; + /** The bottom (+Y) inset size. */ + public final int bottom; + /** The right (+X) inset size. */ + public final int right; + + /** + * Constructs layout insets. + * + * @param top the top (-Y) inset size + * @param left the left (-X) inset size + * @param bottom the bottom (+Y) inset size + * @param right the right (+X) inset size + */ + public Insets(int top, int left, int bottom, int right) { + if (top < 0) throw new IllegalArgumentException("top cannot be negative, found " + top); + if (left < 0) throw new IllegalArgumentException("left cannot be negative, found " + left); + if (bottom < 0) throw new IllegalArgumentException("bottom cannot be negative, found " + bottom); + if (right < 0) throw new IllegalArgumentException("right cannot be negative, found " + right); + + this.top = top; + this.left = left; + this.bottom = bottom; + this.right = right; + } + + /** + * Constructs layout insets. + * + * @param vertical the vertical (Y) size of the insets + * @param horizontal the horizontal (X) size of the insets + */ + public Insets(int vertical, int horizontal) { + this(vertical, horizontal, vertical, horizontal); + } + + /** + * Constructs layout insets. + * + * @param size the size of the insets on all sides + */ + public Insets(int size) { + this(size, size, size, size); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Insets insets = (Insets) o; + return top == insets.top && left == insets.left && bottom == insets.bottom && right == insets.right; + } + + @Override + public int hashCode() { + return Objects.hash(top, left, bottom, right); + } + + @Override + public String toString() { + return "Insets{" + + "top=" + top + + ", left=" + left + + ", bottom=" + bottom + + ", right=" + right + + '}'; + } +} |