source: lost-haven/gamegui/Textbox.java

Last change on this file was 8edd04e, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 4 years ago

Make the decompiled game code compile successfully

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