diff options
Diffstat (limited to 'src/main/java/io')
15 files changed, 191 insertions, 115 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java b/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java index f3df523..52bd317 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java @@ -10,6 +10,7 @@ import io.github.cottonmc.cotton.gui.widget.data.Texture; import juuxel.libninepatch.NinePatch; import juuxel.libninepatch.TextureRegion; +import java.util.Objects; import java.util.function.Consumer; /** @@ -31,8 +32,8 @@ public interface BackgroundPainter { /** * The {@code VANILLA} background painter draws a vanilla-like GUI panel using nine-patch textures. * - * <p>This background painter uses {@code libgui:textures/widget/panel_light.png} as the light texture and - * {@code libgui:textures/widget/panel_dark.png} as the dark texture. + * <p>This background painter uses {@code libgui:textures/gui/sprites/widget/panel_light.png} as the light texture and + * {@code libgui:textures/gui/sprites/widget/panel_dark.png} as the dark texture. * * <p>This background painter is the default painter for root panels. * * You can override {@link io.github.cottonmc.cotton.gui.GuiDescription#addPainters()} to customize the painter yourself. @@ -40,8 +41,8 @@ public interface BackgroundPainter { * @since 1.5.0 */ public static BackgroundPainter VANILLA = createLightDarkVariants( - createNinePatch(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/panel_light.png")), - createNinePatch(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/panel_dark.png")) + createGuiSprite(new Identifier(LibGuiCommon.MOD_ID, "widget/panel_light")), + createGuiSprite(new Identifier(LibGuiCommon.MOD_ID, "widget/panel_dark")) ); /** @@ -158,4 +159,19 @@ public interface BackgroundPainter { else light.paintBackground(context, left, top, panel); }; } + + /** + * Creates a background painter that uses a texture from the GUI atlas. + * + * <p>This method can be used to draw tiled or nine-slice GUI sprites from resource packs + * as a simpler and more data-driven alternative to {@link #createNinePatch(Identifier)}. + * + * @param texture the texture ID + * @return a new background painter that uses a GUI sprite + * @since 9.0.0 + */ + static BackgroundPainter createGuiSprite(Identifier texture) { + Objects.requireNonNull(texture, "Texture cannot be null"); + return (context, left, top, panel) -> context.drawGuiTexture(texture, left, top, panel.getWidth(), panel.getHeight()); + } } 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<T extends SyncedGuiDescription> 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<T extends SyncedGuiDescription> 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<T extends SyncedGuiDescription> 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<S extends Screen & CottonScreenImpl> { } } - 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..75975b2 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/WidgetTextures.java @@ -0,0 +1,56 @@ +package io.github.cottonmc.cotton.gui.impl.client; + +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; +import io.github.cottonmc.cotton.gui.impl.mixin.client.SliderWidgetAccessor; + +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") + ); + private static final ScrollBarTextures LIGHT_SCROLL_BAR = new ScrollBarTextures( + id("widget/scroll_bar/background_light"), + id("widget/scroll_bar/thumb_light"), + id("widget/scroll_bar/thumb_pressed_light"), + id("widget/scroll_bar/thumb_hovered_light") + ); + private static final ScrollBarTextures DARK_SCROLL_BAR = new ScrollBarTextures( + id("widget/scroll_bar/background_dark"), + id("widget/scroll_bar/thumb_dark"), + id("widget/scroll_bar/thumb_pressed_dark"), + id("widget/scroll_bar/thumb_hovered_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; + } + + public static ScrollBarTextures getScrollBarTextures(boolean dark) { + return dark ? DARK_SCROLL_BAR : LIGHT_SCROLL_BAR; + } + + private static Identifier id(String path) { + return new Identifier(LibGuiCommon.MOD_ID, path); + } + + public record ScrollBarTextures(Identifier background, Identifier thumb, Identifier thumbPressed, + Identifier thumbHovered) { + } +} 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 && mouseX<getWidth() && mouseY<getHeight()); - int state = 1; //1=regular. 2=hovered. 0=disabled. - if (!enabled) { - state = 0; - } else if (hovered || isFocused()) { - state = 2; - } - - float px = 1/256f; - float buttonLeft = 0 * px; - float buttonTop = (46 + (state*20)) * px; - int halfWidth = getWidth()/2; - if (halfWidth>198) 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..3a2f967 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 @@ -6,11 +6,12 @@ import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; import net.minecraft.client.gui.screen.narration.NarrationPart; import net.minecraft.text.Text; -import net.minecraft.util.Identifier; import net.minecraft.util.math.RotationAxis; 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.impl.mixin.client.SliderWidgetAccessor; import io.github.cottonmc.cotton.gui.widget.data.Axis; import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; import org.jetbrains.annotations.Nullable; @@ -175,22 +176,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 +195,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<D, W extends WWidget> 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 2b7bb0c..add8688 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 @@ -7,17 +7,14 @@ import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; import net.minecraft.client.gui.screen.narration.NarrationPart; import net.minecraft.util.Identifier; -import io.github.cottonmc.cotton.gui.client.BackgroundPainter; import io.github.cottonmc.cotton.gui.impl.LibGuiCommon; import io.github.cottonmc.cotton.gui.impl.client.NarrationMessages; -import io.github.cottonmc.cotton.gui.impl.client.NinePatchTextureRendererImpl; +import io.github.cottonmc.cotton.gui.impl.client.WidgetTextures; import io.github.cottonmc.cotton.gui.widget.data.Axis; import io.github.cottonmc.cotton.gui.widget.data.InputResult; -import juuxel.libninepatch.NinePatch; - -import static io.github.cottonmc.cotton.gui.client.BackgroundPainter.createNinePatch; public class WScrollBar extends WWidget { + private static final Identifier FOCUS_TEXTURE = new Identifier(LibGuiCommon.MOD_ID, "widget/scroll_bar/focus"); private static final int SCROLLING_SPEED = 4; protected Axis axis = Axis.HORIZONTAL; @@ -49,34 +46,35 @@ public class WScrollBar extends WWidget { public void paint(DrawContext context, int x, int y, int mouseX, int mouseY) { var matrices = context.getMatrices(); boolean darkMode = shouldRenderInDarkMode(); + var textures = WidgetTextures.getScrollBarTextures(darkMode); - Painters.BACKGROUND.paintBackground(context, x, y, this); + context.drawGuiTexture(textures.background(), x, y, getWidth(), getHeight()); - NinePatch<Identifier> painter = (darkMode ? Painters.SCROLL_BAR_DARK : Painters.SCROLL_BAR); + Identifier thumbTexture = textures.thumb(); if (maxValue <= 0) return; if (sliding) { - painter = (darkMode ? Painters.SCROLL_BAR_PRESSED_DARK : Painters.SCROLL_BAR_PRESSED); + thumbTexture = textures.thumbPressed(); } else if (isWithinBounds(mouseX, mouseY)) { - painter = (darkMode ? Painters.SCROLL_BAR_HOVERED_DARK : Painters.SCROLL_BAR_HOVERED); + thumbTexture = textures.thumbHovered(); } matrices.push(); if (axis == Axis.HORIZONTAL) { matrices.translate(x + 1 + getHandlePosition(), y + 1, 0); - painter.draw(NinePatchTextureRendererImpl.INSTANCE, context, getHandleSize(), height - 2); + context.drawGuiTexture(thumbTexture, 0, 0, getHandleSize(), getHeight() - 2); if (isFocused()) { - Painters.FOCUS.draw(NinePatchTextureRendererImpl.INSTANCE, context, getHandleSize(), height - 2); + context.drawGuiTexture(FOCUS_TEXTURE, 0, 0, getHandleSize(), getHeight() - 2); } } else { matrices.translate(x + 1, y + 1 + getHandlePosition(), 0); - painter.draw(NinePatchTextureRendererImpl.INSTANCE, context, width - 2, getHandleSize()); + context.drawGuiTexture(thumbTexture, 0, 0, getWidth() - 2, getHandleSize()); if (isFocused()) { - Painters.FOCUS.draw(NinePatchTextureRendererImpl.INSTANCE, context, width - 2, getHandleSize()); + context.drawGuiTexture(FOCUS_TEXTURE, 0, 0, getWidth() - 2, getHandleSize()); } } @@ -204,8 +202,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; } @@ -255,19 +253,4 @@ public class WScrollBar extends WWidget { builder.put(NarrationPart.TITLE, NarrationMessages.SCROLL_BAR_TITLE); builder.put(NarrationPart.USAGE, NarrationMessages.SLIDER_USAGE); } - - @Environment(EnvType.CLIENT) - static final class Painters { - static final NinePatch<Identifier> SCROLL_BAR = NinePatch.builder(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/scroll_bar_light.png")).cornerSize(4).cornerUv(0.25f).build(); - static final NinePatch<Identifier> SCROLL_BAR_DARK = NinePatch.builder(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/scroll_bar_dark.png")).cornerSize(4).cornerUv(0.25f).build(); - static final NinePatch<Identifier> SCROLL_BAR_PRESSED = NinePatch.builder(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/scroll_bar_pressed_light.png")).cornerSize(4).cornerUv(0.25f).build(); - static final NinePatch<Identifier> SCROLL_BAR_PRESSED_DARK = NinePatch.builder(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/scroll_bar_pressed_dark.png")).cornerSize(4).cornerUv(0.25f).build(); - static final NinePatch<Identifier> SCROLL_BAR_HOVERED = NinePatch.builder(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/scroll_bar_hovered_light.png")).cornerSize(4).cornerUv(0.25f).build(); - static final NinePatch<Identifier> SCROLL_BAR_HOVERED_DARK = NinePatch.builder(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/scroll_bar_hovered_dark.png")).cornerSize(4).cornerUv(0.25f).build(); - static final BackgroundPainter BACKGROUND = BackgroundPainter.createLightDarkVariants( - createNinePatch(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/background_light.png")), - createNinePatch(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/background_dark.png")) - ); - static final NinePatch<Identifier> FOCUS = NinePatch.builder(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/focus.png")).cornerSize(4).cornerUv(0.25f).build(); - } } 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 @@ -11,6 +11,18 @@ public enum InputResult { 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. * * @param processed whether an input event was processed |