From d05fefa628604ab87181e7502b3674f8627b56f6 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Tue, 30 Jun 2020 19:57:15 +0300 Subject: Add more methods for working with screen titles --- .../github/cottonmc/cotton/gui/GuiDescription.java | 41 +++++++++++++++++++++- .../cottonmc/cotton/gui/SyncedGuiDescription.java | 23 +++++++++++- .../cotton/gui/client/CottonClientScreen.java | 3 +- .../cotton/gui/client/CottonInventoryScreen.java | 3 +- .../gui/client/LightweightGuiDescription.java | 25 +++++++++++-- 5 files changed, 89 insertions(+), 6 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 8c0f300..cf1226b 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 javax.annotation.Nullable; import io.github.cottonmc.cotton.gui.impl.FocusHandler; 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 net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.screen.PropertyDelegate; @@ -23,8 +24,30 @@ public interface GuiDescription { public int getTitleColor(); public GuiDescription setRootPanel(WPanel panel); + + /** + * Sets the title color of this GUI. + * + *
The dark-mode title color will also be set by this method.
+ * If the specified color is {@link io.github.cottonmc.cotton.gui.widget.WLabel#DEFAULT_TEXT_COLOR},
+ * the dark-mode color will be {@link io.github.cottonmc.cotton.gui.widget.WLabel#DEFAULT_DARKMODE_TEXT_COLOR};
+ * otherwise it will be the specified color.
+ *
+ * @param color the new title color
+ * @return this GUI
+ */
public GuiDescription setTitleColor(int color);
-
+
+ /**
+ * Sets the light and dark title colors of this GUI.
+ *
+ * @param lightColor the light-mode color
+ * @param darkColor the dark-mode color
+ * @return this GUI
+ * @since 2.1.0
+ */
+ GuiDescription setTitleColor(int lightColor, int darkColor);
+
/** Sets the object which manages the integer properties used by WBars */
public GuiDescription setPropertyDelegate(PropertyDelegate delegate);
@@ -98,4 +121,20 @@ public interface GuiDescription {
* @since 2.0.0
*/
void setTitleVisible(boolean titleVisible);
+
+ /**
+ * Gets the horizontal alignment of the GUI title.
+ *
+ * @return the alignment
+ * @since 2.1.0
+ */
+ HorizontalAlignment getTitleAlignment();
+
+ /**
+ * Sets the horizontal alignment of the GUI title.
+ *
+ * @param alignment the new alignment
+ * @since 2.1.0
+ */
+ void setTitleAlignment(HorizontalAlignment alignment);
}
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 f4ab13e..cd47af4 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/SyncedGuiDescription.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/SyncedGuiDescription.java
@@ -8,6 +8,7 @@ import javax.annotation.Nullable;
import io.github.cottonmc.cotton.gui.client.BackgroundPainter;
import io.github.cottonmc.cotton.gui.client.LibGuiClient;
import io.github.cottonmc.cotton.gui.widget.*;
+import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.Block;
@@ -39,6 +40,7 @@ public class SyncedGuiDescription extends ScreenHandler implements GuiDescriptio
protected int darkTitleColor = WLabel.DEFAULT_DARKMODE_TEXT_COLOR;
protected boolean fullscreen = false;
protected boolean titleVisible = true;
+ protected HorizontalAlignment titleAlignment = HorizontalAlignment.LEFT;
protected WWidget focus;
@@ -71,9 +73,18 @@ public class SyncedGuiDescription extends ScreenHandler implements GuiDescriptio
this.rootPanel = panel;
return this;
}
-
+
+ @Override
public SyncedGuiDescription setTitleColor(int color) {
this.titleColor = color;
+ this.darkTitleColor = (color == WLabel.DEFAULT_TEXT_COLOR) ? WLabel.DEFAULT_DARKMODE_TEXT_COLOR : color;
+ return this;
+ }
+
+ @Override
+ public SyncedGuiDescription setTitleColor(int lightColor, int darkColor) {
+ this.titleColor = lightColor;
+ this.darkTitleColor = darkColor;
return this;
}
@@ -529,4 +540,14 @@ public class SyncedGuiDescription extends ScreenHandler implements GuiDescriptio
public void setTitleVisible(boolean titleVisible) {
this.titleVisible = titleVisible;
}
+
+ @Override
+ public HorizontalAlignment getTitleAlignment() {
+ return titleAlignment;
+ }
+
+ @Override
+ public void setTitleAlignment(HorizontalAlignment titleAlignment) {
+ this.titleAlignment = titleAlignment;
+ }
}
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 db1e0d2..95702a6 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
@@ -99,7 +99,8 @@ public class CottonClientScreen extends Screen implements TextHoverRendererScree
}
if (getTitle() != null && description.isTitleVisible()) {
- textRenderer.draw(matrices, getTitle(), left + titleX, top + titleY, description.getTitleColor());
+ int width = description.getRootPanel().getWidth();
+ ScreenDrawing.drawString(matrices, getTitle(), description.getTitleAlignment(), left + titleX, top + titleY, width, description.getTitleColor());
}
}
}
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 abc2d17..4287565 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
@@ -266,7 +266,8 @@ public class CottonInventoryScreen