diff options
author | Juuz <6596629+Juuxel@users.noreply.github.com> | 2021-05-27 18:03:49 +0300 |
---|---|---|
committer | Juuz <6596629+Juuxel@users.noreply.github.com> | 2021-05-27 18:03:49 +0300 |
commit | d52ec93090f68c80d49735d911f79e0de01990ee (patch) | |
tree | 6a7075614b55f87a99eab466331fd79abb3a2a20 /src/main | |
parent | 494e8a6d34bb47e2e6f5c4301d0dcb8863e45f23 (diff) | |
download | LibGui-d52ec93090f68c80d49735d911f79e0de01990ee.tar.gz LibGui-d52ec93090f68c80d49735d911f79e0de01990ee.tar.bz2 LibGui-d52ec93090f68c80d49735d911f79e0de01990ee.zip |
Add title position property to GuiDescriptions
The new field is used for both fullscreen and normal screens.
Also fixes titles being incorrectly positioned by default.
Diffstat (limited to 'src/main')
7 files changed, 62 insertions, 12 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/GuiDescription.java b/src/main/java/io/github/cottonmc/cotton/gui/GuiDescription.java index b192808..71fc6a5 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/GuiDescription.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/GuiDescription.java @@ -5,6 +5,7 @@ import net.fabricmc.api.Environment; import net.minecraft.screen.PropertyDelegate; import io.github.cottonmc.cotton.gui.impl.FocusHandler; +import io.github.cottonmc.cotton.gui.math.Vec2i; 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; @@ -137,4 +138,19 @@ public interface GuiDescription { * @since 2.1.0 */ void setTitleAlignment(HorizontalAlignment alignment); + + /** + * Gets the position of the screen title. + * + * @since 4.0.0 + */ + Vec2i getTitlePos(); + + /** + * Sets the position of the screen title. + * + * @param titlePos the new title position + * @since 4.0.0 + */ + void setTitlePos(Vec2i titlePos); } 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 b0bf93d..10adbce 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/SyncedGuiDescription.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/SyncedGuiDescription.java @@ -27,6 +27,7 @@ import net.minecraft.world.World; import io.github.cottonmc.cotton.gui.client.BackgroundPainter; import io.github.cottonmc.cotton.gui.client.LibGui; +import io.github.cottonmc.cotton.gui.math.Vec2i; import io.github.cottonmc.cotton.gui.networking.NetworkSide; import io.github.cottonmc.cotton.gui.widget.WGridPanel; import io.github.cottonmc.cotton.gui.widget.WLabel; @@ -58,6 +59,7 @@ public class SyncedGuiDescription extends ScreenHandler implements GuiDescriptio protected HorizontalAlignment titleAlignment = HorizontalAlignment.LEFT; protected WWidget focus; + private Vec2i titlePos = new Vec2i(8, 6); public SyncedGuiDescription(ScreenHandlerType<?> type, int syncId, PlayerInventory playerInventory) { super(type, syncId); @@ -519,6 +521,16 @@ public class SyncedGuiDescription extends ScreenHandler implements GuiDescriptio this.titleAlignment = titleAlignment; } + @Override + public Vec2i getTitlePos() { + return titlePos; + } + + @Override + public void setTitlePos(Vec2i titlePos) { + this.titlePos = titlePos; + } + /** * Gets the network side this GUI description runs on. * diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java index 2acc371..df5108c 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java @@ -88,19 +88,16 @@ public class CottonClientScreen extends Screen implements CottonScreenImpl { if (description!=null) { WPanel root = description.getRootPanel(); if (root!=null) { + titleX = description.getTitlePos().x(); + titleY = description.getTitlePos().y(); + if (!description.isFullscreen()) { this.left = (screenWidth - root.getWidth()) / 2; this.top = (screenHeight - root.getHeight()) / 2; - this.titleX = 0; - this.titleY = 0; } else { this.left = 0; this.top = 0; - // Offset the title coordinates a little from the edge - this.titleX = 10; - this.titleY = 10; - root.setSize(screenWidth, screenHeight); } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java index 68f28fe..9c344cd 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java @@ -118,19 +118,16 @@ public class CottonInventoryScreen<T extends SyncedGuiDescription> extends Handl if (backgroundHeight<16) backgroundHeight=300; } + titleX = description.getTitlePos().x(); + titleY = description.getTitlePos().y(); + if (!description.isFullscreen()) { x = (screenWidth / 2) - (backgroundWidth / 2); y = (screenHeight / 2) - (backgroundHeight / 2); - titleX = 0; - titleY = 0; } else { x = 0; y = 0; - // Offset the title coordinates a little from the edge - titleX = 10; - titleY = 10; - if (basePanel != null) { basePanel.setSize(screenWidth, screenHeight); } 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 55b62a4..fd0d8dd 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 @@ -4,6 +4,7 @@ import net.minecraft.screen.PropertyDelegate; import io.github.cottonmc.cotton.gui.GuiDescription; import io.github.cottonmc.cotton.gui.ValidatedSlot; +import io.github.cottonmc.cotton.gui.math.Vec2i; import io.github.cottonmc.cotton.gui.widget.WGridPanel; import io.github.cottonmc.cotton.gui.widget.WLabel; import io.github.cottonmc.cotton.gui.widget.WPanel; @@ -25,6 +26,7 @@ public class LightweightGuiDescription implements GuiDescription { protected boolean fullscreen = false; protected boolean titleVisible = true; protected HorizontalAlignment titleAlignment = HorizontalAlignment.LEFT; + private Vec2i titlePos = new Vec2i(8, 6); @Override public WPanel getRootPanel() { @@ -137,4 +139,14 @@ public class LightweightGuiDescription implements GuiDescription { public void setTitleAlignment(HorizontalAlignment titleAlignment) { this.titleAlignment = titleAlignment; } + + @Override + public Vec2i getTitlePos() { + return titlePos; + } + + @Override + public void setTitlePos(Vec2i titlePos) { + this.titlePos = titlePos; + } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/math/Vec2i.java b/src/main/java/io/github/cottonmc/cotton/gui/math/Vec2i.java new file mode 100644 index 0000000..4515c09 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/math/Vec2i.java @@ -0,0 +1,10 @@ +package io.github.cottonmc.cotton.gui.math; + +/** + * An immutable, two-dimensional int vector. + * This record can be used to represent positions on the screen. + * + * @since 4.0.0 + */ +public record Vec2i(int x, int y) { +} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/math/package-info.java b/src/main/java/io/github/cottonmc/cotton/gui/math/package-info.java new file mode 100644 index 0000000..2ab2ffb --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/math/package-info.java @@ -0,0 +1,6 @@ +/** + * LibGui-specific math data types. + * + * @since 4.0.0 + */ +package io.github.cottonmc.cotton.gui.math; |