1 | package gamegui;
|
---|
2 |
|
---|
3 | import java.awt.*;
|
---|
4 | import java.awt.image.*;
|
---|
5 | import java.awt.event.*;
|
---|
6 |
|
---|
7 | import utils.*;
|
---|
8 |
|
---|
9 | public class Textbox extends Member
|
---|
10 | {
|
---|
11 | private String label;
|
---|
12 | private String text;
|
---|
13 | private Font font;
|
---|
14 | private int textStart;
|
---|
15 | private boolean selected;
|
---|
16 | private int cursorState;
|
---|
17 | private int blinkInterval;
|
---|
18 | private long lastCursorChange;
|
---|
19 | private boolean password;
|
---|
20 | protected boolean noBorder;
|
---|
21 | protected BufferedImage source;
|
---|
22 | protected Graphics2D srcGraphics;
|
---|
23 |
|
---|
24 | public Textbox(String newName, int newX, int newY, int newWidth, int newHeight, String newLabel, Font newFont, boolean isPass) {
|
---|
25 | super(newName, newX, newY, newWidth, newHeight);
|
---|
26 |
|
---|
27 | label = new String(newLabel);
|
---|
28 | text = new String();
|
---|
29 | font = newFont;
|
---|
30 | textStart = 0;
|
---|
31 | selected = false;
|
---|
32 | cursorState = 0;
|
---|
33 | blinkInterval = 1000/2;
|
---|
34 | password = isPass;
|
---|
35 | noBorder = false;
|
---|
36 |
|
---|
37 | source = Utils.gc.createCompatibleImage(getWidth(), getHeight());
|
---|
38 | srcGraphics = source.createGraphics();
|
---|
39 | }
|
---|
40 |
|
---|
41 | public void setBorder(boolean b) {
|
---|
42 | noBorder = !b;
|
---|
43 | }
|
---|
44 |
|
---|
45 | public void handleEvent(KeyEvent e) {
|
---|
46 | if(32 <= e.getKeyChar() && e.getKeyChar() <= 126)
|
---|
47 | text = text + Character.toString(e.getKeyChar());
|
---|
48 | else if(e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
|
---|
49 | if(text.length() > 0)
|
---|
50 | text = text.substring(0, text.length() - 1);
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | public void draw(Graphics g) {
|
---|
55 | String drawnString = new String();
|
---|
56 | FontMetrics metrics = g.getFontMetrics(font);
|
---|
57 |
|
---|
58 | if(selected && System.currentTimeMillis() - lastCursorChange > blinkInterval) {
|
---|
59 | if(cursorState == 0)
|
---|
60 | cursorState = 1;
|
---|
61 | else
|
---|
62 | cursorState = 0;
|
---|
63 |
|
---|
64 | lastCursorChange = System.currentTimeMillis();
|
---|
65 | }
|
---|
66 |
|
---|
67 | if(password) {
|
---|
68 | for(int x=0;x<text.length();x++)
|
---|
69 | drawnString+="*";
|
---|
70 | }else
|
---|
71 | drawnString = text;
|
---|
72 |
|
---|
73 | if(metrics.stringWidth(drawnString) + 9 > getWidth())
|
---|
74 | textStart = metrics.stringWidth(drawnString)+9-getWidth();
|
---|
75 | else
|
---|
76 | textStart = 0;
|
---|
77 |
|
---|
78 | srcGraphics.setColor(Color.black);
|
---|
79 | srcGraphics.fillRect(0, 0, source.getWidth(), source.getHeight());
|
---|
80 |
|
---|
81 | g.setColor(Color.white);
|
---|
82 | g.setFont(font);
|
---|
83 | srcGraphics.setColor(Color.white);
|
---|
84 | srcGraphics.setFont(font);
|
---|
85 |
|
---|
86 | srcGraphics.drawString(drawnString, 5-textStart, (getHeight() + metrics.getHeight())/2 - 2);
|
---|
87 |
|
---|
88 | g.drawImage(source, getX(), getY(), null);
|
---|
89 | g.drawString(label, getX() - metrics.stringWidth(label) - 10, getY() + (getHeight() + metrics.getHeight())/2 - 2);
|
---|
90 |
|
---|
91 | if(selected && cursorState == 1)
|
---|
92 | g.drawLine(getX() + metrics.stringWidth(drawnString) - textStart + 6, getY() + 5, getX() + metrics.stringWidth(drawnString) - textStart + 6, getY() + getHeight() - 5);
|
---|
93 |
|
---|
94 | g.setColor(new Color(106, 106, 106));
|
---|
95 | if(!noBorder)
|
---|
96 | g.drawRect(getX(), getY(), getWidth(), getHeight());
|
---|
97 | }
|
---|
98 |
|
---|
99 | public boolean isClicked(int xCoord, int yCoord) {
|
---|
100 | if(xCoord < getX() || getX() + getWidth() < xCoord)
|
---|
101 | return false;
|
---|
102 | if(yCoord < getY() || getY() + getHeight() < yCoord)
|
---|
103 | return false;
|
---|
104 |
|
---|
105 | return true;
|
---|
106 | }
|
---|
107 |
|
---|
108 | public void clear() {
|
---|
109 | text = "";
|
---|
110 | textStart = 0;
|
---|
111 | selected = false;
|
---|
112 | }
|
---|
113 |
|
---|
114 | public int getBlinkInterval() {
|
---|
115 | return blinkInterval;
|
---|
116 | }
|
---|
117 |
|
---|
118 | public int getCursorState() {
|
---|
119 | return cursorState;
|
---|
120 | }
|
---|
121 |
|
---|
122 | public Font getFont() {
|
---|
123 | return font;
|
---|
124 | }
|
---|
125 |
|
---|
126 | public String getLabel() {
|
---|
127 | return label;
|
---|
128 | }
|
---|
129 |
|
---|
130 | public long getLastCursorChange() {
|
---|
131 | return lastCursorChange;
|
---|
132 | }
|
---|
133 |
|
---|
134 | public boolean isSelected() {
|
---|
135 | return selected;
|
---|
136 | }
|
---|
137 |
|
---|
138 | public String getText() {
|
---|
139 | return text;
|
---|
140 | }
|
---|
141 |
|
---|
142 | public int getTextStart() {
|
---|
143 | return textStart;
|
---|
144 | }
|
---|
145 |
|
---|
146 | public void setText(String newText) {
|
---|
147 | text = newText;
|
---|
148 | }
|
---|
149 |
|
---|
150 | public void setSelected(boolean isSelected) {
|
---|
151 | selected = isSelected;
|
---|
152 | }
|
---|
153 |
|
---|
154 | public void setTextStart(int textStart) {
|
---|
155 | this.textStart = textStart;
|
---|
156 | }
|
---|
157 |
|
---|
158 | public void setCursorState(int cursorState) {
|
---|
159 | this.cursorState = cursorState;
|
---|
160 | }
|
---|
161 |
|
---|
162 | public void setLastCursorChange(long lastCursorChange) {
|
---|
163 | this.lastCursorChange = lastCursorChange;
|
---|
164 | }
|
---|
165 | }
|
---|