From b302114c6f9ba2dec238c532bb8708765b213bd3 Mon Sep 17 00:00:00 2001 From: johni0702 Date: Fri, 4 Dec 2015 09:23:06 +0100 Subject: [PATCH] Add GuiField type taking only numbers --- .../gui/element/AbstractGuiNumberField.java | 129 ++++++++++++++++++ .../minecraft/gui/element/GuiNumberField.java | 39 ++++++ .../gui/element/IGuiNumberField.java | 43 ++++++ 3 files changed, 211 insertions(+) create mode 100644 src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiNumberField.java create mode 100644 src/main/java/de/johni0702/minecraft/gui/element/GuiNumberField.java create mode 100644 src/main/java/de/johni0702/minecraft/gui/element/IGuiNumberField.java diff --git a/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiNumberField.java b/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiNumberField.java new file mode 100644 index 00000000..b80ddaee --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiNumberField.java @@ -0,0 +1,129 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.element; + +import com.google.common.base.Preconditions; +import de.johni0702.minecraft.gui.container.GuiContainer; + +import java.util.regex.Pattern; + +// TODO: This is suboptimal e.g. if there are trailing zeros, they stay (should be fixed after TextField is done w/o MC) +public abstract class AbstractGuiNumberField> + extends AbstractGuiTextField implements IGuiNumberField { + + private int precision; + private volatile Pattern precisionPattern; + + public AbstractGuiNumberField() { + } + + public AbstractGuiNumberField(GuiContainer container) { + super(container); + } + + { + setValue(0); + } + + @Override + public T setText(String text) { + if (!isTextValid(text)) { + throw new IllegalArgumentException(text + " is not a valid number!"); + } + return super.setText(text); + } + + @SuppressWarnings("ResultOfMethodCallIgnored") + private boolean isTextValid(String text) { + try { + if (precision == 0) { + Integer.parseInt(text); + return true; + } else { + Double.parseDouble(text); + return precisionPattern.matcher(text).matches(); + } + } catch (NumberFormatException e) { + return false; + } + } + + @Override + protected void onTextChanged(String from) { + if (isTextValid(getText())) { + super.onTextChanged(from); + } else { + setText(from); + } + } + + @Override + public byte getByte() { + return Byte.parseByte(getText()); + } + + @Override + public short getShort() { + return Short.parseShort(getText()); + } + + @Override + public int getInteger() { + return Integer.parseInt(getText()); + } + + @Override + public long getLong() { + return Long.parseLong(getText()); + } + + @Override + public float getFloat() { + return Float.parseFloat(getText()); + } + + @Override + public double getDouble() { + return Double.parseDouble(getText()); + } + + @Override + public T setValue(int value) { + setText(Integer.toString(value)); + return getThis(); + } + + @Override + public T setValue(double value) { + setText(String.format("%." + precision + "f", value)); + return getThis(); + } + + @Override + public T setPrecision(int precision) { + Preconditions.checkArgument(precision >= 0, "precision must not be negative"); + precisionPattern = Pattern.compile(String.format("-?[0-9]*+((\\.[0-9]{0,%d})?)||(\\.)?", precision)); + this.precision = precision; + return getThis(); + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/element/GuiNumberField.java b/src/main/java/de/johni0702/minecraft/gui/element/GuiNumberField.java new file mode 100644 index 00000000..f25b90e8 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/element/GuiNumberField.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.element; + +import de.johni0702.minecraft.gui.container.GuiContainer; + +public class GuiNumberField extends AbstractGuiNumberField { + public GuiNumberField() { + } + + public GuiNumberField(GuiContainer container) { + super(container); + } + + @Override + protected GuiNumberField getThis() { + return this; + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/element/IGuiNumberField.java b/src/main/java/de/johni0702/minecraft/gui/element/IGuiNumberField.java new file mode 100644 index 00000000..78f686fc --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/element/IGuiNumberField.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.element; + +public interface IGuiNumberField> extends IGuiTextField { + byte getByte(); + short getShort(); + int getInteger(); + long getLong(); + float getFloat(); + double getDouble(); + + T setValue(int value); + T setValue(double value); + + /** + * Sets the amount of digits allowed after the decimal point. + * A value of {@code 0} is equal to integer precision. + * Negative values are not allowed. + * @param precision Number of digits allowed + */ + T setPrecision(int precision); +}