This repository has been archived by the owner on Feb 1, 2022. It is now read-only.
/
GuiNumTextField.java
112 lines (98 loc) · 2.03 KB
/
GuiNumTextField.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package fuj1n.recmod.client.gui;
import net.minecraft.client.gui.*;
public class GuiNumTextField extends Gui
{
private FontRenderer fn;
private int x, y, width, height;
public int min, max;
public String value = "";
public boolean hasFocus, canFocus = true, visible = true;
public GuiNumTextField(FontRenderer fn, int x, int y, int width, int height, int min, int max)
{
this.fn = fn;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.min = min;
this.max = max;
}
public GuiNumTextField(FontRenderer fn, int x, int y, int width, int height, int min, int max, int value)
{
this.fn = fn;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.min = min;
this.max = max;
this.value = Integer.toString(value);
}
public void draw (String label)
{
if (visible)
{
if (label != null && !label.equals(""))
{
fn.drawString(label, x - 5 - fn.getStringWidth(label), y + height / 2 - fn.FONT_HEIGHT / 2, 0x000000);
}
drawRect(x - 1, y - 1, x + width + 1, y + height + 1, 0xFFFFFFFF);
drawRect(x, y, x + width, y + height, 0xFF000000);
fn.drawString(value + (hasFocus ? "_" : ""), x + 5, y + height / 2 - fn.FONT_HEIGHT / 2, 0xFFFFFF);
}
}
public void keyTyped (char c, int i)
{
if (!canFocus)
hasFocus = false;
if (!hasFocus)
return;
if ((value.length() == 0 && c == '-' && min < 0) || Character.isDigit(c))
{
if (value(value + c) >= min && value(value + c) <= max)
{
value += c;
}
}
else if (i == 14)
{
if (value.length() > 0)
{
value = value.substring(0, value.length() - 1);
}
}
}
public void mouseClicked (int x, int y)
{
if (canFocus)
{
if (x > this.x && y > this.y && x < this.x + width && y < this.y + height)
{
hasFocus = true;
}
else
{
hasFocus = false;
}
}
else
{
hasFocus = false;
}
}
public int value ()
{
return value(value);
}
public int value (String value)
{
try
{
return Integer.parseInt(value);
}
catch (Exception e)
{
}
return 0;
}
}