Files
CustomNPC-Plus/src/main/java/noppes/npcs/client/gui/builder/FieldDef.java
T

296 lines
12 KiB
Java
Raw Normal View History

2026-02-02 20:40:17 -05:00
package noppes.npcs.client.gui.builder;
2026-02-02 20:40:17 -05:00
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import noppes.npcs.controllers.AnimationController;
import noppes.npcs.controllers.data.Animation;
import noppes.npcs.client.gui.SubGuiColorSelector;
import noppes.npcs.client.gui.select.GuiAnimationSelection;
import noppes.npcs.client.gui.select.GuiSoundSelection;
import noppes.npcs.client.gui.util.SubGuiInterface;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
2026-02-02 20:40:17 -05:00
* Declarative field definition for GUI rendering.
* Built via static factories and chaining methods.
2026-02-02 20:40:17 -05:00
* Client-side only — never instantiate or reference on the server.
*/
2026-02-02 20:40:17 -05:00
@SideOnly(Side.CLIENT)
public class FieldDef {
private final String label;
private final FieldType type;
2026-02-02 20:40:17 -05:00
private String tab = null;
private ColumnHint column = ColumnHint.FULL;
private Supplier<Object> getter;
private Consumer<Object> setter;
private BooleanSupplier visibleWhen = () -> true;
2026-02-02 18:26:52 -05:00
private BooleanSupplier enabledWhen = () -> true;
private String tooltip = null;
private String hoverText = null;
// Numeric range
private float min = Float.MIN_VALUE;
private float max = Float.MAX_VALUE;
// Enum support
private Class<? extends Enum<?>> enumClass;
private String[] stringEnumValues;
private Supplier<String> stringEnumGetter;
private Consumer<String> stringEnumSetter;
// Sub-gui support
private Supplier<SubGuiInterface> subGuiFactory;
private Consumer<SubGuiInterface> subGuiResultHandler;
// Button display
private Supplier<String> buttonLabelSupplier;
private Supplier<Integer> buttonTextColorSupplier;
private Runnable clearAction;
private FieldDef(String label, FieldType type) {
this.label = label;
this.type = type;
}
// ═══════════════════════════════════════════════════════════════════
// STATIC FACTORIES
// ═══════════════════════════════════════════════════════════════════
2026-02-02 18:26:52 -05:00
public static FieldDef section(String label) {
return new FieldDef(label, FieldType.SECTION_HEADER);
}
public static FieldDef floatField(String label, Supplier<Float> getter, Consumer<Float> setter) {
FieldDef def = new FieldDef(label, FieldType.FLOAT);
def.getter = () -> getter.get();
def.setter = v -> setter.accept(((Number) v).floatValue());
return def;
}
public static FieldDef intField(String label, Supplier<Integer> getter, Consumer<Integer> setter) {
FieldDef def = new FieldDef(label, FieldType.INT);
def.getter = () -> getter.get();
def.setter = v -> setter.accept(((Number) v).intValue());
return def;
}
public static FieldDef boolField(String label, Supplier<Boolean> getter, Consumer<Boolean> setter) {
FieldDef def = new FieldDef(label, FieldType.BOOLEAN);
def.getter = () -> getter.get();
def.setter = v -> setter.accept((Boolean) v);
return def;
}
@SuppressWarnings("unchecked")
public static <E extends Enum<E>> FieldDef enumField(String label, Class<E> enumClass, Supplier<E> getter, Consumer<E> setter) {
FieldDef def = new FieldDef(label, FieldType.ENUM);
def.enumClass = enumClass;
def.getter = () -> getter.get();
def.setter = v -> setter.accept((E) v);
return def;
}
public static FieldDef stringEnumField(String label, String[] values, Supplier<String> getter, Consumer<String> setter) {
FieldDef def = new FieldDef(label, FieldType.STRING_ENUM);
def.stringEnumValues = values;
def.stringEnumGetter = getter;
def.stringEnumSetter = setter;
return def;
}
public static FieldDef stringField(String label, Supplier<String> getter, Consumer<String> setter) {
FieldDef def = new FieldDef(label, FieldType.STRING);
def.getter = () -> getter.get();
def.setter = v -> setter.accept((String) v);
return def;
}
2026-02-02 18:26:52 -05:00
public static FieldDef labelField(String label, Supplier<String> textSupplier) {
FieldDef def = new FieldDef(label, FieldType.LABEL);
def.getter = () -> textSupplier.get();
return def;
}
public static FieldDef subGuiField(String label, Supplier<SubGuiInterface> factory, Consumer<SubGuiInterface> resultHandler) {
FieldDef def = new FieldDef(label, FieldType.SUB_GUI);
def.subGuiFactory = factory;
def.subGuiResultHandler = resultHandler;
return def;
}
2026-02-02 20:40:17 -05:00
/**
* Creates a FieldDef with a custom FieldType. Used by extension code
* (e.g. AbilityFieldDefs) to create fields with types like EFFECTS_LIST
* that the base GuiFieldBuilder does not handle.
*/
public static FieldDef custom(String label, FieldType type, Supplier<Object> getter, Consumer<Object> setter) {
FieldDef def = new FieldDef(label, type);
def.getter = getter;
def.setter = setter;
return def;
}
// ═══════════════════════════════════════════════════════════════════
// CONVENIENCE FACTORIES (SUB_GUI wrappers)
// ═══════════════════════════════════════════════════════════════════
public static FieldDef colorSubGui(String label, Supplier<Integer> getter, Consumer<Integer> setter) {
return subGuiField(label,
() -> new SubGuiColorSelector(getter.get()),
gui -> setter.accept(((SubGuiColorSelector) gui).color & 0x00FFFFFF))
.buttonLabel(() -> String.format("%06X", getter.get() & 0xFFFFFF))
.buttonTextColor(() -> getter.get() & 0xFFFFFF);
}
public static FieldDef soundSubGui(String label, Supplier<String> getter, Consumer<String> setter) {
return subGuiField(label,
() -> new GuiSoundSelection(getter.get()),
gui -> {
GuiSoundSelection s = (GuiSoundSelection) gui;
if (s.selectedResource != null) setter.accept(s.selectedResource.toString());
})
.buttonLabel(() -> {
String s = getter.get();
2026-02-02 18:26:52 -05:00
return s == null || s.isEmpty() ? "gui.none" : s;
})
.clearable(() -> setter.accept(""));
}
public static FieldDef animSubGui(String label,
Supplier<Integer> idGetter, Consumer<Integer> idSetter,
Supplier<String> nameGetter, Consumer<String> nameSetter) {
return subGuiField(label,
() -> new GuiAnimationSelection(idGetter.get(), nameGetter.get()),
gui -> {
GuiAnimationSelection sel = (GuiAnimationSelection) gui;
if (sel.isBuiltInSelected()) {
nameSetter.accept(sel.selectedBuiltInName);
idSetter.accept(-1);
} else {
idSetter.accept(sel.selectedAnimationId);
nameSetter.accept("");
}
})
.buttonLabel(() -> {
String name = nameGetter.get();
2026-02-02 18:26:52 -05:00
if (name != null && !name.isEmpty()) return name;
int id = idGetter.get();
2026-02-02 20:40:17 -05:00
if (id >= 0) {
Animation anim = AnimationController.Instance != null
? (Animation) AnimationController.Instance.get(id) : null;
String animName = anim != null ? anim.getName() : "";
return animName != null && !animName.isEmpty()
? "(ID: " + id + ") " + animName : "ID: " + id;
}
return "gui.none";
})
.clearable(() -> { idSetter.accept(-1); nameSetter.accept(""); });
}
// ═══════════════════════════════════════════════════════════════════
// CHAINING METHODS
// ═══════════════════════════════════════════════════════════════════
2026-02-02 20:40:17 -05:00
public FieldDef tab(String tabName) {
this.tab = tabName;
return this;
}
public FieldDef range(float min, float max) {
this.min = min;
this.max = max;
return this;
}
public FieldDef column(ColumnHint column) {
this.column = column;
return this;
}
public FieldDef visibleWhen(BooleanSupplier condition) {
this.visibleWhen = condition;
return this;
}
2026-02-02 18:26:52 -05:00
public FieldDef enabledWhen(BooleanSupplier condition) {
this.enabledWhen = condition;
return this;
}
public FieldDef tooltip(String tooltip) {
this.tooltip = tooltip;
return this;
}
public FieldDef hover(String hoverText) {
this.hoverText = hoverText;
return this;
}
public FieldDef buttonLabel(Supplier<String> supplier) {
this.buttonLabelSupplier = supplier;
return this;
}
public FieldDef buttonTextColor(Supplier<Integer> supplier) {
this.buttonTextColorSupplier = supplier;
return this;
}
public FieldDef clearable(Runnable action) {
this.clearAction = action;
return this;
}
// ═══════════════════════════════════════════════════════════════════
// GETTERS
// ═══════════════════════════════════════════════════════════════════
public String getLabel() { return label; }
public FieldType getType() { return type; }
2026-02-02 20:40:17 -05:00
public String getTab() { return tab; }
public ColumnHint getColumn() { return column; }
public boolean isVisible() { return visibleWhen.getAsBoolean(); }
2026-02-02 18:26:52 -05:00
public boolean isEnabled() { return enabledWhen.getAsBoolean(); }
public String getTooltip() { return tooltip; }
public String getHoverText() { return hoverText; }
public float getMin() { return min; }
public float getMax() { return max; }
public boolean hasRange() { return min != Float.MIN_VALUE || max != Float.MAX_VALUE; }
public Class<? extends Enum<?>> getEnumClass() { return enumClass; }
public String[] getStringEnumValues() { return stringEnumValues; }
public Supplier<SubGuiInterface> getSubGuiFactory() { return subGuiFactory; }
public Consumer<SubGuiInterface> getSubGuiResultHandler() { return subGuiResultHandler; }
public boolean hasClearAction() { return clearAction != null; }
public Runnable getClearAction() { return clearAction; }
public Object getValue() {
return getter != null ? getter.get() : null;
}
public void setValue(Object value) {
if (setter != null) setter.accept(value);
}
public String getStringEnumValue() {
return stringEnumGetter != null ? stringEnumGetter.get() : null;
}
public void setStringEnumValue(String value) {
if (stringEnumSetter != null) stringEnumSetter.accept(value);
}
public String getButtonLabel() {
if (buttonLabelSupplier != null) return buttonLabelSupplier.get();
return label;
}
public Integer getButtonTextColor() {
return buttonTextColorSupplier != null ? buttonTextColorSupplier.get() : null;
}
}