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