diff options
Diffstat (limited to 'src/main')
49 files changed, 1058 insertions, 1034 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/SyncedGuiDescription.java b/src/main/java/io/github/cottonmc/cotton/gui/SyncedGuiDescription.java index ce0dd4b..fae723e 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/SyncedGuiDescription.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/SyncedGuiDescription.java @@ -26,7 +26,7 @@ import net.minecraft.server.world.ServerWorld; import net.minecraft.world.World; import io.github.cottonmc.cotton.gui.client.BackgroundPainter; -import io.github.cottonmc.cotton.gui.client.LibGuiClient; +import io.github.cottonmc.cotton.gui.client.LibGui; import io.github.cottonmc.cotton.gui.networking.NetworkSide; import io.github.cottonmc.cotton.gui.widget.WGridPanel; import io.github.cottonmc.cotton.gui.widget.WLabel; @@ -80,7 +80,7 @@ public class SyncedGuiDescription extends ScreenHandler implements GuiDescriptio } public int getTitleColor() { - return (world.isClient && LibGuiClient.config.darkMode) ? darkTitleColor : titleColor; + return (world.isClient && LibGui.isDarkMode()) ? darkTitleColor : titleColor; } public SyncedGuiDescription setRootPanel(WPanel panel) { @@ -164,7 +164,7 @@ public class SyncedGuiDescription extends ScreenHandler implements GuiDescriptio /** WILL MODIFY toInsert! Returns true if anything was inserted. */ private boolean insertIntoExisting(ItemStack toInsert, Slot slot, PlayerEntity player) { ItemStack curSlotStack = slot.getStack(); - if (!curSlotStack.isEmpty() && canStacksCombine(toInsert, curSlotStack) && slot.canInsert(toInsert)) { + if (!curSlotStack.isEmpty() && ItemStack.canCombine(toInsert, curSlotStack) && slot.canInsert(toInsert)) { int combinedAmount = curSlotStack.getCount() + toInsert.getCount(); int maxAmount = Math.min(toInsert.getMaxCount(), slot.getMaxItemCount()); if (combinedAmount <= maxAmount) { @@ -299,52 +299,6 @@ public class SyncedGuiDescription extends ScreenHandler implements GuiDescriptio } @Nullable - public WWidget doMouseUp(int x, int y, int state) { - if (rootPanel!=null) return rootPanel.onMouseUp(x, y, state); - return null; - } - - @Nullable - public WWidget doMouseDown(int x, int y, int button) { - if (rootPanel!=null) return rootPanel.onMouseDown(x, y, button); - return null; - } - - public void doMouseDrag(int x, int y, int button, double deltaX, double deltaY) { - if (rootPanel!=null) rootPanel.onMouseDrag(x, y, button, deltaX, deltaY); - } - - public void doClick(int x, int y, int button) { - if (focus!=null) { - int wx = focus.getAbsoluteX(); - int wy = focus.getAbsoluteY(); - - if (x>=wx && x<wx+focus.getWidth() && y>=wy && y<wy+focus.getHeight()) { - //Do nothing, focus will get the click soon - } else { - //Invalidate the component first - WWidget lastFocus = focus; - focus = null; - lastFocus.onFocusLost(); - } - } - - //if (rootPanel!=null) rootPanel.onClick(x, y, button); - } - - public void doCharType(char ch) { - if (focus!=null) focus.onCharTyped(ch); - } - - //public void doKeyPress(int key) { - // if (focus!=null) focus.onKeyPressed(key); - //} - - //public void doKeyRelease(int key) { - // if (focus!=null) focus.onKeyReleased(key); - //} - - @Nullable @Override public PropertyDelegate getPropertyDelegate() { return propertyDelegate; 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 f00bf80..13ca534 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 @@ -1,5 +1,6 @@ package io.github.cottonmc.cotton.gui.client; +import net.minecraft.client.util.math.MatrixStack; import net.minecraft.util.Identifier; import io.github.cottonmc.cotton.gui.widget.WItemSlot; @@ -13,11 +14,12 @@ import io.github.cottonmc.cotton.gui.widget.WWidget; public interface BackgroundPainter { /** * Paint the specified panel to the screen. + * @param matrices The rendering matrix stack * @param left The absolute position of the left of the panel, in gui-screen coordinates * @param top The absolute position of the top of the panel, in gui-screen coordinates * @param panel The panel being painted */ - public void paintBackground(int left, int top, WWidget panel); + public void paintBackground(MatrixStack matrices, int left, int top, WWidget panel); /** * The {@code VANILLA} background painter draws a vanilla-like gui panel using {@linkplain NinePatch nine-patch textures}. @@ -40,9 +42,9 @@ public interface BackgroundPainter { /** * The {@code SLOT} background painter draws item slots or slot-like widgets. */ - public static BackgroundPainter SLOT = (left, top, panel) -> { + public static BackgroundPainter SLOT = (matrices, left, top, panel) -> { if (!(panel instanceof WItemSlot)) { - ScreenDrawing.drawBeveledPanel(left-1, top-1, panel.getWidth()+2, panel.getHeight()+2, 0xB8000000, 0x4C000000, 0xB8FFFFFF); + ScreenDrawing.drawBeveledPanel(matrices, left-1, top-1, panel.getWidth()+2, panel.getHeight()+2, 0xB8000000, 0x4C000000, 0xB8FFFFFF); } else { WItemSlot slot = (WItemSlot)panel; for(int x = 0; x < slot.getWidth()/18; ++x) { @@ -53,26 +55,26 @@ public interface BackgroundPainter { //this will cause a slightly discolored bottom border on vanilla backgrounds but it's necessary for color support, it shouldn't be *too* visible unless you're looking for it int hi = 0xB8FFFFFF; if (slot.isBigSlot()) { - ScreenDrawing.drawBeveledPanel((x * 18) + left - 4, (y * 18) + top - 4, 26, 26, + ScreenDrawing.drawBeveledPanel(matrices, (x * 18) + left - 4, (y * 18) + top - 4, 26, 26, lo, bg, hi); if (slot.getFocusedSlot() == index) { int sx = (x * 18) + left - 4; int sy = (y * 18) + top - 4; - ScreenDrawing.coloredRect(sx, sy, 26, 1, 0xFF_FFFFA0); - ScreenDrawing.coloredRect(sx, sy + 1, 1, 26 - 1, 0xFF_FFFFA0); - ScreenDrawing.coloredRect(sx + 26 - 1, sy + 1, 1, 26 - 1, 0xFF_FFFFA0); - ScreenDrawing.coloredRect(sx + 1, sy + 26 - 1, 26 - 1, 1, 0xFF_FFFFA0); + ScreenDrawing.coloredRect(matrices, sx, sy, 26, 1, 0xFF_FFFFA0); + ScreenDrawing.coloredRect(matrices, sx, sy + 1, 1, 26 - 1, 0xFF_FFFFA0); + ScreenDrawing.coloredRect(matrices, sx + 26 - 1, sy + 1, 1, 26 - 1, 0xFF_FFFFA0); + ScreenDrawing.coloredRect(matrices, sx + 1, sy + 26 - 1, 26 - 1, 1, 0xFF_FFFFA0); } } else { - ScreenDrawing.drawBeveledPanel((x * 18) + left, (y * 18) + top, 16+2, 16+2, + ScreenDrawing.drawBeveledPanel(matrices, (x * 18) + left, (y * 18) + top, 16+2, 16+2, lo, bg, hi); if (slot.getFocusedSlot() == index) { int sx = (x * 18) + left; int sy = (y * 18) + top; - ScreenDrawing.coloredRect(sx, sy, 18, 1, 0xFF_FFFFA0); - ScreenDrawing.coloredRect(sx, sy + 1, 1, 18 - 1, 0xFF_FFFFA0); - ScreenDrawing.coloredRect(sx + 18 - 1, sy + 1, 1, 18 - 1, 0xFF_FFFFA0); - ScreenDrawing.coloredRect(sx + 1, sy + 18 - 1, 18 - 1, 1, 0xFF_FFFFA0); + ScreenDrawing.coloredRect(matrices, sx, sy, 18, 1, 0xFF_FFFFA0); + ScreenDrawing.coloredRect(matrices, sx, sy + 1, 1, 18 - 1, 0xFF_FFFFA0); + ScreenDrawing.coloredRect(matrices, sx + 18 - 1, sy + 1, 1, 18 - 1, 0xFF_FFFFA0); + ScreenDrawing.coloredRect(matrices, sx + 1, sy + 18 - 1, 18 - 1, 1, 0xFF_FFFFA0); } } } @@ -85,11 +87,11 @@ public interface BackgroundPainter { * * @param panelColor the panel background color * @return a colorful gui panel painter - * @see ScreenDrawing#drawGuiPanel(int, int, int, int, int) + * @see ScreenDrawing#drawGuiPanel(MatrixStack, int, int, int, int, int) */ public static BackgroundPainter createColorful(int panelColor) { - return (left, top, panel) -> { - ScreenDrawing.drawGuiPanel(left-8, top-8, panel.getWidth()+16, panel.getHeight()+16, panelColor); + return (matrices, left, top, panel) -> { + ScreenDrawing.drawGuiPanel(matrices, left-8, top-8, panel.getWidth()+16, panel.getHeight()+16, panelColor); }; } @@ -101,11 +103,11 @@ public interface BackgroundPainter { * @return a colorful gui panel painter */ public static BackgroundPainter createColorful(int panelColor, float contrast) { - return (left, top, panel) -> { + return (matrices, left, top, panel) -> { int shadowColor = ScreenDrawing.multiplyColor(panelColor, 1.0f - contrast); int hilightColor = ScreenDrawing.multiplyColor(panelColor, 1.0f + contrast); - ScreenDrawing.drawGuiPanel(left-8, top-8, panel.getWidth()+16, panel.getHeight()+16, shadowColor, panelColor, hilightColor, 0xFF000000); + ScreenDrawing.drawGuiPanel(matrices, left-8, top-8, panel.getWidth()+16, panel.getHeight()+16, shadowColor, panelColor, hilightColor, 0xFF000000); }; } @@ -146,9 +148,9 @@ public interface BackgroundPainter { * @since 1.5.0 */ public static BackgroundPainter createLightDarkVariants(BackgroundPainter light, BackgroundPainter dark) { - return (left, top, panel) -> { - if (LibGuiClient.config.darkMode) dark.paintBackground(left, top, panel); - else light.paintBackground(left, top, panel); + return (matrices, left, top, panel) -> { + if (LibGui.isDarkMode()) dark.paintBackground(matrices, left, top, panel); + else light.paintBackground(matrices, left, top, panel); }; } } 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 055262e..cdb65af 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 @@ -7,11 +7,14 @@ import net.minecraft.text.Style; import net.minecraft.text.Text; import io.github.cottonmc.cotton.gui.GuiDescription; +import io.github.cottonmc.cotton.gui.impl.CottonScreenImpl; +import io.github.cottonmc.cotton.gui.impl.MouseInputHandler; import io.github.cottonmc.cotton.gui.widget.WPanel; import io.github.cottonmc.cotton.gui.widget.WWidget; +import org.jetbrains.annotations.Nullable; import org.lwjgl.opengl.GL11; -public class CottonClientScreen extends Screen implements TextHoverRendererScreen { +public class CottonClientScreen extends Screen implements CottonScreenImpl { protected GuiDescription description; protected int left = 0; protected int top = 0; @@ -63,6 +66,17 @@ public class CottonClientScreen extends Screen implements TextHoverRendererScree this.client.keyboard.setRepeatEvents(false); } + @Nullable + @Override + public WWidget getLastResponder() { + return lastResponder; + } + + @Override + public void setLastResponder(@Nullable WWidget lastResponder) { + this.lastResponder = lastResponder; + } + /** * Repositions the root panel. * @@ -157,68 +171,46 @@ public class CottonClientScreen extends Screen implements TextHoverRendererScree } } - boolean result = super.mouseClicked(mouseX, mouseY, mouseButton); + super.mouseClicked(mouseX, mouseY, mouseButton); int containerX = (int)mouseX-left; int containerY = (int)mouseY-top; - if (containerX<0 || containerY<0 || containerX>=width || containerY>=height) return result; - if (lastResponder==null) { - lastResponder = description.getRootPanel().hit(containerX, containerY); - if (lastResponder!=null) lastResponder.onMouseDown(containerX-lastResponder.getAbsoluteX(), containerY-lastResponder.getAbsoluteY(), mouseButton); - } else { - //This is a drag instead - } - return result; - + if (containerX<0 || containerY<0 || containerX>=width || containerY>=height) return true; + MouseInputHandler.onMouseDown(description, this, containerX, containerY, mouseButton); + + return true; } @Override public boolean mouseReleased(double mouseX, double mouseY, int mouseButton) { if (description.getRootPanel()==null) return super.mouseReleased(mouseX, mouseY, mouseButton); - boolean result = super.mouseReleased(mouseX, mouseY, mouseButton); + super.mouseReleased(mouseX, mouseY, mouseButton); int containerX = (int)mouseX-left; int containerY = (int)mouseY-top; - - if (lastResponder!=null) { - lastResponder.onMouseUp(containerX-lastResponder.getAbsoluteX(), containerY-lastResponder.getAbsoluteY(), mouseButton); - if (containerX>=0 && containerY>=0 && containerX<width && containerY<height) { - lastResponder.onClick(containerX-lastResponder.getAbsoluteX(), containerY-lastResponder.getAbsoluteY(), mouseButton); - } - } else { - description.getRootPanel().onMouseUp(containerX, containerY, mouseButton); - } - - lastResponder = null; - return result; + MouseInputHandler.onMouseUp(description, this, containerX, containerY, mouseButton); + + return true; } @Override public boolean mouseDragged(double mouseX, double mouseY, int mouseButton, double deltaX, double deltaY) { if (description.getRootPanel()==null) return super.mouseDragged(mouseX, mouseY, mouseButton, deltaX, deltaY); - boolean result = super.mouseDragged(mouseX, mouseY, mouseButton, deltaX, deltaY); + super.mouseDragged(mouseX, mouseY, mouseButton, deltaX, deltaY); int containerX = (int)mouseX-left; int containerY = (int)mouseY-top; - - if (lastResponder!=null) { - lastResponder.onMouseDrag(containerX-lastResponder.getAbsoluteX(), containerY-lastResponder.getAbsoluteY(), mouseButton, deltaX, deltaY); - return result; - } else { - if (containerX<0 || containerY<0 || containerX>=width || containerY>=height) return result; - description.getRootPanel().onMouseDrag(containerX, containerY, mouseButton, deltaX, deltaY); - } - return result; + MouseInputHandler.onMouseDrag(description, this, containerX, containerY, mouseButton, deltaX, deltaY); + + return true; } @Override public boolean mouseScrolled(double mouseX, double mouseY, double amount) { if (description.getRootPanel()==null) return super.mouseScrolled(mouseX, mouseY, amount); - WPanel root = description.getRootPanel(); int containerX = (int)mouseX-left; int containerY = (int)mouseY-top; - - WWidget child = root.hit(containerX, containerY); - child.onMouseScroll(containerX - child.getAbsoluteX(), containerY - child.getAbsoluteY(), amount); + MouseInputHandler.onMouseScroll(description, containerX, containerY, amount); + return true; } @@ -226,12 +218,9 @@ public class CottonClientScreen extends Screen implements TextHoverRendererScree public void mouseMoved(double mouseX, double mouseY) { if (description.getRootPanel()==null) return; - WPanel root = description.getRootPanel(); int containerX = (int)mouseX-left; int containerY = (int)mouseY-top; - - WWidget child = root.hit(containerX, containerY); - child.onMouseMove(containerX - child.getAbsoluteX(), containerY - child.getAbsoluteY()); + MouseInputHandler.onMouseMove(description, containerX, containerY); } @Override @@ -262,7 +251,7 @@ public class CottonClientScreen extends Screen implements TextHoverRendererScree //} @Override - public void renderTextHover(MatrixStack matrices, Style textStyle, int x, int y) { + public void renderTextHover(MatrixStack matrices, @Nullable Style textStyle, int x, int y) { renderTextHoverEffect(matrices, textStyle, 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 79111d2..f69bb65 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonHud.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonHud.java @@ -6,7 +6,6 @@ import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; import net.minecraft.client.MinecraftClient; import net.minecraft.client.util.Window; -import net.minecraft.client.util.math.MatrixStack; import io.github.cottonmc.cotton.gui.widget.WWidget; @@ -19,27 +18,40 @@ import java.util.Set; * Manages widgets that are painted on the in-game HUD. */ @Environment(EnvType.CLIENT) -public enum CottonHud implements HudRenderCallback { - INSTANCE; // TODO (4.0): Migrate from singleton to static methods +public final class CottonHud { + private CottonHud() {} + + private static final Set<WWidget> widgets = new HashSet<>(); + private static final Map<WWidget, Positioner> positioners = new HashMap<>(); static { - HudRenderCallback.EVENT.register(INSTANCE); + HudRenderCallback.EVENT.register((matrices, tickDelta) -> { + Window window = MinecraftClient.getInstance().getWindow(); + int hudWidth = window.getScaledWidth(); + int hudHeight = window.getScaledHeight(); + for (WWidget widget : widgets) { + Positioner positioner = positioners.get(widget); + if (positioner != null) { + positioner.reposition(widget, hudWidth, hudHeight); + } + + widget.paint(matrices, widget.getX(), widget.getY(), -1, -1); + } + }); + ClientTickEvents.END_CLIENT_TICK.register(client -> { - for (WWidget widget : INSTANCE.widgets) { + for (WWidget widget : widgets) { widget.tick(); } }); } - private final Set<WWidget> widgets = new HashSet<>(); - private final Map<WWidget, Positioner> positioners = new HashMap<>(); - /** * Adds a new widget to the HUD. * * @param widget the widget */ - public void add(WWidget widget) { + public static void add(WWidget widget) { widgets.add(widget); } @@ -51,7 +63,7 @@ public enum CottonHud implements HudRenderCallback { * @param y the y offset * @see Positioner#of documentation about the offsets */ - public void add(WWidget widget, int x, int y) { + public static void add(WWidget widget, int x, int y) { add(widget, Positioner.of(x, y)); } @@ -65,7 +77,7 @@ public enum CottonHud implements HudRenderCallback { * @param height the height of the widget * @see Positioner#of documentation about the offsets */ - public void add(WWidget widget, int x, int y, int width, int height) { + public static void add(WWidget widget, int x, int y, int width, int height) { add(widget, Positioner.of(x, y)); widget.setSize(width, height); } @@ -76,7 +88,7 @@ public enum CottonHud implements HudRenderCallback { * @param widget the widget * @param positioner the positioner */ - public void add(WWidget widget, Positioner positioner) { + public static void add(WWidget widget, Positioner positioner) { widgets.add(widget); setPositioner(widget, positioner); } @@ -89,7 +101,7 @@ public enum CottonHud implements HudRenderCallback { * @param width the width of the widget * @param height the height of the widget */ - public void add(WWidget widget, Positioner positioner, int width, int height) { + public static void add(WWidget widget, Positioner positioner, int width, int height) { widgets.add(widget); widget.setSize(width, height); setPositioner(widget, positioner); @@ -101,7 +113,7 @@ public enum CottonHud implements HudRenderCallback { * @param widget the widget * @param positioner the positioner */ - public void setPositioner(WWidget widget, Positioner positioner) { + public static void setPositioner(WWidget widget, Positioner positioner) { positioners.put(widget, positioner); } @@ -110,25 +122,10 @@ public enum CottonHud implements HudRenderCallback { * * @param widget the widget */ - public void remove(WWidget widget) { + public static void remove(WWidget widget) { widgets.remove(widget); } - @Override - public void onHudRender(MatrixStack matrices, float tickDelta) { - Window window = MinecraftClient.getInstance().getWindow(); - int hudWidth = window.getScaledWidth(); - int hudHeight = window.getScaledHeight(); - for (WWidget widget : widgets) { - Positioner positioner = positioners.get(widget); - if (positioner != null) { - positioner.reposition(widget, hudWidth, hudHeight); - } - - widget.paint(matrices, widget.getX(), widget.getY(), -1, -1); - } - } - /** * Positioner |
