diff options
author | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2020-05-08 19:08:01 +0300 |
---|---|---|
committer | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2020-05-08 19:08:01 +0300 |
commit | 6c91649d223aeee29204ed8d71b8ee1a7072e1ec (patch) | |
tree | 82661b8017b28f2e5314da3fd3795340d8e51fcb | |
parent | 8fac3dd7e960e99aae80b53cc490ea50407ee756 (diff) | |
parent | 9743d57ec08b0488e2c7152615d54dd813a1bd3f (diff) | |
download | LibGui-6c91649d223aeee29204ed8d71b8ee1a7072e1ec.tar.gz LibGui-6c91649d223aeee29204ed8d71b8ee1a7072e1ec.tar.bz2 LibGui-6c91649d223aeee29204ed8d71b8ee1a7072e1ec.zip |
Merge branch 'master' into new-breaking
# Conflicts:
# src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java
22 files changed, 499 insertions, 117 deletions
diff --git a/build.gradle b/build.gradle index dc89864..3d0e1db 100644 --- a/build.gradle +++ b/build.gradle @@ -15,7 +15,7 @@ buildscript { System.out.println(rootProject.name); if (rootProject.name.equalsIgnoreCase("LibGUI")) { System.out.println("Added libgui to classpath"); - classpath 'fabric-loom:fabric-loom.gradle.plugin:0.2.6-SNAPSHOT' + classpath 'fabric-loom:fabric-loom.gradle.plugin:0.2.7-SNAPSHOT' } } } diff --git a/gradle.properties b/gradle.properties index 9d3b564..b1db372 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=20w12a - yarn_mappings=20w12a+build.14 - loader_version=0.7.8+build.186 + minecraft_version=20w17a + yarn_mappings=20w17a+build.4 + loader_version=0.8.2+build.194 # Mod Properties - mod_version = 1.8.0 + mod_version = 1.9.0 maven_group = io.github.cottonmc archives_base_name = LibGui # Dependencies - fabric_version=0.5.5+build.311-1.16 + fabric_version=0.6.2+build.327-1.16 jankson_version=2.0.1+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 87dfd34..403b5c0 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java @@ -103,15 +103,15 @@ public class CottonInventoryController extends ScreenHandler implements GuiDescr if (blockInventory!=null) { if (slot.inventory==blockInventory) { //Try to transfer the item from the block into the player's inventory - if (!this.insertItem(toTransfer, this.playerInventory, true)) { + if (!this.insertItem(toTransfer, this.playerInventory, true, player)) { return ItemStack.EMPTY; } - } else if (!this.insertItem(toTransfer, this.blockInventory, false)) { //Try to transfer the item from the player to the block + } else if (!this.insertItem(toTransfer, this.blockInventory, false, player)) { //Try to transfer the item from the player to the block return ItemStack.EMPTY; } } else { //There's no block, just swap between the player's storage and their hotbar - if (!swapHotbar(toTransfer, slotNumber, this.playerInventory)) { + if (!swapHotbar(toTransfer, slotNumber, this.playerInventory, player)) { return ItemStack.EMPTY; } } @@ -130,9 +130,9 @@ public class CottonInventoryController extends ScreenHandler implements GuiDescr } /** WILL MODIFY toInsert! Returns true if anything was inserted. */ - private boolean insertIntoExisting(ItemStack toInsert, Slot slot) { + private boolean insertIntoExisting(ItemStack toInsert, Slot slot, PlayerEntity player) { ItemStack curSlotStack = slot.getStack(); - if (!curSlotStack.isEmpty() && canStacksCombine(toInsert, curSlotStack)) { + if (!curSlotStack.isEmpty() && canStacksCombine(toInsert, curSlotStack) && slot.canTakeItems(player)) { int combinedAmount = curSlotStack.getCount() + toInsert.getCount(); if (combinedAmount <= toInsert.getMaxCount()) { toInsert.setCount(0); @@ -166,7 +166,7 @@ public class CottonInventoryController extends ScreenHandler implements GuiDescr return false; } - private boolean insertItem(ItemStack toInsert, Inventory inventory, boolean walkBackwards) { + private boolean insertItem(ItemStack toInsert, Inventory inventory, boolean walkBackwards, PlayerEntity player) { //Make a unified list of slots *only from this inventory* ArrayList<Slot> inventorySlots = new ArrayList<>(); for(Slot slot : slots) { @@ -179,13 +179,13 @@ public class CottonInventoryController extends ScreenHandler implements GuiDescr if (walkBackwards) { for(int i=inventorySlots.size()-1; i>=0; i--) { Slot curSlot = inventorySlots.get(i); - if (insertIntoExisting(toInsert, curSlot)) inserted = true; + if (insertIntoExisting(toInsert, curSlot, player)) inserted = true; if (toInsert.isEmpty()) break; } } else { for(int i=0; i<inventorySlots.size(); i++) { Slot curSlot = inventorySlots.get(i); - if (insertIntoExisting(toInsert, curSlot)) inserted = true; + if (insertIntoExisting(toInsert, curSlot, player)) inserted = true; if (toInsert.isEmpty()) break; } @@ -212,7 +212,7 @@ public class CottonInventoryController extends ScreenHandler implements GuiDescr return inserted; } - private boolean swapHotbar(ItemStack toInsert, int slotNumber, Inventory inventory) { + private boolean swapHotbar(ItemStack toInsert, int slotNumber, Inventory inventory, PlayerEntity player) { //Feel out the slots to see what's storage versus hotbar ArrayList<Slot> storageSlots = new ArrayList<>(); ArrayList<Slot> hotbarSlots = new ArrayList<>(); @@ -236,7 +236,7 @@ public class CottonInventoryController extends ScreenHandler implements GuiDescr //swap from hotbar to storage for(int i=0; i<storageSlots.size(); i++) { Slot curSlot = storageSlots.get(i); - if (insertIntoExisting(toInsert, curSlot)) inserted = true; + if (insertIntoExisting(toInsert, curSlot, player)) inserted = true; if (toInsert.isEmpty()) break; } if (!toInsert.isEmpty()) { @@ -250,7 +250,7 @@ public class CottonInventoryController extends ScreenHandler implements GuiDescr //swap from storage to hotbar for(int i=0; i<hotbarSlots.size(); i++) { Slot curSlot = hotbarSlots.get(i); - if (insertIntoExisting(toInsert, curSlot)) inserted = true; + if (insertIntoExisting(toInsert, curSlot, player)) inserted = true; if (toInsert.isEmpty()) break; } if (!toInsert.isEmpty()) { @@ -333,13 +333,19 @@ public class CottonInventoryController extends ScreenHandler implements GuiDescr Block b = state.getBlock(); if (b instanceof InventoryProvider) { - return ((InventoryProvider)b).getInventory(state, world, pos); + Inventory inventory = ((InventoryProvider)b).getInventory(state, world, pos); + if (inventory != null) { + return inventory; + } } BlockEntity be = world.getBlockEntity(pos); if (be!=null) { if (be instanceof InventoryProvider) { - return ((InventoryProvider)be).getInventory(state, world, pos); + Inventory inventory = ((InventoryProvider)be).getInventory(state, world, pos); + if (inventory != null) { + return inventory; + } } else if (be instanceof Inventory) { return (Inventory)be; } 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 da54dda..84eee0f 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/EmptyInventory.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/EmptyInventory.java @@ -13,32 +13,32 @@ public class EmptyInventory implements Inventory { public void clear() {} @Override - public int getInvSize() { + public int size() { return 0; } @Override - public boolean isInvEmpty() { + public boolean isEmpty() { return true; } @Override - public ItemStack getInvStack(int slot) { + public ItemStack getStack(int slot) { return ItemStack.EMPTY; } @Override - public ItemStack takeInvStack(int slot, int count) { + public ItemStack removeStack(int slot, int count) { return ItemStack.EMPTY; } @Override - public ItemStack removeInvStack(int slot) { + public ItemStack removeStack(int slot) { return ItemStack.EMPTY; } @Override - public void setInvStack(int slot, ItemStack stack) { + public void setStack(int slot, ItemStack stack) { } @Override @@ -46,7 +46,7 @@ public class EmptyInventory implements Inventory { } @Override - public boolean canPlayerUseInv(PlayerEntity player) { + public boolean canPlayerUse(PlayerEntity player) { return true; } 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 e6b30af..cce4cfe 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/ValidatedSlot.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/ValidatedSlot.java @@ -17,12 +17,12 @@ public class ValidatedSlot extends Slot { @Override public boolean canInsert(ItemStack stack) { - return modifiable && inventory.isValidInvStack(slotNumber, stack); + return modifiable && inventory.isValid(slotNumber, stack); } @Override public boolean canTakeItems(PlayerEntity player) { - return modifiable && inventory.canPlayerUseInv(player); + return modifiable && inventory.canPlayerUse(player); } @Override 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 1a8c326..05f3b68 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 @@ -5,6 +5,7 @@ import io.github.cottonmc.cotton.gui.widget.WPanel; import io.github.cottonmc.cotton.gui.widget.WWidget; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.LiteralText; import net.minecraft.text.Text; @@ -55,7 +56,7 @@ public class CottonClientScreen extends Screen implements TextHoverRendererScree } public void paint(int mouseX, int mouseY) { - super.renderBackground(); + super.renderBackground(ScreenDrawing.matrices); if (description!=null) { WPanel root = description.getRootPanel(); @@ -65,16 +66,17 @@ public class CottonClientScreen extends Screen implements TextHoverRendererScree } if (getTitle() != null) { - textRenderer.draw(getTitle().asFormattedString(), left, top, description.getTitleColor()); + textRenderer.method_27528(ScreenDrawing.matrices, getTitle(), left, top, description.getTitleColor()); } } @SuppressWarnings("deprecation") @Override - public void render(int mouseX, int mouseY, float partialTicks) { + public void render(MatrixStack matrices, int mouseX, int mouseY, float partialTicks) { + ScreenDrawing.matrices = matrices; paint(mouseX, mouseY); - super.render(mouseX, mouseY, partialTicks); + super.render(matrices, mouseX, mouseY, partialTicks); if (description!=null) { WPanel root = description.getRootPanel(); @@ -222,6 +224,6 @@ public class CottonClientScreen extends Screen implements TextHoverRendererScree @Override public void renderTextHover(Text text, int x, int y) { - renderTextHoverEffect(text, x, y); + renderTextHoverEffect(ScreenDrawing.matrices, text, x, y); } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonHud.java b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonHud.java index 6294ac6..0e6fa4f 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonHud.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonHud.java @@ -6,6 +6,7 @@ import net.fabricmc.api.Environment; import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; import net.minecraft.client.MinecraftClient; import net.minecraft.client.util.Window; +import net.minecraft.client.util.math.MatrixStack; import java.util.*; @@ -104,7 +105,8 @@ public enum CottonHud implements HudRenderCallback { } @Override - public void onHudRender(float tickDelta) { + public void onHudRender(MatrixStack matrices, float tickDelta) { + ScreenDrawing.matrices = matrices; Window window = MinecraftClient.getInstance().getWindow(); int hudWidth = window.getScaledWidth(); int hudHeight = window.getScaledHeight(); 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 a9597dc..c64ffa2 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 @@ -2,6 +2,7 @@ package io.github.cottonmc.cotton.gui.client; import net.minecraft.client.gui.screen.ingame.HandledScreen; import net.minecraft.client.render.DiffuseLighting; +import net.minecraft.client.util.math.MatrixStack; import org.lwjgl.glfw.GLFW; import io.github.cottonmc.cotton.gui.CottonInventoryController; @@ -182,12 +183,12 @@ public class CottonInventoryScreen<T extends CottonCraftingController> extends H WWidget child = root.hit(containerX, containerY); child.onMouseMove(containerX - child.getAbsoluteX(), containerY - child.getAbsoluteY()); } - + @Override - protected void drawBackground(float partialTicks, int mouseX, int mouseY) {} //This is just an AbstractContainerScreen thing; most Screens don't work this way. + protected void drawBackground(MatrixStack matrices, float partialTicks, int mouseX, int mouseY) {} //This is just an AbstractContainerScreen thing; most Screens don't work this way. public void paint(int mouseX, int mouseY) { - super.renderBackground(); + super.renderBackground(ScreenDrawing.matrices); if (description!=null) { WPanel root = description.getRootPanel(); @@ -197,16 +198,17 @@ public class CottonInventoryScreen<T extends CottonCraftingController> extends H } if (getTitle() != null) { - textRenderer.draw(getTitle().asFormattedString(), x, y, description.getTitleColor()); + textRenderer.method_27528(ScreenDrawing.matrices, getTitle(), x, y, description.getTitleColor()); } } @SuppressWarnings("deprecation") @Override - public void render(int mouseX, int mouseY, float partialTicks) { + public void render(MatrixStack matrices, int mouseX, int mouseY, float partialTicks) { + ScreenDrawing.matrices = matrices; paint(mouseX, mouseY); - super.render(mouseX, mouseY, partialTicks); + super.render(matrices, mouseX, mouseY, partialTicks); DiffuseLighting.disable(); //Needed because super.render leaves dirty state if (description!=null) { @@ -219,7 +221,7 @@ public class CottonInventoryScreen<T extends CottonCraftingController> extends H } } - drawMouseoverTooltip(mouseX, mouseY); //Draws the itemstack tooltips + drawMouseoverTooltip(matrices, mouseX, mouseY); //Draws the itemstack tooltips } @Override @@ -235,6 +237,6 @@ public class CottonInventoryScreen<T extends CottonCraftingController> extends H @Override public void renderTextHover(Text text, int x, int y) { - renderTextHoverEffect(text, x, y); + renderTextHoverEffect(ScreenDrawing.matrices, text, x, y); } } 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 0cfac29..199909f 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 @@ -1,5 +1,7 @@ package io.github.cottonmc.cotton.gui.client; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.text.Text; import org.lwjgl.opengl.GL11; import com.mojang.blaze3d.platform.GlStateManager; @@ -17,9 +19,23 @@ import net.minecraft.util.Identifier; * {@code ScreenDrawing} contains utility methods for drawing contents on a screen. */ public class ScreenDrawing { + // Internal MatrixStack for rendering strings. + // TODO (2.0): Remove + static MatrixStack matrices; + private ScreenDrawing() {} /** + * Gets the currently bound matrix stack. + * + * @return the matrix stack + * @since 1.9.0 + */ + public static MatrixStack getMatrices() { + return matrices; + } + + /** * Draws a textured rectangle. * * @param x the x coordinate of the box on-screen @@ -295,19 +311,51 @@ public class ScreenDrawing { public static void drawString(String s, Alignment align, int x, int y, int width, int color) { switch(align) { case LEFT: { - MinecraftClient.getInstance().textRenderer.draw(s, x, y, color); + MinecraftClient.getInstance().textRenderer.draw(matrices, s, x, y, color); } break; case CENTER: { int wid = MinecraftClient.getInstance().textRenderer.getStringWidth(s); int l = (width/2) - (wid/2); - MinecraftClient.getInstance().textRenderer.draw(s, x+l, y, color); + MinecraftClient.getInstance().textRenderer.draw(matrices, s, x+l, y, color); } break; case RIGHT: { int wid = MinecraftClient.getInstance().textRenderer.getStringWidth(s); int l = width - wid; - MinecraftClient.getInstance().textRenderer.draw(s, x+l, y, color); + MinecraftClient.getInstance().textRenderer.draw(matrices, s, x+l, y, color); + } + break; + } + } + + /** + * Draws a text component with a custom alignment. + * + * @param text the text + * @param align the alignment of the string + * @param x the X position + * @param y the Y position + * @param width the width of the string, used for aligning + * @param color the text color + * @since 1.9.0 + */ + public static void drawString(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); + } + break; + case CENTER: { + int wid = MinecraftClient.getInstance().textRenderer.method_27525(text); + int l = (width/2) - (wid/2); + MinecraftClient.getInstance().textRenderer.method_27528(matrices, text, x+l, y, color); + } + break; + case RIGHT: { + int wid = MinecraftClient.getInstance().textRenderer.method_27525(text); + int l = width - wid; + MinecraftClient.getInstance().textRenderer.method_27528(matrices, text, x+l, y, color); } break; } @@ -326,19 +374,50 @@ public class ScreenDrawing { public static void drawStringWithShadow(String s, Alignment align, int x, int y, int width, int color) { switch(align) { case LEFT: { - MinecraftClient.getInstance().textRenderer.drawWithShadow(s, x, y, color); + MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, s, x, y, color); } break; case CENTER: { int wid = MinecraftClient.getInstance().textRenderer.getStringWidth(s); int l = (width/2) - (wid/2); - MinecraftClient.getInstance().textRenderer.drawWithShadow(s, x+l, y, color); + MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, s, x+l, y, color); } break; case RIGHT: { int wid = MinecraftClient.getInstance().textRenderer.getStringWidth(s); int l = width - wid; - MinecraftClient.getInstance().textRenderer.drawWithShadow(s, x+l, y, color); + MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, s, x+l, y, color); + } + break; + } + } + + /** + * Draws a shadowed text component. + * + * @param text the text component + * @param align the alignment of the string + * @param x the X position + * @param y the Y position + * @param width the width of the string, used for aligning + * @param color the text color + */ + public static void drawStringWithShadow(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); + } + break; + case CENTER: { + int wid = MinecraftClient.getInstance().textRenderer.method_27525(text); + int l = (width/2) - (wid/2); + MinecraftClient.getInstance().textRenderer.method_27528(matrices, text, x+l, y, color); + } + break; + case RIGHT: { + int wid = MinecraftClient.getInstance().textRenderer.method_27525(text); + int l = width - wid; + MinecraftClient.getInstance().textRenderer.method_27528(matrices, text, x+l, y, color); } break; } @@ -353,7 +432,19 @@ public class ScreenDrawing { * @param color the text color */ public static void drawString(String s, int x, int y, int color) { - MinecraftClient.getInstance().textRenderer.draw(s, x, y, color); + MinecraftClient.getInstance().textRenderer.draw(matrices, s, x, y, color); + } + + /** + * Draws a left-aligned text component. + * + * @param text the text component + * @param x the X position + * @param y the Y position + * @param color the text color + */ + public static void drawString(Text text, int x, int y, int color) { + MinecraftClient.getInstance().textRenderer.method_27528(matrices, text, x, y, color); } /** @@ -361,8 +452,8 @@ public class ScreenDrawing { */ @Deprecated public static void drawCenteredWithShadow(String s, int x, int y, int color) { - TextRenderer render = MinecraftClient.getInstance().getFontManager().getTextRenderer(MinecraftClient.DEFAULT_TEXT_RENDERER_ID); - render.drawWithShadow(s, (float)(x - render.getStringWidth(s) / 2), (float)y, color); + TextRenderer render = MinecraftClient.getInstance().textRenderer; + render.drawWithShadow(matrices, s, (float)(x - render.getStringWidth(s) / 2), (float)y, color); } public static int colorAtOpacity(int opaque, float opacity) { 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 a086b3a..b5e1344 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 @@ -1,5 +1,7 @@ package io.github.cottonmc.cotton.gui.widget; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; import net.minecraft.util.math.MathHelper; import org.lwjgl.glfw.GLFW; @@ -103,6 +105,7 @@ public abstract class WAbstractSlider extends WWidget { return true; } + @Environment(EnvType.CLIENT) @Override public WWidget onMouseDown(int x, int y, int button) { // Check if cursor is inside or <=2px away from track @@ -112,6 +115,7 @@ public abstract class WAbstractSlider extends WWidget { return super.onMouseDown(x, y, button); } + @Environment(EnvType.CLIENT) @Override public void onMouseDrag(int x, int y, int button) { if (isFocused()) { @@ -120,6 +124,7 @@ public abstract class WAbstractSlider extends WWidget { } } + @Environment(EnvType.CLIENT) @Override public void onClick(int x, int y, int button) { moveSlider(x, y); @@ -134,6 +139,7 @@ public abstract class WAbstractSlider extends WWidget { if (value != previousValue) onValueChanged(value); } + @Environment(EnvType.CLIENT) @Override public WWidget onMouseUp(int x, int y, int button) { dragging = false; @@ -141,6 +147,7 @@ public abstract class WAbstractSlider extends WWidget { return super.onMouseUp(x, y, button); } + @Environment(EnvType.CLIENT) @Override public void onMouseScroll(int x, int y, double amount) { int previous = value; @@ -152,6 +159,7 @@ public abstract class WAbstractSlider extends WWidget { } } + @Environment(EnvType.CLIENT) @Override public void tick() { if (draggingFinishedFromScrollingTimer > 0) { @@ -242,6 +250,7 @@ public abstract class WAbstractSlider extends WWidget { if (valueChangeListener != null) valueChangeListener.accept(value); } + @Environment(EnvType.CLIENT) @Override public void onKeyPressed(int ch, int key, int modifiers) { boolean valueChanged = false; @@ -269,6 +278,7 @@ public abstract class WAbstractSlider extends WWidget { } } + @Environment(EnvType.CLIENT) @Override public void onKeyReleased(int ch, int key, int modifiers) { if (pendingDraggingFinishedFromKeyboard && (isDecreasingKey(ch) || isIncreasingKey(ch))) { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WBar.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WBar.java index 519d1a3..168e540 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WBar.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WBar.java @@ -7,6 +7,7 @@ import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.screen.PropertyDelegate; +import net.minecraft.text.LiteralText; import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; import net.minecraft.util.Identifier; @@ -160,23 +161,23 @@ public class WBar extends WWidget { } @Override - public void addInformation(List<String> information) { + public void addTooltip(List<Text> information) { if (tooltipLabel!=null) { int value = (field>=0) ? properties.get(field) : 0; int valMax = (max>=0) ? properties.get(max) : maxValue; - String formatted = tooltipLabel; + Text formatted; try { - formatted = new TranslatableText(tooltipLabel, Integer.valueOf(value), Integer.valueOf(valMax)).asFormattedString(); + formatted = new TranslatableText(tooltipLabel, Integer.valueOf(value), Integer.valueOf(valMax)); } catch (Throwable t) { - formatted = t.getLocalizedMessage(); + formatted = new LiteralText(t.getLocalizedMessage()); } //Fallback to raw tooltipLabel information.add(formatted); } if (tooltipTextComponent!=null) { try { - information.add(tooltipTextComponent.asFormattedString()); + information.add(tooltipTextComponent); } catch (Throwable t) { - information.add(t.getLocalizedMessage()); + information.add(new LiteralText(t.getLocalizedMessage())); } } } 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 fbf664d..b3fc425 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 @@ -2,6 +2,8 @@ package io.github.cottonmc.cotton.gui.widget; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.widget.data.Alignment; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.widget.AbstractButtonWidget; import net.minecraft.client.sound.PositionedSoundInstance; @@ -58,7 +60,7 @@ public class WButton extends WWidget { color = 0xFFFFA0; }*/ - ScreenDrawing.drawStringWithShadow(label.asFormattedString(), alignment, x, y + ((20 - 8) / 2), width, color); //LibGuiClient.config.darkMode ? darkmodeColor : color); + ScreenDrawing.drawStringWithShadow(label, alignment, x, y + ((20 - 8) / 2), width, color); //LibGuiClient.config.darkMode ? darkmodeColor : color); } } @@ -66,7 +68,8 @@ public class WButton extends WWidget { public void setSize(int x, int y) { super.setSize(x, 20); } - + + @Environment(EnvType.CLIENT) @Override public void onClick(int x, int y, int button) { super.onClick(x, y, button); 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 be516c7..d3d91b0 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 @@ -41,6 +41,7 @@ public class WItem extends WWidget { return true; } + @Environment(EnvType.CLIENT) @Override public void tick() { if (ticks++ >= duration) { @@ -55,7 +56,6 @@ public class WItem extends WWidget { RenderSystem.pushMatrix(); RenderSystem.enableDepthTest(); RenderSystem.translatef(x, y, 0); - RenderSystem.scalef(1.2f, 1.2f, 1.0f); MinecraftClient mc = MinecraftClient.getInstance(); ItemRenderer renderer = mc.getItemRenderer(); 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 d925c17..1ab42b1 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 @@ -23,7 +23,14 @@ public class WLabel extends WWidget { protected int color; protected int darkmodeColor; + /** + * The default text color for light mode labels. + */ public static final int DEFAULT_TEXT_COLOR = 0x404040; + + /** + * The default text color for {@linkplain io.github.cottonmc.cotton.gui.client.LibGuiConfig#darkMode dark mode} labels. + */ public static final int DEFAULT_DARKMODE_TEXT_COLOR = 0xbcbcbc; /** @@ -69,8 +76,7 @@ public class WLabel extends WWidget { @Override public void paintBackground(int x, int y, int mouseX, int mouseY) { - String translated = text.asFormattedString(); - ScreenDrawing.drawString(translated, alignment, x, y, this.getWidth(), LibGuiClient.config.darkMode ? darkmodeColor : color); + ScreenDrawing.drawString(text, alignment, x, y, this.getWidth(), LibGuiClient.config.darkMode ? darkmodeColor : color); Text hoveredText = getTextAt(mouseX, mouseY); if (hoveredText != null) { @@ -81,6 +87,7 @@ public class WLabel extends WWidget { } } + @Environment(EnvType.CLIENT) @Override public void onClick(int x, int y, int button) { Text hoveredText = getTextAt(x, y); @@ -96,14 +103,7 @@ public class WLabel extends WWidget { @Nullable private Text getTextAt(int x, int y) { if (isWithinBounds(x, y)) { - int i = 0; - for (Text component : text) { - TextRenderer renderer = MinecraftClient.getInstance().textRenderer; - i += renderer.getStringWidth(component.asFormattedString()); - if (i > x) { - return component; - } - } + return MinecraftClient.getInstance().textRenderer.method_27527().method_27489(text, x); } return null; } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java index 4541427..d9ab1b0 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java @@ -25,19 +25,47 @@ public class WLabeledSlider extends WAbstractSlider { @Nullable private LabelUpdater labelUpdater = null; private Alignment labelAlignment = Alignment.CENTER; + /** + * Constructs a horizontal slider with no default label. + * + * @param min the minimum value + * @param max the maximum value + */ public WLabeledSlider(int min, int max) { this(min, max, Axis.HORIZONTAL); } + /** + * Constructs a slider with no default label. + * + * @param min the minimum value + * @param max the maximum value + * @param axis the slider axis + */ public WLabeledSlider(int min, int max, Axis axis) { super(min, max, axis); } + /** + * Constructs a slider. + * + * @param min the minimum value + * @param max the maximum value + * @param axis the slider axis + * @param label the slider label (can be null) + */ public WLabeledSlider(int min, int max, Axis axis, @Nullable Text label) { this(min, max, axis); this.label = label; } + /** + * Constructs a horizontal slider. + * + * @param min the minimum value + * @param max the maximum value + * @param label the slider label (can be null) + */ public WLabeledSlider(int min, int max, @Nullable Text label) { this(min, max); this.label = label; @@ -52,11 +80,21 @@ public class WLabeledSlider extends WAbstractSlider { } } + /** + * Gets the current label of this slider. + * + * @return the label + */ @Nullable public Text getLabel() { return label; } + /** + * Sets the label of this slider. + * + * @param label the new label + */ public void setLabel(@Nullable Text label) { this.label = label; } @@ -69,19 +107,39 @@ public class WLabeledSlider extends WAbstractSlider { } } + /** + * Gets the text alignment of this slider's label. + * + * @return the alignment + */ public Alignment getLabelAlignment() { return labelAlignment; } + /** + * Sets the text alignment of this slider's label. + * + * @param labelAlignment the new alignment + */ public void setLabelAlignment(Alignment labelAlignment) { this.labelAlignment = labelAlignment; } + /** + * Gets the {@link LabelUpdater} of this slider. + * + * @return the label updater + */ @Nullable public LabelUpdater getLabelUpdater() { return labelUpdater; } + /** + * Sets the {@link LabelUpdater} of this slider. + * + * @param labelUpdater the new label updater + */ public void setLabelUpdater(@Nullable LabelUpdater labelUpdater) { this.labelUpdater = labelUpdater; } @@ -129,7 +187,7 @@ public class WLabeledSlider extends WAbstractSlider { if (label != null) { int color = isMouseInsideBounds(mouseX, mouseY) ? 0xFFFFA0 : 0xE0E0E0; - ScreenDrawing.drawStringWithShadow(label.asFormattedString(), labelAlignment, 2, aHeight / 2 - 4, aWidth - 4, color); + ScreenDrawing.drawStringWithShadow(label, labelAlignment, 2, aHeight / 2 - 4, aWidth - 4, color); } RenderSystem.popMatrix(); } @@ -150,8 +208,19 @@ public class WLabeledSlider extends WAbstractSlider { ScreenDrawing.texturedRect(x + halfWidth, y, halfWidth, 20, AbstractButtonWidget.WIDGETS_LOCATION, buttonEndLeft, buttonTop, 200 * px, buttonTop + buttonHeight, 0xFFFFFFFF); } + /** + * A label updater updates the label of a slider based on the current value. + * + * <p>Useful for situations when you want to have display values on the slider. + */ @FunctionalInterface public interface LabelUpdater { + /** + * Gets the updated label for the new slider value. + * + * @param value the slider value + * @return the label + */ Text updateLabel(int value); } } 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 7c92191..ff2b897 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 @@ -16,25 +16,49 @@ import io.github.cottonmc.cotton.gui.widget.data.Axis; * <p> W is the WWidget class that will represent a single D of data. */ public class WListPanel<D, W extends WWidget> extends WClippedPanel { + /** + * The list of data that this list represents. + */ protected List<D> data; + + /** + * The supplier of new empty widgets. + */ protected Supplier<W> supplier; + + /** + * The widget configurator that configures the passed widget + * to display the passed data. + */ protected BiConsumer<D, W> configurator; protected HashMap<D, W> configured = new HashMap<>(); protected List<W> unconfigured = new ArrayList<>(); + + /** + * The height of each child cell. + */ protected int cellHeight = 20; + + /** + * Whether this list has a fixed height for items. + */ protected boolean fixedHeight = false; protected int margin = 4; - + + /** + * The scroll bar of this list. + */ protected WScrollBar scrollBar = new WScrollBar(Axis.VERTICAL); - int lastScroll = -1; + private int lastScroll = -1; public WListPanel(List<D> data, Supplier<W> supplier, BiConsumer<D, W> configurator) { this.data = data; this.supplier = supplier; this.configurator = configurator; scrollBar.setMaxValue(data.size()); + scrollBar.setParent(this); } /** @@ -157,7 +181,13 @@ public class WListPanel<D, W extends WWidget> extends WClippedPanel { //System.out.println("Children: "+children.size()); } - + + /** + * Sets the height of this list's items to a constant value. + * + * @param height the item height + * @return this list + */ public WListPanel<D, W> setListItemHeight(int height) { cellHeight = height; fixedHeight = true; 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 1233d7d..e7bdeab 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 @@ -9,7 +9,15 @@ import io.github.cottonmc.cotton.gui.client.BackgroundPainter; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +/** + * Panels are widgets tthat contain other widgets. + */ public abstract class WPanel extends WWidget { + /** + * The widgets contained within this panel. + * + * <p>The list is mutable. + */ protected final List<WWidget> children = Lists.newArrayList(); @Environment(EnvType.CLIENT) private BackgroundPainter backgroundPainter = null; @@ -21,7 +29,12 @@ public abstract class WPanel extends WWidget { child.createPeers(c); } } - + + /** + * Removes the widget from this panel. + * + * @param w the removed widget + */ public void remove(WWidget w) { children.remove(w); } @@ -30,13 +43,24 @@ public abstract class WPanel extends WWidget { public boolean canResize() { return true; } - + + /** + * Sets the {@link BackgroundPainter} of this panel. + * + * @param painter the new painter + * @return this panel + */ @Environment(EnvType.CLIENT) public WPanel setBackgroundPainter(BackgroundPainter painter) { this.backgroundPainter = painter; return this; } - + + /** + * Gets the current {@link BackgroundPainter} of this panel. + * + * @return the painter + */ @Environment(EnvType.CLIENT) public BackgroundPainter getBackgroundPainter() { return this.backgroundPainter; @@ -62,7 +86,8 @@ public abstract class WPanel extends WWidget { int pushDown = w.getY()+w.getHeight(); this.setSize(Math.max(this.getWidth(), pushRight), Math.max(this.getHeight(), pushDown)); } - + + @Environment(EnvType.CLIENT) @Override public WWidget onMouseUp(int x, int y, int button) { if (children.isEmpty()) return super.onMouseUp(x, y, button); @@ -77,7 +102,8 @@ public abstract class WPanel extends WWidget { } return super.onMouseUp(x, y, button); } - + + @Environment(EnvType.CLIENT) @Override public WWidget onMouseDown(int x, int y, int button) { if (children.isEmpty()) return super.onMouseDown(x, y, button); @@ -92,7 +118,8 @@ public abstract class WPanel extends WWidget { } return super.onMouseDown(x, y, button); } - + + @Environment(EnvType.CLIENT) @Override public void onMouseDrag(int x, int y, int button) { if (children.isEmpty()) return; @@ -169,7 +196,11 @@ public abstract class WPanel extends WWidget { child.paintForeground(x + child.getX(), y + child.getY(), mouseX, mouseY); } } - + + /** + * Ticks all children of this panel. + */ + @Environment(EnvType.CLIENT) @Override public void tick() { for(WWidget child : children) child.tick(); diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java index aa64567..7f2dac3 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java @@ -3,6 +3,8 @@ package io.github.cottonmc.cotton.gui.widget; import io.github.cottonmc.cotton.gui.client.LibGuiClient; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.widget.data.Axis; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; public class WScrollBar extends WWidget { protected Axis axis = Axis.HORIZONTAL; @@ -150,12 +152,14 @@ public class WScrollBar extends WWidget { sliding = true; return this; } - + + @Environment(EnvType.CLIENT) @Override public void onMouseDrag(int x, int y, int button) { adjustSlider(x, y); } - + + @Environment(EnvType.CLIENT) @Override public WWidget onMouseUp(int x, int y, int button) { //TODO: Clicking before or after the handle should jump instead of scrolling 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 7982b2d..2052379 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 @@ -10,6 +10,7 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.util.Texts; +import net.minecraft.text.Style; import net.minecraft.text.Text; import javax.annotation.Nullable; @@ -45,10 +46,15 @@ public class WText extends WWidget { wrappingScheduled = true; } + @Override + public boolean canResize() { + return true; + } + @Environment(EnvType.CLIENT) private void wrapLines() { TextRenderer font = MinecraftClient.getInstance().textRenderer; - wrappedLines = Texts.wrapLines(text, width, font, true, true); + wrappedLines = font.method_27527().method_27491(text, width, Style.field_24360, false); } @Environment(EnvType.CLIENT) @@ -59,11 +65,7 @@ public class WText extends WWidget { if (lineIndex >= 0 && lineIndex < wrappedLines.size()) { Text line = wrappedLines.get(lineIndex); - int xi = 0; - for (Text part : line) { - xi += font.getStringWidth(part.asFormattedString()); - if (xi > x) return part; - } + return font.method_27527().method_27489(line, x); } return null; @@ -72,7 +74,7 @@ public class WText extends WWidget { @Environment(EnvType.CLIENT) @Override public void paintBackground(int x, int y, int mouseX, int mouseY) { - if (wrappingScheduled) { + if (wrappedLines == null || wrappingScheduled) { wrapLines(); wrappingScheduled = false; } @@ -81,9 +83,8 @@ public class WText extends WWidget { for (int i = 0; i < wrappedLines.size(); i++) { Text line = wrappedLines.get(i); int c = LibGuiClient.config.darkMode ? darkmodeColor : color; - String str = line.asFormattedString(); - ScreenDrawing.drawString(str, alignment, x, y + i * font.fontHeight, width, c); + ScreenDrawing.drawString(line, alignment, x, y + i * font.fontHeight, width, c); } Text hoveredText = getTextAt(mouseX, mouseY); @@ -95,6 +96,7 @@ public class WText extends WWidget { } } + @Environment(EnvType.CLIENT) @Override public void onClick(int x, int y, int button) { if (button != 0) return; // only left clicks @@ -110,6 +112,7 @@ public class WText extends WWidget { } public WText setText(Text text) { + Objects.requireNonNull(text, "text is null"); this.text = text; wrappingScheduled = true; @@ -140,4 +143,26 @@ public class WText extends WWidget { this.darkmodeColor = this.color; return this; } + + /** + * Gets the alignment of this text widget. + * + * @return the alignment + * @since 1.9.0 + */ + public Alignment getAlignment() { + return alignment; + } + + /** + * Sets the alignment of this text widget. + * + * @param alignment the new alignment + * @return this widget + * @since 1.9.0 + */ + public WText setAlignment(Alignment alignment) { + this.alignment = alignment; + return this; + } } 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 97023aa..75e7628 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 @@ -321,7 +321,7 @@ public class WTextField extends WWidget { int textColor = this.editable ? this.enabledColor : this.uneditableColor; //TODO: Scroll offset - String trimText = font.trimToWidth(this.text, this.width-OFFSET_X_TEXT); + String trimText = font.method_27523(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 @@ -347,16 +347,16 @@ public class WTextField extends WWidget { int preCursorAdvance = textX; if (!trimText.isEmpty()) { String string_2 = trimText.substring(0,adjustedCursor); - preCursorAdvance = font.drawWithShadow(string_2, textX, textY, textColor); + preCursorAdvance = font.drawWithShadow(ScreenDrawing.getMatrices(), string_2, textX, textY, textColor); } if (adjustedCursor<trimText.length()) { - font.drawWithShadow(trimText.substring(adjustedCursor), preCursorAdvance-1, (float)textY, textColor); + font.drawWithShadow(ScreenDrawing.getMatrices(), trimText.substring(adjustedCursor), preCursorAdvance-1, (float)textY, textColor); } if (text.length()==0 && this.suggestion != null) { - font.drawWithShadow(this.suggestion, textX, textY, -8355712); + font.drawWithShadow(ScreenDrawing.getMatrices(), this.suggestion, textX, textY, -8355712); } //int var10002; @@ -378,7 +378,7 @@ public class WTextField extends WWidget { // DrawableHelper.fill(int_9, var10001, var10002, var10003 + 9, -3092272); } else { - font.drawWithShadow("_", preCursorAdvance, textY, textColor); + font.drawWithShadow(ScreenDrawing.getMatrices(), "_", preCursorAdvance, textY, textColor); } } @@ -529,13 +529,15 @@ public class WTextField extends WWidget { renderButton(x, y); } - + + @Environment(EnvType.CLIENT) @Override public void onClick(int x, int y, int button) { requestFocus(); cursor = getCaretPos(this.text, x-OFFSET_X_TEXT); } - + + @Environment(EnvType.CLIENT) @Override public void onCharTyped(char ch) { if (this.text.length()<this.maxLength) { @@ -553,7 +555,8 @@ public class WTextField extends WWidget { public void insertText(int ofs, String s) { //TODO: Implement } - + + @Environment(EnvType.CLIENT) @Override public void onKeyPressed(int ch, int key, int modifiers) { if (!this.editable) return; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java index 0e069f0..d6aee69 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java @@ -2,6 +2,7 @@ package io.github.cottonmc.cotton.gui.widget; import io.github.cottonmc.cotton.gui.client.LibGuiClient; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; +import io.github.cottonmc.cotton.gui.widget.data.Alignment; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; @@ -75,7 +76,7 @@ public class WToggleButton extends WWidget { ScreenDrawing.texturedRect(x, y, 18, 18, isOn ? onImage : offImage, 0xFFFFFFFF); if (label!=null) { - ScreenDrawing.drawString(label.asFormattedString(), x + 22, y+6, LibGuiClient.config.darkMode ? darkmodeColor : color); + ScreenDrawing.drawString(label, x + 22, y+6, LibGuiClient.config.darkMode ? darkmodeColor : color); } } 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 a1957af..a2242b2 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 @@ -4,37 +4,86 @@ import java.util.ArrayList; import java.util.List; import io.github.cottonmc.cotton.gui.GuiDescription; +import io.github.cottonmc.cotton.gui.client.ScreenDrawing; 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.text.LiteralText; +import net.minecraft.text.Text; +import javax.annotation.Nullable; + +/** + * The base class for all widgets. + */ public class WWidget { + /** + * The containing panel of this widget. + * Can be null if this widget is the root panel or a HUD widget. + */ + @Nullable protected WPanel parent; protected int x = 0; protected int y = 0; protected int width = 18; protected int height = 18; + + /** + * The containing {@link GuiDescription} of this widget. + * Can be null if this widget is a {@linkplain io.github.cottonmc.cotton.gui.client.CottonHud HUD} widget. + */ + @Nullable protected GuiDescription host; - + + /** + * Sets the location of this widget relative to its parent. + * + * @param x the new X coordinate + * @param y the new Y coordinate + */ public void setLocation(int x, int y) { this.x = x; this.y = y; } - + + /** + * Sets the size of this widget. + * + * <p>Overriding methods may restrict one of the dimensions to be + * a constant value, for example {@code super.setSize(x, 20)}. + * + * @param x the new width + * @param y the new height + */ public void setSize(int x, int y) { this.width = x; this.height = y; } - + + /** + * Gets the X coordinate of this widget relative to its parent. + * + * @return the X coordinate + */ public int getX() { return x; } - + + /** + * Gets the Y coordinate of this widget relative to its parent. + * + * @return the Y coordinate + */ public int getY() { return y; } - + + /** + * Gets the absolute X coordinate of this widget. + * + * @return the absolute X coordinate + */ public int getAbsoluteX() { if (parent==null) { return getX(); @@ -42,7 +91,12 @@ public class WWidget { return getX() + parent.getAbsoluteX(); } } - + + /** + * Gets the absolute Y coordinate of this widget. + * + * @return the absolute Y coordinate + */ public int getAbsoluteY() { if (parent==null) { return getY(); @@ -58,11 +112,21 @@ public class WWidget { public int getHeight() { return height; } - + + /** + * Checks whether this widget can be resized using {@link #setSize}. + * + * @return true if this widget can be resized, false otherwise + */ public boolean canResize() { return false; } - + + /** + * Sets the parent panel of this widget. + * + * @param parent the new parent + */ public void setParent(WPanel parent) { this.parent = parent; } @@ -73,6 +137,7 @@ public class WWidget { * @param y The Y coordinate of the event, in widget-space (0 is the top edge of this widget) * @param button The mouse button that was used. Button numbering is consistent with LWJGL Mouse (0=left, 1=right, 2=mousewheel click) */ + @Environment(EnvType.CLIENT) public WWidget onMouseDown(int x, int y, int button) { return this; } @@ -90,6 +155,7 @@ public class WWidget { * * @since 1.5.0 */ + @Environment(EnvType.CLIENT) public void onMouseDrag(int x, int y, int button, double deltaX, double deltaY) { onMouseDrag(x, y, button); } @@ -100,6 +166,7 @@ public class WWidget { * @param y The Y coordinate of the event, in widget-space (0 is the top edge of this widget) * @param button The mouse button that was used. Button numbering is consistent with LWJGL Mouse (0=left, 1=right, 2=mousewheel click) */ + @Environment(EnvType.CLIENT) public void onMouseDrag(int x, int y, int button) { } @@ -109,6 +176,7 @@ public class WWidget { * @param y The Y coordinate of the event, in widget-space (0 is the top edge of this widget) * @param button The mouse button that was used. Button numbering is consistent with LWJGL Mouse (0=left, 1=right, 2=mousewheel click) */ + @Environment(EnvType.CLIENT) public WWidget onMouseUp(int x, int y, int button) { return this; } @@ -119,6 +187,7 @@ public class WWidget { * @param y The Y coordinate of the event, in widget-space (0 is the top edge of this widget) * @param button The mouse button that was used. Button numbering is consistent with LWJGL Mouse (0=left, 1=right, 2=mousewheel click) */ + @Environment(EnvType.CLIENT) public void onClick(int x, int y, int button) { } @@ -128,6 +197,7 @@ public class WWidget { * @param y The Y coordinate of the event, in widget-space (0 is the top edge of this widget) * @param amount The scrolled amount. Positive values are up and negative values are down. */ + @Environment(EnvType.CLIENT) public void onMouseScroll(int x, int y, double amount) { } @@ -138,6 +208,7 @@ public class WWidget { * @param y The Y coordinate of the event, in widget-space (0 is the top edge of this widget) * @since 1.5.0 */ + @Environment(EnvType.CLIENT) public void onMouseMove(int x, int y) { } @@ -146,6 +217,7 @@ public class WWidget { * and may be called for characters that do not directly have a corresponding keyboard key. * @param ch the character typed */ + @Environment(EnvType.CLIENT) public void onCharTyped(char ch) { } @@ -153,6 +225,7 @@ public class WWidget { * Notifies this widget that a key has been pressed. * @param key the GLFW scancode of the key */ + @Environment(EnvType.CLIENT) public void onKeyPressed(int ch, int key, int modifiers) { } @@ -160,6 +233,7 @@ public class WWidget { * Notifies this widget that a key has been released * @param key the GLFW scancode of the key */ + @Environment(EnvType.CLIENT) public void onKeyReleased(int ch, int key, int modifiers) { } @@ -216,25 +290,40 @@ public class WWidget { // renderTooltip(mouseX, mouseY); //} } - + + /** + * Checks whether a location is within this widget's bounds. + * + * <p>The default implementation checks that X and Y are at least 0 and below the width and height of this widget. + * + * @param x the X coordinate + * @param y the Y coordinate + * @return true if the location is within this widget, false otherwise + */ public boolean isWithinBounds(int x, int y) { return x>=0 && y>=0 && x<this.width && y<this.height; } /** - * Internal method to render tooltip data. This requires an overriden {@link #addInformation(List) - * addInformation} method to insert data into the tooltip - without this, the method returns early, because no work + * Internal method to render tooltip data. This requires an overriden {@link #addTooltip(List) + * addTooltip} method to insert data into the tooltip - without this, the method returns early, because no work */ @Environment(EnvType.CLIENT) public void renderTooltip(int x, int y, int tX, int tY) { - List<String> info = new ArrayList<>(); - addInformation(info); + List<Text> info = new ArrayList<>(); + addTooltip(info); + + List<String> stringInfo = new ArrayList<>(); + addInformation(stringInfo); + for (String line : stringInfo) { + info.add(new LiteralText(line)); + } if (info.size() == 0) return; - + Screen screen = MinecraftClient.getInstance().currentScreen; - screen.renderTooltip(info, tX+x, tY+y); + screen.renderTooltip(ScreenDrawing.getMatrices(), info, tX+x, tY+y); } /** @@ -248,9 +337,18 @@ public class WWidget { /** * Adds information to this widget's tooltip. If information remains empty after this call, no tooltip will be drawn. * @param information List containing all previous tooltip data. + * @deprecated Replaced with {@link #addTooltip(List)} */ + @Deprecated public void addInformation(List<String> information) { } + + /** + * Adds lines to this widget's tooltip. If the lines remain empty after this call, no tooltip will be drawn. + * @param tooltip List containing all previous tooltip data. + */ + public void addTooltip(List<Text> tooltip) { + } /** * Find the most specific child node at this location. For non-panel widgets, returns this widget. @@ -258,6 +356,10 @@ public class WWidget { public WWidget hit(int x, int y) { return this; } - + + /** + * Executes a client-side tick for this widget. + */ + @Environment(EnvType.CLIENT) public void tick() {} } |