92 lines
3.0 KiB
Java
92 lines
3.0 KiB
Java
package io.gitlab.jfronny.respackopts.model.tree;
|
|
|
|
import io.gitlab.jfronny.muscript.data.dynamic.DNumber;
|
|
import io.gitlab.jfronny.libjf.config.api.v1.dsl.CategoryBuilder;
|
|
import io.gitlab.jfronny.respackopts.Respackopts;
|
|
import io.gitlab.jfronny.respackopts.RespackoptsConfig;
|
|
import io.gitlab.jfronny.respackopts.model.enums.ConfigSyncMode;
|
|
import io.gitlab.jfronny.respackopts.util.IndentingStringBuilder;
|
|
|
|
import java.util.Objects;
|
|
|
|
public class ConfigNumericEntry extends ConfigEntry<Double> {
|
|
public Double min = null;
|
|
public Double max = null;
|
|
|
|
public ConfigNumericEntry() {
|
|
super(Double.class);
|
|
setValue(0d);
|
|
}
|
|
|
|
@Override
|
|
public Double setDefault(Double value) {
|
|
if ((min != null && value < min) || (max != null && value > max))
|
|
throw new RuntimeException("Default value (" + value + ") out of range in number entry definition (" + min + "-" + max + ")");
|
|
return super.setDefault(value);
|
|
}
|
|
|
|
@Override
|
|
public void sync(ConfigEntry<Double> source, ConfigSyncMode mode) {
|
|
super.sync(source, mode);
|
|
ConfigNumericEntry n = (ConfigNumericEntry) source;
|
|
if (mode == ConfigSyncMode.RESPACK_LOAD) {
|
|
if (n.min != null)
|
|
min = n.min;
|
|
if (n.max != null)
|
|
max = n.max;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void appendString(IndentingStringBuilder sb) {
|
|
sb.line(getValue() + " (" + getDefault() + ", " + min + "-" + max + ")");
|
|
}
|
|
|
|
@Override
|
|
public ConfigEntry<Double> clone() {
|
|
ConfigNumericEntry ce = new ConfigNumericEntry();
|
|
ce.max = max;
|
|
ce.min = min;
|
|
ce.setValue(getValue());
|
|
ce.setDefault(getDefault());
|
|
return ce;
|
|
}
|
|
|
|
@Override
|
|
public void buildShader(StringBuilder sb, String valueName) {
|
|
sb.append("\n#define ");
|
|
sb.append(valueName);
|
|
sb.append(' ');
|
|
sb.append(getValue().toString());
|
|
}
|
|
|
|
@Override
|
|
public DNumber getDynamic() {
|
|
return this::getValue;
|
|
}
|
|
|
|
@Override
|
|
public CategoryBuilder<?> buildEntry(GuiEntryBuilderParam args) {
|
|
return args.builder().value(args.name(), getValue(), min == null ? Double.NEGATIVE_INFINITY : min, max == null ? Double.POSITIVE_INFINITY : max, this::getValue, v -> {
|
|
if (!Objects.equals(getValue(), v)) {
|
|
if (RespackoptsConfig.debugLogs) Respackopts.LOGGER.info("ConfigNumericEntryNormal SaveCallback");
|
|
args.saveCallback();
|
|
}
|
|
setValue(v);
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object o) {
|
|
if (this == o) return true;
|
|
if (!(o instanceof ConfigNumericEntry that)) return false;
|
|
if (!super.equals(o)) return false;
|
|
return Objects.equals(getValue(), that.getValue()) && Objects.equals(getDefault(), that.getDefault()) && Objects.equals(min, that.min) && Objects.equals(max, that.max);
|
|
}
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
return Objects.hash(super.hashCode(), getValue(), getDefault(), min, max);
|
|
}
|
|
}
|