Auto generate a simple Config Screen
This commit is contained in:
parent
d31425623d
commit
092e0a39e8
3 changed files with 33 additions and 6 deletions
|
@ -28,13 +28,11 @@ public class NamedPathConfig extends PathConfig{
|
|||
public Bool(boolean def, String entry, ResourceLocation path) { super(entry, path); this.defaultValue=def;}
|
||||
}
|
||||
|
||||
|
||||
public static class Str extends ConfigToken{
|
||||
public final String defaultValue;
|
||||
public Str(String def, String entry, String... path) { super(entry, path); this.defaultValue=def;}
|
||||
public Str(String def, String entry, ResourceLocation path) { super(entry, path); this.defaultValue=def;}
|
||||
}
|
||||
|
||||
ConfigToken(String entry, String... path) { super(entry, path); }
|
||||
ConfigToken(String entry, ResourceLocation path) { super(entry, path); }
|
||||
}
|
||||
|
@ -54,7 +52,7 @@ public class NamedPathConfig extends PathConfig{
|
|||
onInit();
|
||||
}
|
||||
|
||||
List<ConfigToken> getAllOptions(){
|
||||
public List<ConfigToken> getAllOptions(){
|
||||
List<ConfigToken> res = new LinkedList<>();
|
||||
for (Field fl : this.getClass().getDeclaredFields()){
|
||||
int modifiers = fl.getModifiers();
|
||||
|
|
|
@ -4,6 +4,11 @@ import net.minecraft.client.gui.screens.Screen;
|
|||
import net.minecraft.network.chat.CommonComponents;
|
||||
import net.minecraft.network.chat.TranslatableComponent;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import ru.bclib.config.Configs;
|
||||
import ru.bclib.config.NamedPathConfig;
|
||||
import ru.bclib.config.NamedPathConfig.ConfigToken;
|
||||
import ru.bclib.config.NamedPathConfig.ConfigToken.Bool;
|
||||
import ru.bclib.gui.gridlayout.GridColumn;
|
||||
import ru.bclib.gui.gridlayout.GridRow;
|
||||
import ru.bclib.gui.gridlayout.GridScreen;
|
||||
|
||||
|
@ -13,12 +18,36 @@ public class MainScreen extends GridScreen{
|
|||
super(parent, new TranslatableComponent("title.bclib.modmenu.main"));
|
||||
}
|
||||
|
||||
protected TranslatableComponent getComponent(NamedPathConfig config, ConfigToken.Bool token, String type){
|
||||
String path = "";
|
||||
for (String p : token.getPath()){
|
||||
path += "." + p;
|
||||
|
||||
}
|
||||
return new TranslatableComponent(type + ".config." + path );
|
||||
}
|
||||
|
||||
protected void addRow(GridColumn grid, NamedPathConfig config, ConfigToken token){
|
||||
if (token instanceof Bool){
|
||||
addRow(grid, config, (ConfigToken.Bool)token);
|
||||
}
|
||||
|
||||
grid.addSpacerRow(2);
|
||||
}
|
||||
|
||||
protected void addRow(GridColumn grid, NamedPathConfig config, ConfigToken.Bool token){
|
||||
GridRow row = grid.addRow();
|
||||
row.addCheckbox(getComponent(config, token, "title"), config.get(token), 20);
|
||||
|
||||
}
|
||||
@Override
|
||||
protected void initLayout() {
|
||||
final int BUTTON_HEIGHT = 20;
|
||||
grid.addSpacerRow(20);
|
||||
GridRow row = grid.addRow();
|
||||
|
||||
Configs.CLIENT_CONFIG.getAllOptions().forEach(o -> addRow(grid, Configs.CLIENT_CONFIG, o));
|
||||
|
||||
GridRow row = grid.addRow();
|
||||
row.addFiller();
|
||||
row.addButton(CommonComponents.GUI_BACK, BUTTON_HEIGHT, font, (button)->{
|
||||
onClose();
|
||||
|
|
|
@ -53,13 +53,13 @@ class ModMenuScreenFactoryImpl {
|
|||
* the {@link #createEntrypoint(ModMenuIntegration)}-Method.
|
||||
* <p>
|
||||
* Example:
|
||||
* <pre>{@code public class} ModMenu extends ModMenuIntegration {
|
||||
* <pre>{@code public class ModMenu extends ModMenuIntegration {
|
||||
* public static final ModMenuApiMarker entrypointObject = createEntrypoint(new EntryPoint());
|
||||
*
|
||||
* public EntryPoint() {
|
||||
* super(GridScreen::new);
|
||||
* }
|
||||
* }</pre>
|
||||
* }}</pre>
|
||||
* You'd also need to add the ModMenu-Entrypoint to your <i>fabric.mod.json</i>:
|
||||
* <pre>"entrypoints": {
|
||||
* ...
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue