From 2a05170b93d9522cc9d54d954a783dd6a7f01c1c Mon Sep 17 00:00:00 2001 From: Juuz <6596629+Juuxel@users.noreply.github.com> Date: Sat, 9 Sep 2023 13:59:24 +0300 Subject: Initial port to 1.20.2 --- .../cotton/gui/client/CottonClientScreen.java | 17 ++++++---- .../cotton/gui/client/CottonInventoryScreen.java | 12 +++---- .../cotton/gui/impl/client/MouseInputHandler.java | 4 +-- .../cotton/gui/impl/client/WidgetTextures.java | 37 ++++++++++++++++++++++ .../impl/mixin/client/PressableWidgetAccessor.java | 15 +++++++++ .../impl/mixin/client/SliderWidgetAccessor.java | 25 +++++++++++++++ .../cotton/gui/widget/WAbstractSlider.java | 6 ++-- .../github/cottonmc/cotton/gui/widget/WButton.java | 33 +++---------------- .../cottonmc/cotton/gui/widget/WLabeledSlider.java | 33 ++++--------------- .../cottonmc/cotton/gui/widget/WListPanel.java | 4 +-- .../cottonmc/cotton/gui/widget/WScrollBar.java | 4 +-- .../cottonmc/cotton/gui/widget/WScrollPanel.java | 13 ++++++-- .../github/cottonmc/cotton/gui/widget/WWidget.java | 9 +++--- .../cotton/gui/widget/data/InputResult.java | 12 +++++++ 14 files changed, 142 insertions(+), 82 deletions(-) create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/impl/client/WidgetTextures.java create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/impl/mixin/client/PressableWidgetAccessor.java create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/impl/mixin/client/SliderWidgetAccessor.java (limited to 'src/main/java/io') 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 877528c..071bb39 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 @@ -123,10 +123,13 @@ public class CottonClientScreen extends Screen implements CottonScreenImpl { } } - private void paint(DrawContext context, int mouseX, int mouseY) { - renderBackground(context); + private void paint(DrawContext context, int mouseX, int mouseY, float delta) { + renderBackground(context, mouseX, mouseY, delta); if (description!=null) { + context.getMatrices().push(); + context.getMatrices().translate(0f, 0f, 0.01f); + WPanel root = description.getRootPanel(); if (root!=null) { GL11.glEnable(GL11.GL_SCISSOR_TEST); @@ -140,12 +143,14 @@ public class CottonClientScreen extends Screen implements CottonScreenImpl { int width = description.getRootPanel().getWidth(); ScreenDrawing.drawString(context, getTitle().asOrderedText(), description.getTitleAlignment(), left + titleX, top + titleY, width - 2 * titleX, description.getTitleColor()); } + + context.getMatrices().pop(); } } @Override public void render(DrawContext context, int mouseX, int mouseY, float partialTicks) { - paint(context, mouseX, mouseY); + paint(context, mouseX, mouseY, partialTicks); super.render(context, mouseX, mouseY, partialTicks); @@ -207,12 +212,12 @@ public class CottonClientScreen extends Screen implements CottonScreenImpl { } @Override - public boolean mouseScrolled(double mouseX, double mouseY, double amount) { - super.mouseScrolled(mouseX, mouseY, amount); + public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) { + super.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount); int containerX = (int)mouseX-left; int containerY = (int)mouseY-top; - mouseInputHandler.onMouseScroll(containerX, containerY, amount); + mouseInputHandler.onMouseScroll(containerX, containerY, horizontalAmount, verticalAmount); return true; } 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 b1ed4ce..3d807e0 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 @@ -223,12 +223,12 @@ public class CottonInventoryScreen extends Handl } @Override - public boolean mouseScrolled(double mouseX, double mouseY, double amount) { - super.mouseScrolled(mouseX, mouseY, amount); + public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) { + super.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount); int containerX = (int)mouseX-x; int containerY = (int)mouseY-y; - mouseInputHandler.onMouseScroll(containerX, containerY, amount); + mouseInputHandler.onMouseScroll(containerX, containerY, horizontalAmount, verticalAmount); return true; } @@ -275,8 +275,8 @@ public class CottonInventoryScreen extends Handl @Override protected void drawBackground(DrawContext context, float partialTicks, int mouseX, int mouseY) {} //This is just an AbstractContainerScreen thing; most Screens don't work this way. - private void paint(DrawContext context, int mouseX, int mouseY) { - renderBackground(context); + private void paint(DrawContext context, int mouseX, int mouseY, float delta) { + renderBackground(context, mouseX, mouseY, delta); if (description!=null) { WPanel root = description.getRootPanel(); @@ -292,7 +292,7 @@ public class CottonInventoryScreen extends Handl @Override public void render(DrawContext context, int mouseX, int mouseY, float partialTicks) { - paint(context, mouseX, mouseY); + paint(context, mouseX, mouseY, partialTicks); super.render(context, mouseX, mouseY, partialTicks); DiffuseLighting.disableGuiDepthLighting(); //Needed because super.render leaves dirty state diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/client/MouseInputHandler.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/MouseInputHandler.java index 0814e16..15aa875 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/impl/client/MouseInputHandler.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/MouseInputHandler.java @@ -85,10 +85,10 @@ public final class MouseInputHandler { } } - public void onMouseScroll(int containerX, int containerY, double amount) { + public void onMouseScroll(int containerX, int containerY, double horizontalAmount, double verticalAmount) { runTree( screen.getDescription().getRootPanel().hit(containerX, containerY), - widget -> widget.onMouseScroll(containerX - widget.getAbsoluteX(), containerY - widget.getAbsoluteY(), amount) + widget -> widget.onMouseScroll(containerX - widget.getAbsoluteX(), containerY - widget.getAbsoluteY(), horizontalAmount, verticalAmount) ); } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/client/WidgetTextures.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/WidgetTextures.java new file mode 100644 index 0000000..86a2630 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/WidgetTextures.java @@ -0,0 +1,37 @@ +package io.github.cottonmc.cotton.gui.impl.client; + +import io.github.cottonmc.cotton.gui.impl.mixin.client.SliderWidgetAccessor; + +import net.minecraft.client.gui.screen.ButtonTextures; +import net.minecraft.util.Identifier; + +import io.github.cottonmc.cotton.gui.impl.LibGuiCommon; +import io.github.cottonmc.cotton.gui.impl.mixin.client.PressableWidgetAccessor; + +public final class WidgetTextures { + private static final ButtonTextures LIGHT_LABELED_SLIDER_HANDLE = new ButtonTextures( + SliderWidgetAccessor.libgui$getHandleTexture(), + SliderWidgetAccessor.libgui$getHandleHighlightedTexture() + ); + private static final ButtonTextures DARK_LABELED_SLIDER_HANDLE = new ButtonTextures( + id("widget/slider_handle_dark"), + id("widget/slider_handle_highlighted_dark") + ); + private static final ButtonTextures DARK_BUTTON = new ButtonTextures( + id("widget/button_dark"), + id("widget/button_disabled_dark"), + id("widget/button_highlighted_dark") + ); + + public static ButtonTextures getButtonTextures(boolean dark) { + return dark ? DARK_BUTTON : PressableWidgetAccessor.libgui$getTextures(); + } + + public static ButtonTextures getLabeledSliderHandleTextures(boolean dark) { + return dark ? DARK_LABELED_SLIDER_HANDLE : LIGHT_LABELED_SLIDER_HANDLE; + } + + private static Identifier id(String path) { + return new Identifier(LibGuiCommon.MOD_ID, path); + } +} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/mixin/client/PressableWidgetAccessor.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/mixin/client/PressableWidgetAccessor.java new file mode 100644 index 0000000..b984af1 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/mixin/client/PressableWidgetAccessor.java @@ -0,0 +1,15 @@ +package io.github.cottonmc.cotton.gui.impl.mixin.client; + +import net.minecraft.client.gui.screen.ButtonTextures; +import net.minecraft.client.gui.widget.PressableWidget; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(PressableWidget.class) +public interface PressableWidgetAccessor { + @Accessor("TEXTURES") + static ButtonTextures libgui$getTextures() { + throw new AssertionError(); + } +} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/mixin/client/SliderWidgetAccessor.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/mixin/client/SliderWidgetAccessor.java new file mode 100644 index 0000000..ce9ea1a --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/mixin/client/SliderWidgetAccessor.java @@ -0,0 +1,25 @@ +package io.github.cottonmc.cotton.gui.impl.mixin.client; + +import net.minecraft.client.gui.widget.SliderWidget; +import net.minecraft.util.Identifier; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(SliderWidget.class) +public interface SliderWidgetAccessor { + @Accessor("TEXTURE") + static Identifier libgui$getTexture() { + throw new AssertionError(); + } + + @Accessor("HANDLE_TEXTURE") + static Identifier libgui$getHandleTexture() { + throw new AssertionError(); + } + + @Accessor("HANDLE_HIGHLIGHTED_TEXTURE") + static Identifier libgui$getHandleHighlightedTexture() { + throw new AssertionError(); + } +} 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 945c121..3621233 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 @@ -183,13 +183,13 @@ public abstract class WAbstractSlider extends WWidget { @Environment(EnvType.CLIENT) @Override - public InputResult onMouseScroll(int x, int y, double amount) { + public InputResult onMouseScroll(int x, int y, double horizontalAmount, double verticalAmount) { if (direction == Direction.LEFT || direction == Direction.DOWN) { - amount = -amount; + verticalAmount = -verticalAmount; } int previous = value; - value = MathHelper.clamp(value + (int) Math.signum(amount) * MathHelper.ceil(valueToCoordRatio * Math.abs(amount) * 2), min, max); + value = MathHelper.clamp(value + (int) Math.signum(verticalAmount) * MathHelper.ceil(valueToCoordRatio * Math.abs(verticalAmount) * 2), min, max); if (previous != value) { onValueChanged(value); 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 1230905..5367483 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 @@ -4,23 +4,23 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.screen.ButtonTextures; import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; import net.minecraft.client.gui.screen.narration.NarrationPart; import net.minecraft.client.gui.widget.ClickableWidget; import net.minecraft.client.sound.PositionedSoundInstance; import net.minecraft.sound.SoundEvents; import net.minecraft.text.Text; -import net.minecraft.util.Identifier; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.impl.client.NarrationMessages; +import io.github.cottonmc.cotton.gui.impl.client.WidgetTextures; import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; import io.github.cottonmc.cotton.gui.widget.data.InputResult; import io.github.cottonmc.cotton.gui.widget.icon.Icon; import org.jetbrains.annotations.Nullable; public class WButton extends WWidget { - private static final Identifier DARK_WIDGETS_LOCATION = new Identifier("libgui", "textures/widget/dark_widgets.png"); private static final int BUTTON_HEIGHT = 20; private static final int ICON_SPACING = 2; @@ -89,27 +89,9 @@ public class WButton extends WWidget { @Environment(EnvType.CLIENT) @Override public void paint(DrawContext context, int x, int y, int mouseX, int mouseY) { - boolean hovered = (mouseX>=0 && mouseY>=0 && mouseX198) halfWidth=198; - float buttonWidth = halfWidth*px; - float buttonHeight = 20*px; - - float buttonEndLeft = (200-(getWidth()/2)) * px; - - Identifier texture = getTexture(this); - ScreenDrawing.texturedRect(context, x, y, getWidth()/2, 20, texture, buttonLeft, buttonTop, buttonLeft+buttonWidth, buttonTop+buttonHeight, 0xFFFFFFFF); - ScreenDrawing.texturedRect(context, x+(getWidth()/2), y, getWidth()/2, 20, texture, buttonEndLeft, buttonTop, 200*px, buttonTop+buttonHeight, 0xFFFFFFFF); + boolean hovered = isWithinBounds(mouseX, mouseY); + ButtonTextures textures = WidgetTextures.getButtonTextures(shouldRenderInDarkMode()); + context.drawGuiTexture(textures.get(enabled, hovered || isFocused()), x, y, getWidth(), getHeight()); if (icon != null) { icon.paint(context, x+ICON_SPACING, y+(BUTTON_HEIGHT-iconSize)/2, iconSize); @@ -266,9 +248,4 @@ public class WButton extends WWidget { } } } - - @Environment(EnvType.CLIENT) - static Identifier getTexture(WWidget widget) { - return widget.shouldRenderInDarkMode() ? DARK_WIDGETS_LOCATION : ClickableWidget.WIDGETS_TEXTURE; - } } 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 d8cca29..467a634 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,5 +1,8 @@ package io.github.cottonmc.cotton.gui.widget; +import io.github.cottonmc.cotton.gui.impl.client.WidgetTextures; +import io.github.cottonmc.cotton.gui.impl.mixin.client.SliderWidgetAccessor; + import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.gui.DrawContext; @@ -175,22 +178,17 @@ public class WLabeledSlider extends WAbstractSlider { matrices.translate(0, height, 0); matrices.multiply(RotationAxis.POSITIVE_Z.rotationDegrees(270)); } - drawButton(context, 0, 0, 0, aWidth); + context.drawGuiTexture(SliderWidgetAccessor.libgui$getTexture(), 0, 0, aWidth, aHeight); - // 1: regular, 2: hovered, 0: disabled/dragging int thumbX = Math.round(coordToValueRatio * (value - min)); int thumbY = 0; int thumbWidth = getThumbWidth(); int thumbHeight = aHeight; boolean hovering = rotMouseX >= thumbX && rotMouseX <= thumbX + thumbWidth && rotMouseY >= thumbY && rotMouseY <= thumbY + thumbHeight; - int thumbState = dragging || hovering ? 2 : 1; - - drawButton(context, thumbX, thumbY, thumbState, thumbWidth); - if (thumbState == 1 && isFocused()) { - float px = 1 / 32f; - ScreenDrawing.texturedRect(context, thumbX, thumbY, thumbWidth, thumbHeight, WSlider.LIGHT_TEXTURE, 24*px, 0*px, 32*px, 20*px, 0xFFFFFFFF); - } + var thumbTextures = WidgetTextures.getLabeledSliderHandleTextures(shouldRenderInDarkMode()); + var thumbTexture = thumbTextures.get(true, dragging || hovering); + context.drawGuiTexture(thumbTexture, thumbX, thumbY, thumbWidth, thumbHeight); if (label != null) { int color = isMouseInsideBounds(mouseX, mouseY) ? 0xFFFFA0 : 0xE0E0E0; @@ -199,23 +197,6 @@ public class WLabeledSlider extends WAbstractSlider { matrices.pop(); } - // state = 1: regular, 2: hovered, 0: disabled/dragging - @Environment(EnvType.CLIENT) - private void drawButton(DrawContext context, int x, int y, int state, int width) { - float px = 1 / 256f; - float buttonLeft = 0 * px; - float buttonTop = (46 + (state * 20)) * px; - int halfWidth = width / 2; - if (halfWidth > 198) halfWidth = 198; - float buttonWidth = halfWidth * px; - float buttonHeight = 20 * px; - float buttonEndLeft = (200 - halfWidth) * px; - - Identifier texture = WButton.getTexture(this); - ScreenDrawing.texturedRect(context, x, y, halfWidth, 20, texture, buttonLeft, buttonTop, buttonLeft + buttonWidth, buttonTop + buttonHeight, 0xFFFFFFFF); - ScreenDrawing.texturedRect(context, x + halfWidth, y, halfWidth, 20, texture, buttonEndLeft, buttonTop, 200 * px, buttonTop + buttonHeight, 0xFFFFFFFF); - } - @Environment(EnvType.CLIENT) @Override public void addNarrations(NarrationMessageBuilder builder) { 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 06db362..9b0bc58 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 @@ -226,8 +226,8 @@ public class WListPanel extends WClippedPanel { } @Override - public InputResult onMouseScroll(int x, int y, double amount) { - return scrollBar.onMouseScroll(0, 0, amount); + public InputResult onMouseScroll(int x, int y, double horizontalAmount, double verticalAmount) { + return scrollBar.onMouseScroll(0, 0, horizontalAmount, verticalAmount); } /** 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 0846d68..0c1af59 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 @@ -228,8 +228,8 @@ public class WScrollBar extends WWidget { @Environment(EnvType.CLIENT) @Override - public InputResult onMouseScroll(int x, int y, double amount) { - setValue(getValue() + (int) -amount * SCROLLING_SPEED); + public InputResult onMouseScroll(int x, int y, double horizontalAmount, double verticalAmount) { + setValue(getValue() + (int) (horizontalAmount - verticalAmount) * SCROLLING_SPEED); return InputResult.PROCESSED; } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollPanel.java index facac1b..620adc8 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollPanel.java @@ -143,12 +143,19 @@ public class WScrollPanel extends WClippedPanel { } @Override - public InputResult onMouseScroll(int x, int y, double amount) { + public InputResult onMouseScroll(int x, int y, double horizontalAmount, double verticalAmount) { + var horizontalResult = InputResult.IGNORED; + var verticalResult = InputResult.IGNORED; + + if (hasHorizontalScrollbar()) { + horizontalResult = horizontalScrollBar.onMouseScroll(x, y, horizontalAmount, 0); + } + if (hasVerticalScrollbar()) { - return verticalScrollBar.onMouseScroll(0, 0, amount); + verticalResult = verticalScrollBar.onMouseScroll(0, 0, 0, verticalAmount); } - return InputResult.IGNORED; + return horizontalResult.or(verticalResult); } @Override 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 1486928..e8c3908 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 @@ -212,13 +212,14 @@ public class WWidget { /** * Notifies this widget that the mouse has been scrolled inside its bounds. * - * @param x The X coordinate of the event, in widget-space (0 is the left edge of this widget) - * @param y The Y coordinate of the event, in widget-space (0 is the top edge of this widget) - * @param amount The scrolled amount. Positive values are up and negative values are down. + * @param x The X coordinate of the event, in widget-space (0 is the left edge of this widget) + * @param y The Y coordinate of the event, in widget-space (0 is the top edge of this widget) + * @param horizontalAmount The scrolled horizontal amount. Positive values are right and negative values are left. + * @param verticalAmount The scrolled vertical amount. Positive values are up and negative values are down. * @return {@link InputResult#PROCESSED} if the event is handled, {@link InputResult#IGNORED} otherwise. */ @Environment(EnvType.CLIENT) - public InputResult onMouseScroll(int x, int y, double amount) { + public InputResult onMouseScroll(int x, int y, double horizontalAmount, double verticalAmount) { return InputResult.IGNORED; } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/data/InputResult.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/InputResult.java index 4fed438..479977e 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/data/InputResult.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/InputResult.java @@ -10,6 +10,18 @@ public enum InputResult { PROCESSED, IGNORED; + /** + * Combines this input result with another. + * The combination is {@link #PROCESSED} is at least one of the inputs is. + * + * @param other the other input result + * @return the combined input result + * @since 9.0.0 + */ + public InputResult or(InputResult other) { + return this == IGNORED && other == IGNORED ? IGNORED : PROCESSED; + } + /** * Gets the corresponding input result for a {@code processed} boolean. * -- cgit