-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from SpiritGameStudios/config-gui-updates
Config upgrades and GUI module
- Loading branch information
Showing
68 changed files
with
3,706 additions
and
889 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ deps.loader=0.15.11 | |
deps.yarn=1.21+build.9 | ||
deps.modmenu=11.0.1 | ||
deps.fabricapi=0.103.0+1.21.1 | ||
deps.tomlj=1.1.1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 0 additions & 46 deletions
46
specter-config/src/client/java/dev/spiritstudios/specter/api/ConfigScreenManager.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
specter-config/src/client/java/dev/spiritstudios/specter/api/config/ConfigScreenWidgets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package dev.spiritstudios.specter.api.config; | ||
|
||
import dev.spiritstudios.specter.api.core.util.PatternMap; | ||
import dev.spiritstudios.specter.api.gui.widget.SpecterButtonWidget; | ||
import dev.spiritstudios.specter.api.gui.widget.SpecterSliderWidget; | ||
import net.minecraft.block.Block; | ||
import net.minecraft.client.MinecraftClient; | ||
import net.minecraft.client.gui.widget.ClickableWidget; | ||
import net.minecraft.client.gui.widget.TextFieldWidget; | ||
import net.minecraft.item.Item; | ||
import net.minecraft.registry.Registries; | ||
import net.minecraft.registry.Registry; | ||
import net.minecraft.screen.ScreenTexts; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.Formatting; | ||
import net.minecraft.util.Identifier; | ||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.function.BiFunction; | ||
import java.util.stream.Collectors; | ||
|
||
@SuppressWarnings("unchecked") | ||
public final class ConfigScreenWidgets { | ||
private static final PatternMap<BiFunction<Value<?>, String, ? extends ClickableWidget>> widgetFactories = new PatternMap<>(); | ||
|
||
static { | ||
addRegistry(Item.class, Registries.ITEM); | ||
addRegistry(Block.class, Registries.BLOCK); | ||
} | ||
|
||
private static final BiFunction<Value<?>, String, ? extends ClickableWidget> BOOLEAN_WIDGET_FACTORY = (configValue, id) -> { | ||
Value<Boolean> value = (Value<Boolean>) configValue; | ||
|
||
return SpecterButtonWidget.builder( | ||
() -> Text.translatable(value.translationKey(id)).append(": ").append(ScreenTexts.onOrOff(value.get())), | ||
button -> value.set(!value.get()) | ||
).build(); | ||
}; | ||
|
||
private static final BiFunction<Value<?>, String, ? extends ClickableWidget> INTEGER_WIDGET_FACTORY = (configValue, id) -> { | ||
NumericValue<Integer> value = (NumericValue<Integer>) configValue; | ||
|
||
return SpecterSliderWidget.builder(value.get()) | ||
.message((val) -> Text.translatable(value.translationKey(id)).append(String.format(": %.0f", val))) | ||
.range(value.range().min(), value.range().max()) | ||
.step(value.step() == 0 ? 1 : value.step()) | ||
.onValueChanged((val) -> value.set(val.intValue())) | ||
.build(); | ||
}; | ||
|
||
private static final BiFunction<Value<?>, String, ? extends ClickableWidget> DOUBLE_WIDGET_FACTORY = (configValue, id) -> { | ||
NumericValue<Double> value = (NumericValue<Double>) configValue; | ||
|
||
return SpecterSliderWidget.builder(value.get()) | ||
.message((val) -> Text.translatable(value.translationKey(id)).append(String.format(": %.2f", val))) | ||
.range(value.range()) | ||
.step(value.step()) | ||
.onValueChanged(value::set) | ||
.build(); | ||
}; | ||
|
||
private static final BiFunction<Value<?>, String, ? extends ClickableWidget> FLOAT_WIDGET_FACTORY = (configValue, id) -> { | ||
NumericValue<Float> value = (NumericValue<Float>) configValue; | ||
|
||
return SpecterSliderWidget.builder(value.get()) | ||
.message((val) -> Text.translatable(configValue.translationKey(id)).append(String.format(": %.1f", val))) | ||
.range(value.range().min(), value.range().max()) | ||
.step(value.step()) | ||
.onValueChanged((val) -> value.set(val.floatValue())) | ||
.build(); | ||
}; | ||
|
||
private static final BiFunction<Value<?>, String, ? extends ClickableWidget> STRING_WIDGET_FACTORY = (configValue, id) -> { | ||
Value<String> value = (Value<String>) configValue; | ||
|
||
TextFieldWidget widget = new TextFieldWidget(MinecraftClient.getInstance().textRenderer, 0, 0, 0, 0, Text.of(value.get())); | ||
widget.setPlaceholder(Text.translatableWithFallback("%s.placeholder".formatted(configValue.translationKey(id)), "").formatted(Formatting.DARK_GRAY)); | ||
|
||
widget.setText(value.get()); | ||
widget.setChangedListener(value::set); | ||
widget.setSelectionEnd(0); | ||
widget.setSelectionStart(0); | ||
|
||
return widget; | ||
}; | ||
|
||
private static final BiFunction<Value<?>, String, ? extends ClickableWidget> ENUM_WIDGET_FACTORY = (configValue, id) -> { | ||
Value<Enum<?>> value = (Value<Enum<?>>) configValue; | ||
|
||
List<Enum<?>> enumValues = Arrays.stream(configValue.defaultValue().getClass().getEnumConstants()) | ||
.filter(val -> val instanceof Enum<?>) | ||
.map(val -> (Enum<?>) val) | ||
.collect(Collectors.toList()); | ||
|
||
if (enumValues.isEmpty()) throw new IllegalArgumentException("Enum values cannot be null"); | ||
|
||
return SpecterButtonWidget.builder( | ||
() -> Text.translatable(configValue.translationKey(id)).append(": ").append(Text.translatable("%s.%s".formatted(configValue.translationKey(id), value.get().toString().toLowerCase()))), | ||
button -> { | ||
Enum<?> current = value.get(); | ||
int index = enumValues.indexOf(current); | ||
value.set(enumValues.get((index + 1) % enumValues.size())); | ||
} | ||
).build(); | ||
}; | ||
|
||
private ConfigScreenWidgets() { | ||
} | ||
|
||
public static <T> void addRegistry(Class<T> clazz, Registry<T> registry) { | ||
widgetFactories.put(clazz, (configValue, id) -> { | ||
Value<T> value = (Value<T>) configValue; | ||
|
||
TextFieldWidget widget = new TextFieldWidget(MinecraftClient.getInstance().textRenderer, 0, 0, 0, 0, Text.of(registry.getEntry(value.get()).getIdAsString())); | ||
widget.setPlaceholder(Text.translatableWithFallback("%s.placeholder".formatted(configValue.translationKey(id)), "").formatted(Formatting.DARK_GRAY)); | ||
|
||
widget.setText(value.get().toString()); | ||
widget.setChangedListener(val -> { | ||
Identifier identifier = Identifier.tryParse(val); | ||
if (identifier == null) return; | ||
|
||
registry.getOrEmpty(identifier).ifPresent(value::set); | ||
}); | ||
widget.setSelectionEnd(0); | ||
widget.setSelectionStart(0); | ||
|
||
return widget; | ||
}); | ||
} | ||
|
||
public static void add(Class<?> clazz, BiFunction<Value<?>, String, ? extends ClickableWidget> factory) { | ||
widgetFactories.put(clazz, factory); | ||
} | ||
|
||
@ApiStatus.Internal | ||
public static <T> BiFunction<Value<?>, String, ? extends ClickableWidget> getWidgetFactory(Value<T> value) { | ||
// We are using a switch instead of just adding to our map for 2 reasons: | ||
// 1. It's (usually) faster than a map lookup, as most of the time the value will be one of these types | ||
// 2. It lets us handle the lowercased names of primitive types, which are different Class<> instances because reasons | ||
return switch (value.defaultValue()) { | ||
case Boolean ignored -> BOOLEAN_WIDGET_FACTORY; | ||
case Integer ignored -> INTEGER_WIDGET_FACTORY; | ||
case Double ignored -> DOUBLE_WIDGET_FACTORY; | ||
case Float ignored -> FLOAT_WIDGET_FACTORY; | ||
case String ignored -> STRING_WIDGET_FACTORY; | ||
case Enum<?> ignored -> ENUM_WIDGET_FACTORY; | ||
default -> widgetFactories.get(value.defaultValue().getClass()); | ||
}; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...ritstudios/specter/api/ModMenuHelper.java → ...ios/specter/api/config/ModMenuHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
specter-config/src/client/java/dev/spiritstudios/specter/api/config/RootConfigScreen.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package dev.spiritstudios.specter.api.config; | ||
|
||
import net.minecraft.client.gui.screen.Screen; | ||
|
||
public class RootConfigScreen extends ConfigScreen { | ||
private final ConfigHolder<?, ?> holder; | ||
|
||
public RootConfigScreen(ConfigHolder<?, ?> holder, Screen parent) { | ||
super(holder.get(), holder.id().toTranslationKey(), parent); | ||
this.holder = holder; | ||
} | ||
|
||
@Override | ||
public void save() { | ||
this.holder.save(); | ||
} | ||
} |
Oops, something went wrong.