diff options
author | xander <xander@isxander.dev> | 2022-09-03 13:42:49 +0100 |
---|---|---|
committer | xander <xander@isxander.dev> | 2022-09-03 13:42:49 +0100 |
commit | b775bae81d3ec0c8fb64b0c680e375b6bca4fe5f (patch) | |
tree | 55c1944af1e8453766703537807701712452a1a2 | |
parent | 8a29f957e99c957efd11d6c279e5f97639f05d9e (diff) | |
download | YetAnotherConfigLib-b775bae81d3ec0c8fb64b0c680e375b6bca4fe5f.tar.gz YetAnotherConfigLib-b775bae81d3ec0c8fb64b0c680e375b6bca4fe5f.tar.bz2 YetAnotherConfigLib-b775bae81d3ec0c8fb64b0c680e375b6bca4fe5f.zip |
add tooltips to action buttons and messages
4 files changed, 100 insertions, 68 deletions
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<OrderedText> 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<OrderedText> 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<CategoryWidget> categoryButtons; - public ButtonWidget finishedSaveButton, cancelResetButton, undoButton; + public final List<TooltipButtonWidget> 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<Integer> 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<Integer> 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; diff --git a/src/main/resources/assets/yet-another-config-lib/lang/en_us.json b/src/main/resources/assets/yet-another-config-lib/lang/en_us.json index 78945de..d3a1a17 100644 --- a/src/main/resources/assets/yet-another-config-lib/lang/en_us.json +++ b/src/main/resources/assets/yet-another-config-lib/lang/en_us.json @@ -9,9 +9,17 @@ "yacl.control.action.execute": "EXECUTE", "yacl.gui.save": "Save Changes", + "yacl.gui.save.tooltip": "Makes the changes made permanent.", "yacl.gui.finished": "Finished", + "yacl.gui.finished.tooltip": "Closes the GUI.", "yacl.gui.cancel": "Cancel", + "yacl.gui.cancel.tooltip": "Forgets pending changes and closes the GUI.", "yacl.gui.reset": "Reset", + "yacl.gui.reset.tooltip": "Resets all options to default. (This is reversible!)", "yacl.gui.undo": "Undo", - "yocl.gui.fail_apply": "Failed to apply" + "yacl.gui.undo.tooltip": "Reverts all options back to what they were before editing.", + "yocl.gui.fail_apply": "Failed to apply", + "yocl.gui.fail_apply.tooltip": "There was an error and the changes couldn't be applied.", + "yocl.gui.save_before_exit": "Save before exiting!", + "yocl.gui.save_before_exit.tooltip": "Save or cancel to exit the GUI." } |