From e0c09e24821a7d78f0dfa4aeb574ec96d86d021f Mon Sep 17 00:00:00 2001 From: Adam Viola Date: Sun, 20 Jun 2021 16:00:45 -0400 Subject: [PATCH] Added swap-to-last-brightness functionality to dedicated brightness keybinds --- .gitignore | 1 + gradle.properties | 2 +- .../boostedbrightness/BoostedBrightness.java | 89 ++++++++++++++----- .../mixin/MixinDoubleOption.java | 8 +- .../ui/BrightnessListWidget.java | 21 +++-- .../ui/BrightnessSliderWidget.java | 7 +- src/main/resources/fabric.mod.json | 2 +- 7 files changed, 85 insertions(+), 45 deletions(-) diff --git a/.gitignore b/.gitignore index 550b373..1f0aa4c 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,4 @@ bin/ # fabric run/ +remappedSrc/ diff --git a/gradle.properties b/gradle.properties index f1d56df..9fafa65 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ org.gradle.jvmargs=-Xmx1G # Mod Properties - mod_version = 2.0.0 + mod_version = 2.1.0 maven_group = net.boostedbrightness archives_base_name = boosted-brightness diff --git a/src/main/java/net/boostedbrightness/BoostedBrightness.java b/src/main/java/net/boostedbrightness/BoostedBrightness.java index fe5beb3..acf4c1a 100644 --- a/src/main/java/net/boostedbrightness/BoostedBrightness.java +++ b/src/main/java/net/boostedbrightness/BoostedBrightness.java @@ -29,8 +29,9 @@ public class BoostedBrightness implements ClientModInitializer { public static double maxBrightness = 12.0; private static double step = 0.1; - public static int selectedBrightness = 0; public static ArrayList brightnesses; + private static int brightnessIndex = 0; + private static int lastBrightnessIndex = 0; private static final KeyBinding NEXT_BIND = new KeyBinding( "key.boosted-brightness.next", @@ -55,12 +56,15 @@ public class BoostedBrightness implements ClientModInitializer { private static final KeyBinding[] SELECT_BINDS = new KeyBinding[MAX_BRIGHTNESSES]; + public static MinecraftClient client; + @Override public void onInitializeClient() { KeyBindingHelper.registerKeyBinding(NEXT_BIND); KeyBindingHelper.registerKeyBinding(RAISE_BIND); KeyBindingHelper.registerKeyBinding(LOWER_BIND); + // Register binds for each brightness setting for (int i = 0; i < MAX_BRIGHTNESSES; i++) { SELECT_BINDS[i] = new KeyBinding( "key.boosted-brightness.select" + (i + 1), @@ -72,8 +76,42 @@ public void onInitializeClient() { KeyBindingHelper.registerKeyBinding(SELECT_BINDS[i]); } - loadConfig(); ClientTickEvents.END_CLIENT_TICK.register(this::onEndTick); + loadConfig(); + client = MinecraftClient.getInstance(); + } + + public static int numBrightnesses() { + return brightnesses.size(); + } + + public static int getBrightnessIndex() { + return brightnessIndex; + } + + public static void setBrightnessIndex(int index) { + brightnessIndex = index; + client.options.gamma = getBrightness(); + } + + public static double getBrightness() { + return brightnesses.get(brightnessIndex); + } + + public static double getBrightness(int index) { + return brightnesses.get(index); + } + + public static void changeBrightness(double brightness) { + brightnesses.set(getBrightnessIndex(), brightness); + client.options.gamma = getBrightness(); + } + + public static void changeBrightness(int index, double brightness) { + if (index == brightnessIndex) + changeBrightness(brightness); + else + brightnesses.set(index, brightness); } private void loadConfig() { @@ -88,19 +126,27 @@ private void loadConfig() { asDouble(config.get(String.valueOf(i)), brightness -> brightnesses.add(brightness)); } - asInt(config.get("selected"), selected -> selectedBrightness = selected - 1); - selectedBrightness = Math.max(0, Math.min(brightnesses.size() - 1, selectedBrightness)); + asInt(config.get("selected"), selected -> brightnessIndex = selected - 1); + brightnessIndex = Math.max(0, Math.min(numBrightnesses() - 1, brightnessIndex)); + + if (config.has("last")) { + asInt(config.get("last"), last -> lastBrightnessIndex = last - 1); + lastBrightnessIndex = Math.max(0, Math.min(numBrightnesses() - 1, lastBrightnessIndex)); + } else { + lastBrightnessIndex = 0; + } } catch (IOException | JsonSyntaxException ex) { logException(ex, "Failed to load BoostedBrightness config"); } + // If the config file fails to properly load, default to 2 brightness levels if (brightnesses == null || brightnesses.size() < 2) { brightnesses = new ArrayList<>(); brightnesses.add(1.0); brightnesses.add(maxBrightness); - - selectedBrightness = 0; + brightnessIndex = 0; + lastBrightnessIndex = 0; } } @@ -109,7 +155,9 @@ public static void saveConfig() { config.addProperty("min", minBrightness); config.addProperty("max", maxBrightness); config.addProperty("step", step); - config.addProperty("selected", selectedBrightness + 1); + // Store selectedBrightness + 1 for human readability + config.addProperty("selected", brightnessIndex + 1); + config.addProperty("last", lastBrightnessIndex + 1); for (int i = 0; i < brightnesses.size(); i++) { config.addProperty(String.valueOf(i + 1), brightnesses.get(i)); @@ -144,8 +192,8 @@ private void showOverlay(MinecraftClient client) { new TranslatableText( "overlay.boosted-brightness.change", new Object[]{ - selectedBrightness + 1, - Math.round(brightnesses.get(selectedBrightness) * 100) + getBrightnessIndex() + 1, + Math.round(getBrightness() * 100) } ).styled(s -> s.withColor(GREEN)), false @@ -155,18 +203,17 @@ private void showOverlay(MinecraftClient client) { private void onEndTick(MinecraftClient client) { // Check next brightness keybind while (NEXT_BIND.wasPressed()) { - selectedBrightness = (selectedBrightness + 1) % brightnesses.size(); - client.options.gamma = brightnesses.get(selectedBrightness); - + lastBrightnessIndex = getBrightnessIndex(); + setBrightnessIndex((lastBrightnessIndex + 1) % numBrightnesses()); showOverlay(client); } - + // Check set brightness keybind - for (int i = 0; i < brightnesses.size(); i++) { - if (SELECT_BINDS[i].isPressed()) { - selectedBrightness = i; - client.options.gamma = brightnesses.get(selectedBrightness); - + for (int i = 0; i < numBrightnesses(); i++) { + while (SELECT_BINDS[i].wasPressed()) { + int nextBrightnessIndex = (i != brightnessIndex) ? i : lastBrightnessIndex; + lastBrightnessIndex = getBrightnessIndex(); + setBrightnessIndex(nextBrightnessIndex); showOverlay(client); } } @@ -182,10 +229,8 @@ private void onEndTick(MinecraftClient client) { // Raise/lower selected brightness if (offset != 0) { - double brightness = Math.max(minBrightness, Math.min(maxBrightness, client.options.gamma + offset)); - brightnesses.set(selectedBrightness, brightness); - client.options.gamma = brightness; - + double brightness = Math.max(minBrightness, Math.min(maxBrightness, getBrightness() + offset)); + changeBrightness(brightness); showOverlay(client); } } diff --git a/src/main/java/net/boostedbrightness/mixin/MixinDoubleOption.java b/src/main/java/net/boostedbrightness/mixin/MixinDoubleOption.java index 448e6b3..7ba4386 100644 --- a/src/main/java/net/boostedbrightness/mixin/MixinDoubleOption.java +++ b/src/main/java/net/boostedbrightness/mixin/MixinDoubleOption.java @@ -53,10 +53,10 @@ private void init(String key, double min, double max, float step, Function entries = this.children(); BoostedBrightness.brightnesses.add(1.0); - int size = BoostedBrightness.brightnesses.size(); + int size = BoostedBrightness.numBrightnesses(); entries.add(size - 1, BrightnessEntry.create(size - 1, this.width, this)); @@ -63,8 +63,8 @@ public void removeBrightness(int index) { if (oldSize == BoostedBrightness.MAX_BRIGHTNESSES) { entries.add(BrightnessEntry.create(-1, this.width, this)); } - if (BoostedBrightness.selectedBrightness == BoostedBrightness.brightnesses.size()) { - BoostedBrightness.selectedBrightness -= 1; + if (BoostedBrightness.getBrightnessIndex() == BoostedBrightness.numBrightnesses()) { + BoostedBrightness.setBrightnessIndex(BoostedBrightness.getBrightnessIndex() - 1); } } @@ -95,7 +95,7 @@ public Optional getHoveredButton(double mouseX, double mouseY) } protected boolean isSelectedEntry(int index) { - return BoostedBrightness.selectedBrightness == index; + return BoostedBrightness.getBrightnessIndex() == index; } public static class BrightnessEntry extends ElementListWidget.Entry { @@ -114,7 +114,7 @@ public static BrightnessListWidget.BrightnessEntry create(int index, int width, ArrayList widgets = new ArrayList<>(); if (index >= 0) { - widgets.add(new BrightnessSliderWidget(index, width / 2 - 120, 0, 240, 20, BrightnessSliderWidget.sliderValue(BoostedBrightness.brightnesses.get(index)))); + widgets.add(new BrightnessSliderWidget(index, width / 2 - 120, 0, 240, 20, BrightnessSliderWidget.sliderValue(BoostedBrightness.getBrightness(index)))); if (index >= 2) widgets.add(new ButtonWidget(width / 2 + 120 + 5, 0, 20, 20, new LiteralText("X"), (buttonWidget) -> { listWidget.removeBrightness(index); })); @@ -152,8 +152,7 @@ public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) { } } if (!mouseOnButton && this.index >= 0) { - BoostedBrightness.selectedBrightness = this.index; - listWidget.client.options.gamma = BoostedBrightness.brightnesses.get(this.index); + BoostedBrightness.setBrightnessIndex(this.index); } return super.mouseClicked(mouseX, mouseY, mouseButton); diff --git a/src/main/java/net/boostedbrightness/ui/BrightnessSliderWidget.java b/src/main/java/net/boostedbrightness/ui/BrightnessSliderWidget.java index 0727dd1..ea2bacc 100644 --- a/src/main/java/net/boostedbrightness/ui/BrightnessSliderWidget.java +++ b/src/main/java/net/boostedbrightness/ui/BrightnessSliderWidget.java @@ -1,7 +1,6 @@ package net.boostedbrightness.ui; import net.boostedbrightness.BoostedBrightness; -import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.widget.SliderWidget; import net.minecraft.text.LiteralText; import net.minecraft.text.Text; @@ -33,11 +32,7 @@ public void updateValue() { @Override protected void applyValue() { - BoostedBrightness.brightnesses.set(this.index, brightness()); - if (this.index == BoostedBrightness.selectedBrightness) { - MinecraftClient client = MinecraftClient.getInstance(); - client.options.gamma = brightness(); - } + BoostedBrightness.changeBrightness(this.index, brightness()); } @Override diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index ee6514a..a1efdfe 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -1,7 +1,7 @@ { "schemaVersion": 1, "id": "boostedbrightness", - "version": "2.0.0", + "version": "2.1.0", "name": "Boosted Brightness", "description": "Allows you to set your brightness beyond default levels.",