diff options
author | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2020-05-20 15:41:29 +0300 |
---|---|---|
committer | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2020-05-20 15:41:29 +0300 |
commit | ae87b1554ab83d4e294afc614aca910d59fe9de7 (patch) | |
tree | a4c81fa6c9065c072e46ece1792222efae9c6b89 | |
parent | e8b0829afbd32830dc1dc9b45f6fbd82d0fde2fa (diff) | |
parent | 0c90421761cd68f1aef6c1972644cb560509e095 (diff) | |
download | LibGui-ae87b1554ab83d4e294afc614aca910d59fe9de7.tar.gz LibGui-ae87b1554ab83d4e294afc614aca910d59fe9de7.tar.bz2 LibGui-ae87b1554ab83d4e294afc614aca910d59fe9de7.zip |
Merge branch 'unstable/2.0' into scroll-panel
# Conflicts:
# src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java
# src/main/java/io/github/cottonmc/cotton/gui/GuiDescription.java
# src/main/java/io/github/cottonmc/cotton/gui/style/StyleEntry.java
# src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java
# src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java
# src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java
# src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java
20 files changed, 327 insertions, 143 deletions
diff --git a/gradle.properties b/gradle.properties index b1db372..e4903dc 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,15 +3,15 @@ org.gradle.jvmargs=-Xmx1G # Fabric Properties # check these on https://fabricmc.net/use - minecraft_version=20w17a - yarn_mappings=20w17a+build.4 - loader_version=0.8.2+build.194 + minecraft_version=20w20b + yarn_mappings=20w20b+build.7 + loader_version=0.8.3+build.196 # Mod Properties - mod_version = 1.9.0 + mod_version = 2.0.0 maven_group = io.github.cottonmc archives_base_name = LibGui # Dependencies - fabric_version=0.6.2+build.327-1.16 - jankson_version=2.0.1+j1.2.0 + fabric_version=0.10.9+build.346-1.16 + jankson_version=2.1.0+j1.2.0 diff --git a/src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java b/src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java index 3a93706..272d251 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java @@ -1,5 +1,12 @@ package io.github.cottonmc.cotton.gui; +import java.util.ArrayList; + +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 net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.block.Block; @@ -10,25 +17,14 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.inventory.Inventory; import net.minecraft.item.ItemStack; -import net.minecraft.screen.ArrayPropertyDelegate; -import net.minecraft.screen.PropertyDelegate; -import net.minecraft.screen.ScreenHandler; -import net.minecraft.screen.ScreenHandlerContext; +import net.minecraft.screen.*; import net.minecraft.screen.slot.Slot; import net.minecraft.screen.slot.SlotActionType; import net.minecraft.world.World; -import io.github.cottonmc.cotton.gui.client.BackgroundPainter; -import io.github.cottonmc.cotton.gui.client.LibGuiClient; -import io.github.cottonmc.cotton.gui.widget.WGridPanel; -import io.github.cottonmc.cotton.gui.widget.WLabel; -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 javax.annotation.Nullable; -import java.util.ArrayList; - +/** + * A screen handler-based GUI description for GUIs with slots. + */ public class CottonInventoryController extends ScreenHandler implements GuiDescription { protected Inventory blockInventory; @@ -329,11 +325,31 @@ public class CottonInventoryController extends ScreenHandler implements GuiDescr this.propertyDelegate = delegate; return this; } - + + /** + * Creates a player inventory widget from this panel's {@linkplain #playerInventory player inventory}. + * + * @return the created inventory widget + */ public WPlayerInvPanel createPlayerInventoryPanel() { return new WPlayerInvPanel(this.playerInventory); } - + + /** + * Gets the block inventory at the context. + * + * <p>If no inventory is found, returns {@link EmptyInventory#INSTANCE}. + * + * <p>Searches for these implementations in the following order: + * <ol> + * <li>Blocks implementing {@code InventoryProvider}</li> + * <li>Block entities implementing {@code InventoryProvider}</li> + * <li>Block entities implementing {@code Inventory}</li> + * </ol> + * + * @param ctx the context + * @return the found inventory + */ public static Inventory getBlockInventory(ScreenHandlerContext ctx) { return ctx.run((world, pos) -> { BlockState state = world.getBlockState(pos); @@ -361,7 +377,17 @@ public class CottonInventoryController extends ScreenHandler implements GuiDescr return EmptyInventory.INSTANCE; }).orElse(EmptyInventory.INSTANCE); } - + + /** + * Gets the property delegate at the context. + * + * <p>If no property delegate is found, returns an empty property delegate with no properties. + * + * <p>Searches for blocks and block entities implementing {@link PropertyDelegateHolder}. + * + * @param ctx the context + * @return the found property delegate + */ public static PropertyDelegate getBlockPropertyDelegate(ScreenHandlerContext ctx) { return ctx.run((world, pos) -> { BlockState state = world.getBlockState(pos); diff --git a/src/main/java/io/github/cottonmc/cotton/gui/EmptyInventory.java b/src/main/java/io/github/cottonmc/cotton/gui/EmptyInventory.java index 84eee0f..7376c92 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/EmptyInventory.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/EmptyInventory.java @@ -4,6 +4,9 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.inventory.Inventory; import net.minecraft.item.ItemStack; +/** + * An empty inventory that cannot hold any items. + */ public class EmptyInventory implements Inventory { public static final EmptyInventory INSTANCE = new EmptyInventory(); 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 febaaa7..fec799b 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/GuiDescription.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/GuiDescription.java @@ -1,14 +1,22 @@ package io.github.cottonmc.cotton.gui; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; -import net.minecraft.screen.PropertyDelegate; +import javax.annotation.Nullable; import io.github.cottonmc.cotton.gui.widget.WPanel; import io.github.cottonmc.cotton.gui.widget.WWidget; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.screen.PropertyDelegate; -import javax.annotation.Nullable; - +/** + * A GUI description represents a GUI without depending on screens. + * + * <p>GUI descriptions contain the root panel and the property delegate of the GUI. + * They also manage the focused widget. + * + * @see io.github.cottonmc.cotton.gui.client.LightweightGuiDescription + * @see CottonInventoryController + */ public interface GuiDescription { public WPanel getRootPanel(); public int getTitleColor(); @@ -37,7 +45,7 @@ public interface GuiDescription { @Nullable public WWidget getFocus(); - /** Notifies this gui that the widget waants to acquire focus. */ + /** Notifies this gui that the widget wants to acquire focus. */ public void requestFocus(WWidget widget); /** Notifies this gui that the widget wants to give up its hold over focus. */ diff --git a/src/main/java/io/github/cottonmc/cotton/gui/ValidatedSlot.java b/src/main/java/io/github/cottonmc/cotton/gui/ValidatedSlot.java index cce4cfe..1e9ee3c 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/ValidatedSlot.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/ValidatedSlot.java @@ -4,58 +4,88 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.inventory.Inventory; import net.minecraft.item.ItemStack; import net.minecraft.screen.slot.Slot; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; public class ValidatedSlot extends Slot { + private static final Logger LOGGER = LogManager.getLogger(); private final int slotNumber; - private boolean modifiable = true; - - public ValidatedSlot(Inventory inventoryIn, int index, int xPosition, int yPosition) { - super(inventoryIn, index, xPosition, yPosition); - if (inventoryIn==null) throw new IllegalArgumentException("Can't make an itemslot from a null inventory!"); + private boolean insertingAllowed = true; + private boolean takingAllowed = true; + + public ValidatedSlot(Inventory inventory, int index, int x, int y) { + super(inventory, index, x, y); + if (inventory==null) throw new IllegalArgumentException("Can't make an itemslot from a null inventory!"); this.slotNumber = index; } @Override public boolean canInsert(ItemStack stack) { - return modifiable && inventory.isValid(slotNumber, stack); + return insertingAllowed && inventory.isValid(slotNumber, stack); } @Override public boolean canTakeItems(PlayerEntity player) { - return modifiable && inventory.canPlayerUse(player); + return takingAllowed && inventory.canPlayerUse(player); } @Override public ItemStack getStack() { if (inventory==null) { - System.out.println("Prevented null-inventory from WItemSlot with slot #: "+slotNumber); + LOGGER.warn("Prevented null-inventory from WItemSlot with slot #: {}", slotNumber); return ItemStack.EMPTY; } ItemStack result = super.getStack(); if (result==null) { - System.out.println("Prevented null-itemstack crash from: "+inventory.getClass().getCanonicalName()); + LOGGER.warn("Prevented null-itemstack crash from: {}", inventory.getClass().getCanonicalName()); return ItemStack.EMPTY; } return result; } + public int getInventoryIndex() { + return slotNumber; + } + /** - * Returns true if the item in this slot can be modified by players. + * Returns whether items can be inserted into this slot. * - * @return true if this slot is modifiable - * @since 1.8.0 + * @return true if items can be inserted, false otherwise + * @since 1.10.0 */ - public boolean isModifiable() { - return modifiable; + public boolean isInsertingAllowed() { + return insertingAllowed; } - public void setModifiable(boolean modifiable) { - this.modifiable = modifiable; + /** + * Sets whether inserting items into this slot is allowed. + * + * @param insertingAllowed true if items can be inserted, false otherwise + * @since 1.10.0 + */ + public void setInsertingAllowed(boolean insertingAllowed) { + this.insertingAllowed = insertingAllowed; } - public int getInventoryIndex() { - return slotNumber; + /** + * Returns whether items can be taken from this slot. + * + * @return true if items can be taken, false otherwise + * @since 1.10.0 + */ + public boolean isTakingAllowed() { + return takingAllowed; + } + + /** + * Sets whether taking items from this slot is allowed. + * + * @param takingAllowed true if items can be taken, false otherwise + * @since 1.10.0 + */ + public void setTakingAllowed(boolean takingAllowed) { + this.takingAllowed = takingAllowed; } }
\ No newline at end of file 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 b2b4943..0ca52a4 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 @@ -9,6 +9,7 @@ import io.github.cottonmc.cotton.gui.widget.WWidget; * Background painters are used to paint the background of a widget. * The background painter instance of a widget can be changed to customize the look of a widget. */ +@FunctionalInterface public interface BackgroundPainter { /** * Paint the specified panel to the screen. @@ -18,30 +19,20 @@ public interface BackgroundPainter { */ public void paintBackground(int left, int top, WWidget panel); - /** - * The {@code VANILLA} background painter draws a vanilla-like gui panel using {@link ScreenDrawing#drawGuiPanel(int, int, int, int)}. - * - * <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. - */ - public static BackgroundPainter VANILLA = (left, top, panel) -> { - ScreenDrawing.drawGuiPanel(left-8, top-8, panel.getWidth()+16, panel.getHeight()+16); - }; - - /** - * The {@code VANILLA_9PATCH} background painter draws a vanilla-like gui panel using {@linkplain NinePatch nine-patch textures}. + * The {@code VANILLA} background painter draws a vanilla-like gui panel using {@linkplain NinePatch nine-patch textures}. * * <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_9PATCH = createLightDarkVariants( + 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) ); 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 13824ad..b5ac34e 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 @@ -67,7 +67,7 @@ public class CottonClientScreen extends Screen implements TextHoverRendererScree } if (getTitle() != null) { - textRenderer.method_27528(matrices, getTitle(), left, top, description.getTitleColor()); + textRenderer.draw(matrices, getTitle(), left, top, 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 9cfe88d..ea24abf 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 @@ -198,7 +198,7 @@ public class CottonInventoryScreen<T extends CottonInventoryController> extends } if (getTitle() != null) { - textRenderer.method_27528(matrices, getTitle(), x, y, description.getTitleColor()); + textRenderer.draw(matrices, getTitle(), x, y, description.getTitleColor()); } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java b/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java index 4d892ce..4868f92 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java @@ -298,13 +298,13 @@ public class ScreenDrawing { } break; case CENTER: { - int wid = MinecraftClient.getInstance().textRenderer.getStringWidth(s); + int wid = MinecraftClient.getInstance().textRenderer.getWidth(s); int l = (width/2) - (wid/2); MinecraftClient.getInstance().textRenderer.draw(matrices, s, x+l, y, color); } break; case RIGHT: { - int wid = MinecraftClient.getInstance().textRenderer.getStringWidth(s); + int wid = MinecraftClient.getInstance().textRenderer.getWidth(s); int l = width - wid; MinecraftClient.getInstance().textRenderer.draw(matrices, s, x+l, y, color); } @@ -327,19 +327,19 @@ public class ScreenDrawing { public static void drawString(MatrixStack matrices, Text text, Alignment align, int x, int y, int width, int color) { switch(align) { case LEFT: { - MinecraftClient.getInstance().textRenderer.method_27528(matrices, text, x, y, color); + MinecraftClient.getInstance().textRenderer.draw(matrices, text, x, y, color); } break; case CENTER: { - int wid = MinecraftClient.getInstance().textRenderer.method_27525(text); + int wid = MinecraftClient.getInstance().textRenderer.getWidth(text); int l = (width/2) - (wid/2); - MinecraftClient.getInstance().textRenderer.method_27528(matrices, text, x+l, y, color); + MinecraftClient.getInstance().textRenderer.draw(matrices, text, x+l, y, color); } break; case RIGHT: { - int wid = MinecraftClient.getInstance().textRenderer.method_27525(text); + int wid = MinecraftClient.getInstance().textRenderer.getWidth(text); int l = width - wid; - MinecraftClient.getInstance().textRenderer.method_27528(matrices, text, x+l, y, color); + MinecraftClient.getInstance().textRenderer.draw(matrices, text, x+l, y, color); } break; } @@ -363,13 +363,13 @@ public class ScreenDrawing { } break; case CENTER: { - int wid = MinecraftClient.getInstance().textRenderer.getStringWidth(s); + int wid = MinecraftClient.getInstance().textRenderer.getWidth(s); int l = (width/2) - (wid/2); MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, s, x+l, y, color); } break; case RIGHT: { - int wid = MinecraftClient.getInstance().textRenderer.getStringWidth(s); + int wid = MinecraftClient.getInstance().textRenderer.getWidth(s); int l = width - wid; MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, s, x+l, y, color); } @@ -391,19 +391,19 @@ public class ScreenDrawing { public static void drawStringWithShadow(MatrixStack matrices, Text text, Alignment align, int x, int y, int width, int color) { switch(align) { case LEFT: { - MinecraftClient.getInstance().textRenderer.method_27528(matrices, text, x, y, color); + MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, text, x, y, color); } break; case CENTER: { - int wid = MinecraftClient.getInstance().textRenderer.method_27525(text); + int wid = MinecraftClient.getInstance().textRenderer.getWidth(text); int l = (width/2) - (wid/2); - MinecraftClient.getInstance().textRenderer.method_27528(matrices, text, x+l, y, color); + MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, text, x+l, y, color); } break; case RIGHT: { - int wid = MinecraftClient.getInstance().textRenderer.method_27525(text); + int wid = MinecraftClient.getInstance().textRenderer.getWidth(text); int l = width - wid; - MinecraftClient.getInstance().textRenderer.method_27528(matrices, text, x+l, y, color); + MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, text, x+l, y, color); } break; } @@ -432,7 +432,7 @@ public class ScreenDrawing { * @param color the text color */ public static void drawString(MatrixStack matrices, Text text, int x, int y, int color) { - MinecraftClient.getInstance().textRenderer.method_27528(matrices, text, x, y, color); + MinecraftClient.getInstance().textRenderer.draw(matrices, text, x, y, color); } public static int colorAtOpacity(int opaque, float opacity) { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/style/StyleEntry.java b/src/main/java/io/github/cottonmc/cotton/gui/style/StyleEntry.java deleted file mode 100644 index 2376a07..0000000 --- a/src/main/java/io/github/cottonmc/cotton/gui/style/StyleEntry.java +++ /dev/null @@ -1,17 +0,0 @@ -package io.github.cottonmc.cotton.gui.style; - -import io.github.cottonmc.cotton.gui.widget.data.Color; - -import java.util.HashMap; - -public class StyleEntry { - private String selector = "*"; - private HashMap<String, String> customEntries = new HashMap<>(); - - private Color foreground; - private Color background; - - public Color getForeground() { - return (foreground!=null) ? foreground : Color.WHITE; - } -} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java index 3367b1d..17b88c1 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java @@ -348,6 +348,16 @@ public abstract class WAbstractSlider extends WWidget { : (ch == GLFW.GLFW_KEY_RIGHT || ch == GLFW.GLFW_KEY_UP); } + /** + * The direction enum represents all four directions a slider can face. + * + * <p>For example, a slider whose value grows towards the right faces right. + * + * <p>The default direction for vertical sliders is {@link #UP} and + * the one for horizontal sliders is {@link #RIGHT}. + * + * @since 2.0.0 + */ public enum Direction { UP(Axis.VERTICAL, false), DOWN(Axis.VERTICAL, true), @@ -362,10 +372,22 @@ public abstract class WAbstractSlider extends WWidget { this.inverted = inverted; } + /** + * Gets the direction's axis. + * + * @return the axis + */ public Axis getAxis() { return axis; } + /** + * Returns whether this slider is inverted. + * + * <p>An inverted slider will have reversed keyboard control. + * + * @return whether this slider is inverted + */ public boolean isInverted() { return inverted; } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WItem.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WItem.java index a7f3921..ffba2c3 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WItem.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WItem.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList; import com.mojang.blaze3d.systems.RenderSystem; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import net.fabricmc.loader.api.FabricLoader; import net.minecraft.client.MinecraftClient; import net.minecraft.client.render.item.ItemRenderer; import net.minecraft.client.util.math.MatrixStack; @@ -61,7 +62,7 @@ public class WItem extends WWidget { MinecraftClient mc = MinecraftClient.getInstance(); ItemRenderer renderer = mc.getItemRenderer(); renderer.zOffset = 100f; - renderer.renderGuiItem(mc.player, items.get(current), x + getWidth() / 2 - 9, y + getHeight() / 2 - 9); + renderer.method_27951(mc.player, items.get(current), x + getWidth() / 2 - 9, y + getHeight() / 2 - 9); renderer.zOffset = 0f; } @@ -109,7 +110,7 @@ public class WItem extends WWidget { ImmutableList.Builder<ItemStack> builder = ImmutableList.builder(); for (ItemConvertible item : tag.values()) { - builder.add(item.asItem().getStackForRender()); + builder.add(new ItemStack(item)); } return builder.build(); diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java index 9006457..012cf16 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java @@ -1,29 +1,29 @@ package io.github.cottonmc.cotton.gui.widget; -import com.google.common.collect.Lists; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; -import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.inventory.Inventory; +import java.util.ArrayList; +import java.util.List; import io.github.cottonmc.cotton.gui.GuiDescription; import io.github.cottonmc.cotton.gui.ValidatedSlot; import io.github.cottonmc.cotton.gui.client.BackgroundPainter; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; - -import java.util.List; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.inventory.Inventory; public class WItemSlot extends WWidget { - private final List<ValidatedSlot> peers = Lists.newArrayList(); + private final List<ValidatedSlot> peers = new ArrayList<>(); private BackgroundPainter backgroundPainter; private Inventory inventory; private int startIndex = 0; private int slotsWide = 1; private int slotsHigh = 1; private boolean big = false; - private boolean modifiable = true; - - public WItemSlot(Inventory inventory, int startIndex, int slotsWide, int slotsHigh, boolean big, boolean ltr) { + private boolean insertingAllowed = true; + private boolean takingAllowed = true; + + public WItemSlot(Inventory inventory, int startIndex, int slotsWide, int slotsHigh, boolean big) { this.inventory = inventory; this.startIndex = startIndex; this.slotsWide = slotsWide; @@ -60,7 +60,14 @@ public class WItemSlot extends WWidget { return w; } - + + /** + * Creates a 9x3 slot widget from the "main" part of a player inventory. + * + * @param inventory the player inventory + * @return the created slot widget + * @see WPlayerInvPanel + */ public static WItemSlot ofPlayerStorage(Inventory inventory) { WItemSlot w = new WItemSlot(); w.inventory = inventory; @@ -89,17 +96,69 @@ public class WItemSlot extends WWidget { /** * Returns true if the contents of this {@code WItemSlot} can be modified by players. * - * @return true if this slot is modifiable + * @return true if items can be inserted into or taken from this slot widget, false otherwise * @since 1.8.0 */ public boolean isModifiable() { - return modifiable; + return takingAllowed || insertingAllowed; } public WItemSlot setModifiable(boolean modifiable) { - this.modifiable = modifiable; + this.insertingAllowed = modifiable; + this.takingAllowed = modifiable; for (ValidatedSlot peer : peers) { - peer.setModifiable(modifiable); + peer.setInsertingAllowed(modifiable); + peer.setTakingAllowed(modifiable); + } + return this; + } + + /** + * Returns whether items can be inserted into this slot. + * + * @return true if items can be inserted, false otherwise + * @since 1.10.0 + */ + public boolean isInsertingAllowed() { + return insertingAllowed; + } + + /** + * Sets whether inserting items into this slot is allowed. + * + * @param insertingAllowed true if items can be inserted, false otherwise + * @return this slot widget + * @since 1.10.0 + */ + public WItemSlot setInsertingAllowed(boolean insertingAllowed) { + this.insertingAllowed = insertingAllowed; + for (ValidatedSlot peer : peers) { + peer.setInsertingAllowed(insertingAllowed); + } + return this; + } + + /** + * Returns whether items can be taken from this slot. + * + * @return true if items can be taken, false otherwise + * @since 1.10.0 + */ + public boolean isTakingAllowed() { + return takingAllowed; + } + + /** + * Sets whether taking items from this slot is allowed. + * + * @param takingAllowed true if items can be taken, false otherwise + * @return this slot widget + * @since 1.10.0 + */ + public WItemSlot setTakingAllowed(boolean takingAllowed) { + this.takingAllowed = takingAllowed; + for (ValidatedSlot peer : peers) { + peer.setTakingAllowed(takingAllowed); } return this; } @@ -112,14 +171,29 @@ public class WItemSlot extends WWidget { for (int y = 0; y < slotsHigh; y++) { for (int x = 0; x < slotsWide; x++) { - ValidatedSlot slot = new ValidatedSlot(inventory, index, this.getAbsoluteX() + (x * 18), this.getAbsoluteY() + (y * 18)); - slot.setModifiable(modifiable); + ValidatedSlot slot = createSlotPeer(inventory, index, this.getAbsoluteX() + (x * 18), this.getAbsoluteY() + (y * 18)); + slot.setInsertingAllowed(insertingAllowed); + slot.setTakingAllowed(takingAllowed); peers.add(slot); c.addSlotPeer(slot); index++; } } } + + /** + * Creates a slot peer for this slot widget. + * + * @param inventory the slot inventory + * @param index the index in the inventory + * @param x the X coordinate + * @param y the Y coordinate + * @return the created slot instance + * @since 1.11.0 + */ + protected ValidatedSlot createSlotPeer(Inventory inventory, int index, int x, int y) { + return new ValidatedSlot(inventory, index, x, y); + } @Environment(EnvType.CLIENT) public void setBackgroundPainter(BackgroundPainter painter) { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java index c0fc7f6..663c58a 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java @@ -102,9 +102,9 @@ public class WLabel extends WWidget { @Environment(EnvType.CLIENT) @Nullable - private Text getTextAt(int x, int y) { + public Text getTextAt(int x, int y) { if (isWithinBounds(x, y)) { - return MinecraftClient.getInstance().textRenderer.method_27527().method_27489(text, x); + return MinecraftClient.getInstance().textRenderer.trimToWidth(text, x); } return null; } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java index 786072f..9ba8562 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java @@ -55,6 +55,13 @@ public class WListPanel<D, W extends WWidget> extends WClippedPanel { protected WScrollBar scrollBar = new WScrollBar(Axis.VERTICAL); private int lastScroll = -1; + /** + * Constructs a list panel. + * + * @param data the list data + * @param supplier the widget supplier that creates unconfigured widgets + * @param configurator the widget configurator that configures widgets to display the passed data + */ public WListPanel(List<D> data, Supplier<W> supplier, BiConsumer<D, W> configurator) { this.data = data; this.supplier = supplier; 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 5b99ca9..d1f0f4a 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,17 +1,16 @@ package io.github.cottonmc.cotton.gui.widget; -import com.google.common.collect.Lists; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; -import net.minecraft.client.util.math.MatrixStack; +import java.util.ArrayList; +import java.util.List; import io.github.cottonmc.cotton.gui.GuiDescription; import io.github.cottonmc.cotton.gui.client.BackgroundPainter; - -import java.util.List; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.util.math.MatrixStack; /** - * Panels are widgets tthat contain other widgets. + * Panels are widgets that contain other widgets. */ public abstract class WPanel extends WWidget { /** @@ -19,7 +18,7 @@ public abstract class WPanel extends WWidget { * * <p>The list is mutable. */ - protected final List<WWidget> children = Lists.newArrayList(); + protected final List<WWidget> children = new ArrayList<>(); @Environment(EnvType.CLIENT) private BackgroundPainter backgroundPainter = null; 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 d637cd6..47df88f 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 @@ -1,12 +1,16 @@ package io.github.cottonmc.cotton.gui.widget; -import net.minecraft.entity.player.PlayerInventory; - import io.github.cottonmc.cotton.gui.client.BackgroundPainter; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.entity.player.PlayerInventory; +/** + * A player inventory widget that has a visually separate hotbar. + */ public class WPlayerInvPanel extends WPlainPanel { - private WItemSlot inv; - private WItemSlot hotbar; + private final WItemSlot inv; + private final WItemSlot hotbar; public WPlayerInvPanel(PlayerInventory playerInventory) { inv = WItemSlot.ofPlayerStorage(playerInventory); @@ -15,6 +19,13 @@ public class WPlayerInvPanel extends WPlainPanel { this.add(hotbar, 0, 58); } + /** + * Sets the background painter of this inventory widget's slots. + * + * @param painter the new painter + * @return this panel + */ + @Environment(EnvType.CLIENT) @Override public WPanel setBackgroundPainter(BackgroundPainter painter) { super.setBackgroundPainter(null); diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java index 6172a8c..ea7f721 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java @@ -6,7 +6,6 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.text.Style; import net.minecraft.text.Text; import io.github.cottonmc.cotton.gui.client.LibGuiClient; @@ -55,18 +54,18 @@ public class WText extends WWidget { @Environment(EnvType.CLIENT) private void wrapLines() { TextRenderer font = MinecraftClient.getInstance().textRenderer; - wrappedLines = font.method_27527().method_27491(text, width, Style.field_24360, false); + wrappedLines = font.wrapLines(text, width); } @Environment(EnvType.CLIENT) @Nullable - protected Text getTextAt(int x, int y) { + public Text getTextAt(int x, int y) { TextRenderer font = MinecraftClient.getInstance().textRenderer; int lineIndex = y / font.fontHeight; if (lineIndex >= 0 && lineIndex < wrappedLines.size()) { Text line = wrappedLines.get(lineIndex); - return font.method_27527().method_27489(line, x); + return font.trimToWidth(line, x); } return null; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java index e5d5b13..429fa4f 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java @@ -320,7 +320,7 @@ public class WTextField extends WWidget { int textColor = this.editable ? this.enabledColor : this.uneditableColor; //TODO: Scroll offset - String trimText = font.method_27523(this.text, this.width-OFFSET_X_TEXT); + String trimText = font.trimToWidth(this.text, this.width-OFFSET_X_TEXT); boolean selection = (select!=-1); boolean focused = this.isFocused(); //this.isFocused() && this.focusedTicks / 6 % 2 == 0 && boolean_1; //Blinks the cursor @@ -675,7 +675,7 @@ public class WTextField extends WWidget { TextRenderer font = MinecraftClient.getInstance().textRenderer; int lastAdvance = 0; for(int i=0; i<s.length()-1; i++) { - int advance = font.getStringWidth(s.substring(0,i+1)); + int advance = font.getWidth(s.substring(0,i+1)); int charAdvance = advance-lastAdvance; if (x<advance + (charAdvance/2)) return i+1; @@ -696,7 +696,7 @@ public class WTextField extends WWidget { if (pos==0) return 0;//-1; TextRenderer font = MinecraftClient.getInstance().textRenderer; - int ofs = font.getStringWidth(s.substring(0, pos))+1; + int ofs = font.getWidth(s.substring(0, pos))+1; return ofs; //(font.isRightToLeft()) ? -ofs : ofs; } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java index 8b4c06c..656f24f 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java @@ -1,31 +1,40 @@ package io.github.cottonmc.cotton.gui.widget; +import java.util.ArrayList; +import java.util.List; + +import io.github.cottonmc.cotton.gui.GuiDescription; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.Text; - -import io.github.cottonmc.cotton.gui.GuiDescription; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import javax.annotation.Nullable; -import java.util.ArrayList; -import java.util.List; /** * The base class for all widgets. */ public class WWidget { + private static final Logger LOGGER = LogManager.getLogger(); + /** * The containing panel of this widget. * Can be null if this widget is the root panel or a HUD widget. */ @Nullable protected WPanel parent; + + /** The X coordinate of this widget relative to its parent. */ protected int x = 0; + /** The Y coordinate of this widget relative to its parent. */ protected int y = 0; + /** The width of this widget, defaults to 18 pixels. */ protected int width = 18; + /** The height of this widget, defaults to 18 pixels. */ protected int height = 18; /** @@ -244,23 +253,44 @@ public class WWidget { public void onFocusLost() { } + /** + * Tests whether this widget has focus. + * + * @return true if this widget widget has focus, false otherwise + * @see GuiDescription#isFocused(WWidget) + */ public boolean isFocused() { if (host==null) return false; return host.isFocused(this); } + /** + * If this widget has a host, requests the focus from the host. + * + * @see GuiDescription#requestFocus(WWidget) + */ public void requestFocus() { if (host!=null) { host.requestFocus(this); } else { - System.out.println("host is null"); + LOGGER.warn("Requesting focus for {}, but the host is null", this); } } + /** + * If this widget has a host, releases this widget's focus. + * + * @see GuiDescription#releaseFocus(WWidget) + */ public void releaseFocus() { if (host!=null) host.releaseFocus(this); } + /** + * Tests whether this widget can have the focus in the GUI. + * + * @return true if this widget can be focused, false otherwise + */ public boolean canFocus() { return false; } |