This repository has been archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc26ff0
commit 8265acb
Showing
2 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/main/java/io/github/darkkronicle/advancedchatcore/gui/ContextMenu.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Text, ContextConsumer> 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<Text, ContextConsumer> 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<Text, ContextConsumer> 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); | ||
} | ||
} |