Skip to content

Commit

Permalink
more ui and config stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
andi-makes committed Feb 7, 2024
1 parent 35afe00 commit 7b03c09
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 50 deletions.
1 change: 0 additions & 1 deletion common/src/main/java/dev/schmarrn/lighty/ModeLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public static void loadMode(ResourceLocation id) {
ModeLoader.mode = modeToLoad;
Config.LAST_USED_MODE.setValue(id);
Compute.clear();
//enable();
}

public static void disable() {
Expand Down
14 changes: 14 additions & 0 deletions common/src/main/java/dev/schmarrn/lighty/config/BooleanConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
package dev.schmarrn.lighty.config;

import net.minecraft.client.OptionInstance;
import net.minecraft.client.gui.components.Tooltip;
import net.minecraft.network.chat.Component;

public class BooleanConfig extends ConfigType<Boolean> {
@Override
public OptionInstance<Boolean> getOptionInstance() {
return OptionInstance.createBoolean(
this.getTranslationKey(),
object -> Tooltip.create(Component.translatable(this.getTranslationTooltipKey(), object)),
this.getValue(),
this::setValue
);
}

BooleanConfig(String key, Boolean defaultValue) {
super(key, defaultValue);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package dev.schmarrn.lighty.config;

import net.minecraft.client.OptionInstance;

public class ColorConfig extends ConfigType<Integer> {
@Override
public OptionInstance<Integer> getOptionInstance() {
return null;
}

public ColorConfig(String key, Integer defaultValue) {
super(key, defaultValue);
}
Expand Down
8 changes: 4 additions & 4 deletions common/src/main/java/dev/schmarrn/lighty/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public class Config {

public static final ResourceLocationConfig LAST_USED_MODE = new ResourceLocationConfig("lighty.last_used_mode", new ResourceLocation("lighty:carpet_mode"));

public static final IntegerConfig SKY_THRESHOLD = new IntegerConfig("lighty.sky_threshold", 0);
public static final IntegerConfig BLOCK_THRESHOLD = new IntegerConfig("lighty.block_threshold", 0);
public static final IntegerConfig OVERLAY_DISTANCE = new IntegerConfig("lighty.overlay_distance", 2);
public static final IntegerConfig OVERLAY_BRIGHTNESS = new IntegerConfig("lighty.overlay_brightness", 10);
public static final IntegerConfig SKY_THRESHOLD = new IntegerConfig("lighty.sky_threshold", 0, 0, 15);
public static final IntegerConfig BLOCK_THRESHOLD = new IntegerConfig("lighty.block_threshold", 0, 0, 15);
public static final IntegerConfig OVERLAY_DISTANCE = new IntegerConfig("lighty.overlay_distance", 2, 1, 32);
public static final IntegerConfig OVERLAY_BRIGHTNESS = new IntegerConfig("lighty.overlay_brightness", 10, 0, 15);

public static final BooleanConfig SHOW_SAFE = new BooleanConfig("lighty.show_safe", true);

Expand Down
11 changes: 11 additions & 0 deletions common/src/main/java/dev/schmarrn/lighty/config/ConfigType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.schmarrn.lighty.config;

import net.minecraft.client.OptionInstance;

abstract class ConfigType<T> extends ConfigSerDe{
public void onChange(T value){}

Expand All @@ -26,6 +28,15 @@ public String getKey() {
return KEY;
}

public abstract OptionInstance<T> getOptionInstance();
protected String getTranslationKey() {
return KEY.replaceFirst("\\.", ".options.");
}

protected String getTranslationTooltipKey() {
return getTranslationKey() + ".tooltip";
}

ConfigType(String key, T defaultValue) {
this.KEY = key;
this.DEFAULT_VALUE = defaultValue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,40 @@
package dev.schmarrn.lighty.config;

import com.mojang.serialization.Codec;
import dev.schmarrn.lighty.Lighty;
import net.minecraft.client.OptionInstance;
import net.minecraft.client.Options;
import net.minecraft.client.gui.components.Tooltip;
import net.minecraft.network.chat.Component;

public class IntegerConfig extends ConfigType<Integer> {
IntegerConfig(String key, Integer defaultValue) {
private final int min, max;
@Override
public OptionInstance<Integer> getOptionInstance() {
return new OptionInstance<>(
this.getTranslationKey(),
object -> Tooltip.create(Component.translatable(this.getTranslationTooltipKey(), object)),
(component, integer) -> Options.genericValueLabel(component, Component.literal(integer.toString())),
new OptionInstance.IntRange(this.min, this.max),
Codec.intRange(this.min, this.max),
this.getValue(),
this::setValue
);
}

IntegerConfig(String key, Integer defaultValue, int min, int max) {
super(key, defaultValue);
this.min = min;
this.max = max;
}

@Override
public void setValue(Integer newValue) {
if (newValue >= min && newValue <= max) {
super.setValue(newValue);
} else {
Lighty.LOGGER.error("Lighty Config: {}, new value {} out of bounds: [{}, {}]. Ignoring new value.", getKey(), newValue, min, max);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package dev.schmarrn.lighty.config;

import net.minecraft.client.OptionInstance;
import net.minecraft.resources.ResourceLocation;

public class ResourceLocationConfig extends ConfigType<ResourceLocation> {
@Override
public OptionInstance<ResourceLocation> getOptionInstance() {
return null;
}

ResourceLocationConfig(String key, ResourceLocation defaultValue) {
super(key, defaultValue);
}
Expand Down
49 changes: 5 additions & 44 deletions common/src/main/java/dev/schmarrn/lighty/ui/SettingsScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,52 +44,13 @@ protected void init() {
assert this.minecraft != null;
OptionsList list = new OptionsList(this.minecraft, this.width, this.height - 32, 32, 25);

list.addBig(new OptionInstance<>(
"lighty.options.overlay_distance",
object -> Tooltip.create(Component.translatable("lighty.options.overlay_distance.tooltip", object)),
(component, integer) -> Options.genericValueLabel(component, Component.literal(integer.toString())),
new OptionInstance.IntRange(1, 32),
Codec.intRange(1, 32),
Config.OVERLAY_DISTANCE.getValue(),
Config.OVERLAY_DISTANCE::setValue
));

list.addBig(new OptionInstance<>(
"lighty.options.overlay_brightness",
object -> Tooltip.create(Component.translatable("lighty.options.overlay_brightness.tooltip", object)),
(component, integer) -> Options.genericValueLabel(component, Component.literal(integer.toString())),
new OptionInstance.IntRange(0, 15),
Codec.intRange(0, 15),
Config.OVERLAY_BRIGHTNESS.getValue(),
Config.OVERLAY_BRIGHTNESS::setValue
));

list.addBig(Config.OVERLAY_DISTANCE.getOptionInstance());
list.addBig(Config.OVERLAY_BRIGHTNESS.getOptionInstance());
list.addSmall(
new OptionInstance[]{
new OptionInstance<>(
"lighty.options.block_threshold",
object -> Tooltip.create(Component.translatable("lighty.options.block_threshold.tooltip", object)),
(component, integer) -> Options.genericValueLabel(component, Component.literal(integer.toString())),
new OptionInstance.IntRange(0, 15),
Codec.intRange(0, 15),
Config.BLOCK_THRESHOLD.getValue(),
Config.BLOCK_THRESHOLD::setValue
),
new OptionInstance<>(
"lighty.options.sky_threshold",
object -> Tooltip.create(Component.translatable("lighty.options.sky_threshold.tooltip", object)),
(component, integer) -> Options.genericValueLabel(component, Component.literal(integer.toString())),
new OptionInstance.IntRange(0, 15),
Codec.intRange(0, 15),
Config.SKY_THRESHOLD.getValue(),
Config.SKY_THRESHOLD::setValue
),
OptionInstance.createBoolean(
"lighty.options.show_safe",
object -> Tooltip.create(Component.translatable("lighty.options.show_safe.tooltip", object)),
Config.SHOW_SAFE.getValue(),
Config.SHOW_SAFE::setValue
)
Config.BLOCK_THRESHOLD.getOptionInstance(),
Config.SKY_THRESHOLD.getOptionInstance(),
Config.SHOW_SAFE.getOptionInstance()
}
);

Expand Down

0 comments on commit 7b03c09

Please sign in to comment.