diff options
| author | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2020-05-09 14:01:58 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-09 14:01:58 +0300 |
| commit | b3d673df2fa83bc092ec2ba37eb936a4255b1b59 (patch) | |
| tree | 4bbf521a83d1564c6179a2d5343dce6d719e6286 /src | |
| parent | 9743d57ec08b0488e2c7152615d54dd813a1bd3f (diff) | |
| parent | 0060aa9e7db6c26d031c2bfb7f5acfa00378b349 (diff) | |
| download | LibGui-b3d673df2fa83bc092ec2ba37eb936a4255b1b59.tar.gz LibGui-b3d673df2fa83bc092ec2ba37eb936a4255b1b59.tar.bz2 LibGui-b3d673df2fa83bc092ec2ba37eb936a4255b1b59.zip | |
Merge pull request #40 from Juuxel/new-breaking
1.16 breaking changes
Diffstat (limited to 'src')
26 files changed, 316 insertions, 452 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/CottonCraftingController.java b/src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java index 6bc7dcd..2721ff3 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/CottonCraftingController.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java @@ -17,20 +17,15 @@ 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<Inventory> implements GuiDescription { +public class CottonInventoryController extends ScreenHandler implements GuiDescription { protected Inventory blockInventory; protected PlayerInventory playerInventory; - protected RecipeType<?> recipeType; protected World world; protected PropertyDelegate propertyDelegate; @@ -40,20 +35,18 @@ public class CottonCraftingController extends AbstractRecipeScreenHandler<Invent protected WWidget focus; - public CottonCraftingController(RecipeType<?> recipeType, int syncId, PlayerInventory playerInventory) { + public CottonInventoryController(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) { + public CottonInventoryController(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); @@ -67,12 +60,12 @@ public class CottonCraftingController extends AbstractRecipeScreenHandler<Invent return LibGuiClient.config.darkMode ? darkTitleColor : titleColor; } - public CottonCraftingController setRootPanel(WPanel panel) { + public CottonInventoryController setRootPanel(WPanel panel) { this.rootPanel = panel; return this; } - public CottonCraftingController setTitleColor(int color) { + public CottonInventoryController setTitleColor(int color) { this.titleColor = color; return this; } @@ -378,52 +371,11 @@ public class CottonCraftingController extends AbstractRecipeScreenHandler<Invent }).orElse(new ArrayPropertyDelegate(0)); } - //extends CraftingContainer<Inventory> { + //extends ScreenHandler { @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(); + public boolean canUse(PlayerEntity entity) { + return (blockInventory!=null) ? blockInventory.canPlayerUse(entity) : true; } - - @Override - public boolean matches(Recipe<? super Inventory> 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.canPlayerUse(entity) : true; - } - //} //} @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 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 5dca5f1..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.CottonCraftingController; 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<T extends CottonCraftingController> extends HandledScreen<T> implements TextHoverRendererScreen { - protected CottonCraftingController description; +public class CottonInventoryScreen<T extends CottonInventoryController> extends HandledScreen<T> 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<T extends CottonCraftingController> 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<T extends CottonCraftingController> 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<T extends CottonCraftingController> 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..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; @@ -19,23 +18,9 @@ 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 @@ -79,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(); @@ -140,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(); @@ -301,14 +284,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 +316,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 +349,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 +381,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 +413,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/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<Screen, ? extends Screen> getConfigScreenFactory() { + public ConfigScreenFactory<?> getModConfigScreenFactory() { return screen -> new CottonClientScreen(new TranslatableText("options.libgui.libgui_settings"), new ConfigGui(screen)) { public void onClose() { this.client.openScreen(screen); 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/WAbstractSlider.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java index b5e1344..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 @@ -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.LEFT : 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 = width - x; + break; + case RIGHT: + default: + axisPos = x; + break; + } + + int pos = axisPos - getThumbWidth() / 2; int rawValue = min + Math.round(valueToCoordRatio * pos); int previousValue = value; value = MathHelper.clamp(rawValue, min, max); @@ -150,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); @@ -246,6 +270,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); } @@ -287,11 +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, false), + DOWN(Axis.VERTICAL, true), + LEFT(Axis.HORIZONTAL, true), + RIGHT(Axis.HORIZONTAL, false); + + private final Axis axis; |
