Skip to content

Commit

Permalink
Fix delete shortcut by keeping ShortcutsConfigListWidget instance acr…
Browse files Browse the repository at this point in the history
…oss initializations and updating text field positions (#660)
  • Loading branch information
kevinthegreat1 authored Apr 21, 2024
1 parent 01b4ff4 commit 9e1fb6e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ protected void addShortcutAfterSelected() {
getCategory().ifPresent(category -> children().add(children().indexOf(getSelectedOrNull()) + 1, new ShortcutEntry(category)));
}

protected void updatePositions() {
for (AbstractShortcutEntry child : children()) {
child.updatePositions();
}
}

@Override
protected boolean removeEntry(AbstractShortcutEntry entry) {
return super.removeEntry(entry);
Expand All @@ -90,10 +96,11 @@ private Stream<ShortcutEntry> getNotEmptyShortcuts() {
return children().stream().filter(ShortcutEntry.class::isInstance).map(ShortcutEntry.class::cast).filter(ShortcutEntry::isNotEmpty);
}

protected static abstract class AbstractShortcutEntry extends ElementListWidget.Entry<AbstractShortcutEntry> {
public static abstract class AbstractShortcutEntry extends ElementListWidget.Entry<AbstractShortcutEntry> {
protected void updatePositions() {}
}

private class ShortcutCategoryEntry extends AbstractShortcutEntry {
protected class ShortcutCategoryEntry extends AbstractShortcutEntry {
private final Map<String, String> shortcutsMap;
private final Text targetName;
private final Text replacementName;
Expand Down Expand Up @@ -236,5 +243,12 @@ public void render(DrawContext context, int index, int y, int x, int entryWidth,
replacement.render(context, mouseX, mouseY, tickDelta);
context.drawCenteredTextWithShadow(client.textRenderer, "→", width / 2, y + 5, 0xFFFFFF);
}

@Override
protected void updatePositions() {
super.updatePositions();
target.setX(width / 2 - 160);
replacement.setX(width / 2 + 10);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ public class ShortcutsConfigScreen extends Screen {
private ButtonWidget buttonDelete;
private ButtonWidget buttonNew;
private ButtonWidget buttonDone;
private boolean initialized;
private double scrollAmount;
private final Screen parent;

public ShortcutsConfigScreen() {
this(null);
this(null);
}

public ShortcutsConfigScreen(Screen parent) {
Expand All @@ -36,15 +37,21 @@ public void setTooltip(Text tooltip) {
@Override
protected void init() {
super.init();
shortcutsConfigListWidget = new ShortcutsConfigListWidget(client, this, width, height - 96, 32, 25);
if (initialized) {
shortcutsConfigListWidget.setDimensions(width, height - 96);
shortcutsConfigListWidget.updatePositions();
} else {
shortcutsConfigListWidget = new ShortcutsConfigListWidget(client, this, width, height - 96, 32, 25);
initialized = true;
}
addDrawableChild(shortcutsConfigListWidget);
GridWidget gridWidget = new GridWidget();
gridWidget.getMainPositioner().marginX(5).marginY(2);
GridWidget.Adder adder = gridWidget.createAdder(2);
buttonDelete = ButtonWidget.builder(Text.translatable("selectServer.delete"), button -> {
if (client != null && shortcutsConfigListWidget.getSelectedOrNull() instanceof ShortcutsConfigListWidget.ShortcutEntry shortcutEntry) {
scrollAmount = shortcutsConfigListWidget.getScrollAmount();
client.setScreen(new ConfirmScreen(this::deleteEntry, Text.translatable("skyblocker.shortcuts.deleteQuestion"), Text.translatable("skyblocker.shortcuts.deleteWarning", shortcutEntry), Text.translatable("selectServer.deleteButton"), ScreenTexts.CANCEL));
client.setScreen(new ConfirmScreen(this::deleteEntry, Text.translatable("skyblocker.shortcuts.deleteQuestion"), Text.stringifiedTranslatable("skyblocker.shortcuts.deleteWarning", shortcutEntry), Text.translatable("selectServer.deleteButton"), ScreenTexts.CANCEL));
}
}).build();
adder.add(buttonDelete);
Expand Down Expand Up @@ -86,16 +93,18 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) {

@Override
public void close() {
if (client != null && shortcutsConfigListWidget.hasChanges()) {
client.setScreen(new ConfirmScreen(confirmedAction -> {
if (confirmedAction) {
this.client.setScreen(parent);
} else {
client.setScreen(this);
}
}, Text.translatable("text.skyblocker.quit_config"), Text.translatable("text.skyblocker.quit_config_sure"), Text.translatable("text.skyblocker.quit_discard"), ScreenTexts.CANCEL));
} else {
this.client.setScreen(parent);
if (client != null) {
if (shortcutsConfigListWidget.hasChanges()) {
client.setScreen(new ConfirmScreen(confirmedAction -> {
if (confirmedAction) {
this.client.setScreen(parent);
} else {
client.setScreen(this);
}
}, Text.translatable("text.skyblocker.quit_config"), Text.translatable("text.skyblocker.quit_config_sure"), Text.translatable("text.skyblocker.quit_discard"), ScreenTexts.CANCEL));
} else {
this.client.setScreen(parent);
}
}
}

Expand Down

0 comments on commit 9e1fb6e

Please sign in to comment.