From b775bae81d3ec0c8fb64b0c680e375b6bca4fe5f Mon Sep 17 00:00:00 2001 From: xander Date: Sat, 3 Sep 2022 13:42:49 +0100 Subject: add tooltips to action buttons and messages --- .../java/dev/isxander/yacl/gui/CategoryWidget.java | 35 ---------- .../dev/isxander/yacl/gui/TooltipButtonWidget.java | 49 ++++++++++++++ .../java/dev/isxander/yacl/gui/YACLScreen.java | 74 ++++++++++++---------- 3 files changed, 91 insertions(+), 67 deletions(-) delete mode 100644 src/main/java/dev/isxander/yacl/gui/CategoryWidget.java create mode 100644 src/main/java/dev/isxander/yacl/gui/TooltipButtonWidget.java (limited to 'src/main/java') diff --git a/src/main/java/dev/isxander/yacl/gui/CategoryWidget.java b/src/main/java/dev/isxander/yacl/gui/CategoryWidget.java deleted file mode 100644 index 04c4c22..0000000 --- a/src/main/java/dev/isxander/yacl/gui/CategoryWidget.java +++ /dev/null @@ -1,35 +0,0 @@ -package dev.isxander.yacl.gui; - -import dev.isxander.yacl.api.ConfigCategory; -import dev.isxander.yacl.impl.YACLConstants; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.widget.ButtonWidget; -import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.text.OrderedText; - -import java.util.List; - -public class CategoryWidget extends ButtonWidget { - public float hoveredTicks = 0; - public final List wrappedDescription; - private int prevMouseX, prevMouseY; - - public CategoryWidget(YACLScreen screen, ConfigCategory category, int x, int y, int width, int height) { - super(x, y, width, height, category.name(), btn -> screen.changeCategory(screen.categoryButtons.indexOf(btn))); - wrappedDescription = MinecraftClient.getInstance().textRenderer.wrapLines(category.tooltip(), screen.width / 2); - } - - @Override - public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { - super.render(matrices, mouseX, mouseY, delta); - - if (isHovered() && (!YACLConstants.HOVER_MOUSE_RESET || (prevMouseX == mouseX && prevMouseY == mouseY))) { - hoveredTicks += delta; - } else { - hoveredTicks = 0; - } - - prevMouseX = mouseX; - prevMouseY = mouseY; - } -} diff --git a/src/main/java/dev/isxander/yacl/gui/TooltipButtonWidget.java b/src/main/java/dev/isxander/yacl/gui/TooltipButtonWidget.java new file mode 100644 index 0000000..b8abbbd --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/TooltipButtonWidget.java @@ -0,0 +1,49 @@ +package dev.isxander.yacl.gui; + +import dev.isxander.yacl.impl.YACLConstants; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.text.OrderedText; +import net.minecraft.text.Text; + +import java.util.List; + +public class TooltipButtonWidget extends ButtonWidget { + private float hoveredTicks = 0; + private int prevMouseX, prevMouseY; + + private final Screen screen; + private List wrappedDescription; + + public TooltipButtonWidget(Screen screen, int x, int y, int width, int height, Text message, Text tooltip, PressAction onPress) { + super(x, y, width, height, message, onPress); + this.screen = screen; + setTooltip(tooltip); + } + + @Override + public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { + super.render(matrices, mouseX, mouseY, delta); + + if (isHovered() && (!YACLConstants.HOVER_MOUSE_RESET || (prevMouseX == mouseX && prevMouseY == mouseY))) { + hoveredTicks += delta; + } else { + hoveredTicks = 0; + } + + prevMouseX = mouseX; + prevMouseY = mouseY; + } + + public void renderTooltip(MatrixStack matrices, int mouseX, int mouseY) { + if (hoveredTicks >= YACLConstants.HOVER_TICKS) { + screen.renderOrderedTooltip(matrices, wrappedDescription, mouseX, mouseY); + } + } + + public void setTooltip(Text tooltip) { + wrappedDescription = MinecraftClient.getInstance().textRenderer.wrapLines(tooltip, screen.width / 2); + } +} diff --git a/src/main/java/dev/isxander/yacl/gui/YACLScreen.java b/src/main/java/dev/isxander/yacl/gui/YACLScreen.java index 35ec48a..5263c57 100644 --- a/src/main/java/dev/isxander/yacl/gui/YACLScreen.java +++ b/src/main/java/dev/isxander/yacl/gui/YACLScreen.java @@ -6,6 +6,7 @@ import dev.isxander.yacl.api.YetAnotherConfigLib; import dev.isxander.yacl.api.utils.Dimension; import dev.isxander.yacl.api.utils.OptionUtils; import dev.isxander.yacl.impl.YACLConstants; +import net.minecraft.client.gui.Element; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.widget.ButtonWidget; import net.minecraft.client.util.math.MatrixStack; @@ -23,10 +24,11 @@ public class YACLScreen extends Screen { private final Screen parent; public OptionListWidget optionList; - public final List categoryButtons; - public ButtonWidget finishedSaveButton, cancelResetButton, undoButton; + public final List categoryButtons; + public TooltipButtonWidget finishedSaveButton, cancelResetButton, undoButton; public Text saveButtonMessage; + public Text saveButtonTooltipMessage; private int saveButtonMessageTime; @@ -47,11 +49,12 @@ public class YACLScreen extends Screen { Dimension categoryDim = Dimension.ofInt(width / 3 / 2, padding, columnWidth - padding * 2, 20); int idx = 0; for (ConfigCategory category : config.categories()) { - CategoryWidget categoryWidget = new CategoryWidget( + TooltipButtonWidget categoryWidget = new TooltipButtonWidget( this, - category, categoryDim.x() - categoryDim.width() / 2, categoryDim.y(), - categoryDim.width(), categoryDim.height() + categoryDim.width(), categoryDim.height(), + category.name(), category.tooltip(), + (btn) -> changeCategory(categoryButtons.indexOf(btn)) ); if (idx == currentCategoryIdx) categoryWidget.active = false; @@ -63,7 +66,7 @@ public class YACLScreen extends Screen { } Dimension actionDim = Dimension.ofInt(width / 3 / 2, height - padding - 20, columnWidth - padding * 2, 20); - finishedSaveButton = new ButtonWidget(actionDim.x() - actionDim.width() / 2, actionDim.y(), actionDim.width(), actionDim.height(), Text.empty(), (btn) -> { + finishedSaveButton = new TooltipButtonWidget(this, actionDim.x() - actionDim.width() / 2, actionDim.y(), actionDim.width(), actionDim.height(), Text.empty(), Text.empty(), (btn) -> { saveButtonMessage = null; if (pendingChanges()) { @@ -71,14 +74,14 @@ public class YACLScreen extends Screen { OptionUtils.forEachOptions(config, option -> { if (option.changed()) { YACLConstants.LOGGER.error("Option '{}' was saved as '{}' but the changes don't seem to have applied.", option.name().getString(), option.pendingValue()); - setSaveButtonMessage(Text.translatable("yocl.gui.fail_apply").formatted(Formatting.RED)); + setSaveButtonMessage(Text.translatable("yocl.gui.fail_apply").formatted(Formatting.RED), Text.translatable("yocl.gui.fail_apply.tooltip")); } }); config.saveFunction().run(); } else close(); }); actionDim.expand(-actionDim.width() / 2 - 2, 0).move(-actionDim.width() / 2 - 2, -22); - cancelResetButton = new ButtonWidget(actionDim.x() - actionDim.width() / 2, actionDim.y(), actionDim.width(), actionDim.height(), Text.translatable("yacl.gui.cancel"), (btn) -> { + cancelResetButton = new TooltipButtonWidget(this, actionDim.x() - actionDim.width() / 2, actionDim.y(), actionDim.width(), actionDim.height(), Text.empty(), Text.empty(), (btn) -> { if (pendingChanges()) { OptionUtils.forEachOptions(config, Option::forgetPendingValue); close(); @@ -88,7 +91,7 @@ public class YACLScreen extends Screen { }); actionDim.move(actionDim.width() + 4, 0); - undoButton = new ButtonWidget(actionDim.x() - actionDim.width() / 2, actionDim.y(), actionDim.width(), actionDim.height(), Text.translatable("yacl.gui.undo"), (btn) -> { + undoButton = new TooltipButtonWidget(this, actionDim.x() - actionDim.width() / 2, actionDim.y(), actionDim.width(), actionDim.height(), Text.translatable("yacl.gui.undo"), Text.translatable("yacl.gui.undo.tooltip"), (btn) -> { OptionUtils.forEachOptions(config, Option::forgetPendingValue); }); @@ -112,13 +115,33 @@ public class YACLScreen extends Screen { optionList.render(matrices, mouseX, mouseY, delta); - for (CategoryWidget categoryWidget : categoryButtons) { - if (categoryWidget.hoveredTicks > YACLConstants.HOVER_TICKS) { - renderOrderedTooltip(matrices, categoryWidget.wrappedDescription, mouseX, mouseY); + for (Element child : children()) { + if (child instanceof TooltipButtonWidget tooltipButtonWidget) { + tooltipButtonWidget.renderTooltip(matrices, mouseX, mouseY); } } } + public void changeCategory(int idx) { + int currentIndex = 0; + for (ButtonWidget categoryWidget : categoryButtons) { + categoryWidget.active = currentIndex != idx; + currentIndex++; + } + currentCategoryIdx = idx; + refreshGUI(); + } + + private void updateActionAvailability() { + boolean pendingChanges = pendingChanges(); + + undoButton.active = pendingChanges; + finishedSaveButton.setMessage(pendingChanges ? Text.translatable("yacl.gui.save") : Text.translatable("yacl.gui.finished")); + finishedSaveButton.setTooltip(pendingChanges ? Text.translatable("yacl.gui.save.tooltip") : Text.translatable("yacl.gui.finished.tooltip")); + cancelResetButton.setMessage(pendingChanges ? Text.translatable("yacl.gui.cancel") : Text.translatable("yacl.gui.reset")); + cancelResetButton.setTooltip(pendingChanges ? Text.translatable("yacl.gui.cancel.tooltip") : Text.translatable("yacl.gui.reset.tooltip")); + } + @Override public void tick() { updateActionAvailability(); @@ -126,29 +149,24 @@ public class YACLScreen extends Screen { if (saveButtonMessage != null) { if (saveButtonMessageTime > 140) { saveButtonMessage = null; + saveButtonTooltipMessage = null; saveButtonMessageTime = 0; } else { saveButtonMessageTime++; finishedSaveButton.setMessage(saveButtonMessage); + if (saveButtonTooltipMessage != null) { + finishedSaveButton.setTooltip(saveButtonTooltipMessage); + } } } } - private void setSaveButtonMessage(Text message) { + private void setSaveButtonMessage(Text message, Text tooltip) { saveButtonMessage = message; + saveButtonTooltipMessage = tooltip; saveButtonMessageTime = 0; } - public void changeCategory(int idx) { - int currentIndex = 0; - for (ButtonWidget categoryWidget : categoryButtons) { - categoryWidget.active = currentIndex != idx; - currentIndex++; - } - currentCategoryIdx = idx; - refreshGUI(); - } - private boolean pendingChanges() { AtomicBoolean pendingChanges = new AtomicBoolean(false); OptionUtils.consumeOptions(config, (option) -> { @@ -162,14 +180,6 @@ public class YACLScreen extends Screen { return pendingChanges.get(); } - private void updateActionAvailability() { - boolean pendingChanges = pendingChanges(); - - undoButton.active = pendingChanges; - finishedSaveButton.setMessage(pendingChanges ? Text.translatable("yacl.gui.save") : Text.translatable("yacl.gui.finished")); - cancelResetButton.setMessage(pendingChanges ? Text.translatable("yacl.gui.cancel") : Text.translatable("yacl.gui.reset")); - } - private void refreshGUI() { init(client, width, height); } @@ -177,7 +187,7 @@ public class YACLScreen extends Screen { @Override public boolean shouldCloseOnEsc() { if (pendingChanges()) { - setSaveButtonMessage(finishedSaveButton.getMessage().copy().formatted(Formatting.GREEN, Formatting.BOLD)); + setSaveButtonMessage(Text.translatable("yocl.gui.save_before_exit").formatted(Formatting.RED), Text.translatable("yocl.gui.save_before_exit.tooltip")); return false; } return true; -- cgit