From 13e95de78cfb9fb872dc9efd5b1f553c549d4c3c Mon Sep 17 00:00:00 2001 From: Pixirora Date: Mon, 22 Jul 2024 19:21:56 -0400 Subject: [PATCH] Only send button click event if it used the primary button --- .../impl/ui/controls/MouseButton.java | 26 +++++++++++++++++++ .../kitten_cube/impl/ui/screen/Screen.java | 3 ++- .../impl/ui/screen/ScreenTemplate.java | 5 ++-- .../impl/ui/widget/StaticIcon.java | 3 ++- .../ui/widget/button/RectangularButton.java | 8 ++++-- .../ui/widget/surface/ClickableSurface.java | 3 ++- .../ui/widget/text/PushableTextLines.java | 3 ++- .../impl/service/UICompatImpl.java | 2 -- .../impl/ui/screen/MinecraftScreen.java | 3 ++- .../impl/ui/screen/ScreenImpl.java | 3 ++- .../impl/ui/screen/MinecraftScreen.java | 3 ++- .../impl/ui/screen/ScreenImpl.java | 3 ++- .../impl/ui/screen/MinecraftScreen.java | 3 ++- .../impl/ui/screen/ScreenImpl.java | 3 ++- 14 files changed, 55 insertions(+), 16 deletions(-) create mode 100644 kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/controls/MouseButton.java diff --git a/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/controls/MouseButton.java b/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/controls/MouseButton.java new file mode 100644 index 0000000..8e45965 --- /dev/null +++ b/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/controls/MouseButton.java @@ -0,0 +1,26 @@ +package net.pixaurora.kitten_cube.impl.ui.controls; + +import net.pixaurora.kitten_heart.impl.KitTunes; + +public enum MouseButton { + PRIMARY(0), + SECONDARY(1), + MIDDLE(2); + + private final int defaultOpenglCode; + + private MouseButton(int defaultOpenglCode) { + this.defaultOpenglCode = defaultOpenglCode; + } + + public static MouseButton fromGlfwCode(int buttonCode) { + for (MouseButton button : MouseButton.values()) { + if (button.defaultOpenglCode == buttonCode) { + return button; + } + } + + KitTunes.LOGGER.error("Unrecognized button code on click: `" + buttonCode + "`! Defaulting to PRIMARY"); + return MouseButton.PRIMARY; + } +} diff --git a/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/screen/Screen.java b/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/screen/Screen.java index 0628c3c..3ea8b80 100644 --- a/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/screen/Screen.java +++ b/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/screen/Screen.java @@ -3,11 +3,12 @@ import net.pixaurora.kitten_cube.impl.math.Point; import net.pixaurora.kitten_cube.impl.math.Size; import net.pixaurora.kitten_cube.impl.ui.Drawable; +import net.pixaurora.kitten_cube.impl.ui.controls.MouseButton; public interface Screen extends Drawable { public void init(Size window); - public void handleClick(Point mousePos, int button); + public void handleClick(Point mousePos, MouseButton button); public default void onExit() { } diff --git a/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/screen/ScreenTemplate.java b/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/screen/ScreenTemplate.java index de54326..931e162 100644 --- a/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/screen/ScreenTemplate.java +++ b/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/screen/ScreenTemplate.java @@ -6,6 +6,7 @@ import net.pixaurora.kitten_cube.impl.math.Point; import net.pixaurora.kitten_cube.impl.math.Size; +import net.pixaurora.kitten_cube.impl.ui.controls.MouseButton; import net.pixaurora.kitten_cube.impl.ui.display.AlignedGuiDisplay; import net.pixaurora.kitten_cube.impl.ui.display.GuiDisplay; import net.pixaurora.kitten_cube.impl.ui.screen.align.AlignmentStrategy; @@ -41,12 +42,12 @@ public final void init(Size window) { } @Override - public final void handleClick(Point mousePos, int button) { + public final void handleClick(Point mousePos, MouseButton button) { mousePos = this.alignmentMethod().inverseAlign(mousePos, this.window.get()); for (Widget widget : this.widgets) { if (widget.isWithinBounds(mousePos)) { - widget.onClick(mousePos); + widget.onClick(mousePos, button); return; } } diff --git a/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/widget/StaticIcon.java b/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/widget/StaticIcon.java index 28b6bc5..69da538 100644 --- a/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/widget/StaticIcon.java +++ b/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/widget/StaticIcon.java @@ -1,6 +1,7 @@ package net.pixaurora.kitten_cube.impl.ui.widget; import net.pixaurora.kitten_cube.impl.math.Point; +import net.pixaurora.kitten_cube.impl.ui.controls.MouseButton; import net.pixaurora.kitten_cube.impl.ui.display.GuiDisplay; import net.pixaurora.kitten_cube.impl.ui.texture.Texture; import net.pixaurora.kitten_cube.impl.ui.widget.surface.RectangularSurface; @@ -29,6 +30,6 @@ public WidgetSurface surface() { } @Override - public void onClick(Point mousePos) { + public void onClick(Point mousePos, MouseButton button) { } } diff --git a/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/widget/button/RectangularButton.java b/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/widget/button/RectangularButton.java index b303dfa..4c2fa56 100644 --- a/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/widget/button/RectangularButton.java +++ b/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/widget/button/RectangularButton.java @@ -5,6 +5,7 @@ import net.pixaurora.kitten_cube.impl.math.Size; import net.pixaurora.kitten_cube.impl.text.Color; import net.pixaurora.kitten_cube.impl.text.Component; +import net.pixaurora.kitten_cube.impl.ui.controls.MouseButton; import net.pixaurora.kitten_cube.impl.ui.display.GuiDisplay; import net.pixaurora.kitten_cube.impl.ui.sound.Sound; import net.pixaurora.kitten_cube.impl.ui.texture.GuiTexture; @@ -73,8 +74,11 @@ public void setDisabledStatus(boolean isDisabled) { } @Override - public void onClick(Point mousePos) { + public void onClick(Point mousePos, MouseButton button) { MinecraftClient.playSound(Sound.BUTTON_CLICK); - this.action.onClick(this); + + if (button == MouseButton.PRIMARY) { + this.action.onClick(this); + } } } diff --git a/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/widget/surface/ClickableSurface.java b/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/widget/surface/ClickableSurface.java index ef94bcd..b70964a 100644 --- a/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/widget/surface/ClickableSurface.java +++ b/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/widget/surface/ClickableSurface.java @@ -1,7 +1,8 @@ package net.pixaurora.kitten_cube.impl.ui.widget.surface; import net.pixaurora.kitten_cube.impl.math.Point; +import net.pixaurora.kitten_cube.impl.ui.controls.MouseButton; public interface ClickableSurface extends WidgetSurface { - public void onClick(Point mousePos); + public void onClick(Point mousePos, MouseButton button); } diff --git a/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/widget/text/PushableTextLines.java b/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/widget/text/PushableTextLines.java index d41b01a..f35a9d8 100644 --- a/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/widget/text/PushableTextLines.java +++ b/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/widget/text/PushableTextLines.java @@ -7,6 +7,7 @@ import net.pixaurora.kitten_cube.impl.math.Point; import net.pixaurora.kitten_cube.impl.text.Color; import net.pixaurora.kitten_cube.impl.text.Component; +import net.pixaurora.kitten_cube.impl.ui.controls.MouseButton; import net.pixaurora.kitten_cube.impl.ui.display.GuiDisplay; import net.pixaurora.kitten_cube.impl.ui.widget.Widget; @@ -40,7 +41,7 @@ public void draw(GuiDisplay gui, Point mousePos) { } @Override - public void onClick(Point mousePos) { + public void onClick(Point mousePos, MouseButton button) { } @Override diff --git a/kitten-square/r1.17.0/src/main/java/net/pixaurora/kitten_square/impl/service/UICompatImpl.java b/kitten-square/r1.17.0/src/main/java/net/pixaurora/kitten_square/impl/service/UICompatImpl.java index 377464e..1802d36 100644 --- a/kitten-square/r1.17.0/src/main/java/net/pixaurora/kitten_square/impl/service/UICompatImpl.java +++ b/kitten-square/r1.17.0/src/main/java/net/pixaurora/kitten_square/impl/service/UICompatImpl.java @@ -17,8 +17,6 @@ import net.pixaurora.kitten_cube.impl.ui.screen.Screen; import net.pixaurora.kitten_cube.impl.ui.sound.Sound; import net.pixaurora.kitten_cube.impl.ui.widget.text.TextBox; -import net.pixaurora.kitten_heart.impl.resource.ResourcePathImpl; -import net.pixaurora.kitten_heart.impl.resource.ResourcePathUtils; import net.pixaurora.kitten_heart.impl.service.MinecraftUICompat; import net.pixaurora.kitten_square.impl.FakeComponent; import net.pixaurora.kitten_square.impl.SoundUtil; diff --git a/kitten-square/r1.17.0/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/MinecraftScreen.java b/kitten-square/r1.17.0/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/MinecraftScreen.java index d0b793f..56af33a 100644 --- a/kitten-square/r1.17.0/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/MinecraftScreen.java +++ b/kitten-square/r1.17.0/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/MinecraftScreen.java @@ -2,6 +2,7 @@ import net.pixaurora.kitten_cube.impl.math.Point; import net.pixaurora.kitten_cube.impl.math.Size; +import net.pixaurora.kitten_cube.impl.ui.controls.MouseButton; import net.pixaurora.kitten_cube.impl.ui.display.GuiDisplay; import net.pixaurora.kitten_cube.impl.ui.screen.Screen; @@ -22,7 +23,7 @@ public void init(Size window) { } @Override - public void handleClick(Point mousePos, int button) { + public void handleClick(Point mousePos, MouseButton button) { } public net.minecraft.client.gui.screens.Screen parent() { diff --git a/kitten-square/r1.17.0/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/ScreenImpl.java b/kitten-square/r1.17.0/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/ScreenImpl.java index e8c1bba..b2caade 100644 --- a/kitten-square/r1.17.0/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/ScreenImpl.java +++ b/kitten-square/r1.17.0/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/ScreenImpl.java @@ -4,6 +4,7 @@ import net.minecraft.network.chat.Component; import net.pixaurora.kitten_cube.impl.math.Point; import net.pixaurora.kitten_cube.impl.math.Size; +import net.pixaurora.kitten_cube.impl.ui.controls.MouseButton; import net.pixaurora.kitten_cube.impl.ui.display.GuiDisplay; import net.pixaurora.kitten_cube.impl.ui.screen.Screen; import net.pixaurora.kitten_square.impl.ui.ConversionCacheImpl; @@ -54,7 +55,7 @@ public boolean shouldCloseOnEsc() { public boolean mouseClicked(double x, double y, int button) { Point mousePos = Point.of((int) x, (int) y); - this.screen.handleClick(mousePos, button); + this.screen.handleClick(mousePos, MouseButton.fromGlfwCode(button)); return false; } diff --git a/kitten-square/r1.20.4/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/MinecraftScreen.java b/kitten-square/r1.20.4/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/MinecraftScreen.java index d0b793f..56af33a 100644 --- a/kitten-square/r1.20.4/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/MinecraftScreen.java +++ b/kitten-square/r1.20.4/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/MinecraftScreen.java @@ -2,6 +2,7 @@ import net.pixaurora.kitten_cube.impl.math.Point; import net.pixaurora.kitten_cube.impl.math.Size; +import net.pixaurora.kitten_cube.impl.ui.controls.MouseButton; import net.pixaurora.kitten_cube.impl.ui.display.GuiDisplay; import net.pixaurora.kitten_cube.impl.ui.screen.Screen; @@ -22,7 +23,7 @@ public void init(Size window) { } @Override - public void handleClick(Point mousePos, int button) { + public void handleClick(Point mousePos, MouseButton button) { } public net.minecraft.client.gui.screens.Screen parent() { diff --git a/kitten-square/r1.20.4/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/ScreenImpl.java b/kitten-square/r1.20.4/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/ScreenImpl.java index adc6d4f..2740a1a 100644 --- a/kitten-square/r1.20.4/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/ScreenImpl.java +++ b/kitten-square/r1.20.4/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/ScreenImpl.java @@ -4,6 +4,7 @@ import net.minecraft.network.chat.Component; import net.pixaurora.kitten_cube.impl.math.Point; import net.pixaurora.kitten_cube.impl.math.Size; +import net.pixaurora.kitten_cube.impl.ui.controls.MouseButton; import net.pixaurora.kitten_cube.impl.ui.display.GuiDisplay; import net.pixaurora.kitten_cube.impl.ui.screen.Screen; import net.pixaurora.kitten_square.impl.ui.ConversionCacheImpl; @@ -53,7 +54,7 @@ public boolean shouldCloseOnEsc() { public boolean mouseClicked(double x, double y, int button) { Point mousePos = Point.of((int) x, (int) y); - this.screen.handleClick(mousePos, button); + this.screen.handleClick(mousePos, MouseButton.fromGlfwCode(button)); return false; } diff --git a/kitten-square/r1.21.0/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/MinecraftScreen.java b/kitten-square/r1.21.0/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/MinecraftScreen.java index d0b793f..56af33a 100644 --- a/kitten-square/r1.21.0/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/MinecraftScreen.java +++ b/kitten-square/r1.21.0/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/MinecraftScreen.java @@ -2,6 +2,7 @@ import net.pixaurora.kitten_cube.impl.math.Point; import net.pixaurora.kitten_cube.impl.math.Size; +import net.pixaurora.kitten_cube.impl.ui.controls.MouseButton; import net.pixaurora.kitten_cube.impl.ui.display.GuiDisplay; import net.pixaurora.kitten_cube.impl.ui.screen.Screen; @@ -22,7 +23,7 @@ public void init(Size window) { } @Override - public void handleClick(Point mousePos, int button) { + public void handleClick(Point mousePos, MouseButton button) { } public net.minecraft.client.gui.screens.Screen parent() { diff --git a/kitten-square/r1.21.0/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/ScreenImpl.java b/kitten-square/r1.21.0/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/ScreenImpl.java index adc6d4f..2740a1a 100644 --- a/kitten-square/r1.21.0/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/ScreenImpl.java +++ b/kitten-square/r1.21.0/src/main/java/net/pixaurora/kitten_square/impl/ui/screen/ScreenImpl.java @@ -4,6 +4,7 @@ import net.minecraft.network.chat.Component; import net.pixaurora.kitten_cube.impl.math.Point; import net.pixaurora.kitten_cube.impl.math.Size; +import net.pixaurora.kitten_cube.impl.ui.controls.MouseButton; import net.pixaurora.kitten_cube.impl.ui.display.GuiDisplay; import net.pixaurora.kitten_cube.impl.ui.screen.Screen; import net.pixaurora.kitten_square.impl.ui.ConversionCacheImpl; @@ -53,7 +54,7 @@ public boolean shouldCloseOnEsc() { public boolean mouseClicked(double x, double y, int button) { Point mousePos = Point.of((int) x, (int) y); - this.screen.handleClick(mousePos, button); + this.screen.handleClick(mousePos, MouseButton.fromGlfwCode(button)); return false; }