diff options
Diffstat (limited to 'src/main/java')
16 files changed, 278 insertions, 234 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 83ae894..6f81346 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/SyncedGuiDescription.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/SyncedGuiDescription.java @@ -293,52 +293,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/CottonClientScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java index 8fc53e1..22a5959 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 @@ -1,5 +1,7 @@ package io.github.cottonmc.cotton.gui.client; +import io.github.cottonmc.cotton.gui.impl.MouseInputHandler; + import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.util.math.MatrixStack; @@ -8,11 +10,13 @@ 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.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 TextHoverRendererScreen, CottonScreenImpl { protected GuiDescription description; protected int left = 0; protected int top = 0; @@ -64,6 +68,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. * @@ -158,68 +173,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; } @@ -227,12 +220,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 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 6b21af7..042126d 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,5 +1,7 @@ package io.github.cottonmc.cotton.gui.client; +import io.github.cottonmc.cotton.gui.impl.MouseInputHandler; + import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.ingame.HandledScreen; import net.minecraft.client.render.DiffuseLighting; @@ -10,8 +12,10 @@ import net.minecraft.text.Style; import net.minecraft.text.Text; import io.github.cottonmc.cotton.gui.SyncedGuiDescription; +import io.github.cottonmc.cotton.gui.impl.CottonScreenImpl; import io.github.cottonmc.cotton.gui.widget.WPanel; import io.github.cottonmc.cotton.gui.widget.WWidget; +import org.jetbrains.annotations.Nullable; import org.lwjgl.glfw.GLFW; import org.lwjgl.opengl.GL11; @@ -20,7 +24,7 @@ import org.lwjgl.opengl.GL11; * * @param <T> the description type */ -public class CottonInventoryScreen<T extends SyncedGuiDescription> extends HandledScreen<T> implements TextHoverRendererScreen { +public class CottonInventoryScreen<T extends SyncedGuiDescription> extends HandledScreen<T> implements TextHoverRendererScreen, CottonScreenImpl { protected SyncedGuiDescription description; protected WWidget lastResponder = null; @@ -78,6 +82,17 @@ public class CottonInventoryScreen<T extends SyncedGuiDescription> extends Handl this.client.keyboard.setRepeatEvents(false); } + @Nullable + @Override + public WWidget getLastResponder() { + return lastResponder; + } + + @Override + public void setLastResponder(@Nullable WWidget lastResponder) { + this.lastResponder = lastResponder; + } + /** * Clears the heavyweight peers of this screen's GUI description. */ @@ -174,60 +189,40 @@ public class CottonInventoryScreen<T extends SyncedGuiDescription> extends Handl int containerX = (int)mouseX-x; int containerY = (int)mouseY-y; if (containerX<0 || containerY<0 || containerX>=width || containerY>=height) return result; - if (lastResponder==null) { - lastResponder = description.doMouseDown(containerX, containerY, mouseButton); - } else { - //This is a drag instead - } - return result; + MouseInputHandler.onMouseDown(description, this, containerX, containerY, mouseButton); + + return true; } @Override public boolean mouseReleased(double mouseX, double mouseY, int mouseButton) { //Testing shows that STATE IS ACTUALLY BUTTON - boolean result = super.mouseReleased(mouseX, mouseY, mouseButton); + super.mouseReleased(mouseX, mouseY, mouseButton); int containerX = (int)mouseX-x; int containerY = (int)mouseY-y; - - 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.doMouseUp(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) { - boolean result = super.mouseDragged(mouseX, mouseY, mouseButton, deltaX, deltaY); + super.mouseDragged(mouseX, mouseY, mouseButton, deltaX, deltaY); int containerX = (int)mouseX-x; int containerY = (int)mouseY-y; - - 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.doMouseDrag(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-x; int containerY = (int)mouseY-y; - - WWidget child = root.hit(containerX, containerY); - child.onMouseScroll(containerX - child.getAbsoluteX(), containerY - child.getAbsoluteY(), amount); + MouseInputHandler.onMouseScroll(description, containerX, containerY, amount); + return true; } @@ -235,12 +230,9 @@ public class CottonInventoryScreen<T extends SyncedGuiDescription> extends Handl public void mouseMoved(double mouseX, double mouseY) { if (description.getRootPanel()==null) return; - WPanel root = description.getRootPanel(); int containerX = (int)mouseX-x; int containerY = (int)mouseY-y; - - WWidget child = root.hit(containerX, containerY); - child.onMouseMove(containerX - child.getAbsoluteX(), containerY - child.getAbsoluteY()); + MouseInputHandler.onMouseMove(description, containerX, containerY); } @Override diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/CottonScreenImpl.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/CottonScreenImpl.java new file mode 100644 index 0000000..de8e974 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/CottonScreenImpl.java @@ -0,0 +1,21 @@ +package io.github.cottonmc.cotton.gui.impl; + +import io.github.cottonmc.cotton.gui.widget.WWidget; + +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.gui.screen.Screen; + +import org.jetbrains.annotations.Nullable; + +@Environment(EnvType.CLIENT) +public interface CottonScreenImpl { + default Screen asScreen() { + return (Screen) this; + } + + @Nullable + WWidget getLastResponder(); + + void setLastResponder(@Nullable WWidget lastResponder); +} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/MouseInputHandler.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/MouseInputHandler.java new file mode 100644 index 0000000..a378d42 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/MouseInputHandler.java @@ -0,0 +1,113 @@ +package io.github.cottonmc.cotton.gui.impl; + +import io.github.cottonmc.cotton.gui.GuiDescription; +import io.github.cottonmc.cotton.gui.widget.WWidget; +import io.github.cottonmc.cotton.gui.widget.data.InputResult; + +import java.util.function.Function; + +import org.jetbrains.annotations.Nullable; + +/** + * The implementation for mouse inputs. + */ +public final class MouseInputHandler { + public static void onMouseDown(GuiDescription description, CottonScreenImpl screen, int containerX, int containerY, int mouseButton) { + if (screen.getLastResponder() == null) { + WWidget lastResponder = description.getRootPanel().hit(containerX, containerY); + screen.setLastResponder(lastResponder); + if (lastResponder != null) { + runTree( + lastResponder, + widget -> widget.onMouseDown(containerX - widget.getAbsoluteX(), containerY - widget.getAbsoluteY(), mouseButton) + ); + } + } else { + // This is a drag instead + } + } + + public static void onMouseUp(GuiDescription description, CottonScreenImpl screen, int containerX, int containerY, int mouseButton) { + WWidget lastResponder = screen.getLastResponder(); + + if (lastResponder != null) { + int width = screen.asScreen().width; + int height = screen.asScreen().height; + + runTree( + lastResponder, + widget -> widget.onMouseUp(containerX - widget.getAbsoluteX(), containerY - widget.getAbsoluteY(), mouseButton) + ); + + if (containerX >= 0 && containerY >= 0 && containerX < width && containerY < height) { + runTree( + lastResponder, + widget -> widget.onClick(containerX - widget.getAbsoluteX(), containerY - widget.getAbsoluteY(), mouseButton) + ); + } + } else { + runTree( + description.getRootPanel().hit(containerX, containerY), + widget -> widget.onMouseUp(containerX - widget.getAbsoluteX(), containerY - widget.getAbsoluteY(), mouseButton) + ); + } + + screen.setLastResponder(null); + } + + public static void onMouseDrag(GuiDescription description, CottonScreenImpl screen, int containerX, int containerY, int mouseButton, double deltaX, double deltaY) { + WWidget lastResponder = screen.getLastResponder(); + + if (lastResponder != null) { + lastResponder.onMouseDrag(containerX - lastResponder.getAbsoluteX(), containerY - lastResponder.getAbsoluteY(), mouseButton, deltaX, deltaY); + } else { + int width = screen.asScreen().width; + int height = screen.asScreen().height; + + if (containerX < 0 || containerY < 0 || containerX >= width || containerY >= height) return; + + runTree( + description.getRootPanel().hit(containerX, containerY), + widget -> widget.onMouseDrag(containerX - widget.getAbsoluteX(), containerY - widget.getAbsoluteY(), mouseButton, deltaX, deltaY) + ); + } + } + + public static void onMouseScroll(GuiDescription description, int containerX, int containerY, double amount) { + runTree( + description.getRootPanel().hit(containerX, containerY), + widget -> widget.onMouseScroll(containerX - widget.getAbsoluteX(), containerY - widget.getAbsoluteY(), amount) + ); + } + + public static void onMouseMove(GuiDescription description, int containerX, int containerY) { + runTree( + description.getRootPanel().hit(containerX, containerY), + widget -> widget.onMouseMove(containerX - widget.getAbsoluteX(), containerY - widget.getAbsoluteY()) + ); + } + + /** + * Traverses the {@code function} up the widget tree until it finds a {@link InputResult#PROCESSED} result. + * + * @param bottom the starting point for the traversal + * @param function the function to run + * @return the first widget to return {@link InputResult#PROCESSED}, or null if none found. + */ + @Nullable + private static WWidget runTree(WWidget bottom, Function<WWidget, InputResult> function) { + WWidget current = bottom; + + while (current != null) { + InputResult result = function.apply(current); + + if (result == InputResult.PROCESSED) { + break; + } else { + current = current.getParent(); + } + } + + return current; + } +} 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 6c422bc..c533bda 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 @@ -1,5 +1,7 @@ package io.github.cottonmc.cotton.gui.widget; +import io.github.cottonmc.cotton.gui.widget.data.InputResult; + import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.util.math.MathHelper; @@ -108,28 +110,31 @@ public abstract class WAbstractSlider extends WWidget { @Environment(EnvType.CLIENT) @Override - public WWidget onMouseDown(int x, int y, int button) { + public InputResult onMouseDown(int x, int y, int button) { // Check if cursor is inside or <=2px away from track if (isMouseInsideBounds(x, y)) { requestFocus(); + return InputResult.PROCESSED; } - return super.onMouseDown(x, y, button); + return InputResult.IGNORED; } @Environment(EnvType.CLIENT) @Override - public void onMouseDrag(int x, int y, int button) { + public InputResult onMouseDrag(int x, int y, int button, double deltaX, double deltaY) { if (isFocused()) { dragging = true; moveSlider(x, y); } + return InputResult.PROCESSED; } @Environment(EnvType.CLIENT) @Override - public void onClick(int x, int y, int button) { + public InputResult onClick(int x, int y, int button) { moveSlider(x, y); if (draggingFinishedListener != null) draggingFinishedListener.accept(value); + return InputResult.PROCESSED; } private void moveSlider(int x, int y) { @@ -160,15 +165,15 @@ public abstract class WAbstractSlider extends WWidget { @Environment(EnvType.CLIENT) @Override - public WWidget onMouseUp(int x, int y, int button) { + public InputResult onMouseUp(int x, int y, int button) { dragging = false; if (draggingFinishedListener != null) draggingFinishedListener.accept(value); - return super.onMouseUp(x, y, button); + return InputResult.PROCESSED; } @Environment(EnvType.CLIENT) @Override - public void onMouseScroll(int x, int y, double amount) { + public InputResult onMouseScroll(int x, int y, double amount) { if (direction == Direction.LEFT || direction == Direction.DOWN) { amount = -amount; } @@ -180,6 +185,7 @@ public abstract class WAbstractSlider extends WWidget { onValueChanged(value); pendingDraggingFinishedFromScrolling = true; } + return InputResult.PROCESSED; } @Environment(EnvType.CLIENT) 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 deee662..4f0b28c 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 @@ -1,5 +1,7 @@ package io.github.cottonmc.cotton.gui.widget; +import io.github.cottonmc.cotton.gui.widget.data.InputResult; + import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; @@ -120,7 +122,7 @@ public class WButton extends WWidget { @Environment(EnvType.CLIENT) @Override - public void onClick(int x, int y, int button) { + public InputResult onClick(int x, int y, int button) { super.onClick(x, y, button); if (enabled && isWithinBounds(x, y)) { @@ -128,6 +130,7 @@ public class WButton extends WWidget { if (onClick!=null) onClick.run(); } + return InputResult.PROCESSED; } @Environment(EnvType.CLIENT) diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java index 2583448..991796b 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java @@ -1,5 +1,6 @@ package io.github.cottonmc.cotton.gui.widget; +import io.github.cottonmc.cotton.gui.widget.data.InputResult; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; @@ -111,7 +112,7 @@ public class WLabel extends WWidget { @Environment(EnvType.CLIENT) @Override - public void onClick(int x, int y, int button) { + public InputResult onClick(int x, int y, int button) { Style hoveredTextStyle = getTextStyleAt(x, y); if (hoveredTextStyle != null) { Screen screen = MinecraftClient.getInstance().currentScreen; @@ -119,6 +120,7 @@ public class WLabel extends WWidget { screen.handleTextClick(hoveredTextStyle); } } + return InputResult.PROCESSED; } /** diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java index d4b5626..51a10ce 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java @@ -83,70 +83,6 @@ public abstract class WPanel extends WWidget { this.setSize(Math.max(this.getWidth(), pushRight), Math.max(this.getHeight(), pushDown)); } - @Environment(EnvType.CLIENT) - @Override - public WWidget onMouseUp(int x, int y, int button) { - if (children.isEmpty()) return super.onMouseUp(x, y, button); - for(int i=children.size()-1; i>=0; i--) { //Backwards so topmost widgets get priority - WWidget child = children.get(i); - if ( x>=child.getX() && - y>=child.getY() && - x<child.getX()+child.getWidth() && - y<child.getY()+child.getHeight()) { - return child.onMouseUp(x-child.getX(), y-child.getY(), button); - } - } - return super.onMouseUp(x, y, button); - } - - @Environment(EnvType.CLIENT) - @Override - public WWidget onMouseDown(int x, int y, int button) { - if (children.isEmpty()) return super.onMouseDown(x, y, button); - for(int i=children.size()-1; i>=0; i--) { //Backwards so topmost widgets get priority - WWidget child = children.get(i); - if ( x>=child.getX() && - y>=child.getY() && - x<child.getX()+child.getWidth() && - y<child.getY()+child.getHeight()) { - return child.onMouseDown(x-child.getX(), y-child.getY(), button); - } - } - return super.onMouseDown(x, y, button); - } - - @Environment(EnvType.CLIENT) - @Override - public void onMouseDrag(int x, int y, int button) { - if (children.isEmpty()) return; - for(int i=children.size()-1; i>=0; i--) { //Backwards so topmost widgets get priority - WWidget child = children.get(i); - if ( x>=child.getX() && - y>=child.getY() && - x<child.getX()+child.getWidth() && - y<child.getY()+child.getHeight()) { - child.onMouseDrag(x-child.getX(), y-child.getY(), button); - return; //Only send the message to the first valid recipient - } - } - super.onMouseDrag(x, y, button); - } - /* - @Override - public void onClick(int x, int y, int button) { - if (children.isEmpty()) return; - for(int i=children.size()-1; i>=0; i--) { //Backwards so topmost widgets get priority - WWidget child = children.get(i); - if ( x>=child.getX() && - y>=child.getY() && - x<child.getX()+child.getWidth() && - y<child.getY()+child.getHeight()) { - child.onClick(x-child.getX(), y-child.getY(), button); - return; //Only send the message to the first valid recipient - } - } - }*/ - /** * Finds the most specific child node at this location. */ 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 5f9a6d5..2316212 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 @@ -1,5 +1,7 @@ package io.github.cottonmc.cotton.gui.widget; +import io.github.cottonmc.cotton.gui.widget.data.InputResult; + import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.util.math.MatrixStack; @@ -167,7 +169,7 @@ public class WScrollBar extends WWidget { } @Override - public WWidget onMouseDown(int x, int y, int button) { + public InputResult onMouseDown(int x, int y, int button) { //TODO: Clicking before or after the handle should jump instead of scrolling requestFocus(); @@ -179,23 +181,25 @@ public class WScrollBar extends WWidget { anchorValue = value; } sliding = true; - return this; + + return InputResult.PROCESSED; } @Environment(EnvType.CLIENT) @Override - public void onMouseDrag(int x, int y, int button) { + public InputResult onMouseDrag(int x, int y, int button, double deltaX, double deltaY) { adjustSlider(x, y); + return InputResult.PROCESSED; } @Environment(EnvType.CLIENT) @Override - public WWidget onMouseUp(int x, int y, int button) { + public InputResult onMouseUp(int x, int y, int button) { //TODO: Clicking before or after the handle should jump instead of scrolling anchor = -1; anchorValue = -1; sliding = false; - return this; + return InputResult.PROCESSED; } @Override @@ -217,8 +221,9 @@ public class WScrollBar extends WWidget { @Environment(EnvType.CLIENT) @Override - public void onMouseScroll(int x, int y, double amount) { + public InputResult onMouseScroll(int x, int y, double amount) { setValue(getValue() + (int) -amount); + return InputResult.PROCESSED; } public int getValue() { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTabPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTabPanel.java index 7e5d02f..209d624 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTabPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTabPanel.java @@ -1,5 +1,7 @@ package io.github.cottonmc.cotton.gui.widget; +import io.github.cottonmc.cotton.gui.widget.data.InputResult; + import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; @@ -111,9 +113,7 @@ public class WTabPanel extends WPanel { @Environment(EnvType.CLIENT) @Override - public void onClick(int x, int y, int button) { - super.onClick(x, y, button); - + public InputResult onClick(int x, int y, int button) { MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F)); for (WTab tab : tabWidgets) { @@ -122,6 +122,7 @@ public class WTabPanel extends WPanel { mainPanel.setSelectedCard(data.getWidget()); WTabPanel.this.layout(); + return InputResult.PROCESSED; } @Environment(EnvType.CLIENT) diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java index 4ce4440..574d05e 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java @@ -1,5 +1,6 @@ package io.github.cottonmc.cotton.gui.widget; +import io.github.cottonmc.cotton.gui.widget.data.InputResult; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; @@ -125,13 +126,15 @@ public class WText extends WWidget { @Environment(EnvType.CLIENT) @Override - public void onClick(int x, int y, int button) { - if (button != 0) return; // only left clicks + public InputResult onClick(int x, int y, int button) { + if (button != 0) return InputResult.IGNORED; // only left clicks Style hoveredTextStyle = getTextStyleAt(x, y); if (hoveredTextStyle != null) { MinecraftClient.getInstance().currentScreen.handleTextClick(hoveredTextStyle); } + + return InputResult.PROCESSED; } /** diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java index d1b16ea..0857ee0 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java @@ -2,6 +2,9 @@ package io.github.cottonmc.cotton.gui.widget; import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.systems.RenderSystem; + +import io.github.cottonmc.cotton.gui.widget.data.InputResult; + import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; @@ -349,9 +352,10 @@ public class WTextField extends WWidget { @Environment(EnvType.CLIENT) @Override - public void onClick(int x, int y, int button) { + public InputResult onClick(int x, int y, int button) { requestFocus(); cursor = getCaretPos(this.text, x-OFFSET_X_TEXT); + return InputResult.PROCESSED; } @Environment(EnvType.CLIENT) diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java index 2f3cfc7..5ef3083 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java @@ -1,5 +1,7 @@ package io.github.cottonmc.cotton.gui.widget; +import io.github.cottonmc.cotton.gui.widget.data.InputResult; + import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; @@ -123,13 +125,14 @@ public class WToggleButton extends WWidget { @Environment(EnvType.CLIENT) @Override - public void onClick(int x, int y, int button) { + public InputResult onClick(int x, int y, int button) { super.onClick(x, y, button); MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F)); this.isOn = !this.isOn; onToggle(this.isOn); + return InputResult.PROCESSED; } @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 a77bd5e..c5e4678 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 @@ -1,5 +1,7 @@ package io.github.cottonmc.cotton.gui.widget; +import io.github.cottonmc.cotton.gui.widget.data.InputResult; + import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; @@ -153,17 +155,16 @@ public class WWidget { * @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 button The mouse button that was used. Button numbering is consistent with LWJGL Mouse (0=left, 1=right, 2=mousewheel click) + * @return {@link InputResult#PROCESSED} if the event is handled, {@link InputResult#IGNORED} otherwise. */ @Environment(EnvType.CLIENT) - public WWidget onMouseDown(int x, int y, int button) { - return this; + public InputResult onMouseDown(int x, int y, int button) { + return InputResult.IGNORED; } /** * Notifies this widget that the mouse has been moved while pressed and inside its bounds. * - * <p>The default implementation calls {@link #onMouseDrag(int, int, int)} for backwards compatibility. - * * @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 button The mouse button that was used. Button numbering is consistent with LWJGL Mouse (0=left, 1=right, 2=mousewheel click) @@ -171,20 +172,11 @@ public class WWidget { * @param deltaY The amount of dragging on the Y axis * * @since 1.5.0 + * @return {@link InputResult#PROCESSED} if the event is handled, {@link InputResult#IGNORED} otherwise. */ @Environment(EnvType.CLIENT) - public void onMouseDrag(int x, int y, int button, double deltaX, double deltaY) { - onMouseDrag(x, y, button); - } - - /** - * Notifies this widget that the mouse has been moved while pressed and 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 button The mouse button that was used. Button numbering is consistent with LWJGL Mouse (0=left, 1=right, 2=mousewheel click) - */ - @Environment(EnvType.CLIENT) - public void onMouseDrag(int x, int y, int button) { + public InputResult onMouseDrag(int x, int y, int button, double deltaX, double deltaY) { + return InputResult.IGNORED; } /** @@ -192,10 +184,11 @@ public class WWidget { * @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 button The mouse button that was used. Button numbering is consistent with LWJGL Mouse (0=left, 1=right, 2=mousewheel click) + * @return {@link InputResult#PROCESSED} if the event is handled, {@link InputResult#IGNORED} otherwise. */ @Environment(EnvType.CLIENT) - public WWidget onMouseUp(int x, int y, int button) { - return this; + public InputResult onMouseUp(int x, int y, int button) { + return InputResult.IGNORED; } /** @@ -203,9 +196,11 @@ public class WWidget { * @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 button The mouse button that was used. Button numbering is consistent with LWJGL Mouse (0=left, 1=right, 2=mousewheel click) + * @return {@link InputResult#PROCESSED} if the event is handled, {@link InputResult#IGNORED} otherwise. */ @Environment(EnvType.CLIENT) - public void onClick(int x, int y, int button) { + public InputResult onClick(int x, int y, int button) { + return InputResult.IGNORED; } /** @@ -213,9 +208,11 @@ public class WWidget { * @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. + * @return {@link InputResult#PROCESSED} if the event is handled, {@link InputResult#IGNORED} otherwise. */ @Environment(EnvType.CLIENT) - public void onMouseScroll(int x, int y, double amount) { + public InputResult onMouseScroll(int x, int y, double amount) { + return InputResult.IGNORED; } /** @@ -224,9 +221,11 @@ public class WWidget { * @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) * @since 1.5.0 + * @return {@link InputResult#PROCESSED} if the event is handled, {@link InputResult#IGNORED} otherwise. */ @Environment(EnvType.CLIENT) - public void onMouseMove(int x, int y) { + public InputResult onMouseMove(int x, int y) { + 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 new file mode 100644 index 0000000..991cfd3 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/InputResult.java @@ -0,0 +1,12 @@ +package io.github.cottonmc.cotton.gui.widget.data; + +/** + * Specifies whether an input event was ignored or processed. + * Used for mouse input events. + * + * @since 4.0.0 + */ +public enum InputResult { + PROCESSED, + IGNORED; +} |