Skip to content

Commit

Permalink
Added swap-to-last-brightness functionality to dedicated brightness k…
Browse files Browse the repository at this point in the history
…eybinds
  • Loading branch information
adamviola committed Jun 20, 2021
1 parent 1cf3a77 commit e0c09e2
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 45 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ bin/
# fabric

run/
remappedSrc/
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
89 changes: 67 additions & 22 deletions src/main/java/net/boostedbrightness/BoostedBrightness.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Double> brightnesses;
private static int brightnessIndex = 0;
private static int lastBrightnessIndex = 0;

private static final KeyBinding NEXT_BIND = new KeyBinding(
"key.boosted-brightness.next",
Expand All @@ -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),
Expand All @@ -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() {
Expand All @@ -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;
}
}

Expand All @@ -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));
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ private void init(String key, double min, double max, float step, Function<GameO
}
}

private void setter(GameOptions gameOptions, Double gamma) {
gamma = Math.round(20 * gamma) / 20.0D;
gameOptions.gamma = gamma;
BoostedBrightness.brightnesses.set(BoostedBrightness.selectedBrightness, gamma);
private void setter(GameOptions gameOptions, Double brightness) {
// Round to nearest 0.05
brightness = Math.round(20 * brightness) / 20.0D;
BoostedBrightness.changeBrightness(brightness);
}

private Text displayStringGetter(GameOptions gameOptions, DoubleOption doubleOption) {
Expand Down
21 changes: 10 additions & 11 deletions src/main/java/net/boostedbrightness/ui/BrightnessListWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public BrightnessListWidget(MinecraftClient client, int i, int j, int k, int l,
super(client, i, j, k, l, m);
this.setRenderSelection(true);

if (client.options.gamma != BoostedBrightness.brightnesses.get(BoostedBrightness.selectedBrightness)) {
BoostedBrightness.brightnesses.set(BoostedBrightness.selectedBrightness, client.options.gamma);
if (client.options.gamma != BoostedBrightness.getBrightness()) {
BoostedBrightness.changeBrightness(client.options.gamma);
}

for (int idx = 0; idx < BoostedBrightness.brightnesses.size(); idx++) {
for (int idx = 0; idx < BoostedBrightness.numBrightnesses(); idx++) {
this.addEntry(BrightnessListWidget.BrightnessEntry.create(idx, this.width, this));
}

if (BoostedBrightness.brightnesses.size() < BoostedBrightness.MAX_BRIGHTNESSES) {
if (BoostedBrightness.numBrightnesses() < BoostedBrightness.MAX_BRIGHTNESSES) {
this.addEntry(BrightnessListWidget.BrightnessEntry.create(-1, this.width, this));
}
}
Expand All @@ -38,7 +38,7 @@ public void addBrightness() {
List<BrightnessEntry> 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));

Expand All @@ -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);
}
}

Expand Down Expand Up @@ -95,7 +95,7 @@ public Optional<ClickableWidget> 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<BrightnessListWidget.BrightnessEntry> {
Expand All @@ -114,7 +114,7 @@ public static BrightnessListWidget.BrightnessEntry create(int index, int width,
ArrayList<ClickableWidget> 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); }));
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down

0 comments on commit e0c09e2

Please sign in to comment.