mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-19 07:19:05 +00:00
Fix to Type Tab
This commit is contained in:
1 parent
0cbc30f8cb
commit
d3167df1b9
36 files changed
+648
-589
No files matched your search
@@ -3,10 +3,9 @@ package noppes.npcs.client.gui.advanced;
|
||||
import kamkeel.npcs.controllers.data.ability.Ability;
|
||||
import kamkeel.npcs.controllers.data.ability.AbilityEffect;
|
||||
import kamkeel.npcs.controllers.data.ability.Condition;
|
||||
import kamkeel.npcs.controllers.data.ability.gui.FieldDef;
|
||||
import kamkeel.npcs.controllers.data.ability.gui.FieldDefinitionsBuilder;
|
||||
import kamkeel.npcs.controllers.data.ability.gui.FieldType;
|
||||
import kamkeel.npcs.controllers.data.ability.gui.TabTarget;
|
||||
import kamkeel.npcs.controllers.data.ability.gui.AbilityFieldBuilder;
|
||||
import noppes.npcs.client.gui.builder.FieldDef;
|
||||
import noppes.npcs.client.gui.builder.FieldType;
|
||||
import net.minecraft.client.gui.GuiButton;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.StatCollector;
|
||||
@@ -36,6 +35,12 @@ public class SubGuiAbilityConfig extends SubGuiInterface implements ITextfieldLi
|
||||
private static final int TAB_EFFECTS = 3;
|
||||
// Custom tabs start at index 4
|
||||
|
||||
// Tab name strings (matching FieldDef.tab() values)
|
||||
private static final String TAB_NAME_GENERAL = "General";
|
||||
private static final String TAB_NAME_TYPE = "Type";
|
||||
private static final String TAB_NAME_TARGET = "Target";
|
||||
private static final String TAB_NAME_EFFECTS = "Effects";
|
||||
|
||||
private static final int L_LABEL_X = 5;
|
||||
private static final int ROW_H = 24;
|
||||
|
||||
@@ -49,7 +54,7 @@ public class SubGuiAbilityConfig extends SubGuiInterface implements ITextfieldLi
|
||||
private List<Condition> conditions;
|
||||
private int editingConditionIndex = -1;
|
||||
|
||||
private FieldDefinitionsBuilder builder;
|
||||
private AbilityFieldBuilder builder;
|
||||
private FieldDef activeSubGuiField = null;
|
||||
|
||||
// Scroll position preservation per tab
|
||||
@@ -71,9 +76,14 @@ public class SubGuiAbilityConfig extends SubGuiInterface implements ITextfieldLi
|
||||
private void discoverCustomTabs() {
|
||||
customTabNames = new ArrayList<>();
|
||||
for (FieldDef def : fieldDefs) {
|
||||
if (def.getTab() == TabTarget.CUSTOM && def.getCustomTabName() != null
|
||||
&& !customTabNames.contains(def.getCustomTabName())) {
|
||||
customTabNames.add(def.getCustomTabName());
|
||||
String tab = def.getTab();
|
||||
if (tab != null
|
||||
&& !TAB_NAME_GENERAL.equals(tab)
|
||||
&& !TAB_NAME_TYPE.equals(tab)
|
||||
&& !TAB_NAME_TARGET.equals(tab)
|
||||
&& !TAB_NAME_EFFECTS.equals(tab)
|
||||
&& !customTabNames.contains(tab)) {
|
||||
customTabNames.add(tab);
|
||||
}
|
||||
}
|
||||
tabScrollY = new float[4 + customTabNames.size()];
|
||||
@@ -125,40 +135,39 @@ public class SubGuiAbilityConfig extends SubGuiInterface implements ITextfieldLi
|
||||
GuiMenuTopButton closeBtn = new GuiMenuTopButton(-1000, guiLeft + xSize - 22, guiTop - 17, "X");
|
||||
addTopButton(closeBtn);
|
||||
|
||||
// Scroll window
|
||||
// Build scroll window dimensions
|
||||
int swX = guiLeft + 4;
|
||||
int swY = guiTop + 5;
|
||||
int swW = xSize - 8;
|
||||
int swH = ySize - 10;
|
||||
|
||||
GuiScrollWindow sw = new GuiScrollWindow(this, swX, swY, swW, swH, 0);
|
||||
sw.backgroundColor = 0x88000000;
|
||||
addScrollableGui(0, sw);
|
||||
|
||||
int y = 5;
|
||||
int labelCounter = LABEL_ID_START;
|
||||
|
||||
// General tab: type label at top
|
||||
// General tab: type label at top — need to reserve space before building
|
||||
if (activeTab == TAB_GENERAL) {
|
||||
sw.addLabel(new GuiNpcLabel(labelCounter++, "gui.type", 5, y + 5, 0xFFFFFF));
|
||||
sw.addLabel(new GuiNpcLabel(labelCounter++, ability.getTypeId(), 55, y + 5, 0xFFFFFF));
|
||||
y += ROW_H;
|
||||
}
|
||||
|
||||
// Build declarative fields using the builder
|
||||
// Build declarative fields into an auto-created scroll window
|
||||
List<FieldDef> tabFields = getVisibleFieldsForTab(activeTab);
|
||||
builder = new FieldDefinitionsBuilder(this, sw, fontRendererObj)
|
||||
.startIds(DECLARATIVE_ID_START, CLEAR_ID_START, labelCounter)
|
||||
.startY(y);
|
||||
builder = new AbilityFieldBuilder(this, fontRendererObj);
|
||||
builder.startIds(DECLARATIVE_ID_START, CLEAR_ID_START, labelCounter);
|
||||
builder.startY(y);
|
||||
|
||||
y = builder.build(tabFields);
|
||||
GuiScrollWindow sw = builder.buildScrollWindow(tabFields, swX, swY, swW, swH);
|
||||
|
||||
// Conditions on target tab
|
||||
if (activeTab == TAB_TARGET) {
|
||||
y = renderConditions(sw, y, builder.getNextLabelId());
|
||||
// General tab: add type label at top of scroll window
|
||||
if (activeTab == TAB_GENERAL) {
|
||||
sw.addLabel(new GuiNpcLabel(builder.getNextLabelId(), "gui.type", 5, 5 + 5, 0xFFFFFF));
|
||||
sw.addLabel(new GuiNpcLabel(builder.getNextLabelId() + 1, ability.getTypeId(), 55, 5 + 5, 0xFFFFFF));
|
||||
}
|
||||
|
||||
sw.maxScrollY = Math.max(y - swH, 0);
|
||||
// Conditions on target tab — appended after field defs
|
||||
if (activeTab == TAB_TARGET) {
|
||||
int condY = renderConditions(sw, builder.getLastBuildY(), builder.getNextLabelId());
|
||||
sw.maxScrollY = Math.max(condY - swH, 0);
|
||||
}
|
||||
|
||||
// Restore scroll position
|
||||
if (activeTab < tabScrollY.length) {
|
||||
@@ -171,33 +180,27 @@ public class SubGuiAbilityConfig extends SubGuiInterface implements ITextfieldLi
|
||||
private List<FieldDef> getVisibleFieldsForTab(int tabIndex) {
|
||||
List<FieldDef> result = new ArrayList<>();
|
||||
|
||||
String tabName;
|
||||
if (tabIndex >= 4) {
|
||||
// Custom tab — match by custom tab name
|
||||
int customIndex = tabIndex - 4;
|
||||
if (customIndex >= 0 && customIndex < customTabNames.size()) {
|
||||
String tabName = customTabNames.get(customIndex);
|
||||
for (FieldDef def : fieldDefs) {
|
||||
if (def.getTab() == TabTarget.CUSTOM
|
||||
&& tabName.equals(def.getCustomTabName())
|
||||
&& def.isVisible()) {
|
||||
result.add(def);
|
||||
}
|
||||
}
|
||||
tabName = customTabNames.get(customIndex);
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
} else {
|
||||
// Fixed tab
|
||||
TabTarget target;
|
||||
switch (tabIndex) {
|
||||
case TAB_GENERAL: target = TabTarget.GENERAL; break;
|
||||
case TAB_TYPE: target = TabTarget.TYPE; break;
|
||||
case TAB_TARGET: target = TabTarget.TARGET; break;
|
||||
case TAB_EFFECTS: target = TabTarget.EFFECTS; break;
|
||||
default: target = TabTarget.TYPE; break;
|
||||
case TAB_GENERAL: tabName = TAB_NAME_GENERAL; break;
|
||||
case TAB_TYPE: tabName = TAB_NAME_TYPE; break;
|
||||
case TAB_TARGET: tabName = TAB_NAME_TARGET; break;
|
||||
case TAB_EFFECTS: tabName = TAB_NAME_EFFECTS; break;
|
||||
default: tabName = TAB_NAME_TYPE; break;
|
||||
}
|
||||
for (FieldDef def : fieldDefs) {
|
||||
if (def.getTab() == target && def.isVisible()) {
|
||||
result.add(def);
|
||||
}
|
||||
}
|
||||
|
||||
for (FieldDef def : fieldDefs) {
|
||||
if (tabName.equals(def.getTab()) && def.isVisible()) {
|
||||
result.add(def);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package noppes.npcs.client.gui.builder;
|
||||
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public enum ColumnHint {
|
||||
FULL,
|
||||
LEFT,
|
||||
RIGHT
|
||||
}
|
||||
@@ -0,0 +1,296 @@
|
||||
package noppes.npcs.client.gui.builder;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Declarative field definition for GUI rendering.
|
||||
* Built via static factories and chaining methods.
|
||||
* Client-side only — never instantiate or reference on the server.
|
||||
*/
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class FieldDef {
|
||||
|
||||
private final String label;
|
||||
private final FieldType type;
|
||||
private String tab = null;
|
||||
private ColumnHint column = ColumnHint.FULL;
|
||||
private Supplier<Object> getter;
|
||||
private Consumer<Object> setter;
|
||||
private BooleanSupplier visibleWhen = () -> true;
|
||||
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
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
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();
|
||||
if (name != null && !name.isEmpty()) return name;
|
||||
int id = idGetter.get();
|
||||
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
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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; }
|
||||
public String getTab() { return tab; }
|
||||
public ColumnHint getColumn() { return column; }
|
||||
public boolean isVisible() { return visibleWhen.getAsBoolean(); }
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package noppes.npcs.client.gui.builder;
|
||||
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public enum FieldType {
|
||||
FLOAT,
|
||||
INT,
|
||||
BOOLEAN,
|
||||
ENUM,
|
||||
STRING_ENUM,
|
||||
SUB_GUI,
|
||||
STRING,
|
||||
SECTION_HEADER,
|
||||
LABEL,
|
||||
EFFECTS_LIST
|
||||
}
|
||||
@@ -0,0 +1,334 @@
|
||||
package noppes.npcs.client.gui.builder;
|
||||
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import net.minecraft.client.gui.FontRenderer;
|
||||
import net.minecraft.util.StatCollector;
|
||||
import noppes.npcs.client.gui.util.GuiNPCInterface;
|
||||
import noppes.npcs.client.gui.util.GuiNpcButton;
|
||||
import noppes.npcs.client.gui.util.GuiNpcButtonYesNo;
|
||||
import noppes.npcs.client.gui.util.GuiNpcLabel;
|
||||
import noppes.npcs.client.gui.util.GuiNpcTextField;
|
||||
import noppes.npcs.client.gui.util.GuiScrollWindow;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* General-purpose builder that converts a list of {@link FieldDef} into positioned
|
||||
* GUI widgets inside a {@link GuiScrollWindow}.
|
||||
* <p>
|
||||
* Supports 1-column and 2-column layouts. Subclasses can override {@link #buildField}
|
||||
* to handle custom field types.
|
||||
*/
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class GuiFieldBuilder {
|
||||
|
||||
// Layout defaults (2-column mode)
|
||||
protected int contentRight = 330;
|
||||
protected int colLLabel = 5;
|
||||
protected int colLField = 75;
|
||||
protected int colLWidth = 70;
|
||||
protected int colRLabel = 178;
|
||||
protected int colRField = 248;
|
||||
protected int colRWidth = 70;
|
||||
protected int rowHeight = 24;
|
||||
protected int labelPadding = 8;
|
||||
|
||||
// Column mode
|
||||
protected int columnCount = 2;
|
||||
|
||||
// Dependencies
|
||||
protected final GuiNPCInterface parent;
|
||||
protected GuiScrollWindow sw;
|
||||
protected final FontRenderer fontRenderer;
|
||||
|
||||
// Scroll window ID
|
||||
protected int scrollWindowId = 0;
|
||||
|
||||
// ID counters
|
||||
protected int widgetId;
|
||||
protected int clearId;
|
||||
protected int labelId;
|
||||
|
||||
// Starting Y
|
||||
protected int startY = 5;
|
||||
protected int lastBuildY = 0;
|
||||
|
||||
// Output maps
|
||||
protected final Map<Integer, FieldDef> buttonFieldMap = new HashMap<>();
|
||||
protected final Map<Integer, FieldDef> textFieldMap = new HashMap<>();
|
||||
protected final Map<Integer, FieldDef> clearFieldMap = new HashMap<>();
|
||||
|
||||
public GuiFieldBuilder(GuiNPCInterface parent, FontRenderer fontRenderer) {
|
||||
this.parent = parent;
|
||||
this.fontRenderer = fontRenderer;
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// CONFIG CHAINING
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
public GuiFieldBuilder scrollWindowId(int id) { this.scrollWindowId = id; return this; }
|
||||
public GuiFieldBuilder columns(int count) { this.columnCount = Math.max(1, Math.min(2, count)); return this; }
|
||||
public GuiFieldBuilder contentRight(int v) { this.contentRight = v; return this; }
|
||||
public GuiFieldBuilder startIds(int widget, int clear, int label) {
|
||||
this.widgetId = widget; this.clearId = clear; this.labelId = label; return this;
|
||||
}
|
||||
public GuiFieldBuilder startY(int y) { this.startY = y; return this; }
|
||||
public GuiFieldBuilder rowHeight(int h) { this.rowHeight = h; return this; }
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// BUILD
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
/**
|
||||
* Creates a {@link GuiScrollWindow} at the given position/size, builds all fields into it,
|
||||
* and sets maxScrollY automatically. The returned window is ready to use.
|
||||
*/
|
||||
public GuiScrollWindow buildScrollWindow(List<FieldDef> fields, int x, int y, int width, int height) {
|
||||
this.sw = new GuiScrollWindow(parent, x, y, width, height, 0);
|
||||
sw.backgroundColor = 0x88000000;
|
||||
// Register with parent first — this calls initGui() which clears widget lists,
|
||||
// so we must add widgets AFTER this point
|
||||
parent.addScrollableGui(scrollWindowId, sw);
|
||||
int finalY = build(fields);
|
||||
sw.maxScrollY = Math.max(finalY - height, 0);
|
||||
return sw;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes all fields and adds widgets to the scroll window.
|
||||
* @return the final Y position after all fields
|
||||
*/
|
||||
protected int build(List<FieldDef> fields) {
|
||||
buttonFieldMap.clear();
|
||||
textFieldMap.clear();
|
||||
clearFieldMap.clear();
|
||||
|
||||
int y = startY;
|
||||
|
||||
for (int i = 0; i < fields.size(); i++) {
|
||||
FieldDef def = fields.get(i);
|
||||
|
||||
// Let subclasses handle custom types; returns -1 if not handled
|
||||
int customResult = buildField(def, y, fields, i);
|
||||
if (customResult >= 0) {
|
||||
// Subclass handled it, check if index was advanced
|
||||
int newIndex = getLastHandledIndex();
|
||||
if (newIndex > i) i = newIndex;
|
||||
y = customResult;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Section header
|
||||
if (def.getType() == FieldType.SECTION_HEADER) {
|
||||
y += 3;
|
||||
String sectionText = StatCollector.translateToLocal(def.getLabel());
|
||||
if (def.getTooltip() != null && !def.getTooltip().isEmpty()) {
|
||||
sectionText += " (" + StatCollector.translateToLocal(def.getTooltip()) + ")";
|
||||
}
|
||||
sw.addLabel(new GuiNpcLabel(labelId++, sectionText, colLLabel, y + 2, 0xFFFF55));
|
||||
y += 15;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Two-column pair: LEFT followed by RIGHT (only in 2-column mode)
|
||||
if (columnCount == 2
|
||||
&& def.getColumn() == ColumnHint.LEFT
|
||||
&& i + 1 < fields.size()
|
||||
&& fields.get(i + 1).getColumn() == ColumnHint.RIGHT
|
||||
&& fields.get(i + 1).getType() != FieldType.SECTION_HEADER) {
|
||||
renderFieldAt(def, colLLabel, colLField, colLWidth, y);
|
||||
|
||||
i++;
|
||||
FieldDef rightDef = fields.get(i);
|
||||
renderFieldAt(rightDef, colRLabel, colRField, colRWidth, y);
|
||||
|
||||
y += rowHeight;
|
||||
} else {
|
||||
// Full-width: compute fieldX from label pixel width
|
||||
int fieldW = getFullFieldWidth(def);
|
||||
String translated = StatCollector.translateToLocal(def.getLabel());
|
||||
int labelW = fontRenderer.getStringWidth(translated);
|
||||
int fieldX = colLLabel + labelW + labelPadding;
|
||||
|
||||
renderFieldAt(def, colLLabel, fieldX, fieldW, y);
|
||||
y += rowHeight;
|
||||
}
|
||||
}
|
||||
|
||||
lastBuildY = y;
|
||||
return y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook for subclasses to handle custom field types (e.g. EFFECTS_LIST).
|
||||
* Return the new Y position if handled, or -1 if not handled (base class will process it).
|
||||
* If handling advances the field index, call {@link #setLastHandledIndex(int)}.
|
||||
*/
|
||||
protected int buildField(FieldDef def, int y, List<FieldDef> fields, int index) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
private int lastHandledIndex = -1;
|
||||
protected void setLastHandledIndex(int index) { this.lastHandledIndex = index; }
|
||||
protected int getLastHandledIndex() { return lastHandledIndex; }
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// FIELD RENDERING
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
protected void renderFieldAt(FieldDef def, int labelX, int fieldX, int fieldW, int y) {
|
||||
sw.addLabel(new GuiNpcLabel(labelId++, def.getLabel(), labelX, y + 5, 0xFFFFFF));
|
||||
|
||||
String hover = def.getHoverText() != null ? def.getHoverText() : def.getTooltip();
|
||||
|
||||
switch (def.getType()) {
|
||||
case FLOAT: {
|
||||
float fVal = def.getValue() instanceof Number ? ((Number) def.getValue()).floatValue() : 0f;
|
||||
GuiNpcTextField tf = new GuiNpcTextField(widgetId, parent, fontRenderer, fieldX, y, fieldW, 20, String.valueOf(fVal));
|
||||
tf.setFloatsOnly();
|
||||
if (def.hasRange()) tf.setMinMaxDefaultFloat(def.getMin(), def.getMax(), fVal);
|
||||
if (!def.isEnabled()) tf.setEnabled(false);
|
||||
if (hover != null) tf.setHoverText(hover);
|
||||
sw.addTextField(tf);
|
||||
textFieldMap.put(widgetId, def);
|
||||
widgetId++;
|
||||
clearId++;
|
||||
break;
|
||||
}
|
||||
case INT: {
|
||||
int iVal = def.getValue() instanceof Number ? ((Number) def.getValue()).intValue() : 0;
|
||||
GuiNpcTextField tf = new GuiNpcTextField(widgetId, parent, fontRenderer, fieldX, y, fieldW, 20, String.valueOf(iVal));
|
||||
tf.setIntegersOnly();
|
||||
if (def.hasRange()) tf.setMinMaxDefault((int) def.getMin(), (int) def.getMax(), iVal);
|
||||
if (!def.isEnabled()) tf.setEnabled(false);
|
||||
if (hover != null) tf.setHoverText(hover);
|
||||
sw.addTextField(tf);
|
||||
textFieldMap.put(widgetId, def);
|
||||
widgetId++;
|
||||
clearId++;
|
||||
break;
|
||||
}
|
||||
case STRING: {
|
||||
String sVal = def.getValue() != null ? def.getValue().toString() : "";
|
||||
GuiNpcTextField tf = new GuiNpcTextField(widgetId, parent, fontRenderer, fieldX, y, fieldW, 20, sVal);
|
||||
if (!def.isEnabled()) tf.setEnabled(false);
|
||||
if (hover != null) tf.setHoverText(hover);
|
||||
sw.addTextField(tf);
|
||||
textFieldMap.put(widgetId, def);
|
||||
widgetId++;
|
||||
clearId++;
|
||||
break;
|
||||
}
|
||||
case LABEL: {
|
||||
String text = def.getValue() != null ? def.getValue().toString() : "";
|
||||
sw.addLabel(new GuiNpcLabel(labelId++, text, fieldX, y + 5, 0xAAAAAA));
|
||||
widgetId++;
|
||||
clearId++;
|
||||
break;
|
||||
}
|
||||
case BOOLEAN: {
|
||||
boolean bVal = def.getValue() instanceof Boolean ? (Boolean) def.getValue() : false;
|
||||
GuiNpcButtonYesNo btn = new GuiNpcButtonYesNo(widgetId, fieldX, y, fieldW, 20, bVal);
|
||||
if (!def.isEnabled()) btn.setEnabled(false);
|
||||
if (hover != null) btn.setHoverText(hover);
|
||||
sw.addButton(btn);
|
||||
buttonFieldMap.put(widgetId, def);
|
||||
widgetId++;
|
||||
clearId++;
|
||||
break;
|
||||
}
|
||||
case ENUM: {
|
||||
Class<? extends Enum<?>> enumClass = def.getEnumClass();
|
||||
if (enumClass != null) {
|
||||
Enum<?>[] constants = enumClass.getEnumConstants();
|
||||
String[] names = new String[constants.length];
|
||||
for (int i = 0; i < constants.length; i++) names[i] = constants[i].toString();
|
||||
int selected = def.getValue() instanceof Enum ? ((Enum<?>) def.getValue()).ordinal() : 0;
|
||||
GuiNpcButton btn = new GuiNpcButton(widgetId, fieldX, y, fieldW, 20, names, selected);
|
||||
if (!def.isEnabled()) btn.setEnabled(false);
|
||||
if (hover != null) btn.setHoverText(hover);
|
||||
sw.addButton(btn);
|
||||
buttonFieldMap.put(widgetId, def);
|
||||
}
|
||||
widgetId++;
|
||||
clearId++;
|
||||
break;
|
||||
}
|
||||
case STRING_ENUM: {
|
||||
String[] values = def.getStringEnumValues();
|
||||
if (values != null && values.length > 0) {
|
||||
String curVal = def.getStringEnumValue();
|
||||
int selected = 0;
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
if (values[i].equals(curVal)) { selected = i; break; }
|
||||
}
|
||||
GuiNpcButton btn = new GuiNpcButton(widgetId, fieldX, y, fieldW, 20, values, selected);
|
||||
if (!def.isEnabled()) btn.setEnabled(false);
|
||||
if (hover != null) btn.setHoverText(hover);
|
||||
sw.addButton(btn);
|
||||
buttonFieldMap.put(widgetId, def);
|
||||
}
|
||||
widgetId++;
|
||||
clearId++;
|
||||
break;
|
||||
}
|
||||
case SUB_GUI: {
|
||||
String btnText = def.getButtonLabel();
|
||||
int btnW;
|
||||
if (def.hasClearAction()) {
|
||||
btnW = contentRight - 22 - fieldX;
|
||||
GuiNpcButton clearBtn = new GuiNpcButton(clearId, contentRight - 20, y, 20, 20, "X");
|
||||
sw.addButton(clearBtn);
|
||||
clearFieldMap.put(clearId, def);
|
||||
} else {
|
||||
btnW = contentRight - fieldX;
|
||||
}
|
||||
GuiNpcButton btn = new GuiNpcButton(widgetId, fieldX, y, btnW, 20, btnText);
|
||||
Integer textColor = def.getButtonTextColor();
|
||||
if (textColor != null) btn.setTextColor(textColor);
|
||||
if (!def.isEnabled()) btn.setEnabled(false);
|
||||
if (hover != null) btn.setHoverText(hover);
|
||||
sw.addButton(btn);
|
||||
buttonFieldMap.put(widgetId, def);
|
||||
widgetId++;
|
||||
clearId++;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
// Unknown type — skip, incrementing IDs to stay in sync
|
||||
widgetId++;
|
||||
clearId++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected int getFullFieldWidth(FieldDef def) {
|
||||
switch (def.getType()) {
|
||||
case FLOAT:
|
||||
case INT: return 60;
|
||||
case STRING: return 200;
|
||||
case BOOLEAN: return 50;
|
||||
case LABEL: return 60;
|
||||
case ENUM:
|
||||
case STRING_ENUM: return 120;
|
||||
case SUB_GUI: return def.hasClearAction() ? 175 : 200;
|
||||
default: return 80;
|
||||
}
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// GETTERS
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
public Map<Integer, FieldDef> getButtonFieldMap() { return buttonFieldMap; }
|
||||
public Map<Integer, FieldDef> getTextFieldMap() { return textFieldMap; }
|
||||
public Map<Integer, FieldDef> getClearFieldMap() { return clearFieldMap; }
|
||||
public GuiScrollWindow getScrollWindow() { return sw; }
|
||||
public int getLastBuildY() { return lastBuildY; }
|
||||
public int getNextLabelId() { return labelId; }
|
||||
}
|
||||
@@ -55,7 +55,6 @@ public class GuiAnimationSelection extends SubGuiInterface implements ICustomScr
|
||||
* Constructor for selecting by name (built-in) or ID (user).
|
||||
*/
|
||||
public GuiAnimationSelection(int animationId, String animationName) {
|
||||
drawDefaultBackground = false;
|
||||
title = "";
|
||||
setBackground("menubg.png");
|
||||
xSize = 220;
|
||||
|
||||
@@ -39,7 +39,6 @@ public class GuiSoundSelection extends SubGuiInterface implements ICustomScrollL
|
||||
public static HashMap<String, List<String>> cachedDomains = new HashMap<>();
|
||||
|
||||
public GuiSoundSelection(String sound) {
|
||||
drawDefaultBackground = false;
|
||||
title = "";
|
||||
setBackground("menubg.png");
|
||||
xSize = 366;
|
||||
|
||||
@@ -104,8 +104,10 @@ public class GuiCustomScroll extends GuiScreen {
|
||||
GL11.glTranslatef(guiLeft, guiTop, 0.0F);
|
||||
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
|
||||
|
||||
if (selectable)
|
||||
if (selectable && !hasSubGUI)
|
||||
hover = getMouseOver(i, j);
|
||||
else if (hasSubGUI)
|
||||
hover = -1;
|
||||
|
||||
drawItems();
|
||||
|
||||
|
||||
@@ -97,6 +97,11 @@ public class GuiScrollWindow extends GuiScreen implements ITextfieldListener, IC
|
||||
}
|
||||
|
||||
public void mouseClicked(int i, int j, int k) {
|
||||
// Ignore clicks outside the visible scroll window area
|
||||
if (i < xPos || i > xPos + clipWidth || j < yPos || j > yPos + clipHeight) {
|
||||
return;
|
||||
}
|
||||
|
||||
i = i - xPos;
|
||||
j = (int) (j - yPos + scrollY);
|
||||
|
||||
@@ -246,6 +251,12 @@ public class GuiScrollWindow extends GuiScreen implements ITextfieldListener, IC
|
||||
}
|
||||
|
||||
GL11.glPopMatrix();
|
||||
|
||||
// Draw hover texts in screen space (after scissor/translation are undone)
|
||||
// so tooltips are not clipped and are positioned correctly
|
||||
if (isMouseOver(mouseX, mouseY)) {
|
||||
drawHoverTexts(mouseX, mouseY);
|
||||
}
|
||||
}
|
||||
|
||||
public void drawScreen(int mouseX, int mouseY, float partialTicks, int mouseScroll) {
|
||||
@@ -275,8 +286,18 @@ public class GuiScrollWindow extends GuiScreen implements ITextfieldListener, IC
|
||||
}
|
||||
for (GuiNpcButton button : buttons.values()) {
|
||||
button.updateSubGUI(subGui);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws hover texts for buttons in screen space (called after GL scissor/translate are undone).
|
||||
* Hover state was already computed during drawComponents with scroll-local coords.
|
||||
*/
|
||||
private void drawHoverTexts(int mouseX, int mouseY) {
|
||||
boolean subGui = parent.hasSubGui();
|
||||
for (GuiNpcButton button : buttons.values()) {
|
||||
if (!button.hoverableText.isEmpty()) {
|
||||
button.drawHover(i, j, subGui);
|
||||
button.drawHover(mouseX, mouseY, subGui);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user