Skip to content

Commit

Permalink
Only send button click event if it used the primary button
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixaurora committed Jul 22, 2024
1 parent e13d16a commit 13e95de
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -29,6 +30,6 @@ public WidgetSurface surface() {
}

@Override
public void onClick(Point mousePos) {
public void onClick(Point mousePos, MouseButton button) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 13e95de

Please sign in to comment.