aboutsummaryrefslogtreecommitdiff
path: root/src/client/java/dev/isxander/yacl
diff options
context:
space:
mode:
authorisXander <xander@isxander.dev>2022-12-20 13:31:31 +0000
committerisXander <xander@isxander.dev>2022-12-20 13:31:31 +0000
commit33deb85b9b425cef83dadd63954b7d0d0fb8a841 (patch)
tree5484d44489912a8d90be3e466ab8a29bc3c9047e /src/client/java/dev/isxander/yacl
parentc90bf700201e18bfb42a27590809c680d05f4a46 (diff)
downloadYetAnotherConfigLib-33deb85b9b425cef83dadd63954b7d0d0fb8a841.tar.gz
YetAnotherConfigLib-33deb85b9b425cef83dadd63954b7d0d0fb8a841.tar.bz2
YetAnotherConfigLib-33deb85b9b425cef83dadd63954b7d0d0fb8a841.zip
move button actions into separate methods and log error when value mismatch.
Diffstat (limited to 'src/client/java/dev/isxander/yacl')
-rw-r--r--src/client/java/dev/isxander/yacl/gui/YACLScreen.java112
1 files changed, 78 insertions, 34 deletions
diff --git a/src/client/java/dev/isxander/yacl/gui/YACLScreen.java b/src/client/java/dev/isxander/yacl/gui/YACLScreen.java
index a1dc08e..f2bc4e8 100644
--- a/src/client/java/dev/isxander/yacl/gui/YACLScreen.java
+++ b/src/client/java/dev/isxander/yacl/gui/YACLScreen.java
@@ -6,6 +6,7 @@ import dev.isxander.yacl.api.utils.Dimension;
import dev.isxander.yacl.api.utils.MutableDimension;
import dev.isxander.yacl.api.utils.OptionUtils;
import dev.isxander.yacl.gui.utils.GuiUtils;
+import dev.isxander.yacl.impl.utils.YACLConstants;
import net.minecraft.client.font.MultilineText;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawableHelper;
@@ -53,42 +54,48 @@ public class YACLScreen extends Screen {
int paddedWidth = columnWidth - padding * 2;
MutableDimension<Integer> actionDim = Dimension.ofInt(width / 3 / 2, height - padding - 20, paddedWidth, 20);
- finishedSaveButton = new TooltipButtonWidget(this, actionDim.x() - actionDim.width() / 2, actionDim.y(), actionDim.width(), actionDim.height(), Text.empty(), Text.empty(), (btn) -> {
- saveButtonMessage = null;
-
- if (pendingChanges()) {
- Set<OptionFlag> flags = new HashSet<>();
- OptionUtils.forEachOptions(config, option -> {
- if (option.applyValue()) {
- flags.addAll(option.flags());
- }
- });
- OptionUtils.forEachOptions(config, option -> {
- if (option.changed()) {
- option.forgetPendingValue();
- }
- });
- config.saveFunction().run();
-
- flags.forEach(flag -> flag.accept(client));
- } else close();
- });
+ finishedSaveButton = new TooltipButtonWidget(
+ this,
+ actionDim.x() - actionDim.width() / 2,
+ actionDim.y(),
+ actionDim.width(),
+ actionDim.height(),
+ Text.empty(),
+ Text.empty(),
+ btn -> finishOrSave()
+ );
actionDim.expand(-actionDim.width() / 2 - 2, 0).move(-actionDim.width() / 2 - 2, -22);
- cancelResetButton = new TooltipButtonWidget(this, actionDim.x() - actionDim.width() / 2, actionDim.y(), actionDim.width(), actionDim.height(), Text.empty(), Text.empty(), (btn) -> {
- if (pendingChanges()) { // if pending changes, button acts as a cancel button
- OptionUtils.forEachOptions(config, Option::forgetPendingValue);
- close();
- } else { // if not, button acts as a reset button
- OptionUtils.forEachOptions(config, Option::requestSetDefault);
- }
-
- });
+ cancelResetButton = new TooltipButtonWidget(
+ this,
+ actionDim.x() - actionDim.width() / 2,
+ actionDim.y(),
+ actionDim.width(),
+ actionDim.height(),
+ Text.empty(),
+ Text.empty(),
+ btn -> cancelOrReset()
+ );
actionDim.move(actionDim.width() + 4, 0);
- 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);
- });
-
- searchFieldWidget = new SearchFieldWidget(this, textRenderer, width / 3 / 2 - paddedWidth / 2 + 1, undoButton.getY() - 22, paddedWidth - 2, 18, Text.translatable("gui.recipebook.search_hint"), Text.translatable("gui.recipebook.search_hint"));
+ 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 -> undo()
+ );
+
+ searchFieldWidget = new SearchFieldWidget(
+ this,
+ textRenderer,
+ width / 3 / 2 - paddedWidth / 2 + 1,
+ undoButton.getY() - 22,
+ paddedWidth - 2, 18,
+ Text.translatable("gui.recipebook.search_hint"),
+ Text.translatable("gui.recipebook.search_hint")
+ );
categoryList = new CategoryListWidget(client, this, width, height);
addSelectableChild(categoryList);
@@ -124,6 +131,43 @@ public class YACLScreen extends Screen {
}
}
+ private void finishOrSave() {
+ saveButtonMessage = null;
+
+ if (pendingChanges()) {
+ Set<OptionFlag> flags = new HashSet<>();
+ OptionUtils.forEachOptions(config, option -> {
+ if (option.applyValue()) {
+ flags.addAll(option.flags());
+ }
+ });
+ OptionUtils.forEachOptions(config, option -> {
+ if (option.changed()) {
+ // if still changed after applying, reset to the current value from binding
+ // as something has gone wrong.
+ option.forgetPendingValue();
+ YACLConstants.LOGGER.error("Option '{}' value mismatch after applying! Reset to binding's getter.", option.name().getString());
+ }
+ });
+ config.saveFunction().run();
+
+ flags.forEach(flag -> flag.accept(client));
+ } else close();
+ }
+
+ private void cancelOrReset() {
+ if (pendingChanges()) { // if pending changes, button acts as a cancel button
+ OptionUtils.forEachOptions(config, Option::forgetPendingValue);
+ close();
+ } else { // if not, button acts as a reset button
+ OptionUtils.forEachOptions(config, Option::requestSetDefault);
+ }
+ }
+
+ private void undo() {
+ OptionUtils.forEachOptions(config, Option::forgetPendingValue);
+ }
+
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (optionList.keyPressed(keyCode, scanCode, modifiers)) {