From 8265acb06152d9b0de42699dff6267874e0746a3 Mon Sep 17 00:00:00 2001 From: DarkKronicle Date: Tue, 1 Feb 2022 19:28:17 -0700 Subject: [PATCH] Adding context menu --- gradle.properties | 2 +- .../advancedchatcore/gui/ContextMenu.java | 113 ++++++++++++++++++ 2 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 src/main/java/io/github/darkkronicle/advancedchatcore/gui/ContextMenu.java diff --git a/gradle.properties b/gradle.properties index 732f293..540d7d3 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ yarn_mappings=1.18.1+build.1 loader_version=0.12.5 fabric_api_version=0.44.0+1.18 -mod_version=1.4.1 +mod_version=1.4.2 maven_group=io.github.darkkronicle archives_base_name=AdvancedChatCore diff --git a/src/main/java/io/github/darkkronicle/advancedchatcore/gui/ContextMenu.java b/src/main/java/io/github/darkkronicle/advancedchatcore/gui/ContextMenu.java new file mode 100644 index 0000000..48004b2 --- /dev/null +++ b/src/main/java/io/github/darkkronicle/advancedchatcore/gui/ContextMenu.java @@ -0,0 +1,113 @@ +package io.github.darkkronicle.advancedchatcore.gui; + +import fi.dy.masa.malilib.gui.widgets.WidgetBase; +import io.github.darkkronicle.advancedchatcore.util.Color; +import io.github.darkkronicle.advancedchatcore.util.TextUtil; +import lombok.Getter; +import lombok.Setter; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.DrawableHelper; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.text.Text; + +import java.util.LinkedHashMap; + +public class ContextMenu extends WidgetBase { + + private final LinkedHashMap options; + private Text hoveredEntry = null; + + @Getter + private final int contextX; + + @Getter + private final int contextY; + + @Getter + @Setter + private Runnable close; + + @Setter + @Getter + private Color background; + + @Setter + @Getter + private Color hover; + + public ContextMenu(int x, int y, LinkedHashMap options, Runnable close) { + this(x, y, options, close, new Color(0, 0, 0, 200), new Color(255, 255, 255, 100)); + } + + public ContextMenu(int x, int y, LinkedHashMap options, Runnable close, Color background, Color hover) { + super(x, y, 10, 10); + this.contextX = x; + this.contextY = y; + this.options = options; + updateDimensions(); + this.close = close; + this.background = background; + this.hover = hover; + } + + public void updateDimensions() { + setWidth(TextUtil.getMaxLengthString(options.keySet().stream().map(Text::getString).toList()) + 4); + setHeight(options.size() * (textRenderer.fontHeight + 2)); + int windowWidth = MinecraftClient.getInstance().getWindow().getScaledWidth(); + int windowHeight = MinecraftClient.getInstance().getWindow().getScaledHeight(); + if (x + width > windowWidth) { + x = windowWidth - width; + } + if (y + height > windowHeight) { + y = windowHeight - height; + } + } + + @Override + public boolean onMouseClicked(int mouseX, int mouseY, int mouseButton) { + boolean success = super.onMouseClicked(mouseX, mouseY, mouseButton); + if (success) { + return true; + } + // Didn't click on this + close.run(); + return false; + } + + @Override + protected boolean onMouseClickedImpl(int mouseX, int mouseY, int mouseButton) { + if (mouseButton != 0) { + return false; + } + if (hoveredEntry == null) { + return false; + } + options.get(hoveredEntry).takeAction(contextX, contextY); + close.run(); + return true; + } + + @Override + public void render(int mouseX, int mouseY, boolean selected, MatrixStack matrixStack) { + drawRect(matrixStack, x, y, width, height, background.color()); + int rX = x + 2; + int rY = y + 2; + hoveredEntry = null; + for (Text option : options.keySet()) { + if (mouseX >= x && mouseX <= x + width && mouseY >= rY - 2 && mouseY < rY + fontHeight + 1) { + hoveredEntry = option; + drawRect(matrixStack, rX - 2, rY - 2, width, textRenderer.fontHeight + 2, hover.color()); + } + textRenderer.drawWithShadow(matrixStack, option, rX, rY, -1); + rY += textRenderer.fontHeight + 2; + } + } + + private static void drawRect(MatrixStack stack, int x, int y, int width, int height, int color) { + DrawableHelper.fill(stack, x, y, x + width, y + height, color); + } + + public interface ContextConsumer { + void takeAction(int x, int y); + } +}