Commit 93696d02a1926eaaa5a3d283978f3e60298182d7
1 parent
08cd98f1
Add examples for config panel text fields
Showing
1 changed file
with
26 additions
and
0 deletions
src/main/java/com/examplemod/ExampleModConfigPanel.java
| ... | ... | @@ -4,6 +4,7 @@ import com.mumfrey.liteloader.Configurable; |
| 4 | 4 | import com.mumfrey.liteloader.client.gui.GuiCheckbox; |
| 5 | 5 | import com.mumfrey.liteloader.modconfig.AbstractConfigPanel; |
| 6 | 6 | import com.mumfrey.liteloader.modconfig.ConfigPanelHost; |
| 7 | +import com.mumfrey.liteloader.util.log.LiteLoaderLogger; | |
| 7 | 8 | import net.minecraft.client.resources.I18n; |
| 8 | 9 | |
| 9 | 10 | /** |
| ... | ... | @@ -15,6 +16,9 @@ import net.minecraft.client.resources.I18n; |
| 15 | 16 | */ |
| 16 | 17 | public class ExampleModConfigPanel extends AbstractConfigPanel |
| 17 | 18 | { |
| 19 | + // Preserve references to the text field handles so we can read the values | |
| 20 | + private ConfigTextField txtString, txtNumeric; | |
| 21 | + | |
| 18 | 22 | /* (non-Javadoc) |
| 19 | 23 | * @see com.mumfrey.liteloader.modconfig.ConfigPanel#getPanelTitle() |
| 20 | 24 | */ |
| ... | ... | @@ -42,6 +46,9 @@ public class ExampleModConfigPanel extends AbstractConfigPanel |
| 42 | 46 | mod.setClockVisibility(control.checked = !control.checked); |
| 43 | 47 | } |
| 44 | 48 | }).checked = mod.getClockVisibility(); |
| 49 | + | |
| 50 | + this.txtString = this.addTextField(2, 0, 50, 200, 20).setMaxLength(255); | |
| 51 | + this.txtNumeric = this.addTextField(3, 0, 75, 200, 20).setRegex("^[0-9]*$", false).setMaxLength(6); | |
| 45 | 52 | } |
| 46 | 53 | |
| 47 | 54 | @Override |
| ... | ... | @@ -50,5 +57,24 @@ public class ExampleModConfigPanel extends AbstractConfigPanel |
| 50 | 57 | // This example applies the changes immediately, however you may wish to |
| 51 | 58 | // only save changes when the user clicks "save and close". In which |
| 52 | 59 | // case you should apply your changes here |
| 60 | + | |
| 61 | + | |
| 62 | + // Code below shows how to read and handle the values out of the text | |
| 63 | + // fields, what you do with the values is up to you. | |
| 64 | + | |
| 65 | + LiteLoaderLogger.info("Text field value was %s", this.txtString.getText()); | |
| 66 | + | |
| 67 | + if (this.txtNumeric.isValid()) | |
| 68 | + { | |
| 69 | + // Prefix 0 on the value since an empty string is valid, this saves | |
| 70 | + // us having to special-case the string being empty. Alternatively | |
| 71 | + // we could disallow empty strings in the validation regex. | |
| 72 | + int intValue = Integer.parseInt("0" + this.txtNumeric.getText()); | |
| 73 | + LiteLoaderLogger.info("Numeric field value was %d", intValue); | |
| 74 | + } | |
| 75 | + else | |
| 76 | + { | |
| 77 | + LiteLoaderLogger.info("Numeric field value was not valid!"); | |
| 78 | + } | |
| 53 | 79 | } |
| 54 | 80 | } | ... | ... |