Added ConfigUI
Annotation
This commit is contained in:
parent
0adc5024f1
commit
8f9ff14fac
8 changed files with 240 additions and 95 deletions
|
@ -27,8 +27,11 @@ class SignalingCheckBox extends Checkbox{
|
|||
}
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public class GridCheckboxCell extends GridCell{
|
||||
public class GridCheckboxCell extends GridCell implements GridWidgetWithEnabledState{
|
||||
private boolean checked;
|
||||
private Checkbox lastCheckbox;
|
||||
private boolean enabled;
|
||||
private final float alpha;
|
||||
|
||||
GridCheckboxCell(Component text, boolean checked, float alpha, double width, GridValueType widthType, double height) {
|
||||
this(text, checked, alpha, width, widthType, height, null);
|
||||
|
@ -36,7 +39,9 @@ public class GridCheckboxCell extends GridCell{
|
|||
|
||||
GridCheckboxCell(Component text, boolean checked, float alpha, double width, GridValueType widthType, double height, Consumer<Boolean> onChange) {
|
||||
super(width, height, widthType, null, null);
|
||||
|
||||
lastCheckbox = null;
|
||||
enabled = true;
|
||||
this.alpha = alpha;
|
||||
this.componentPlacer = (transform) -> {
|
||||
Checkbox cb = new SignalingCheckBox(transform.left, transform.top, transform.width, transform.height,
|
||||
text,
|
||||
|
@ -47,11 +52,26 @@ public class GridCheckboxCell extends GridCell{
|
|||
}
|
||||
);
|
||||
cb.setAlpha(alpha);
|
||||
lastCheckbox = cb;
|
||||
setEnabled(enabled);
|
||||
return cb;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
public boolean isChecked(){
|
||||
return checked;
|
||||
}
|
||||
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
if (lastCheckbox!=null){
|
||||
lastCheckbox.active = enabled;
|
||||
lastCheckbox.setAlpha(enabled?alpha:(alpha *0.5f));
|
||||
}
|
||||
this.enabled = enabled;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
package ru.bclib.gui.gridlayout;
|
||||
|
||||
public interface GridWidgetWithEnabledState {
|
||||
public boolean isEnabled();
|
||||
public void setEnabled(boolean enabled);
|
||||
}
|
|
@ -6,11 +6,17 @@ 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.config.NamedPathConfig.ConfigTokenDescription;
|
||||
import ru.bclib.config.NamedPathConfig.DependendConfigToken;
|
||||
import ru.bclib.gui.gridlayout.GridCheckboxCell;
|
||||
import ru.bclib.gui.gridlayout.GridColumn;
|
||||
import ru.bclib.gui.gridlayout.GridRow;
|
||||
import ru.bclib.gui.gridlayout.GridScreen;
|
||||
import ru.bclib.gui.gridlayout.GridWidgetWithEnabledState;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class MainScreen extends GridScreen{
|
||||
|
||||
|
@ -18,28 +24,42 @@ public class MainScreen extends GridScreen{
|
|||
super(parent, new TranslatableComponent("title.bclib.modmenu.main"));
|
||||
}
|
||||
|
||||
protected TranslatableComponent getComponent(NamedPathConfig config, ConfigToken.Bool token, String type){
|
||||
StringBuilder path = new StringBuilder();
|
||||
for (String p : token.getPath()){
|
||||
path.append(".")
|
||||
.append(p);
|
||||
|
||||
}
|
||||
path.append(".").append(token.getEntry());
|
||||
return new TranslatableComponent(type + ".config." + config.configID + path );
|
||||
protected <T> TranslatableComponent getComponent(NamedPathConfig config, ConfigTokenDescription<T> option, String type){
|
||||
return new TranslatableComponent(type + ".config." + config.configID + option.getPath() );
|
||||
}
|
||||
|
||||
protected void addRow(GridColumn grid, NamedPathConfig config, ConfigToken token){
|
||||
if (token instanceof Bool){
|
||||
addRow(grid, config, (ConfigToken.Bool)token);
|
||||
Map<GridWidgetWithEnabledState, Supplier<Boolean>> dependentWidgets = new HashMap<>();
|
||||
protected void updateEnabledState(){
|
||||
dependentWidgets.forEach((cb, supl)->cb.setEnabled(supl.get()));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <T>void addRow(GridColumn grid, NamedPathConfig config, ConfigTokenDescription<T> option){
|
||||
if (Boolean.class.isAssignableFrom(option.token.type)) {
|
||||
addCheckbox(grid, config, (ConfigTokenDescription<Boolean>)option);
|
||||
}
|
||||
|
||||
grid.addSpacerRow(2);
|
||||
}
|
||||
|
||||
protected void addRow(GridColumn grid, NamedPathConfig config, ConfigToken.Bool token){
|
||||
|
||||
protected void addCheckbox(GridColumn grid, NamedPathConfig config, ConfigTokenDescription<Boolean> option){
|
||||
if (option.topPadding>0){
|
||||
grid.addSpacerRow(option.topPadding);
|
||||
}
|
||||
GridRow row = grid.addRow();
|
||||
row.addCheckbox(getComponent(config, token, "title"), config.get(token), font, (state)-> config.set(token, state));
|
||||
if (option.leftPadding>0){
|
||||
row.addSpacer(option.leftPadding);
|
||||
}
|
||||
GridCheckboxCell cb = row.addCheckbox(getComponent(config, option, "title"), config.getRaw(option.token), font, (state)-> {
|
||||
config.set(option.token, state);
|
||||
updateEnabledState();
|
||||
});
|
||||
|
||||
if (option.token instanceof DependendConfigToken) {
|
||||
dependentWidgets.put(cb, ()->option.token.dependenciesTrue(config));
|
||||
cb.setEnabled(option.token.dependenciesTrue(config));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -51,7 +71,7 @@ public class MainScreen extends GridScreen{
|
|||
protected void initLayout() {
|
||||
final int BUTTON_HEIGHT = 20;
|
||||
|
||||
Configs.CLIENT_CONFIG.getAllOptions().forEach(o -> addRow(grid, Configs.CLIENT_CONFIG, o));
|
||||
Configs.CLIENT_CONFIG.getAllOptions().stream().filter(o -> !o.hidden).forEach(o -> addRow(grid, Configs.CLIENT_CONFIG, o));
|
||||
|
||||
grid.addSpacerRow(15);
|
||||
GridRow row = grid.addRow();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue