2018-09-13 12:03:28 -04:00
|
|
|
package noppes.npcs.client.gui.util;
|
|
|
|
|
|
|
|
|
|
import net.minecraft.client.Minecraft;
|
|
|
|
|
import net.minecraft.client.gui.FontRenderer;
|
|
|
|
|
import net.minecraft.client.gui.GuiScreen;
|
|
|
|
|
import net.minecraft.client.gui.GuiTextField;
|
|
|
|
|
|
|
|
|
|
public class GuiNpcTextField extends GuiTextField{
|
|
|
|
|
public boolean enabled = true;
|
|
|
|
|
public boolean inMenu = true;
|
2021-12-03 00:49:10 -06:00
|
|
|
public boolean integersOnly = false;
|
|
|
|
|
public boolean doublesOnly = false;
|
|
|
|
|
public boolean floatsOnly = false;
|
2018-09-13 12:03:28 -04:00
|
|
|
private ITextfieldListener listener;
|
|
|
|
|
public int id;
|
|
|
|
|
public int min = 0,max = Integer.MAX_VALUE,def = 0;
|
2021-12-03 00:49:10 -06:00
|
|
|
public double minDouble = 0, maxDouble = Double.MAX_VALUE, defDouble = 0;
|
|
|
|
|
public float minFloat = 0, maxFloat = Float.MAX_VALUE, defFloat = 0;
|
2018-09-13 12:03:28 -04:00
|
|
|
private static GuiNpcTextField activeTextfield = null;
|
|
|
|
|
public boolean canEdit = true;
|
|
|
|
|
|
|
|
|
|
private final int[] allowedSpecialChars = {14,211,203,205};
|
|
|
|
|
|
|
|
|
|
public GuiNpcTextField(int id,GuiScreen parent, FontRenderer fontRenderer,
|
|
|
|
|
int i, int j, int k, int l, String s) {
|
|
|
|
|
super(fontRenderer, i, j, k, l);
|
|
|
|
|
setMaxStringLength(500);
|
|
|
|
|
this.setText(s);
|
|
|
|
|
this.id = id;
|
|
|
|
|
if(parent instanceof ITextfieldListener)
|
|
|
|
|
listener = (ITextfieldListener) parent;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-02 08:47:13 -05:00
|
|
|
public static boolean isFieldActive(){
|
2018-09-13 12:03:28 -04:00
|
|
|
return activeTextfield != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public GuiNpcTextField(int id,GuiScreen parent,
|
|
|
|
|
int i, int j, int k, int l, String s) {
|
|
|
|
|
this(id, parent, Minecraft.getMinecraft().fontRenderer, i, j, k, l, s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean charAllowed(char c, int i){
|
2021-12-03 00:49:10 -06:00
|
|
|
if(!integersOnly || Character.isDigit(c))
|
2018-09-13 12:03:28 -04:00
|
|
|
return true;
|
|
|
|
|
for(int j : allowedSpecialChars)
|
|
|
|
|
if(j == i)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean textboxKeyTyped(char c, int i)
|
|
|
|
|
{
|
|
|
|
|
if(!charAllowed(c,i) || !canEdit)
|
|
|
|
|
return false;
|
|
|
|
|
return super.textboxKeyTyped(c, i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isEmpty(){
|
|
|
|
|
return getText().trim().length() == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getInteger(){
|
|
|
|
|
return Integer.parseInt(getText());
|
|
|
|
|
}
|
2021-12-03 00:49:10 -06:00
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
public boolean isInteger(){
|
|
|
|
|
try{
|
|
|
|
|
Integer.parseInt(getText());
|
|
|
|
|
return true;
|
|
|
|
|
}catch(NumberFormatException e){
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-03 00:49:10 -06:00
|
|
|
|
|
|
|
|
public double getDouble(){
|
|
|
|
|
return Double.parseDouble(getText());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isDouble() {
|
|
|
|
|
try{
|
|
|
|
|
Double.parseDouble(getText());
|
|
|
|
|
return true;
|
|
|
|
|
}catch(NumberFormatException e){
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float getFloat(){
|
|
|
|
|
return Float.parseFloat(getText());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isFloat() {
|
|
|
|
|
try{
|
|
|
|
|
Float.parseFloat(getText());
|
|
|
|
|
return true;
|
|
|
|
|
}catch(NumberFormatException e){
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
@Override
|
|
|
|
|
public void mouseClicked(int i, int j, int k){
|
|
|
|
|
if(!canEdit)
|
|
|
|
|
return;
|
|
|
|
|
boolean wasFocused = this.isFocused();
|
|
|
|
|
super.mouseClicked(i, j, k);
|
|
|
|
|
if(wasFocused != isFocused()){
|
|
|
|
|
if(wasFocused){
|
|
|
|
|
unFocused();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(isFocused())
|
|
|
|
|
activeTextfield = this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void unFocused(){
|
2021-12-03 00:49:10 -06:00
|
|
|
if(integersOnly && !doublesOnly && !floatsOnly){
|
2018-09-13 12:03:28 -04:00
|
|
|
if(isEmpty() || !isInteger())
|
2021-12-03 00:49:10 -06:00
|
|
|
setText(def+"");
|
|
|
|
|
else if(getInteger() < min)
|
2018-09-13 12:03:28 -04:00
|
|
|
setText(min+"");
|
|
|
|
|
else if(getInteger() > max)
|
|
|
|
|
setText(max+"");
|
|
|
|
|
}
|
2021-12-03 00:49:10 -06:00
|
|
|
else if(doublesOnly && !floatsOnly){
|
2022-01-14 21:48:15 -05:00
|
|
|
if (isEmpty() || !isDouble())
|
|
|
|
|
setText(defDouble + "");
|
|
|
|
|
else if (getDouble() < minDouble)
|
|
|
|
|
setText(minDouble + "");
|
|
|
|
|
else if (getDouble() > maxDouble)
|
|
|
|
|
setText(maxDouble + "");
|
2021-12-03 00:49:10 -06:00
|
|
|
}
|
|
|
|
|
else if(floatsOnly){
|
|
|
|
|
if(isEmpty() || !isFloat())
|
|
|
|
|
setText(defFloat +"");
|
|
|
|
|
else if(getFloat() < minFloat)
|
|
|
|
|
setText(minFloat +"");
|
|
|
|
|
else if(getFloat() > maxFloat)
|
|
|
|
|
setText(maxFloat +"");
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
if(listener != null)
|
|
|
|
|
listener.unFocused(this);
|
|
|
|
|
|
|
|
|
|
if(this == activeTextfield)
|
|
|
|
|
activeTextfield = null;
|
|
|
|
|
}
|
2021-12-03 00:49:10 -06:00
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
@Override
|
|
|
|
|
public void drawTextBox()
|
|
|
|
|
{
|
|
|
|
|
if(enabled)
|
|
|
|
|
super.drawTextBox();
|
|
|
|
|
}
|
2021-12-03 00:49:10 -06:00
|
|
|
public void setMinMaxDefault(int min, int max, int def) {
|
|
|
|
|
this.min = min;
|
|
|
|
|
this.max = max;
|
|
|
|
|
this.def = def;
|
|
|
|
|
}
|
|
|
|
|
public void setMinMaxDefaultDouble(double min, double max, double def) {
|
|
|
|
|
minDouble = min;
|
|
|
|
|
maxDouble = max;
|
|
|
|
|
defDouble = def;
|
|
|
|
|
}
|
|
|
|
|
public void setMinMaxDefaultFloat(float min, float max, float def) {
|
|
|
|
|
minFloat = min;
|
|
|
|
|
maxFloat = max;
|
|
|
|
|
defFloat = def;
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
public static void unfocus() {
|
|
|
|
|
GuiNpcTextField prev = activeTextfield;
|
|
|
|
|
activeTextfield = null;
|
|
|
|
|
if(prev != null){
|
|
|
|
|
prev.unFocused();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void drawTextBox(int mousX, int mousY) {
|
|
|
|
|
drawTextBox();
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 00:49:10 -06:00
|
|
|
public GuiNpcTextField setIntegersOnly() {
|
|
|
|
|
integersOnly = true;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public GuiNpcTextField setDoublesOnly() {
|
|
|
|
|
doublesOnly = true;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
public GuiNpcTextField setFloatsOnly() {
|
|
|
|
|
floatsOnly = true;
|
2018-09-13 12:03:28 -04:00
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|