mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-18 11:29:15 +00:00
Allow config GUI cycling button elements generated from enums to display toString return values, rather than actual values. (#5125)
This commit is contained in:
1 parent
3dd8c8ae6f
commit
d3f27cf6fb
8 files changed
+222
-24
No files matched your search
@@ -207,6 +207,12 @@ public class ConfigElement implements IConfigElement
|
||||
return isProperty ? prop.getValidValues() : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getValidValuesDisplay()
|
||||
{
|
||||
return isProperty ? prop.getValidValuesDisplay() : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLanguageKey()
|
||||
{
|
||||
|
||||
@@ -628,6 +628,25 @@ public class Configuration
|
||||
return prop;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a string Property with a comment using the defined validValues array and otherwise default settings.
|
||||
*
|
||||
* @param category the config category
|
||||
* @param key the Property key value
|
||||
* @param defaultValue the default value
|
||||
* @param comment a String comment
|
||||
* @param validValues an array of valid values that this Property can be set to. If an array is provided the Config GUI control will be
|
||||
* a value cycle button.
|
||||
* @param validValuesDisplay an array of the config GUI display versions of the valid values that this Property can be set to.
|
||||
* @return a string Property with the defined validValues array, validationPattern = null
|
||||
*/
|
||||
public Property get(String category, String key, String defaultValue, String comment, String[] validValues, String[] validValuesDisplay)
|
||||
{
|
||||
Property prop = get(category, key, defaultValue, comment, validValues);
|
||||
prop.setValidValuesDisplay(validValuesDisplay);
|
||||
return prop;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a string array Property without a comment using the default settings.
|
||||
*
|
||||
@@ -1541,6 +1560,22 @@ public class Configuration
|
||||
return getString(name, category, defaultValue, comment, validValues, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string property.
|
||||
*
|
||||
* @param name Name of the property.
|
||||
* @param category Category of the property.
|
||||
* @param defaultValue Default value of the property.
|
||||
* @param comment A brief description what the property does.
|
||||
* @param validValues A list of valid values that this property can be set to.
|
||||
* @param validValuesDisplay an array of the config GUI display versions of the valid values that this Property can be set to.
|
||||
* @return The value of the new string property.
|
||||
*/
|
||||
public String getString(String name, String category, String defaultValue, String comment, String[] validValues, String[] validValuesDisplay)
|
||||
{
|
||||
return setPropertyAndGetString(name, category, defaultValue, comment, validValues, validValuesDisplay, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string property.
|
||||
*
|
||||
@@ -1553,9 +1588,32 @@ public class Configuration
|
||||
* @return The value of the new string property.
|
||||
*/
|
||||
public String getString(String name, String category, String defaultValue, String comment, String[] validValues, String langKey)
|
||||
{
|
||||
return setPropertyAndGetString(name, category, defaultValue, comment, validValues, null, langKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string property.
|
||||
*
|
||||
* @param name Name of the property.
|
||||
* @param category Category of the property.
|
||||
* @param defaultValue Default value of the property.
|
||||
* @param comment A brief description what the property does.
|
||||
* @param validValues A list of valid values that this property can be set to.
|
||||
* @param validValuesDisplay an array of the config GUI display versions of the valid values that this Property can be set to.
|
||||
* @param langKey A language key used for localization of GUIs
|
||||
* @return The value of the new string property.
|
||||
*/
|
||||
public String getString(String name, String category, String defaultValue, String comment, String[] validValues, String[] validValuesDisplay, String langKey)
|
||||
{
|
||||
return setPropertyAndGetString(name, category, defaultValue, comment, validValues, validValuesDisplay, langKey);
|
||||
}
|
||||
|
||||
private String setPropertyAndGetString(String name, String category, String defaultValue, String comment, String[] validValues, String[] validValuesDisplay, String langKey)
|
||||
{
|
||||
Property prop = this.get(category, name, defaultValue);
|
||||
prop.setValidValues(validValues);
|
||||
prop.setValidValuesDisplay(validValuesDisplay);
|
||||
prop.setLanguageKey(langKey);
|
||||
prop.setComment(comment + " [default: " + defaultValue + "]");
|
||||
return prop.getString();
|
||||
@@ -1596,14 +1654,55 @@ public class Configuration
|
||||
* @param category Category of the property.
|
||||
* @param defaultValue Default value of the property.
|
||||
* @param comment A brief description what the property does.
|
||||
* @param validValues A list of valid values that this property can be set to.
|
||||
* @param validValuesDisplay an array of the config GUI display versions of the valid values that this Property can be set to.
|
||||
* @return The value of the new string property.
|
||||
*/
|
||||
public String[] getStringList(String name, String category, String[] defaultValue, String comment, String[] validValues, String[] validValuesDisplay)
|
||||
{
|
||||
return setPropertyAndGetStringList(name, category, defaultValue, comment, validValues, validValuesDisplay, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string list property.
|
||||
*
|
||||
* @param name Name of the property.
|
||||
* @param category Category of the property.
|
||||
* @param defaultValue Default value of the property.
|
||||
* @param comment A brief description what the property does.
|
||||
* @param validValues A list of valid values that this property can be set to.
|
||||
* @param langKey A language key used for localization of GUIs
|
||||
* @return The value of the new string property.
|
||||
*/
|
||||
public String[] getStringList(String name, String category, String[] defaultValue, String comment, String[] validValues, String langKey)
|
||||
{
|
||||
return setPropertyAndGetStringList(name, category, defaultValue, comment, validValues, null, langKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string list property.
|
||||
*
|
||||
* @param name Name of the property.
|
||||
* @param category Category of the property.
|
||||
* @param defaultValue Default value of the property.
|
||||
* @param comment A brief description what the property does.
|
||||
* @param validValues A list of valid values that this property can be set to.
|
||||
* @param validValuesDisplay an array of the config GUI display versions of the valid values that this Property can be set to.
|
||||
* @param langKey A language key used for localization of GUIs
|
||||
* @return The value of the new string property.
|
||||
*/
|
||||
public String[] getStringList(String name, String category, String[] defaultValue, String comment, String[] validValues, String[] validValuesDisplay, String langKey)
|
||||
{
|
||||
return setPropertyAndGetStringList(name, category, defaultValue, comment, validValues, validValuesDisplay, langKey);
|
||||
}
|
||||
|
||||
private String[] setPropertyAndGetStringList(String name, String category, String[] defaultValue, String comment, String[] validValues, String[] validValuesDisplay, String langKey)
|
||||
{
|
||||
Property prop = this.get(category, name, defaultValue);
|
||||
prop.setLanguageKey(langKey);
|
||||
prop.setValidValues(validValues);
|
||||
prop.setComment(comment + " [default: " + prop.getDefault() + "]");
|
||||
prop.setValidValuesDisplay(validValuesDisplay);
|
||||
prop.setLanguageKey(langKey);
|
||||
prop.setComment(comment + " [default: " + defaultValue + "]");
|
||||
return prop.getStringList();
|
||||
}
|
||||
|
||||
|
||||
@@ -259,14 +259,19 @@ public abstract class FieldWrapper implements IFieldWrapper
|
||||
|
||||
Property prop = cfg.getCategory(this.category).get(this.name); // Will be setup in general by ConfigManager
|
||||
|
||||
List<String> lst = Lists.newArrayList();
|
||||
List<String> listValidValues = Lists.newArrayList();
|
||||
List<String> listValidValuesDisplay = Lists.newArrayList();
|
||||
for (Enum e : ((Class<? extends Enum>) field.getType()).getEnumConstants())
|
||||
lst.add(e.name());
|
||||
{
|
||||
listValidValues.add(e.name());
|
||||
listValidValuesDisplay.add(e.toString());
|
||||
}
|
||||
|
||||
prop.setValidationPattern(Pattern.compile(PIPE.join(lst)));
|
||||
prop.setValidValues(lst.toArray(new String[0]));
|
||||
prop.setValidationPattern(Pattern.compile(PIPE.join(listValidValues)));
|
||||
prop.setValidValues(listValidValues.toArray(new String[0]));
|
||||
prop.setValidValuesDisplay(listValidValuesDisplay.toArray(new String[0]));
|
||||
|
||||
String validValues = NEW_LINE.join(lst);
|
||||
String validValues = NEW_LINE.join(listValidValues);
|
||||
|
||||
if (desc != null)
|
||||
prop.setComment(NEW_LINE.join(new String[] { desc, "Valid values:" }) + "\n" + validValues);
|
||||
|
||||
@@ -78,6 +78,7 @@ public class Property
|
||||
private String[] values;
|
||||
private String[] defaultValues;
|
||||
private String[] validValues;
|
||||
private String[] validValuesDisplay;
|
||||
private String langKey;
|
||||
private String minValue;
|
||||
private String maxValue;
|
||||
@@ -98,35 +99,45 @@ public class Property
|
||||
|
||||
public Property(String name, String value, Type type)
|
||||
{
|
||||
this(name, value, type, false, new String[0], name);
|
||||
this(name, value, type, false, new String[0], new String[0], name);
|
||||
}
|
||||
|
||||
public Property(String name, String value, Type type, boolean read)
|
||||
{
|
||||
this(name, value, type, read, new String[0], name);
|
||||
this(name, value, type, read, new String[0], new String[0], name);
|
||||
}
|
||||
|
||||
public Property(String name, String value, Type type, String[] validValues)
|
||||
{
|
||||
this(name, value, type, false, validValues, name);
|
||||
this(name, value, type, false, validValues, new String[0], name);
|
||||
}
|
||||
|
||||
public Property(String name, String value, Type type, String[] validValues, String[] validValuesDisplay)
|
||||
{
|
||||
this(name, value, type, false, validValues, validValuesDisplay, name);
|
||||
}
|
||||
|
||||
public Property(String name, String value, Type type, String langKey)
|
||||
{
|
||||
this(name, value, type, false, new String[0], langKey);
|
||||
this(name, value, type, false, new String[0], new String[0], langKey);
|
||||
}
|
||||
|
||||
public Property(String name, String value, Type type, boolean read, String langKey)
|
||||
{
|
||||
this(name, value, type, read, new String[0], langKey);
|
||||
this(name, value, type, read, new String[0], new String[0], langKey);
|
||||
}
|
||||
|
||||
public Property(String name, String value, Type type, String[] validValues, String langKey)
|
||||
{
|
||||
this(name, value, type, false, validValues, langKey);
|
||||
this(name, value, type, false, validValues, new String[0], langKey);
|
||||
}
|
||||
|
||||
Property(String name, String value, Type type, boolean read, String[] validValues, String langKey)
|
||||
public Property(String name, String value, Type type, String[] validValues, String[] validValuesDisplay, String langKey)
|
||||
{
|
||||
this(name, value, type, false, validValues, validValuesDisplay, langKey);
|
||||
}
|
||||
|
||||
Property(String name, String value, Type type, boolean read, String[] validValues, String[] validValuesDisplay, String langKey)
|
||||
{
|
||||
setName(name);
|
||||
this.value = value;
|
||||
@@ -137,6 +148,7 @@ public class Property
|
||||
this.defaultValue = value;
|
||||
this.defaultValues = new String[0];
|
||||
this.validValues = validValues;
|
||||
this.validValuesDisplay = validValues;
|
||||
this.isListLengthFixed = false;
|
||||
this.maxListLength = -1;
|
||||
this.minValue = String.valueOf(Integer.MIN_VALUE);
|
||||
@@ -166,6 +178,11 @@ public class Property
|
||||
}
|
||||
|
||||
Property(String name, String[] values, Type type, boolean read, String[] validValues, String langKey)
|
||||
{
|
||||
this(name, values, type, read, validValues, new String[0], langKey);
|
||||
}
|
||||
|
||||
Property(String name, String[] values, Type type, boolean read, String[] validValues, String[] validValuesDisplay, String langKey)
|
||||
{
|
||||
setName(name);
|
||||
this.type = type;
|
||||
@@ -179,6 +196,7 @@ public class Property
|
||||
this.defaultValue = this.defaultValue.replaceFirst(", ", "");
|
||||
this.defaultValues = Arrays.copyOf(values, values.length);
|
||||
this.validValues = validValues;
|
||||
this.validValuesDisplay = validValues;
|
||||
this.isListLengthFixed = false;
|
||||
this.maxListLength = -1;
|
||||
this.minValue = String.valueOf(Integer.MIN_VALUE);
|
||||
@@ -692,7 +710,7 @@ public class Property
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the array of valid values that this String Property can be set to, or null if not defined.
|
||||
* Gets the array of valid values that this String Property can be set to, or null or empty if not defined.
|
||||
*
|
||||
* @return a String array of valid values
|
||||
*/
|
||||
@@ -701,6 +719,28 @@ public class Property
|
||||
return this.validValues;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the array of the config GUI display versions of the valid values that this String Property can be set to.
|
||||
* When an array of valid values is defined for a Property the GUI control for that property will be a value cycle button.
|
||||
*
|
||||
* @param validValueAliases a String array of the aliases of valid values
|
||||
*/
|
||||
public Property setValidValuesDisplay(String[] validValuesDisplay)
|
||||
{
|
||||
this.validValuesDisplay = validValuesDisplay;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the array of the config GUI display versions of the valid values that this String Property can be set to, or null or empty if not defined.
|
||||
*
|
||||
* @return a String array of the aliases of the valid values
|
||||
*/
|
||||
public String[] getValidValuesDisplay()
|
||||
{
|
||||
return this.validValuesDisplay;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value in this property as an integer,
|
||||
* if the value is not a valid integer, it will return the initially provided default.
|
||||
|
||||
@@ -50,6 +50,7 @@ public class DummyConfigElement implements IConfigElement
|
||||
protected Object[] values;
|
||||
protected Object[] defaultValues;
|
||||
protected String[] validValues;
|
||||
protected String[] validValuesDisplay;
|
||||
protected Pattern validStringPattern;
|
||||
protected Object minValue;
|
||||
protected Object maxValue;
|
||||
@@ -165,8 +166,8 @@ public class DummyConfigElement implements IConfigElement
|
||||
return Arrays.toString(this.defaultValues);
|
||||
}
|
||||
}
|
||||
|
||||
public DummyConfigElement(String name, Object defaultValue, ConfigGuiType type, String langKey, String[] validValues, Pattern validStringPattern, Object minValue, Object maxValue)
|
||||
|
||||
public DummyConfigElement(String name, Object defaultValue, ConfigGuiType type, String langKey, String[] validValues, String[] validValuesDisplay, Pattern validStringPattern, Object minValue, Object maxValue)
|
||||
{
|
||||
this.name = name;
|
||||
this.defaultValue = defaultValue;
|
||||
@@ -174,6 +175,7 @@ public class DummyConfigElement implements IConfigElement
|
||||
this.type = type;
|
||||
this.langKey = langKey;
|
||||
this.validValues = validValues;
|
||||
this.validValuesDisplay = validValuesDisplay;
|
||||
this.validStringPattern = validStringPattern;
|
||||
if (minValue == null)
|
||||
{
|
||||
@@ -194,7 +196,12 @@ public class DummyConfigElement implements IConfigElement
|
||||
else
|
||||
this.maxValue = maxValue;
|
||||
}
|
||||
|
||||
|
||||
public DummyConfigElement(String name, Object defaultValue, ConfigGuiType type, String langKey, String[] validValues, Pattern validStringPattern, Object minValue, Object maxValue)
|
||||
{
|
||||
this(name, defaultValue, type, langKey, validValues, null, validStringPattern, minValue, maxValue);
|
||||
}
|
||||
|
||||
public DummyConfigElement(String name, Object defaultValue, ConfigGuiType type, String langKey, Pattern validStringPattern)
|
||||
{
|
||||
this(name, defaultValue, type, langKey, null, validStringPattern, null, null);
|
||||
@@ -204,7 +211,12 @@ public class DummyConfigElement implements IConfigElement
|
||||
{
|
||||
this(name, defaultValue, type, langKey, validValues, null, null, null);
|
||||
}
|
||||
|
||||
|
||||
public DummyConfigElement(String name, Object defaultValue, ConfigGuiType type, String langKey, String[] validValues, String[] validValuesDisplay)
|
||||
{
|
||||
this(name, defaultValue, type, langKey, validValues, validValuesDisplay, null, null, null);
|
||||
}
|
||||
|
||||
public DummyConfigElement(String name, Object defaultValue, ConfigGuiType type, String langKey)
|
||||
{
|
||||
this(name, defaultValue, type, langKey, null, null, null, null);
|
||||
@@ -382,6 +394,12 @@ public class DummyConfigElement implements IConfigElement
|
||||
{
|
||||
return validValues;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getValidValuesDisplay()
|
||||
{
|
||||
return validValuesDisplay;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pattern getValidationPattern()
|
||||
|
||||
@@ -476,7 +476,13 @@ public class GuiConfigEntries extends GuiListExtended
|
||||
@Override
|
||||
public void updateValueButtonText()
|
||||
{
|
||||
this.btnValue.displayString = I18n.format(configElement.getValidValues()[currentIndex]);
|
||||
this.btnValue.displayString = I18n.format(getValidValueDisplay());
|
||||
}
|
||||
|
||||
protected String getValidValueDisplay()
|
||||
{
|
||||
String[] validValuesDisplay = configElement.getValidValuesDisplay();
|
||||
return validValuesDisplay != null && validValuesDisplay.length > 0 ? validValuesDisplay[currentIndex] : configElement.getValidValues()[currentIndex];
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -564,14 +570,14 @@ public class GuiConfigEntries extends GuiListExtended
|
||||
@Override
|
||||
public void drawEntry(int slotIndex, int x, int y, int listWidth, int slotHeight, int mouseX, int mouseY, boolean isSelected, float partial)
|
||||
{
|
||||
this.btnValue.packedFGColour = GuiUtils.getColorCode(this.configElement.getValidValues()[currentIndex].charAt(0), true);
|
||||
this.btnValue.packedFGColour = GuiUtils.getColorCode(getValidValueDisplay().charAt(0), true);
|
||||
super.drawEntry(slotIndex, x, y, listWidth, slotHeight, mouseX, mouseY, isSelected, partial);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateValueButtonText()
|
||||
{
|
||||
this.btnValue.displayString = I18n.format(configElement.getValidValues()[currentIndex]) + " - " + I18n.format("fml.configgui.sampletext");
|
||||
this.btnValue.displayString = I18n.format(getValidValueDisplay()) + " - " + I18n.format("fml.configgui.sampletext");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -171,7 +171,13 @@ public interface IConfigElement
|
||||
* select a value from a list of valid values.
|
||||
*/
|
||||
String[] getValidValues();
|
||||
|
||||
|
||||
/**
|
||||
* [Property] Gets a String array of the versions of this property's valid values that will display in the config GUI.
|
||||
* This is generally used for String properties to allow the user to select a value from a list of valid values.
|
||||
*/
|
||||
String[] getValidValuesDisplay();
|
||||
|
||||
/**
|
||||
* [Property] Gets this property's minimum value.
|
||||
*/
|
||||
|
||||
@@ -132,7 +132,25 @@ public class ConfigAnnotationTest
|
||||
public static TEST enu = TEST.BIG;
|
||||
public static NestedType Inner = new NestedType();
|
||||
|
||||
public enum TEST { BIG, BAD, WOLF; }
|
||||
public enum TEST
|
||||
{
|
||||
BIG("Big"),
|
||||
BAD("Bad"),
|
||||
WOLF("Wolf");
|
||||
|
||||
private String name;
|
||||
|
||||
private TEST(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
}
|
||||
public static class NestedType
|
||||
{
|
||||
public String HeyLook = "I'm Inside!";
|
||||
|
||||
Reference in new issue
Block a user