Skip to content

Commit bd11cc0

Browse files
Add dropdown scrolling and allow empty string and negative sign in coordinates
1 parent ee6664d commit bd11cc0

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed

src/main/java/de/hysky/skyblocker/skyblock/waypoint/AbstractWaypointsScreen.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public AbstractWaypointsScreen(Text title, T parent, Multimap<String, WaypointCa
3737
protected void init() {
3838
super.init();
3939
waypointsListWidget = addDrawableChild(new WaypointsListWidget(client, this, width, height - 96, 32, 24));
40-
islandWidget = addDrawableChild(new DropdownWidget<>(client, width - 160, 8, 150, Arrays.asList(Location.values()), this::islandChanged, Location.from(island)));
40+
islandWidget = addDrawableChild(new DropdownWidget<>(client, width - 160, 8, 150, height - 8, Arrays.asList(Location.values()), this::islandChanged, Location.from(island)));
4141
}
4242

4343
@Override
@@ -50,6 +50,14 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
5050
return mouseClicked;
5151
}
5252

53+
@Override
54+
public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) {
55+
if (islandWidget.isMouseOver(mouseX, mouseY) && islandWidget.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount)) {
56+
return true;
57+
}
58+
return super.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount);
59+
}
60+
5361
protected void islandChanged(Location location) {
5462
island = location.id();
5563
waypointsListWidget.setIsland(island);

src/main/java/de/hysky/skyblocker/skyblock/waypoint/DropdownWidget.java

+27-12
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public class DropdownWidget<T> extends ElementListWidget<DropdownWidget.Entry<T>
2020
protected T selected;
2121
protected boolean open;
2222

23-
public DropdownWidget(MinecraftClient minecraftClient, int x, int y, int width, List<T> entries, Consumer<T> selectCallback, T selected) {
24-
super(minecraftClient, width, (entries.size() + 1) * ENTRY_HEIGHT + 8, y, ENTRY_HEIGHT);
23+
public DropdownWidget(MinecraftClient minecraftClient, int x, int y, int width, int maxHeight, List<T> entries, Consumer<T> selectCallback, T selected) {
24+
super(minecraftClient, width, Math.min((entries.size() + 1) * ENTRY_HEIGHT + 8, maxHeight), y, ENTRY_HEIGHT);
2525
setX(x);
2626
this.entries = entries;
2727
this.selectCallback = selectCallback;
@@ -58,29 +58,44 @@ public void renderWidget(DrawContext context, int mouseX, int mouseY, float delt
5858
context.getMatrices().push();
5959
context.getMatrices().translate(0, 0, 100);
6060

61-
context.fill(getX(), getY(), getX() + width, getY() + headerHeight, 0xFF000000);
62-
context.drawHorizontalLine(getX(), getX() + width, getY(), 0xFFFFFFFF);
63-
context.drawHorizontalLine(getX(), getX() + width, getY() + headerHeight, 0xFFFFFFFF);
64-
context.drawVerticalLine(getX(), getY(), getY() + headerHeight, 0xFFFFFFFF);
65-
context.drawVerticalLine(getX() + width, getY(), getY() + headerHeight, 0xFFFFFFFF);
61+
int y = getY() - (int) getScrollAmount();
62+
int height = getMaxPosition();
63+
64+
context.fill(getX(), y, getX() + width, y + headerHeight, 0xFF000000);
65+
context.drawHorizontalLine(getX(), getX() + width, y, 0xFFFFFFFF);
66+
context.drawHorizontalLine(getX(), getX() + width, y + headerHeight, 0xFFFFFFFF);
67+
context.drawVerticalLine(getX(), y, y + headerHeight, 0xFFFFFFFF);
68+
context.drawVerticalLine(getX() + width, y, y + headerHeight, 0xFFFFFFFF);
6669

6770
if (open) {
68-
context.fill(getX(), getY() + headerHeight + 1, getX() + width, getY() + height, 0xFF000000);
69-
context.drawHorizontalLine(getX(), getX() + width, getY() + height, 0xFFFFFFFF);
70-
context.drawVerticalLine(getX(), getY() + headerHeight, getY() + height, 0xFFFFFFFF);
71-
context.drawVerticalLine(getX() + width, getY() + headerHeight, getY() + height, 0xFFFFFFFF);
71+
context.fill(getX(), y + headerHeight + 1, getX() + width, y + height, 0xFF000000);
72+
context.drawHorizontalLine(getX(), getX() + width, y + height, 0xFFFFFFFF);
73+
context.drawVerticalLine(getX(), y + headerHeight, y + height, 0xFFFFFFFF);
74+
context.drawVerticalLine(getX() + width, y + headerHeight, y + height, 0xFFFFFFFF);
7275

7376
super.renderWidget(context, mouseX, mouseY, delta);
7477
} else {
75-
renderHeader(context, getRowLeft(), getY() + 4 - (int) getScrollAmount());
78+
renderHeader(context, getRowLeft(), y + 4);
7679
}
7780

7881
context.getMatrices().pop();
7982
}
8083

84+
@Override
85+
protected void drawMenuListBackground(DrawContext context) {}
86+
87+
@Override
88+
protected void drawHeaderAndFooterSeparators(DrawContext context) {}
89+
90+
@Override
91+
public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) {
92+
return open && super.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount);
93+
}
94+
8195
protected void select(T entry) {
8296
selected = entry;
8397
open = false;
98+
setScrollAmount(0);
8499
if (selected != prevSelected) {
85100
selectCallback.accept(entry);
86101
prevSelected = selected;

src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsListWidget.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ private void updateName(String name) {
239239

240240
private boolean checkInt(String string) {
241241
try {
242-
Integer.parseInt(string);
242+
parseEmptiableInt(string);
243243
return true;
244244
} catch (NumberFormatException e) {
245245
return false;
@@ -261,7 +261,7 @@ private void updateZ(String zString) {
261261
private void updateInt(String newValueString, int currentValue, Int2ObjectFunction<NamedWaypoint> wither) {
262262
try {
263263
int index = category.category.waypoints().indexOf(waypoint);
264-
int newValue = Integer.parseInt(newValueString);
264+
int newValue = parseEmptiableInt(newValueString);
265265
if (newValue == currentValue) return;
266266
waypoint = wither.apply(newValue);
267267
if (index >= 0) {
@@ -275,7 +275,7 @@ private void updateInt(String newValueString, int currentValue, Int2ObjectFuncti
275275
private void updateColor(String colorString) {
276276
try {
277277
int index = category.category.waypoints().indexOf(waypoint);
278-
int colorInt = Integer.parseInt(colorString, 16);
278+
int colorInt = parseEmptiableInt(colorString, 16);
279279
float[] colorComponents = {((colorInt & 0x00FF0000) >> 16) / 255f, ((colorInt & 0x0000FF00) >> 8) / 255f, (colorInt & 0x000000FF) / 255f};
280280
float alpha = ((colorInt & 0xFF000000) >>> 24) / 255f;
281281
if (Arrays.equals(waypoint.getColorComponents(), colorComponents) && waypoint.alpha == alpha) return;
@@ -288,6 +288,15 @@ private void updateColor(String colorString) {
288288
}
289289
}
290290

291+
private int parseEmptiableInt(String value) throws NumberFormatException {
292+
return value.isEmpty() || value.equals("-") ? 0 : Integer.parseInt(value);
293+
}
294+
295+
@SuppressWarnings("SameParameterValue")
296+
private int parseEmptiableInt(String value, int radix) throws NumberFormatException {
297+
return value.isEmpty() || value.equals("-") ? 0 : Integer.parseInt(value, radix);
298+
}
299+
291300
@Override
292301
public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) {
293302
context.drawTextWithShadow(client.textRenderer, "X:", width / 2 - 56, y + 6, 0xFFFFFF);

0 commit comments

Comments
 (0)