aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJuuxel <6596629+Juuxel@users.noreply.github.com>2021-02-27 17:07:57 +0200
committerJuuxel <6596629+Juuxel@users.noreply.github.com>2021-02-27 17:07:57 +0200
commit843ca3ed723b8f35c6bdf5aab19a3b63002f3ef8 (patch)
tree327f461d06f2437f4ade6956effb11650da9e87d /src
parent13a99174d682a31088fee27990b7c8e38239ee84 (diff)
downloadLibGui-843ca3ed723b8f35c6bdf5aab19a3b63002f3ef8.tar.gz
LibGui-843ca3ed723b8f35c6bdf5aab19a3b63002f3ef8.tar.bz2
LibGui-843ca3ed723b8f35c6bdf5aab19a3b63002f3ef8.zip
Add panel layout insets to replace background painter padding
Diffstat (limited to 'src')
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/SyncedGuiDescription.java3
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java13
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/LightweightGuiDescription.java3
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/ConfigGui.java4
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WGridPanel.java39
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java16
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WPlainPanel.java39
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java5
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/data/Insets.java92
9 files changed, 193 insertions, 21 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 a7367cf..2cb1de6 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 c4b545e..fd0a12a 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
@@ -1,10 +1,9 @@
package io.github.cottonmc.cotton.gui.client;
-import io.github.cottonmc.cotton.gui.impl.LibGuiCommon;
-
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.Identifier;
+import io.github.cottonmc.cotton.gui.impl.LibGuiCommon;
import io.github.cottonmc.cotton.gui.widget.WItemSlot;
import io.github.cottonmc.cotton.gui.widget.WWidget;
@@ -28,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(LibGuiCommon.MOD_ID, "textures/widget/panel_light.png"), 8),
- createNinePatch(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/panel_dark.png"), 8)
+ createNinePatch(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/panel_light.png")),
+ createNinePatch(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/panel_dark.png"))
);
/**
@@ -92,7 +89,7 @@ public interface BackgroundPainter {
*/
public static BackgroundPainter createColorful(int panelColor) {
return (matrices, left, top, panel) -> {
- ScreenDrawing.drawGuiPanel(matrices, left-8, top-8, panel.getWidth()+16, panel.getHeight()+16, panelColor);
+ ScreenDrawing.drawGuiPanel(matrices, left, top, panel.getWidth(), panel.getHeight(), panelColor);
};
}
@@ -108,7 +105,7 @@ public interface BackgroundPainter {
int shadowColor = ScreenDrawing.multiplyColor(panelColor, 1.0f - contrast);
int hilightColor = ScreenDrawing.multiplyColor(panelColor, 1.0f + contrast);
- ScreenDrawing.drawGuiPanel(matrices, left-8, top-8, panel.getWidth()+16, panel.getHeight()+16, shadowColor, panelColor, hilightColor, 0xFF000000);
+ ScreenDrawing.drawGuiPanel(matrices, left, top, panel.getWidth(), panel.getHeight(), shadowColor, panelColor, hilightColor, 0xFF000000);
};
}
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/modmenu/ConfigGui.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/ConfigGui.java
index 625b67c..8c237a8 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/ConfigGui.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/ConfigGui.java
@@ -5,17 +5,19 @@ import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.TranslatableText;
import io.github.cottonmc.cotton.gui.client.BackgroundPainter;
-import io.github.cottonmc.cotton.gui.impl.client.LibGuiClient;
import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription;
+import io.github.cottonmc.cotton.gui.impl.client.LibGuiClient;
import io.github.cottonmc.cotton.gui.widget.WButton;
import io.github.cottonmc.cotton.gui.widget.WGridPanel;
import io.github.cottonmc.cotton.gui.widget.WTextField;
import io.github.cottonmc.cotton.gui.widget.WToggleButton;
+import io.github.cottonmc.cotton.gui.widget.data.Insets;
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..0fd8bd4 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,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 in a grid.
*/
public class WGridPanel extends WPanel {
+ private Insets insets = Insets.NONE;
+
/**
* The grid size in pixels.
* Defaults to 18, which is the size of one item slot.
@@ -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,36 @@ 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.
+ *
+ * <p>The insets should be set <i>before</i> adding any widgets
+ * to 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 b472d45..64b2369 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
@@ -6,6 +6,7 @@ import net.minecraft.client.util.math.MatrixStack;
import io.github.cottonmc.cotton.gui.GuiDescription;
import io.github.cottonmc.cotton.gui.client.BackgroundPainter;
+import io.github.cottonmc.cotton.gui.widget.data.Insets;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
@@ -87,8 +88,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..0afd16f 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,37 @@ 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.
+ *
+ * <p>The insets should be set <i>before</i> adding any widgets
+ * to 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 +
+ '}';
+ }
+}