aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--GuiTest/src/main/java/io/github/cottonmc/test/TestDescription.java4
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java45
2 files changed, 40 insertions, 9 deletions
diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/TestDescription.java b/GuiTest/src/main/java/io/github/cottonmc/test/TestDescription.java
index 6c74e28..39b09d6 100644
--- a/GuiTest/src/main/java/io/github/cottonmc/test/TestDescription.java
+++ b/GuiTest/src/main/java/io/github/cottonmc/test/TestDescription.java
@@ -15,6 +15,7 @@ import io.github.cottonmc.cotton.gui.widget.WGridPanel;
import io.github.cottonmc.cotton.gui.widget.WItemSlot;
import io.github.cottonmc.cotton.gui.widget.WLabel;
import io.github.cottonmc.cotton.gui.widget.WTextField;
+import io.github.cottonmc.cotton.gui.widget.data.Texture;
import io.github.cottonmc.cotton.gui.widget.icon.TextureIcon;
public class TestDescription extends SyncedGuiDescription {
@@ -42,7 +43,8 @@ public class TestDescription extends SyncedGuiDescription {
buttonB.setOnClick(() -> slot.setIcon(new TextureIcon(new Identifier("libgui-test", "saddle.png"))));
root.add(buttonB, 5, 3, 4, 1);
- root.add(new WButton(Text.literal("Button C")), 0, 5, 4, 1);
+ TextureIcon testIcon = new TextureIcon(new Texture(new Identifier("libgui-test", "icon.png")));
+ root.add(new WButton(testIcon, Text.literal("Button C")), 0, 5, 4, 1);
root.add(new WButton(Text.literal("Button D")), 5, 5, 4, 1);
root.add(new WTextField(Text.literal("Type something...")).setMaxLength(64), 0, 7, 5, 1);
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 533e436..78874b6 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
@@ -22,10 +22,17 @@ import org.jetbrains.annotations.Nullable;
public class WButton extends WWidget {
private static final Identifier DARK_WIDGETS_LOCATION = new Identifier("libgui", "textures/widget/dark_widgets.png");
+ private static final int BUTTON_HEIGHT = 20;
+ private static final int ICON_SPACING = 2;
- private Text label;
+ @Nullable private Text label;
protected int color = WLabel.DEFAULT_TEXT_COLOR;
protected int darkmodeColor = WLabel.DEFAULT_TEXT_COLOR;
+ /**
+ * The size (width/height) of this button's icon in pixels.
+ * @since 6.4.0
+ */
+ protected int iconSize = 16;
private boolean enabled = true;
protected HorizontalAlignment alignment = HorizontalAlignment.CENTER;
@@ -45,7 +52,7 @@ public class WButton extends WWidget {
* @param icon the icon
* @since 2.2.0
*/
- public WButton(Icon icon) {
+ public WButton(@Nullable Icon icon) {
this.icon = icon;
}
@@ -54,7 +61,7 @@ public class WButton extends WWidget {
*
* @param label the label
*/
- public WButton(Text label) {
+ public WButton(@Nullable Text label) {
this.label = label;
}
@@ -65,7 +72,7 @@ public class WButton extends WWidget {
* @param label the label
* @since 2.2.0
*/
- public WButton(Icon icon, Text label) {
+ public WButton(@Nullable Icon icon, @Nullable Text label) {
this.icon = icon;
this.label = label;
}
@@ -106,7 +113,7 @@ public class WButton extends WWidget {
ScreenDrawing.texturedRect(matrices, x+(getWidth()/2), y, getWidth()/2, 20, texture, buttonEndLeft, buttonTop, 200*px, buttonTop+buttonHeight, 0xFFFFFFFF);
if (icon != null) {
- icon.paint(matrices, x + 1, y + 1, 16);
+ icon.paint(matrices, x+ICON_SPACING, y+(BUTTON_HEIGHT-iconSize)/2, iconSize);
}
if (label!=null) {
@@ -117,14 +124,14 @@ public class WButton extends WWidget {
color = 0xFFFFA0;
}*/
- int xOffset = (icon != null && alignment == HorizontalAlignment.LEFT) ? 18 : 0;
+ int xOffset = (icon != null && alignment == HorizontalAlignment.LEFT) ? ICON_SPACING+iconSize+ICON_SPACING : 0;
ScreenDrawing.drawStringWithShadow(matrices, label.asOrderedText(), alignment, x + xOffset, y + ((20 - 8) / 2), width, color); //LibGuiClient.config.darkMode ? darkmodeColor : color);
}
}
@Override
public void setSize(int x, int y) {
- super.setSize(x, 20);
+ super.setSize(x, BUTTON_HEIGHT);
}
@Environment(EnvType.CLIENT)
@@ -181,7 +188,7 @@ public class WButton extends WWidget {
return this;
}
- public Text getLabel() {
+ public @Nullable Text getLabel() {
return label;
}
@@ -200,6 +207,28 @@ public class WButton extends WWidget {
}
/**
+ * Gets the current height / width of the icon.
+ *
+ * @return the current height / width of the icon
+ * @since 6.4.0
+ */
+ public int getIconSize() {
+ return iconSize;
+ }
+
+ /**
+ * Sets the new size of the icon.
+ *
+ * @param iconSize the new height and width of the icon
+ * @return this button
+ * @since 6.4.0
+ */
+ public WButton setIconSize(int iconSize) {
+ this.iconSize = iconSize;
+ return this;
+ }
+
+ /**
* Gets the icon of this button.
*
* @return the icon