Save configs from ModMenu-Screen

This commit is contained in:
Frank 2021-08-20 16:50:23 +02:00
parent 092e0a39e8
commit b369954c05
7 changed files with 68 additions and 24 deletions

View file

@ -31,13 +31,20 @@ public class GridCheckboxCell extends GridCell{
private boolean checked;
GridCheckboxCell(Component text, boolean checked, float alpha, double width, GridValueType widthType, double height) {
this(text, checked, alpha, width, widthType, height, null);
}
GridCheckboxCell(Component text, boolean checked, float alpha, double width, GridValueType widthType, double height, Consumer<Boolean> onChange) {
super(width, height, widthType, null, null);
this.componentPlacer = (transform) -> {
Checkbox cb = new SignalingCheckBox(transform.left, transform.top, transform.width, transform.height,
text,
checked,
(state)-> this.checked = state
(state)-> {
this.checked = state;
if (onChange!=null) onChange.accept(state);
}
);
cb.setAlpha(alpha);
return cb;

View file

@ -14,6 +14,7 @@ import ru.bclib.gui.gridlayout.GridLayout.VerticalAlignment;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
@Environment(EnvType.CLIENT)
@ -86,6 +87,13 @@ public class GridRow extends GridContainer {
return cell;
}
public GridCheckboxCell addCheckbox(Component text, boolean checked, Font font, Consumer<Boolean> onChange){
final int width = font.width(text.getVisualOrderText()) + 24 + 2 * 12;
GridCheckboxCell cell = new GridCheckboxCell(text, checked, 1.0f, width, widthType, 20, onChange);
this.cells.add(cell);
return cell;
}
public GridCheckboxCell addCheckbox(Component text, boolean checked, int height) {
return addCheckbox(text, checked, 1.0f, height);

View file

@ -19,12 +19,14 @@ public class MainScreen extends GridScreen{
}
protected TranslatableComponent getComponent(NamedPathConfig config, ConfigToken.Bool token, String type){
String path = "";
StringBuilder path = new StringBuilder();
for (String p : token.getPath()){
path += "." + p;
path.append(".")
.append(p);
}
return new TranslatableComponent(type + ".config." + path );
path.append(".").append(token.getEntry());
return new TranslatableComponent(type + ".config." + config.configID + path );
}
protected void addRow(GridColumn grid, NamedPathConfig config, ConfigToken token){
@ -37,21 +39,26 @@ public class MainScreen extends GridScreen{
protected void addRow(GridColumn grid, NamedPathConfig config, ConfigToken.Bool token){
GridRow row = grid.addRow();
row.addCheckbox(getComponent(config, token, "title"), config.get(token), 20);
row.addCheckbox(getComponent(config, token, "title"), config.get(token), font, (state)-> config.set(token, state));
}
@Override
public boolean shouldCloseOnEsc() {
return false;
}
@Override
protected void initLayout() {
final int BUTTON_HEIGHT = 20;
grid.addSpacerRow(20);
Configs.CLIENT_CONFIG.getAllOptions().forEach(o -> addRow(grid, Configs.CLIENT_CONFIG, o));
grid.addSpacerRow(15);
GridRow row = grid.addRow();
row.addFiller();
row.addButton(CommonComponents.GUI_BACK, BUTTON_HEIGHT, font, (button)->{
row.addButton(CommonComponents.GUI_DONE, BUTTON_HEIGHT, font, (button)->{
Configs.CLIENT_CONFIG.saveChanges();
onClose();
});
row.addFiller();
}
}