Skip to content

Commit 252817c

Browse files
committed
Fixed SeedMap Context Menu
1 parent a210f9c commit 252817c

File tree

1 file changed

+64
-25
lines changed

1 file changed

+64
-25
lines changed

src/main/java/dev/xpple/seedmapper/seedmap/SeedMapScreen.java

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ public class SeedMapScreen extends Screen {
192192

193193
private static final int TELEPORT_FIELD_WIDTH = 70;
194194
private static final int WAYPOINT_NAME_FIELD_WIDTH = 100;
195+
private static final double WAYPOINT_CONTEXT_PADDING = 8.0D;
195196

196197
private static double tileSizePixels() {
197198
return TilePos.TILE_SIZE_CHUNKS * SCALED_CHUNK_SIZE * Configs.PixelsPerBiome;
@@ -1445,14 +1446,14 @@ private boolean handleMapRightClicked(MouseButtonEvent mouseButtonEvent, boolean
14451446
return false;
14461447
}
14471448

1448-
Optional<FeatureWidget> clickedWidget = this.featureWidgets.stream()
1449-
.filter(widget -> mouseX >= widget.x && mouseX <= widget.x + widget.width() && mouseY >= widget.y && mouseY <= widget.y + widget.height())
1450-
.findAny();
1451-
14521449
List<ContextMenu.MenuEntry> entries = new ArrayList<>();
14531450
BlockPos clickedPos = this.mouseQuart.toBlockPos().atY(63);
1454-
if (clickedWidget.isPresent() && clickedWidget.get().feature == MapFeature.WAYPOINT) {
1455-
FeatureWidget widget = clickedWidget.get();
1451+
Optional<FeatureWidget> clickedWaypoint = this.featureWidgets.stream()
1452+
.filter(widget -> widget.feature == MapFeature.WAYPOINT)
1453+
.filter(widget -> widget.isContextHit(mouseX, mouseY, WAYPOINT_CONTEXT_PADDING))
1454+
.findFirst();
1455+
if (clickedWaypoint.isPresent()) {
1456+
FeatureWidget widget = clickedWaypoint.get();
14561457
SimpleWaypointsAPI api = SimpleWaypointsAPI.getInstance();
14571458
String identifier = api.getWorldIdentifier(this.minecraft);
14581459
String foundName = null;
@@ -2048,46 +2049,76 @@ record MenuEntry(String label, Runnable action) {}
20482049
private final int y;
20492050
private final List<MenuEntry> entries;
20502051
private final int width = 140;
2051-
private final int entryHeight = 12;
2052+
private final int entryHeight;
2053+
private final int entryBoxHeight;
2054+
private static final int MENU_PADDING = 4;
2055+
private static final int ENTRY_VERTICAL_PADDING = 2;
2056+
private static final int BACKGROUND_COLOR = 0xCC_000000;
2057+
private static final int HOVER_COLOR = 0x66_FFFFFF;
20522058

20532059
ContextMenu(int x, int y, List<MenuEntry> entries) {
20542060
this.x = x;
20552061
this.y = y;
20562062
this.entries = entries;
2063+
this.entryHeight = SeedMapScreen.this.font.lineHeight;
2064+
this.entryBoxHeight = this.entryHeight + ENTRY_VERTICAL_PADDING * 2;
2065+
}
2066+
2067+
private int totalHeight() {
2068+
return this.entries.size() * this.entryBoxHeight + MENU_PADDING * 2;
2069+
}
2070+
2071+
private boolean contains(double mouseX, double mouseY) {
2072+
return mouseX >= this.x && mouseX <= this.x + this.width && mouseY >= this.y && mouseY <= this.y + this.totalHeight();
2073+
}
2074+
2075+
private int indexAt(double mouseX, double mouseY) {
2076+
if (!this.contains(mouseX, mouseY)) {
2077+
return -1;
2078+
}
2079+
double relY = mouseY - (this.y + MENU_PADDING);
2080+
if (relY < 0) {
2081+
return -1;
2082+
}
2083+
int idx = (int) (relY / this.entryBoxHeight);
2084+
return idx >= 0 && idx < this.entries.size() ? idx : -1;
20572085
}
20582086

20592087
void render(GuiGraphics guiGraphics, int mouseX, int mouseY, net.minecraft.client.gui.Font font) {
2060-
int height = this.entries.size() * (this.entryHeight + 6) + 6;
2061-
guiGraphics.fill(this.x, this.y, this.x + this.width, this.y + height, 0xCC_000000);
2062-
int ty = this.y + 4;
2063-
for (MenuEntry entry : this.entries) {
2064-
guiGraphics.drawString(font, Component.literal(entry.label()), this.x + 6, ty, -1);
2065-
ty += this.entryHeight + 6;
2088+
int height = this.totalHeight();
2089+
guiGraphics.fill(this.x, this.y, this.x + this.width, this.y + height, BACKGROUND_COLOR);
2090+
int hoveredIndex = this.indexAt(mouseX, mouseY);
2091+
for (int i = 0; i < this.entries.size(); i++) {
2092+
int entryTop = this.y + MENU_PADDING + i * this.entryBoxHeight;
2093+
if (i == hoveredIndex) {
2094+
guiGraphics.fill(this.x + 1, entryTop, this.x + this.width - 1, entryTop + this.entryBoxHeight, HOVER_COLOR);
2095+
}
2096+
guiGraphics.drawString(font, Component.literal(this.entries.get(i).label()), this.x + 6, entryTop + ENTRY_VERTICAL_PADDING, -1);
20662097
}
20672098
}
20682099

20692100
boolean mouseClicked(MouseButtonEvent event) {
20702101
if (event.button() != InputConstants.MOUSE_BUTTON_LEFT) {
20712102
return false;
20722103
}
2073-
int mx = (int) event.x();
2074-
int my = (int) event.y();
2075-
int height = this.entries.size() * (this.entryHeight + 6) + 6;
2076-
if (mx < this.x || mx > this.x + this.width || my < this.y || my > this.y + height) {
2104+
double mx = event.x();
2105+
double my = event.y();
2106+
if (!this.contains(mx, my)) {
20772107
contextMenu = null;
20782108
return false;
20792109
}
2080-
int index = (my - this.y - 6) / (this.entryHeight + 6);
2081-
if (index >= 0 && index < this.entries.size()) {
2082-
try {
2083-
this.entries.get(index).action.run();
2084-
} catch (Exception e) {
2085-
LOGGER.warn("Context menu action failed", e);
2086-
}
2110+
int index = this.indexAt(mx, my);
2111+
if (index == -1) {
20872112
contextMenu = null;
20882113
return true;
20892114
}
2090-
return false;
2115+
try {
2116+
this.entries.get(index).action.run();
2117+
} catch (Exception e) {
2118+
LOGGER.warn("Context menu action failed", e);
2119+
}
2120+
contextMenu = null;
2121+
return true;
20912122
}
20922123
}
20932124

@@ -2155,6 +2186,14 @@ public boolean isMouseOver(int mouseX, int mouseY) {
21552186
return mouseX >= this.x && mouseX <= this.x + this.width() && mouseY >= this.y && mouseY <= this.y + this.height();
21562187
}
21572188

2189+
public boolean isContextHit(double mouseX, double mouseY, double padding) {
2190+
double minX = this.x - padding;
2191+
double minY = this.y - padding;
2192+
double maxX = this.x + this.width() + padding;
2193+
double maxY = this.y + this.height() + padding;
2194+
return mouseX >= minX && mouseX <= maxX && mouseY >= minY && mouseY <= maxY;
2195+
}
2196+
21582197
public net.minecraft.network.chat.Component getTooltip() {
21592198
// create a readable tooltip from the feature name (replace underscores)
21602199
String raw = this.feature.getName().replace('_', ' ');

0 commit comments

Comments
 (0)