From 8fac3dd7e960e99aae80b53cc490ea50407ee756 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Sun, 29 Mar 2020 22:15:20 +0300 Subject: Make CottonCraftingController extend ScreenHandler directly, rename Closes #39. --- .../cotton/gui/CottonCraftingController.java | 450 --------------------- .../cotton/gui/CottonInventoryController.java | 402 ++++++++++++++++++ .../cotton/gui/client/CottonInventoryScreen.java | 2 +- 3 files changed, 403 insertions(+), 451 deletions(-) delete mode 100644 src/main/java/io/github/cottonmc/cotton/gui/CottonCraftingController.java create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java (limited to 'src/main') diff --git a/src/main/java/io/github/cottonmc/cotton/gui/CottonCraftingController.java b/src/main/java/io/github/cottonmc/cotton/gui/CottonCraftingController.java deleted file mode 100644 index ec0cdc9..0000000 --- a/src/main/java/io/github/cottonmc/cotton/gui/CottonCraftingController.java +++ /dev/null @@ -1,450 +0,0 @@ -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; -import net.minecraft.block.BlockState; -import net.minecraft.block.InventoryProvider; -import net.minecraft.block.entity.BlockEntity; -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.recipe.Recipe; -import net.minecraft.recipe.RecipeFinder; -import net.minecraft.recipe.RecipeInputProvider; -import net.minecraft.recipe.RecipeType; -import net.minecraft.screen.*; -import net.minecraft.screen.slot.Slot; -import net.minecraft.screen.slot.SlotActionType; -import net.minecraft.world.World; - -public class CottonCraftingController extends AbstractRecipeScreenHandler implements GuiDescription { - - protected Inventory blockInventory; - protected PlayerInventory playerInventory; - protected RecipeType recipeType; - protected World world; - protected PropertyDelegate propertyDelegate; - - protected WPanel rootPanel = new WGridPanel(); - protected int titleColor = WLabel.DEFAULT_TEXT_COLOR; - protected int darkTitleColor = WLabel.DEFAULT_DARKMODE_TEXT_COLOR; - - protected WWidget focus; - - public CottonCraftingController(RecipeType recipeType, int syncId, PlayerInventory playerInventory) { - super(null, syncId); - this.blockInventory = null; - this.playerInventory = playerInventory; - this.recipeType = recipeType; - this.world = playerInventory.player.world; - this.propertyDelegate = null;//new ArrayPropertyDelegate(1); - } - - public CottonCraftingController(RecipeType recipeType, int syncId, PlayerInventory playerInventory, Inventory blockInventory, PropertyDelegate propertyDelegate) { - super(null, syncId); - this.blockInventory = blockInventory; - this.playerInventory = playerInventory; - this.recipeType = recipeType; - this.world = playerInventory.player.world; - this.propertyDelegate = propertyDelegate; - if (propertyDelegate!=null && propertyDelegate.size()>0) this.addProperties(propertyDelegate); - } - - public WPanel getRootPanel() { - return rootPanel; - } - - public int getTitleColor() { - return LibGuiClient.config.darkMode ? darkTitleColor : titleColor; - } - - public CottonCraftingController setRootPanel(WPanel panel) { - this.rootPanel = panel; - return this; - } - - public CottonCraftingController setTitleColor(int color) { - this.titleColor = color; - return this; - } - - @Environment(EnvType.CLIENT) - public void addPainters() { - if (this.rootPanel!=null) { - this.rootPanel.setBackgroundPainter(BackgroundPainter.VANILLA); - } - } - - public void addSlotPeer(ValidatedSlot slot) { - this.addSlot(slot); - } - - @Override - public ItemStack onSlotClick(int slotNumber, int button, SlotActionType action, PlayerEntity player) { - if (action==SlotActionType.QUICK_MOVE) { - - if (slotNumber < 0) { - return ItemStack.EMPTY; - } - - if (slotNumber>=this.slots.size()) return ItemStack.EMPTY; - Slot slot = this.slots.get(slotNumber); - if (slot == null || !slot.canTakeItems(player)) { - return ItemStack.EMPTY; - } - - ItemStack remaining = ItemStack.EMPTY; - if (slot != null && slot.hasStack()) { - ItemStack toTransfer = slot.getStack(); - remaining = toTransfer.copy(); - //if (slot.inventory==blockInventory) { - 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)) { - return ItemStack.EMPTY; - } - } else if (!this.insertItem(toTransfer, this.blockInventory, false)) { //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)) { - return ItemStack.EMPTY; - } - } - - if (toTransfer.isEmpty()) { - slot.setStack(ItemStack.EMPTY); - } else { - slot.markDirty(); - } - } - - return remaining; - } else { - return super.onSlotClick(slotNumber, button, action, player); - } - } - - /** WILL MODIFY toInsert! Returns true if anything was inserted. */ - private boolean insertIntoExisting(ItemStack toInsert, Slot slot) { - ItemStack curSlotStack = slot.getStack(); - if (!curSlotStack.isEmpty() && canStacksCombine(toInsert, curSlotStack)) { - int combinedAmount = curSlotStack.getCount() + toInsert.getCount(); - if (combinedAmount <= toInsert.getMaxCount()) { - toInsert.setCount(0); - curSlotStack.setCount(combinedAmount); - slot.markDirty(); - return true; - } else if (curSlotStack.getCount() < toInsert.getMaxCount()) { - toInsert.decrement(toInsert.getMaxCount() - curSlotStack.getCount()); - curSlotStack.setCount(toInsert.getMaxCount()); - slot.markDirty(); - return true; - } - } - return false; - } - - /** WILL MODIFY toInsert! Returns true if anything was inserted. */ - private boolean insertIntoEmpty(ItemStack toInsert, Slot slot) { - ItemStack curSlotStack = slot.getStack(); - if (curSlotStack.isEmpty() && slot.canInsert(toInsert)) { - if (toInsert.getCount() > slot.getMaxStackAmount()) { - slot.setStack(toInsert.split(slot.getMaxStackAmount())); - } else { - slot.setStack(toInsert.split(toInsert.getCount())); - } - - slot.markDirty(); - return true; - } - - return false; - } - - private boolean insertItem(ItemStack toInsert, Inventory inventory, boolean walkBackwards) { - //Make a unified list of slots *only from this inventory* - ArrayList inventorySlots = new ArrayList<>(); - for(Slot slot : slots) { - if (slot.inventory==inventory) inventorySlots.add(slot); - } - if (inventorySlots.isEmpty()) return false; - - //Try to insert it on top of existing stacks - boolean inserted = false; - if (walkBackwards) { - for(int i=inventorySlots.size()-1; i>=0; i--) { - Slot curSlot = inventorySlots.get(i); - if (insertIntoExisting(toInsert, curSlot)) inserted = true; - if (toInsert.isEmpty()) break; - } - } else { - for(int i=0; i=0; i--) { - Slot curSlot = inventorySlots.get(i); - if (insertIntoEmpty(toInsert, curSlot)) inserted = true; - if (toInsert.isEmpty()) break; - } - } else { - for(int i=0; i storageSlots = new ArrayList<>(); - ArrayList hotbarSlots = new ArrayList<>(); - boolean swapToStorage = true; - boolean inserted = false; - - for(Slot slot : slots) { - if (slot.inventory==inventory && slot instanceof ValidatedSlot) { - int index = ((ValidatedSlot)slot).getInventoryIndex(); - if (PlayerInventory.isValidHotbarIndex(index)) { - hotbarSlots.add(slot); - } else { - storageSlots.add(slot); - if (index==slotNumber) swapToStorage = false; - } - } - } - if (storageSlots.isEmpty() || hotbarSlots.isEmpty()) return false; - - if (swapToStorage) { - //swap from hotbar to storage - for(int i=0; i=wx && x=wy && y { - BlockState state = world.getBlockState(pos); - Block b = state.getBlock(); - - if (b instanceof InventoryProvider) { - return ((InventoryProvider)b).getInventory(state, world, pos); - } - - BlockEntity be = world.getBlockEntity(pos); - if (be!=null) { - if (be instanceof InventoryProvider) { - return ((InventoryProvider)be).getInventory(state, world, pos); - } else if (be instanceof Inventory) { - return (Inventory)be; - } - } - - return EmptyInventory.INSTANCE; - }).orElse(EmptyInventory.INSTANCE); - } - - public static PropertyDelegate getBlockPropertyDelegate(ScreenHandlerContext ctx) { - return ctx.run((world, pos) -> { - BlockState state = world.getBlockState(pos); - Block block = state.getBlock(); - if (block instanceof PropertyDelegateHolder) { - return ((PropertyDelegateHolder)block).getPropertyDelegate(); - } - BlockEntity be = world.getBlockEntity(pos); - if (be!=null && be instanceof PropertyDelegateHolder) { - return ((PropertyDelegateHolder)be).getPropertyDelegate(); - } - - return new ArrayPropertyDelegate(0); - }).orElse(new ArrayPropertyDelegate(0)); - } - - //extends CraftingContainer { - @Override - public void populateRecipeFinder(RecipeFinder recipeFinder) { - if (this.blockInventory instanceof RecipeInputProvider) { - ((RecipeInputProvider)this.blockInventory).provideRecipeInputs(recipeFinder); - } - } - - @Override - public void clearCraftingSlots() { - if (this.blockInventory!=null) this.blockInventory.clear(); - } - - @Override - public boolean matches(Recipe recipe) { - if (blockInventory==null || world==null) return false; - return false; //TODO recipe support - } - - @Override - public int getCraftingResultSlotIndex() { - return -1; - } - - @Override - public int getCraftingWidth() { - return 1; - } - - @Override - public int getCraftingHeight() { - return 1; - } - - @Override - @Environment(EnvType.CLIENT) - public int getCraftingSlotCount() { - return 1; - } - - //(implied) extends Container { - @Override - public boolean canUse(PlayerEntity entity) { - return (blockInventory!=null) ? blockInventory.canPlayerUseInv(entity) : true; - } - //} - //} - - @Override - public boolean isFocused(WWidget widget) { - return focus == widget; - } - - @Override - public WWidget getFocus() { - return focus; - } - - @Override - public void requestFocus(WWidget widget) { - //TODO: Are there circumstances where focus can't be stolen? - if (focus==widget) return; //Nothing happens if we're already focused - if (!widget.canFocus()) return; //This is kind of a gotcha but needs to happen - if (focus!=null) focus.onFocusLost(); - focus = widget; - focus.onFocusGained(); - } - - @Override - public void releaseFocus(WWidget widget) { - if (focus==widget) { - focus = null; - widget.onFocusLost(); - } - } -} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java b/src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java new file mode 100644 index 0000000..87dfd34 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java @@ -0,0 +1,402 @@ +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; +import net.minecraft.block.BlockState; +import net.minecraft.block.InventoryProvider; +import net.minecraft.block.entity.BlockEntity; +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.*; +import net.minecraft.screen.slot.Slot; +import net.minecraft.screen.slot.SlotActionType; +import net.minecraft.world.World; + +public class CottonInventoryController extends ScreenHandler implements GuiDescription { + + protected Inventory blockInventory; + protected PlayerInventory playerInventory; + protected World world; + protected PropertyDelegate propertyDelegate; + + protected WPanel rootPanel = new WGridPanel(); + protected int titleColor = WLabel.DEFAULT_TEXT_COLOR; + protected int darkTitleColor = WLabel.DEFAULT_DARKMODE_TEXT_COLOR; + + protected WWidget focus; + + public CottonInventoryController(int syncId, PlayerInventory playerInventory) { + super(null, syncId); + this.blockInventory = null; + this.playerInventory = playerInventory; + this.world = playerInventory.player.world; + this.propertyDelegate = null;//new ArrayPropertyDelegate(1); + } + + public CottonInventoryController(int syncId, PlayerInventory playerInventory, Inventory blockInventory, PropertyDelegate propertyDelegate) { + super(null, syncId); + this.blockInventory = blockInventory; + this.playerInventory = playerInventory; + this.world = playerInventory.player.world; + this.propertyDelegate = propertyDelegate; + if (propertyDelegate!=null && propertyDelegate.size()>0) this.addProperties(propertyDelegate); + } + + public WPanel getRootPanel() { + return rootPanel; + } + + public int getTitleColor() { + return LibGuiClient.config.darkMode ? darkTitleColor : titleColor; + } + + public CottonInventoryController setRootPanel(WPanel panel) { + this.rootPanel = panel; + return this; + } + + public CottonInventoryController setTitleColor(int color) { + this.titleColor = color; + return this; + } + + @Environment(EnvType.CLIENT) + public void addPainters() { + if (this.rootPanel!=null) { + this.rootPanel.setBackgroundPainter(BackgroundPainter.VANILLA); + } + } + + public void addSlotPeer(ValidatedSlot slot) { + this.addSlot(slot); + } + + @Override + public ItemStack onSlotClick(int slotNumber, int button, SlotActionType action, PlayerEntity player) { + if (action==SlotActionType.QUICK_MOVE) { + + if (slotNumber < 0) { + return ItemStack.EMPTY; + } + + if (slotNumber>=this.slots.size()) return ItemStack.EMPTY; + Slot slot = this.slots.get(slotNumber); + if (slot == null || !slot.canTakeItems(player)) { + return ItemStack.EMPTY; + } + + ItemStack remaining = ItemStack.EMPTY; + if (slot != null && slot.hasStack()) { + ItemStack toTransfer = slot.getStack(); + remaining = toTransfer.copy(); + //if (slot.inventory==blockInventory) { + 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)) { + return ItemStack.EMPTY; + } + } else if (!this.insertItem(toTransfer, this.blockInventory, false)) { //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)) { + return ItemStack.EMPTY; + } + } + + if (toTransfer.isEmpty()) { + slot.setStack(ItemStack.EMPTY); + } else { + slot.markDirty(); + } + } + + return remaining; + } else { + return super.onSlotClick(slotNumber, button, action, player); + } + } + + /** WILL MODIFY toInsert! Returns true if anything was inserted. */ + private boolean insertIntoExisting(ItemStack toInsert, Slot slot) { + ItemStack curSlotStack = slot.getStack(); + if (!curSlotStack.isEmpty() && canStacksCombine(toInsert, curSlotStack)) { + int combinedAmount = curSlotStack.getCount() + toInsert.getCount(); + if (combinedAmount <= toInsert.getMaxCount()) { + toInsert.setCount(0); + curSlotStack.setCount(combinedAmount); + slot.markDirty(); + return true; + } else if (curSlotStack.getCount() < toInsert.getMaxCount()) { + toInsert.decrement(toInsert.getMaxCount() - curSlotStack.getCount()); + curSlotStack.setCount(toInsert.getMaxCount()); + slot.markDirty(); + return true; + } + } + return false; + } + + /** WILL MODIFY toInsert! Returns true if anything was inserted. */ + private boolean insertIntoEmpty(ItemStack toInsert, Slot slot) { + ItemStack curSlotStack = slot.getStack(); + if (curSlotStack.isEmpty() && slot.canInsert(toInsert)) { + if (toInsert.getCount() > slot.getMaxStackAmount()) { + slot.setStack(toInsert.split(slot.getMaxStackAmount())); + } else { + slot.setStack(toInsert.split(toInsert.getCount())); + } + + slot.markDirty(); + return true; + } + + return false; + } + + private boolean insertItem(ItemStack toInsert, Inventory inventory, boolean walkBackwards) { + //Make a unified list of slots *only from this inventory* + ArrayList inventorySlots = new ArrayList<>(); + for(Slot slot : slots) { + if (slot.inventory==inventory) inventorySlots.add(slot); + } + if (inventorySlots.isEmpty()) return false; + + //Try to insert it on top of existing stacks + boolean inserted = false; + if (walkBackwards) { + for(int i=inventorySlots.size()-1; i>=0; i--) { + Slot curSlot = inventorySlots.get(i); + if (insertIntoExisting(toInsert, curSlot)) inserted = true; + if (toInsert.isEmpty()) break; + } + } else { + for(int i=0; i=0; i--) { + Slot curSlot = inventorySlots.get(i); + if (insertIntoEmpty(toInsert, curSlot)) inserted = true; + if (toInsert.isEmpty()) break; + } + } else { + for(int i=0; i storageSlots = new ArrayList<>(); + ArrayList hotbarSlots = new ArrayList<>(); + boolean swapToStorage = true; + boolean inserted = false; + + for(Slot slot : slots) { + if (slot.inventory==inventory && slot instanceof ValidatedSlot) { + int index = ((ValidatedSlot)slot).getInventoryIndex(); + if (PlayerInventory.isValidHotbarIndex(index)) { + hotbarSlots.add(slot); + } else { + storageSlots.add(slot); + if (index==slotNumber) swapToStorage = false; + } + } + } + if (storageSlots.isEmpty() || hotbarSlots.isEmpty()) return false; + + if (swapToStorage) { + //swap from hotbar to storage + for(int i=0; i=wx && x=wy && y { + BlockState state = world.getBlockState(pos); + Block b = state.getBlock(); + + if (b instanceof InventoryProvider) { + return ((InventoryProvider)b).getInventory(state, world, pos); + } + + BlockEntity be = world.getBlockEntity(pos); + if (be!=null) { + if (be instanceof InventoryProvider) { + return ((InventoryProvider)be).getInventory(state, world, pos); + } else if (be instanceof Inventory) { + return (Inventory)be; + } + } + + return EmptyInventory.INSTANCE; + }).orElse(EmptyInventory.INSTANCE); + } + + public static PropertyDelegate getBlockPropertyDelegate(ScreenHandlerContext ctx) { + return ctx.run((world, pos) -> { + BlockState state = world.getBlockState(pos); + Block block = state.getBlock(); + if (block instanceof PropertyDelegateHolder) { + return ((PropertyDelegateHolder)block).getPropertyDelegate(); + } + BlockEntity be = world.getBlockEntity(pos); + if (be!=null && be instanceof PropertyDelegateHolder) { + return ((PropertyDelegateHolder)be).getPropertyDelegate(); + } + + return new ArrayPropertyDelegate(0); + }).orElse(new ArrayPropertyDelegate(0)); + } + + //extends ScreenHandler { + @Override + public boolean canUse(PlayerEntity entity) { + return (blockInventory!=null) ? blockInventory.canPlayerUseInv(entity) : true; + } + //} + + @Override + public boolean isFocused(WWidget widget) { + return focus == widget; + } + + @Override + public WWidget getFocus() { + return focus; + } + + @Override + public void requestFocus(WWidget widget) { + //TODO: Are there circumstances where focus can't be stolen? + if (focus==widget) return; //Nothing happens if we're already focused + if (!widget.canFocus()) return; //This is kind of a gotcha but needs to happen + if (focus!=null) focus.onFocusLost(); + focus = widget; + focus.onFocusGained(); + } + + @Override + public void releaseFocus(WWidget widget) { + if (focus==widget) { + focus = null; + widget.onFocusLost(); + } + } +} 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 83c1837..a9597dc 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 @@ -4,7 +4,7 @@ import net.minecraft.client.gui.screen.ingame.HandledScreen; import net.minecraft.client.render.DiffuseLighting; import org.lwjgl.glfw.GLFW; -import io.github.cottonmc.cotton.gui.CottonCraftingController; +import io.github.cottonmc.cotton.gui.CottonInventoryController; import io.github.cottonmc.cotton.gui.widget.WPanel; import io.github.cottonmc.cotton.gui.widget.WWidget; import net.minecraft.client.MinecraftClient; -- cgit From eacd3d2b110d3e3536ca50df489490b30484f8d7 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Fri, 8 May 2020 19:09:36 +0300 Subject: Remove deprecated addInformation --- .../io/github/cottonmc/cotton/gui/widget/WWidget.java | 16 ---------------- 1 file changed, 16 deletions(-) (limited to 'src/main') 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 a2242b2..91d4a98 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 @@ -9,7 +9,6 @@ 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; @@ -313,12 +312,6 @@ public class WWidget { List info = new ArrayList<>(); addTooltip(info); - List stringInfo = new ArrayList<>(); - addInformation(stringInfo); - for (String line : stringInfo) { - info.add(new LiteralText(line)); - } - if (info.size() == 0) return; @@ -333,15 +326,6 @@ public class WWidget { public void validate(GuiDescription host) { //valid = true; } - - /** - * 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 information) { - } /** * Adds lines to this widget's tooltip. If the lines remain empty after this call, no tooltip will be drawn. -- cgit From 040dd6f14eacad8abd903911c0f36ddbd0bf5846 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Fri, 8 May 2020 19:20:30 +0300 Subject: Use non-deprecated ModMenu API --- .../io/github/cottonmc/cotton/gui/client/modmenu/ModMenuSupport.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/main') diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ModMenuSupport.java b/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ModMenuSupport.java index aa69c53..2ab2a47 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ModMenuSupport.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ModMenuSupport.java @@ -1,11 +1,10 @@ package io.github.cottonmc.cotton.gui.client.modmenu; -import java.util.function.Function; import io.github.cottonmc.cotton.gui.client.CottonClientScreen; import io.github.cottonmc.cotton.gui.client.LibGuiClient; +import io.github.prospector.modmenu.api.ConfigScreenFactory; import io.github.prospector.modmenu.api.ModMenuApi; -import net.minecraft.client.gui.screen.Screen; import net.minecraft.text.TranslatableText; public class ModMenuSupport implements ModMenuApi { @@ -16,7 +15,7 @@ public class ModMenuSupport implements ModMenuApi { } @Override - public Function getConfigScreenFactory() { + public ConfigScreenFactory getModConfigScreenFactory() { return screen -> new CottonClientScreen(new TranslatableText("options.libgui.libgui_settings"), new ConfigGui(screen)) { public void onClose() { this.client.openScreen(screen); -- cgit From d5592ae7ab7ee624f594098f99b2f9af964abab8 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Fri, 8 May 2020 19:23:03 +0300 Subject: Painting changes - paintBackground -> paint, removed paintForeground. Closes #42. - paint takes the MatrixStack as well. Closes #52. - Renamed WTextField.renderButton to renderTextField and made it protected. --- .../cotton/gui/CottonInventoryController.java | 2 +- .../cotton/gui/client/CottonClientScreen.java | 19 ++-- .../cottonmc/cotton/gui/client/CottonHud.java | 3 +- .../cotton/gui/client/CottonInventoryScreen.java | 25 +++-- .../cottonmc/cotton/gui/client/ScreenDrawing.java | 105 +++++++++------------ .../cotton/gui/client/TextHoverRendererScreen.java | 3 +- .../cotton/gui/client/modmenu/WKirbSprite.java | 3 +- .../io/github/cottonmc/cotton/gui/widget/WBar.java | 3 +- .../github/cottonmc/cotton/gui/widget/WButton.java | 5 +- .../cottonmc/cotton/gui/widget/WClippedPanel.java | 5 +- .../cottonmc/cotton/gui/widget/WDynamicLabel.java | 5 +- .../github/cottonmc/cotton/gui/widget/WItem.java | 3 +- .../cottonmc/cotton/gui/widget/WItemSlot.java | 3 +- .../github/cottonmc/cotton/gui/widget/WLabel.java | 8 +- .../cottonmc/cotton/gui/widget/WLabeledSlider.java | 5 +- .../cottonmc/cotton/gui/widget/WListPanel.java | 5 +- .../github/cottonmc/cotton/gui/widget/WPanel.java | 14 +-- .../cottonmc/cotton/gui/widget/WScrollBar.java | 3 +- .../github/cottonmc/cotton/gui/widget/WSlider.java | 3 +- .../github/cottonmc/cotton/gui/widget/WSprite.java | 3 +- .../github/cottonmc/cotton/gui/widget/WText.java | 8 +- .../cottonmc/cotton/gui/widget/WTextField.java | 15 +-- .../cottonmc/cotton/gui/widget/WToggleButton.java | 6 +- .../github/cottonmc/cotton/gui/widget/WWidget.java | 72 +++++++------- 24 files changed, 153 insertions(+), 173 deletions(-) (limited to 'src/main') 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 403b5c0..2721ff3 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java @@ -374,7 +374,7 @@ public class CottonInventoryController extends ScreenHandler implements GuiDescr //extends ScreenHandler { @Override public boolean canUse(PlayerEntity entity) { - return (blockInventory!=null) ? blockInventory.canPlayerUseInv(entity) : true; + return (blockInventory!=null) ? blockInventory.canPlayerUse(entity) : true; } //} 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 05f3b68..968cbba 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 @@ -55,36 +55,33 @@ public class CottonClientScreen extends Screen implements TextHoverRendererScree } } - public void paint(int mouseX, int mouseY) { - super.renderBackground(ScreenDrawing.matrices); + public void paint(MatrixStack matrices, int mouseX, int mouseY) { + super.renderBackground(matrices); if (description!=null) { WPanel root = description.getRootPanel(); if (root!=null) { - root.paintBackground(left, top, mouseX-left, mouseY-top); + root.paint(matrices, left, top, mouseX-left, mouseY-top); } } if (getTitle() != null) { - textRenderer.method_27528(ScreenDrawing.matrices, getTitle(), left, top, description.getTitleColor()); + textRenderer.method_27528(matrices, getTitle(), left, top, description.getTitleColor()); } } @SuppressWarnings("deprecation") @Override public void render(MatrixStack matrices, int mouseX, int mouseY, float partialTicks) { - ScreenDrawing.matrices = matrices; - paint(mouseX, mouseY); + paint(matrices, mouseX, mouseY); super.render(matrices, mouseX, mouseY, partialTicks); if (description!=null) { WPanel root = description.getRootPanel(); if (root!=null) { - root.paintForeground(left, top, mouseX, mouseY); - WWidget hitChild = root.hit(mouseX-left, mouseY-top); - if (hitChild!=null) hitChild.renderTooltip(left, top, mouseX-left, mouseY-top); + if (hitChild!=null) hitChild.renderTooltip(matrices, left, top, mouseX-left, mouseY-top); } } } @@ -223,7 +220,7 @@ public class CottonClientScreen extends Screen implements TextHoverRendererScree //} @Override - public void renderTextHover(Text text, int x, int y) { - renderTextHoverEffect(ScreenDrawing.matrices, text, x, y); + public void renderTextHover(MatrixStack matrices, Text text, int x, int y) { + renderTextHoverEffect(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 0e6fa4f..ae0f232 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 @@ -106,7 +106,6 @@ public enum CottonHud implements HudRenderCallback { @Override public void onHudRender(MatrixStack matrices, float tickDelta) { - ScreenDrawing.matrices = matrices; Window window = MinecraftClient.getInstance().getWindow(); int hudWidth = window.getScaledWidth(); int hudHeight = window.getScaledHeight(); @@ -116,7 +115,7 @@ public enum CottonHud implements HudRenderCallback { positioner.reposition(widget, hudWidth, hudHeight); } - widget.paintBackground(widget.getX(), widget.getY(), -1, -1); + widget.paint(matrices, widget.getX(), widget.getY(), -1, -1); } } 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 c64ffa2..a018173 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 @@ -1,11 +1,11 @@ package io.github.cottonmc.cotton.gui.client; +import io.github.cottonmc.cotton.gui.CottonInventoryController; 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; import io.github.cottonmc.cotton.gui.widget.WPanel; import io.github.cottonmc.cotton.gui.widget.WWidget; import net.minecraft.client.MinecraftClient; @@ -13,8 +13,8 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.text.LiteralText; import net.minecraft.text.Text; -public class CottonInventoryScreen extends HandledScreen implements TextHoverRendererScreen { - protected CottonCraftingController description; +public class CottonInventoryScreen extends HandledScreen implements TextHoverRendererScreen { + protected CottonInventoryController description; public static final int PADDING = 8; protected WWidget lastResponder = null; protected WWidget focus = null; @@ -187,26 +187,25 @@ public class CottonInventoryScreen extends H @Override 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(ScreenDrawing.matrices); + public void paint(MatrixStack matrices, int mouseX, int mouseY) { + super.renderBackground(matrices); if (description!=null) { WPanel root = description.getRootPanel(); if (root!=null) { - root.paintBackground(x, y, mouseX-x, mouseY-y); + root.paint(matrices, x, y, mouseX-x, mouseY-y); } } if (getTitle() != null) { - textRenderer.method_27528(ScreenDrawing.matrices, getTitle(), x, y, description.getTitleColor()); + textRenderer.method_27528(matrices, getTitle(), x, y, description.getTitleColor()); } } @SuppressWarnings("deprecation") @Override public void render(MatrixStack matrices, int mouseX, int mouseY, float partialTicks) { - ScreenDrawing.matrices = matrices; - paint(mouseX, mouseY); + paint(matrices, mouseX, mouseY); super.render(matrices, mouseX, mouseY, partialTicks); DiffuseLighting.disable(); //Needed because super.render leaves dirty state @@ -214,10 +213,8 @@ public class CottonInventoryScreen extends H if (description!=null) { WPanel root = description.getRootPanel(); if (root!=null) { - root.paintForeground(x, y, mouseX, mouseY); - WWidget hitChild = root.hit(mouseX-x, mouseY-y); - if (hitChild!=null) hitChild.renderTooltip(x, y, mouseX-x, mouseY-y); + if (hitChild!=null) hitChild.renderTooltip(matrices, x, y, mouseX-x, mouseY-y); } } @@ -236,7 +233,7 @@ public class CottonInventoryScreen extends H } @Override - public void renderTextHover(Text text, int x, int y) { - renderTextHoverEffect(ScreenDrawing.matrices, text, x, y); + public void renderTextHover(MatrixStack matrices, Text text, int x, int y) { + renderTextHoverEffect(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 199909f..4588a51 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 @@ -19,22 +19,8 @@ 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. * @@ -301,14 +287,15 @@ public class ScreenDrawing { /** * Draws a string with a custom alignment. * - * @param s the string - * @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 + * @param matrices the rendering matrix stack + * @param s the string + * @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 drawString(String s, Alignment align, int x, int y, int width, int color) { + public static void drawString(MatrixStack matrices, String s, Alignment align, int x, int y, int width, int color) { switch(align) { case LEFT: { MinecraftClient.getInstance().textRenderer.draw(matrices, s, x, y, color); @@ -332,15 +319,16 @@ public class ScreenDrawing { /** * 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 + * @param matrices the rendering matrix stack + * @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) { + 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); @@ -364,14 +352,15 @@ public class ScreenDrawing { /** * Draws a shadowed string. * - * @param s the string - * @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 + * @param matrices the rendering matrix stack + * @param s the string + * @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(String s, Alignment align, int x, int y, int width, int color) { + public static void drawStringWithShadow(MatrixStack matrices, String s, Alignment align, int x, int y, int width, int color) { switch(align) { case LEFT: { MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, s, x, y, color); @@ -395,14 +384,15 @@ public class ScreenDrawing { /** * 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 + * @param matrices the rendering matrix stack + * @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) { + 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); @@ -426,36 +416,29 @@ public class ScreenDrawing { /** * Draws a left-aligned string. * - * @param s the string - * @param x the X position - * @param y the Y position - * @param color the text color + * @param matrices the rendering matrix stack + * @param s the string + * @param x the X position + * @param y the Y position + * @param color the text color */ - public static void drawString(String s, int x, int y, int color) { + public static void drawString(MatrixStack matrices, String s, int x, int y, int 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 + * @param matrices the rendering matrix stack + * @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) { + public static void drawString(MatrixStack matrices, Text text, int x, int y, int color) { MinecraftClient.getInstance().textRenderer.method_27528(matrices, text, x, y, color); } - /** - * @deprecated for removal; please use {@link #drawStringWithShadow(String, Alignment, int, int, int, int)} - */ - @Deprecated - public static void drawCenteredWithShadow(String s, int x, int y, int 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) { if (opacity<0.0f) opacity=0.0f; if (opacity>1.0f) opacity=1.0f; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/TextHoverRendererScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/TextHoverRendererScreen.java index d09df6f..7c46450 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/TextHoverRendererScreen.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/TextHoverRendererScreen.java @@ -1,10 +1,11 @@ package io.github.cottonmc.cotton.gui.client; +import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.Text; /** * Implemented by LibGui screens to access {@code Screen.renderTextHoverEffect()}. */ public interface TextHoverRendererScreen { - void renderTextHover(Text text, int x, int y); + void renderTextHover(MatrixStack matrices, Text text, int x, int y); } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/WKirbSprite.java b/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/WKirbSprite.java index 52df46c..bc55760 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/WKirbSprite.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/WKirbSprite.java @@ -7,6 +7,7 @@ import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.widget.WWidget; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import net.minecraft.client.util.math.MatrixStack; import net.minecraft.util.Identifier; public class WKirbSprite extends WWidget { @@ -52,7 +53,7 @@ public class WKirbSprite extends WWidget { @Environment(EnvType.CLIENT) @Override - public void paintBackground(int x, int y) { + public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { long now = System.nanoTime() / 1_000_000L; 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 168e540..1884243 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 @@ -6,6 +6,7 @@ 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.util.math.MatrixStack; import net.minecraft.screen.PropertyDelegate; import net.minecraft.text.LiteralText; import net.minecraft.text.Text; @@ -100,7 +101,7 @@ public class WBar extends WWidget { @Environment(EnvType.CLIENT) @Override - public void paintBackground(int x, int y) { + public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { if (bg!=null) { ScreenDrawing.texturedRect(x, y, getWidth(), getHeight(), bg, 0xFFFFFFFF); } else { 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 b3fc425..fc6eb72 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 @@ -7,6 +7,7 @@ import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.widget.AbstractButtonWidget; import net.minecraft.client.sound.PositionedSoundInstance; +import net.minecraft.client.util.math.MatrixStack; import net.minecraft.sound.SoundEvents; import net.minecraft.text.Text; @@ -33,7 +34,7 @@ public class WButton extends WWidget { } @Override - public void paintBackground(int x, int y, int mouseX, int mouseY) { + public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { boolean hovered = (mouseX>=0 && mouseY>=0 && mouseX extends WClippedPanel { } @Override - public void paintBackground(int x, int y, int mouseX, int mouseY) { + public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { if (scrollBar.getValue()!=lastScroll) { layout(); lastScroll = scrollBar.getValue(); } - super.paintBackground(x, y, mouseX, mouseY); + super.paint(matrices, x, y, mouseX, mouseY); /* if (getBackgroundPainter()!=null) { getBackgroundPainter().paintBackground(x, y, this); 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 e7bdeab..fe32718 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 @@ -8,6 +8,7 @@ import io.github.cottonmc.cotton.gui.GuiDescription; import io.github.cottonmc.cotton.gui.client.BackgroundPainter; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import net.minecraft.client.util.math.MatrixStack; /** * Panels are widgets tthat contain other widgets. @@ -180,20 +181,11 @@ public abstract class WPanel extends WWidget { @Environment(EnvType.CLIENT) @Override - public void paintBackground(int x, int y, int mouseX, int mouseY) { + public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { if (backgroundPainter!=null) backgroundPainter.paintBackground(x, y, this); for(WWidget child : children) { - child.paintBackground(x + child.getX(), y + child.getY(), mouseX-child.getX(), mouseY-child.getY()); - } - } - - @Environment(EnvType.CLIENT) - @Override - @Deprecated - public void paintForeground(int x, int y, int mouseX, int mouseY) { - for(WWidget child : children) { - child.paintForeground(x + child.getX(), y + child.getY(), mouseX, mouseY); + child.paint(matrices, x + child.getX(), y + child.getY(), mouseX-child.getX(), mouseY-child.getY()); } } 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 7f2dac3..02805f1 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 @@ -5,6 +5,7 @@ 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; +import net.minecraft.client.util.math.MatrixStack; public class WScrollBar extends WWidget { protected Axis axis = Axis.HORIZONTAL; @@ -32,7 +33,7 @@ public class WScrollBar extends WWidget { } @Override - public void paintBackground(int x, int y, int mouseX, int mouseY) { + public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { if (LibGuiClient.config.darkMode) { ScreenDrawing.drawBeveledPanel(x, y, width, height, 0xFF_212121, 0xFF_2F2F2F, 0xFF_5D5D5D); } else { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java index 2dd3a8e..11acfcd 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java @@ -6,6 +6,7 @@ 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; +import net.minecraft.client.util.math.MatrixStack; import net.minecraft.util.Identifier; import javax.annotation.Nullable; @@ -52,7 +53,7 @@ public class WSlider extends WAbstractSlider { @SuppressWarnings("SuspiciousNameCombination") @Environment(EnvType.CLIENT) @Override - public void paintBackground(int x, int y, int mouseX, int mouseY) { + public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { if (backgroundPainter != null) { backgroundPainter.paintBackground(x, y, this); } else { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WSprite.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSprite.java index 0738875..1c78793 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WSprite.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSprite.java @@ -3,6 +3,7 @@ package io.github.cottonmc.cotton.gui.widget; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import net.minecraft.client.util.math.MatrixStack; import net.minecraft.util.Identifier; public class WSprite extends WWidget { @@ -119,7 +120,7 @@ public class WSprite extends WWidget { @Environment(EnvType.CLIENT) @Override - public void paintBackground(int x, int y) { + public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { if (singleImage) { ScreenDrawing.texturedRect(x, y, getWidth(), getHeight(), frames[0], u1, v1, u2, v2, tint); } else { 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 2052379..094817c 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 @@ -9,7 +9,7 @@ import net.fabricmc.api.Environment; 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.client.util.math.MatrixStack; import net.minecraft.text.Style; import net.minecraft.text.Text; @@ -73,7 +73,7 @@ public class WText extends WWidget { @Environment(EnvType.CLIENT) @Override - public void paintBackground(int x, int y, int mouseX, int mouseY) { + public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { if (wrappedLines == null || wrappingScheduled) { wrapLines(); wrappingScheduled = false; @@ -84,14 +84,14 @@ public class WText extends WWidget { Text line = wrappedLines.get(i); int c = LibGuiClient.config.darkMode ? darkmodeColor : color; - ScreenDrawing.drawString(line, alignment, x, y + i * font.fontHeight, width, c); + ScreenDrawing.drawString(matrices, line, alignment, x, y + i * font.fontHeight, width, c); } Text hoveredText = getTextAt(mouseX, mouseY); if (hoveredText != null) { Screen screen = MinecraftClient.getInstance().currentScreen; if (screen instanceof TextHoverRendererScreen) { - ((TextHoverRendererScreen) screen).renderTextHover(hoveredText, x + mouseX, y + mouseY); + ((TextHoverRendererScreen) screen).renderTextHover(matrices, hoveredText, x + mouseX, y + mouseY); } } } 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 75e7628..d6eadac 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 @@ -5,6 +5,7 @@ import java.util.function.Predicate; import javax.annotation.Nullable; +import net.minecraft.client.util.math.MatrixStack; import org.lwjgl.glfw.GLFW; import org.lwjgl.opengl.GL11; @@ -310,7 +311,7 @@ public class WTextField extends WWidget { }*/ @Environment(EnvType.CLIENT) - public void renderButton(int x, int y) { + protected void renderTextField(MatrixStack matrices, int x, int y) { if (this.font==null) this.font = MinecraftClient.getInstance().textRenderer; int borderColor = (this.isFocused()) ? 0xFF_FFFFA0 : 0xFF_A0A0A0; @@ -347,16 +348,16 @@ public class WTextField extends WWidget { int preCursorAdvance = textX; if (!trimText.isEmpty()) { String string_2 = trimText.substring(0,adjustedCursor); - preCursorAdvance = font.drawWithShadow(ScreenDrawing.getMatrices(), string_2, textX, textY, textColor); + preCursorAdvance = font.drawWithShadow(matrices, string_2, textX, textY, textColor); } if (adjustedCursor= x && mouseX < x+getWidth() && mouseY >= y && mouseY < y+getHeight()) { - // renderTooltip(mouseX, mouseY); - //} + public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { } /** @@ -302,13 +300,13 @@ public class WWidget { public boolean isWithinBounds(int x, int y) { return x>=0 && y>=0 && x info = new ArrayList<>(); addTooltip(info); @@ -316,9 +314,9 @@ public class WWidget { return; Screen screen = MinecraftClient.getInstance().currentScreen; - screen.renderTooltip(ScreenDrawing.getMatrices(), info, tX+x, tY+y); + screen.renderTooltip(matrices, info, tX+x, tY+y); } - + /** * Creates component peers, lays out children, and initializes animation data for this Widget and all its children. * The host container must clear any heavyweight peers from its records before this method is called. @@ -333,7 +331,7 @@ public class WWidget { */ public void addTooltip(List tooltip) { } - + /** * Find the most specific child node at this location. For non-panel widgets, returns this widget. */ -- cgit From 008d6e0462e70164759afcf7e476a3482b0e5556 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Fri, 8 May 2020 19:31:13 +0300 Subject: Replace deprecated RenderSystem matrix usages in widget rendering --- .../java/io/github/cottonmc/cotton/gui/widget/WItem.java | 6 +----- .../github/cottonmc/cotton/gui/widget/WLabeledSlider.java | 15 +++++++++------ 2 files changed, 10 insertions(+), 11 deletions(-) (limited to 'src/main') 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 c7e70ae..c1e07e8 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 @@ -54,17 +54,13 @@ public class WItem extends WWidget { @Environment(EnvType.CLIENT) @Override public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { - RenderSystem.pushMatrix(); RenderSystem.enableDepthTest(); - RenderSystem.translatef(x, y, 0); MinecraftClient mc = MinecraftClient.getInstance(); ItemRenderer renderer = mc.getItemRenderer(); renderer.zOffset = 100f; - renderer.renderGuiItem(mc.player, items.get(current), getWidth() / 2 - 9, getHeight() / 2 - 9); + renderer.renderGuiItem(mc.player, items.get(current), x + getWidth() / 2 - 9, y + getHeight() / 2 - 9); renderer.zOffset = 0f; - - RenderSystem.popMatrix(); } /** 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 4b5617c..3d14674 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 @@ -1,6 +1,5 @@ package io.github.cottonmc.cotton.gui.widget; -import com.mojang.blaze3d.systems.RenderSystem; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.widget.data.Alignment; import io.github.cottonmc.cotton.gui.widget.data.Axis; @@ -8,7 +7,9 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.gui.widget.AbstractButtonWidget; import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.client.util.math.Vector3f; import net.minecraft.text.Text; +import net.minecraft.util.math.Quaternion; import javax.annotation.Nullable; @@ -22,6 +23,8 @@ import javax.annotation.Nullable; * @see WAbstractSlider for more information about listeners */ public class WLabeledSlider extends WAbstractSlider { + private static final Quaternion ROTATION_Z_270 = Vector3f.POSITIVE_X.getDegreesQuaternion(270); + @Nullable private Text label = null; @Nullable private LabelUpdater labelUpdater = null; private Alignment labelAlignment = Alignment.CENTER; @@ -163,11 +166,11 @@ public class WLabeledSlider extends WAbstractSlider { int rotMouseX = axis == Axis.HORIZONTAL ? mouseX : (height - mouseY); int rotMouseY = axis == Axis.HORIZONTAL ? mouseY : mouseX; - RenderSystem.pushMatrix(); - RenderSystem.translatef(x, y, 0); + matrices.push(); + matrices.translate(x, y, 0); if (axis == Axis.VERTICAL) { - RenderSystem.translatef(0, height, 0); - RenderSystem.rotatef(270, 0, 0, 1); + matrices.translate(0, height, 0); + matrices.multiply(ROTATION_Z_270); } drawButton(0, 0, 0, aWidth); @@ -190,7 +193,7 @@ public class WLabeledSlider extends WAbstractSlider { int color = isMouseInsideBounds(mouseX, mouseY) ? 0xFFFFA0 : 0xE0E0E0; ScreenDrawing.drawStringWithShadow(matrices, label, labelAlignment, 2, aHeight / 2 - 4, aWidth - 4, color); } - RenderSystem.popMatrix(); + matrices.pop(); } // state = 1: regular, 2: hovered, 0: disabled/dragging -- cgit From 569e006c737f66c7eed32952226ba4e63000a01a Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Fri, 8 May 2020 19:32:56 +0300 Subject: Remove all deprecated members --- .../cottonmc/cotton/gui/widget/WClippedPanel.java | 13 --------- .../cottonmc/cotton/gui/widget/WListPanel.java | 8 ----- .../github/cottonmc/cotton/gui/widget/WSlider.java | 5 ---- .../cottonmc/cotton/gui/widget/WToggleButton.java | 34 ---------------------- 4 files changed, 60 deletions(-) (limited to 'src/main') diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WClippedPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WClippedPanel.java index cf72c5b..ad93259 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WClippedPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WClippedPanel.java @@ -4,24 +4,11 @@ import net.minecraft.client.util.math.MatrixStack; import org.lwjgl.opengl.GL11; import net.minecraft.client.MinecraftClient; -import net.minecraft.util.Identifier; /** * A panel that is clipped to only render widgets inside its bounds. */ public class WClippedPanel extends WPanel { - @Deprecated - protected Identifier mask; - - /** - * @deprecated {@code WClippedPanel} does not support clipping masks anymore. - */ - @Deprecated - public WClippedPanel setClippingMask(Identifier mask) { - this.mask = mask; - return this; - } - @Override public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { if (getBackgroundPainter()!=null) getBackgroundPainter().paintBackground(x, y, this); 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 a61371a..1d357c0 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 @@ -61,14 +61,6 @@ public class WListPanel extends WClippedPanel { scrollBar.setMaxValue(data.size()); scrollBar.setParent(this); } - - /** - * @deprecated Use {@link #WListPanel(List, Supplier, BiConsumer)} instead. - */ - @Deprecated - public WListPanel(List data, Class listItemClass, Supplier supplier, BiConsumer configurator) { - this(data, supplier, configurator); - } @Override public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java index 11acfcd..f43736f 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java @@ -30,11 +30,6 @@ public class WSlider extends WAbstractSlider { super(min, max, axis); } - @Deprecated - public WSlider(int max, Axis axis) { - this(0, max, axis); - } - @Override protected int getThumbWidth() { return THUMB_SIZE; 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 b5d58ef..87a9265 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 @@ -54,22 +54,6 @@ public class WToggleButton extends WWidget { this.label = label; } - /** - * @deprecated Use {@link #WToggleButton(Identifier, Identifier)} instead. - */ - @Deprecated - public WToggleButton(Identifier onImage, Identifier offImage, int width, int height) { - this(onImage, offImage); - } - - /** - * @deprecated Use {@link #WToggleButton(Identifier, Identifier, Text)} instead. - */ - @Deprecated - public WToggleButton(Text label, Identifier onImage, Identifier offImage, int width, int height) { - this(onImage, offImage, label); - } - @Environment(EnvType.CLIENT) @Override public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { @@ -105,16 +89,6 @@ public class WToggleButton extends WWidget { public boolean getToggle() { return this.isOn; } public void setToggle(boolean on) { this.isOn = on; } - /** - * Set on toggle handler - * - * @deprecated Use {@link #setOnToggle(Consumer)} - */ - @Deprecated - public void setOnToggle(Runnable r) { - this.onToggle = on -> r.run(); - } - @Nullable public Consumer getOnToggle() { return this.onToggle; @@ -135,14 +109,6 @@ public class WToggleButton extends WWidget { return this; } - /** - * @deprecated Use {@link #setColor} instead. - */ - @Deprecated - public WToggleButton color(int light, int dark) { - return setColor(light, dark); - } - public WToggleButton setColor(int light, int dark) { this.color = light; this.darkmodeColor = dark; -- cgit From a1dff63bca844c70819ffd5b9cce3a7b74eb7df4 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Fri, 8 May 2020 19:36:38 +0300 Subject: Remove deprecated usages of color4f from ScreenDrawing --- .../cottonmc/cotton/gui/client/ScreenDrawing.java | 23 ++++++++++------------ 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'src/main') 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 4588a51..786f2ee 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 @@ -9,7 +9,6 @@ import com.mojang.blaze3d.systems.RenderSystem; import io.github.cottonmc.cotton.gui.widget.data.Alignment; import net.minecraft.client.MinecraftClient; -import net.minecraft.client.font.TextRenderer; import net.minecraft.client.render.BufferBuilder; import net.minecraft.client.render.Tessellator; import net.minecraft.client.render.VertexFormats; @@ -65,12 +64,11 @@ public class ScreenDrawing { RenderSystem.enableBlend(); //GlStateManager.disableTexture2D(); RenderSystem.blendFuncSeparate(GlStateManager.SrcFactor.SRC_ALPHA, GlStateManager.DstFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SrcFactor.ONE, GlStateManager.DstFactor.ZERO); - RenderSystem.color4f(r, g, b, 1.0f); - buffer.begin(GL11.GL_QUADS, VertexFormats.POSITION_TEXTURE); //I thought GL_QUADS was deprecated but okay, sure. - buffer.vertex(x, y + height, 0).texture(u1, v2).next(); - buffer.vertex(x + width, y + height, 0).texture(u2, v2).next(); - buffer.vertex(x + width, y, 0).texture(u2, v1).next(); - buffer.vertex(x, y, 0).texture(u1, v1).next(); + buffer.begin(GL11.GL_QUADS, VertexFormats.POSITION_COLOR_TEXTURE); //I thought GL_QUADS was deprecated but okay, sure. + buffer.vertex(x, y + height, 0).color(r, g, b, 1.0f).texture(u1, v2).next(); + buffer.vertex(x + width, y + height, 0).color(r, g, b, 1.0f).texture(u2, v2).next(); + buffer.vertex(x + width, y, 0).color(r, g, b, 1.0f).texture(u2, v1).next(); + buffer.vertex(x, y, 0).color(r, g, b, 1.0f).texture(u1, v1).next(); tessellator.draw(); //GlStateManager.enableTexture2D(); RenderSystem.disableBlend(); @@ -126,12 +124,11 @@ public class ScreenDrawing { RenderSystem.enableBlend(); RenderSystem.disableTexture(); RenderSystem.blendFuncSeparate(GlStateManager.SrcFactor.SRC_ALPHA, GlStateManager.DstFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SrcFactor.ONE, GlStateManager.DstFactor.ZERO); - RenderSystem.color4f(r, g, b, a); - buffer.begin(GL11.GL_QUADS, VertexFormats.POSITION); //I thought GL_QUADS was deprecated but okay, sure. - buffer.vertex(left, top + height, 0.0D).next(); - buffer.vertex(left + width, top + height, 0.0D).next(); - buffer.vertex(left + width, top, 0.0D).next(); - buffer.vertex(left, top, 0.0D).next(); + buffer.begin(GL11.GL_QUADS, VertexFormats.POSITION_COLOR); //I thought GL_QUADS was deprecated but okay, sure. + buffer.vertex(left, top + height, 0.0D).color(r, g, b, a).next(); + buffer.vertex(left + width, top + height, 0.0D).color(r, g, b, a).next(); + buffer.vertex(left + width, top, 0.0D).color(r, g, b, a).next(); + buffer.vertex(left, top, 0.0D).color(r, g, b, a).next(); tessellator.draw(); RenderSystem.enableTexture(); RenderSystem.disableBlend(); -- cgit From bedf10d477896b186e557064ad59cd96a673daa2 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Fri, 8 May 2020 20:21:44 +0300 Subject: Make WScrollBar extend WAbstractSlider for better dragging Still needs testing, but I have to fix the test mod first. --- .../cottonmc/cotton/gui/widget/WScrollBar.java | 115 +++++---------------- 1 file changed, 27 insertions(+), 88 deletions(-) (limited to 'src/main') 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 02805f1..27ee960 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,24 +3,16 @@ 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; import net.minecraft.client.util.math.MatrixStack; -public class WScrollBar extends WWidget { - protected Axis axis = Axis.HORIZONTAL; - protected int value; - protected int maxValue = 100; +public class WScrollBar extends WAbstractSlider { protected int window = 16; - - protected int anchor = -1; - protected int anchorValue = -1; - protected boolean sliding = false; /** * Constructs a horizontal scroll bar. */ public WScrollBar() { + super(0, 100, Axis.HORIZONTAL); } /** @@ -29,9 +21,21 @@ public class WScrollBar extends WWidget { * @param axis the axis */ public WScrollBar(Axis axis) { - this.axis = axis; + super(0, 100, axis); } - + + @Override + protected int getThumbWidth() { + return window; + } + + @Override + protected boolean isMouseInsideBounds(int x, int y) { + return axis == Axis.HORIZONTAL + ? (x >= getHandlePosition() + 1 && x <= getHandlePosition() + getHandleSize()) + : (y >= getHandlePosition() + 1 && y <= getHandlePosition() + getHandleSize()); + } + @Override public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { if (LibGuiClient.config.darkMode) { @@ -39,12 +43,12 @@ public class WScrollBar extends WWidget { } else { ScreenDrawing.drawBeveledPanel(x, y, width, height, 0xFF_373737, 0xFF_8B8B8B, 0xFF_FFFFFF); } - if (maxValue<=0) return; + if (getMaxValue()<=0) return; // Handle colors int top, middle, bottom; - if (sliding) { + if (dragging) { if (LibGuiClient.config.darkMode) { top = 0xFF_6C6C6C; middle = 0xFF_2F2F2F; @@ -87,7 +91,7 @@ public class WScrollBar extends WWidget { * Gets the on-axis size of the scrollbar handle in gui pixels */ public int getHandleSize() { - float percentage = (window>=maxValue) ? 1f : window / (float)maxValue; + float percentage = (window>=getMaxValue()) ? 1f : window / (float)getMaxValue(); int bar = (axis==Axis.HORIZONTAL) ? width-2 : height-2; int result = (int)(percentage*bar); if (result<6) result = 6; @@ -102,15 +106,8 @@ public class WScrollBar extends WWidget { return bar-getHandleSize(); } - public int pixelsToValues(int pixels) { - int bar = (axis==Axis.HORIZONTAL) ? width-2 : height-2; - //int bar = getMovableDistance(); - float percent = pixels / (float)bar; - return (int)(percent*(maxValue-window)); - } - public int getHandlePosition() { - float percent = value / (float)Math.max(maxValue-window, 1); + float percent = value / (float)Math.max(getMaxValue()-window, 1); return (int)(percent * getMovableDistance()); } @@ -119,75 +116,17 @@ public class WScrollBar extends WWidget { * window size */ public int getMaxScrollValue() { - return maxValue - window; - } - - protected void adjustSlider(int x, int y) { - - int delta = 0; - if (axis==Axis.HORIZONTAL) { - delta = x-anchor; - } else { - delta = y-anchor; - } - - int valueDelta = pixelsToValues(delta); - int valueNew = anchorValue + valueDelta; - - if (valueNew>getMaxScrollValue()) valueNew = getMaxScrollValue(); - if (valueNew<0) valueNew = 0; - this.value = valueNew; - } - - @Override - public WWidget onMouseDown(int x, int y, int button) { - //TODO: Clicking before or after the handle should jump instead of scrolling - - if (axis==Axis.HORIZONTAL) { - anchor = x; - anchorValue = value; - } else { - anchor = y; - anchorValue = value; - } - sliding = true; - return this; + return getMaxValue() - window; } - @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 - anchor = -1; - anchorValue = -1; - sliding = false; - return this; - } - - public int getValue() { - return value; - } - - public WScrollBar setValue(int value) { - this.value = value; + public void setValue(int value) { + super.setValue(value); checkValue(); - return this; - } - - public int getMaxValue() { - return maxValue; } - public WScrollBar setMaxValue(int max) { - this.maxValue = max; + public void setMaxValue(int max) { + super.setMaxValue(max); checkValue(); - return this; } public int getWindow() { @@ -204,8 +143,8 @@ public class WScrollBar extends WWidget { * and adjusts it if needed. */ protected void checkValue() { - if (this.value>maxValue-window) { - this.value = maxValue-window; + if (this.value>getMaxValue()-window) { + this.value = getMaxValue()-window; } if (this.value<0) this.value = 0; } -- cgit From 5f415d444efd70fb5747395fe913ace3d89e33cb Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Fri, 8 May 2020 20:46:20 +0300 Subject: Add slider directions, fix scroll bars not scrolling properly --- .../cotton/gui/widget/WAbstractSlider.java | 64 +++++++++++++++++++++- .../cottonmc/cotton/gui/widget/WLabeledSlider.java | 4 +- .../cottonmc/cotton/gui/widget/WScrollBar.java | 4 ++ .../github/cottonmc/cotton/gui/widget/WSlider.java | 8 ++- 4 files changed, 76 insertions(+), 4 deletions(-) (limited to 'src/main') 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 b5e1344..7a2ccec 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 @@ -34,6 +34,7 @@ public abstract class WAbstractSlider extends WWidget { protected int min, max; protected final Axis axis; + protected Direction direction; protected int value; @@ -71,6 +72,7 @@ public abstract class WAbstractSlider extends WWidget { this.max = max; this.axis = axis; this.value = min; + this.direction = (axis == Axis.HORIZONTAL) ? Direction.RIGHT : Direction.UP; } /** @@ -132,7 +134,25 @@ public abstract class WAbstractSlider extends WWidget { } private void moveSlider(int x, int y) { - int pos = (axis == Axis.VERTICAL ? (height - y) : x) - getThumbWidth() / 2; + int axisPos; + + switch (direction) { + case UP: + axisPos = height - y; + break; + case DOWN: + axisPos = y; + break; + case LEFT: + axisPos = x; + break; + case RIGHT: + default: + axisPos = width - x; + break; + } + + int pos = axisPos - getThumbWidth() / 2; int rawValue = min + Math.round(valueToCoordRatio * pos); int previousValue = value; value = MathHelper.clamp(rawValue, min, max); @@ -246,6 +266,31 @@ public abstract class WAbstractSlider extends WWidget { return axis; } + /** + * Gets the direction of this slider. + * + * @return the direction + * @since 2.0.0 + */ + public Direction getDirection() { + return direction; + } + + /** + * Sets the direction of this slider. + * + * @param direction the new direction + * @throws IllegalArgumentException if the {@linkplain Direction#getAxis() direction axis} is not equal to {@link #axis}. + * @since 2.0.0 + */ + public void setDirection(Direction direction) { + if (direction.getAxis() != axis) { + throw new IllegalArgumentException("Incorrect axis: " + axis); + } + + this.direction = direction; + } + protected void onValueChanged(int value) { if (valueChangeListener != null) valueChangeListener.accept(value); } @@ -294,4 +339,21 @@ public abstract class WAbstractSlider extends WWidget { private static boolean isIncreasingKey(int ch) { return ch == GLFW.GLFW_KEY_RIGHT || ch == GLFW.GLFW_KEY_UP; } + + public enum Direction { + UP(Axis.VERTICAL), + DOWN(Axis.VERTICAL), + LEFT(Axis.HORIZONTAL), + RIGHT(Axis.HORIZONTAL); + + private final Axis axis; + + Direction(Axis axis) { + this.axis = axis; + } + + public Axis getAxis() { + return axis; + } + } } 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 3d14674..2ff9f68 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 @@ -163,7 +163,9 @@ public class WLabeledSlider extends WAbstractSlider { public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { int aWidth = axis == Axis.HORIZONTAL ? width : height; int aHeight = axis == Axis.HORIZONTAL ? height : width; - int rotMouseX = axis == Axis.HORIZONTAL ? mouseX : (height - mouseY); + int rotMouseX = axis == Axis.HORIZONTAL + ? (direction == Direction.RIGHT ? width - mouseX : mouseX) + : (direction == Direction.UP ? height - mouseY : mouseY); int rotMouseY = axis == Axis.HORIZONTAL ? mouseY : mouseX; matrices.push(); 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 27ee960..2010a08 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 @@ -22,6 +22,10 @@ public class WScrollBar extends WAbstractSlider { */ public WScrollBar(Axis axis) { super(0, 100, axis); + + if (axis == Axis.VERTICAL) { + setDirection(Direction.DOWN); + } } @Override diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java index f43736f..630f23a 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java @@ -62,7 +62,9 @@ public class WSlider extends WAbstractSlider { if (axis == Axis.VERTICAL) { int trackX = x + width / 2 - TRACK_WIDTH / 2; thumbX = width / 2 - THUMB_SIZE / 2; - thumbY = height - THUMB_SIZE + 1 - (int) (coordToValueRatio * (value - min)); + thumbY = direction == Direction.UP + ? (height - THUMB_SIZE) + 1 - (int) (coordToValueRatio * (value - min)) + : Math.round(coordToValueRatio * (value - min)); thumbXOffset = 0; ScreenDrawing.texturedRect(trackX, y + 1, TRACK_WIDTH, 1, texture, 16*px, 0*px, 22*px, 1*px, 0xFFFFFFFF); @@ -70,7 +72,9 @@ public class WSlider extends WAbstractSlider { ScreenDrawing.texturedRect(trackX, y + height, TRACK_WIDTH, 1, texture, 16*px, 2*px, 22*px, 3*px, 0xFFFFFFFF); } else { int trackY = y + height / 2 - TRACK_WIDTH / 2; - thumbX = Math.round(coordToValueRatio * (value - min)); + thumbX = direction == Direction.RIGHT + ? (width - THUMB_SIZE) + 1 - (int) (coordToValueRatio * (value - min)) + : Math.round(coordToValueRatio * (value - min)); thumbY = height / 2 - THUMB_SIZE / 2; thumbXOffset = 8; -- cgit From cca884e8beb6e0f92c09b2d238b65d26a4b1887f Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Fri, 8 May 2020 20:47:53 +0300 Subject: Fix right-facing horizontal sliders (default) --- src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java | 2 +- src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java | 2 +- src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/main') 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 7a2ccec..d864b3b 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 @@ -72,7 +72,7 @@ public abstract class WAbstractSlider extends WWidget { this.max = max; this.axis = axis; this.value = min; - this.direction = (axis == Axis.HORIZONTAL) ? Direction.RIGHT : Direction.UP; + this.direction = (axis == Axis.HORIZONTAL) ? Direction.LEFT : Direction.UP; } /** 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 2ff9f68..77e51ce 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 @@ -164,7 +164,7 @@ public class WLabeledSlider extends WAbstractSlider { int aWidth = axis == Axis.HORIZONTAL ? width : height; int aHeight = axis == Axis.HORIZONTAL ? height : width; int rotMouseX = axis == Axis.HORIZONTAL - ? (direction == Direction.RIGHT ? width - mouseX : mouseX) + ? (direction == Direction.LEFT ? width - mouseX : mouseX) : (direction == Direction.UP ? height - mouseY : mouseY); int rotMouseY = axis == Axis.HORIZONTAL ? mouseY : mouseX; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java index 630f23a..5934997 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java @@ -72,7 +72,7 @@ public class WSlider extends WAbstractSlider { ScreenDrawing.texturedRect(trackX, y + height, TRACK_WIDTH, 1, texture, 16*px, 2*px, 22*px, 3*px, 0xFFFFFFFF); } else { int trackY = y + height / 2 - TRACK_WIDTH / 2; - thumbX = direction == Direction.RIGHT + thumbX = direction == Direction.LEFT ? (width - THUMB_SIZE) + 1 - (int) (coordToValueRatio * (value - min)) : Math.round(coordToValueRatio * (value - min)); thumbY = height / 2 - THUMB_SIZE / 2; -- cgit From 1046028cffb66f2993ea0dfb72c15b24bccc8995 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Fri, 8 May 2020 21:07:03 +0300 Subject: Sliders now slide properly in inverse directions as well Scrolling scroll bars! --- .../java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/main') 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 d864b3b..c8570fd 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 @@ -170,8 +170,12 @@ public abstract class WAbstractSlider extends WWidget { @Environment(EnvType.CLIENT) @Override public void onMouseScroll(int x, int y, double amount) { + if (direction == Direction.LEFT || direction == Direction.DOWN) { + amount = -amount; + } + int previous = value; - value = MathHelper.clamp(value + (int) (valueToCoordRatio * amount * 2), min, max); + value = MathHelper.clamp(value + (int) Math.signum(amount) * MathHelper.ceil(valueToCoordRatio * Math.abs(amount) * 2), min, max); if (previous != value) { onValueChanged(value); -- cgit From 3642230cc2fa592d6ca6800a1092a53f979c1d95 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Fri, 8 May 2020 21:52:22 +0300 Subject: Fix slider thumbs rendering 1 pixel to the right --- src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main') diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java index 5934997..f68c5bb 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java @@ -73,7 +73,7 @@ public class WSlider extends WAbstractSlider { } else { int trackY = y + height / 2 - TRACK_WIDTH / 2; thumbX = direction == Direction.LEFT - ? (width - THUMB_SIZE) + 1 - (int) (coordToValueRatio * (value - min)) + ? (width - THUMB_SIZE) - (int) (coordToValueRatio * (value - min)) : Math.round(coordToValueRatio * (value - min)); thumbY = height / 2 - THUMB_SIZE / 2; thumbXOffset = 8; -- cgit From 7e61da56c52ebfa00c2fdd036b6dc83d4c0f4e21 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Fri, 8 May 2020 21:52:55 +0300 Subject: Fix left and right being swapped when dragging sliders --- .../java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main') 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 c8570fd..8f93221 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 @@ -144,11 +144,11 @@ public abstract class WAbstractSlider extends WWidget { axisPos = y; break; case LEFT: - axisPos = x; + axisPos = width - x; break; case RIGHT: default: - axisPos = width - x; + axisPos = x; break; } -- cgit From fe0277477fbf0290429c1d593afc7bcc63db3a35 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Fri, 8 May 2020 22:20:06 +0300 Subject: WScrollBar: Fix window --- src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java | 6 ++---- src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) (limited to 'src/main') 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 1d357c0..9416f95 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 @@ -102,9 +102,7 @@ public class WListPanel extends WClippedPanel { this.children.add(scrollBar); scrollBar.setLocation(this.width-scrollBar.getWidth(), 0); scrollBar.setSize(8, this.height); - //scrollBar.window = 6; - scrollBar.setMaxValue(data.size()); - + //super.layout(); //System.out.println("Validating"); @@ -139,7 +137,7 @@ public class WListPanel extends WClippedPanel { //Fix up the scrollbar handle and track metrics scrollBar.setWindow(cellsHigh); - //scrollBar.setMaxValue(data.size()); + scrollBar.setMaxValue(data.size()); int scrollOffset = scrollBar.getValue(); //System.out.println(scrollOffset); 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 2010a08..ff7d5e8 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 @@ -129,7 +129,7 @@ public class WScrollBar extends WAbstractSlider { } public void setMaxValue(int max) { - super.setMaxValue(max); + super.setMaxValue(max - window); checkValue(); } -- cgit From 21c781b162ab649e10c076343cec9dd0f8736deb Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Fri, 8 May 2020 22:47:06 +0300 Subject: Fix slider scrolling with keys --- .../cotton/gui/widget/WAbstractSlider.java | 28 +++++++++++++++------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'src/main') 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 8f93221..3367b1d 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 @@ -336,28 +336,38 @@ public abstract class WAbstractSlider extends WWidget { } } - private static boolean isDecreasingKey(int ch) { - return ch == GLFW.GLFW_KEY_LEFT || ch == GLFW.GLFW_KEY_DOWN; + private boolean isDecreasingKey(int ch) { + return direction.isInverted() + ? (ch == GLFW.GLFW_KEY_RIGHT || ch == GLFW.GLFW_KEY_UP) + : (ch == GLFW.GLFW_KEY_LEFT || ch == GLFW.GLFW_KEY_DOWN); } - private static boolean isIncreasingKey(int ch) { - return ch == GLFW.GLFW_KEY_RIGHT || ch == GLFW.GLFW_KEY_UP; + private boolean isIncreasingKey(int ch) { + return direction.isInverted() + ? (ch == GLFW.GLFW_KEY_LEFT || ch == GLFW.GLFW_KEY_DOWN) + : (ch == GLFW.GLFW_KEY_RIGHT || ch == GLFW.GLFW_KEY_UP); } public enum Direction { - UP(Axis.VERTICAL), - DOWN(Axis.VERTICAL), - LEFT(Axis.HORIZONTAL), - RIGHT(Axis.HORIZONTAL); + UP(Axis.VERTICAL, false), + DOWN(Axis.VERTICAL, true), + LEFT(Axis.HORIZONTAL, true), + RIGHT(Axis.HORIZONTAL, false); private final Axis axis; + private final boolean inverted; - Direction(Axis axis) { + Direction(Axis axis, boolean inverted) { this.axis = axis; + this.inverted = inverted; } public Axis getAxis() { return axis; } + + public boolean isInverted() { + return inverted; + } } } -- cgit From 0060aa9e7db6c26d031c2bfb7f5acfa00378b349 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Fri, 8 May 2020 22:47:25 +0300 Subject: Fix various scroll bar bugs --- .../cottonmc/cotton/gui/widget/WScrollBar.java | 44 +++++++++------------- 1 file changed, 17 insertions(+), 27 deletions(-) (limited to 'src/main') 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 ff7d5e8..c538cda 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 @@ -30,7 +30,7 @@ public class WScrollBar extends WAbstractSlider { @Override protected int getThumbWidth() { - return window; + return Math.round(window * coordToValueRatio); } @Override @@ -86,10 +86,25 @@ public class WScrollBar extends WAbstractSlider { if (axis==Axis.HORIZONTAL) { ScreenDrawing.drawBeveledPanel(x+1+getHandlePosition(), y+1, getHandleSize(), height-2, top, middle, bottom); + + if (isFocused()) { + drawBeveledOutline(x+1+getHandlePosition(), y+1, getHandleSize(), height-2, 0xFF_FFFFA7, 0xFF_C9CA71, 0xFF_8C8F39); + } } else { ScreenDrawing.drawBeveledPanel(x+1, y+1+getHandlePosition(), width-2, getHandleSize(), top, middle, bottom); + + if (isFocused()) { + drawBeveledOutline(x+1, y+1+getHandlePosition(), width-2, getHandleSize(), 0xFF_FFFFA7, 0xFF_C9CA71, 0xFF_8C8F39); + } } } + + private static void drawBeveledOutline(int x, int y, int width, int height, int topleft, int center, int bottomright) { + ScreenDrawing.coloredRect(x, y, width - 1, 1, topleft); //Top shadow + ScreenDrawing.coloredRect(x, y + 1, 1, height - 2, topleft); //Left shadow + ScreenDrawing.coloredRect(x + width - 1, y + 1, 1, height - 1, bottomright); //Right hilight + ScreenDrawing.coloredRect(x + 1, y + height - 1, width - 1, 1, bottomright); //Bottom hilight + } /** * Gets the on-axis size of the scrollbar handle in gui pixels @@ -111,26 +126,12 @@ public class WScrollBar extends WAbstractSlider { } public int getHandlePosition() { - float percent = value / (float)Math.max(getMaxValue()-window, 1); + float percent = value / (float)Math.max(getMaxValue(), 1); return (int)(percent * getMovableDistance()); } - - /** - * Gets the maximum scroll value achievable; this will typically be the maximum value minus the - * window size - */ - public int getMaxScrollValue() { - return getMaxValue() - window; - } - - public void setValue(int value) { - super.setValue(value); - checkValue(); - } public void setMaxValue(int max) { super.setMaxValue(max - window); - checkValue(); } public int getWindow() { @@ -141,15 +142,4 @@ public class WScrollBar extends WAbstractSlider { this.window = window; return this; } - - /** - * Checks that the current value is in the correct range - * and adjusts it if needed. - */ - protected void checkValue() { - if (this.value>getMaxValue()-window) { - this.value = getMaxValue()-window; - } - if (this.value<0) this.value = 0; - } } -- cgit