1 | package gamegui;
|
---|
2 |
|
---|
3 | import java.awt.*;
|
---|
4 | import java.awt.event.*;
|
---|
5 | import java.awt.image.BufferedImage;
|
---|
6 | import java.util.ArrayList;
|
---|
7 |
|
---|
8 | public class MultiTextbox extends Textbox {
|
---|
9 |
|
---|
10 | ArrayList<String> lstStrings;
|
---|
11 | FontMetrics metrics;
|
---|
12 | boolean active;
|
---|
13 |
|
---|
14 | public MultiTextbox(String newName, int newX, int newY, int newWidth, int newHeight, String newLabel, boolean isActive, Font newFont, FontMetrics newMetrics) {
|
---|
15 | super(newName, newX, newY, newWidth, newHeight, newLabel, newFont, false);
|
---|
16 | this.lstStrings = new ArrayList<String>();
|
---|
17 | this.metrics = newMetrics;
|
---|
18 | this.active = isActive;
|
---|
19 | splitString();
|
---|
20 | }
|
---|
21 |
|
---|
22 | public void append(String str) {
|
---|
23 | if (getText().equals("")) {
|
---|
24 | setText(str);
|
---|
25 | } else {
|
---|
26 | setText(String.valueOf(getText()) + "\n" + str);
|
---|
27 | }
|
---|
28 | splitString();
|
---|
29 | if (this.lstStrings.size() * 15 + 6 > getHeight()) {
|
---|
30 | getScrollBar().setSize(getScrollBar().getMaxSize() * getHeight() / (this.lstStrings.size() * 15 + 6));
|
---|
31 | } else {
|
---|
32 | getScrollBar().setSize(getScrollBar().getMaxSize());
|
---|
33 | }
|
---|
34 | getScrollBar().setPosition(getScrollBar().getMaxSize() - getScrollBar().getSize());
|
---|
35 | }
|
---|
36 |
|
---|
37 | public void setText(String s) {
|
---|
38 | super.setText(s);
|
---|
39 | splitString();
|
---|
40 | }
|
---|
41 |
|
---|
42 | public void clear() {
|
---|
43 | super.clear();
|
---|
44 | this.lstStrings = new ArrayList<String>();
|
---|
45 | }
|
---|
46 |
|
---|
47 | public boolean handleEvent(MouseEvent e) {
|
---|
48 | if (!getScrollBar().handleEvent(e)) {
|
---|
49 | return false;
|
---|
50 | }
|
---|
51 | if (e.getY() < getY() + getWidth()) {
|
---|
52 | changeTextStart(-30);
|
---|
53 | } else if (getY() + getHeight() - getWidth() < e.getY()) {
|
---|
54 | changeTextStart(30);
|
---|
55 | }
|
---|
56 | return true;
|
---|
57 | }
|
---|
58 |
|
---|
59 | public void handleEvent(KeyEvent e) {
|
---|
60 | if (!this.active) {
|
---|
61 | return;
|
---|
62 | }
|
---|
63 | super.handleEvent(e);
|
---|
64 | splitString();
|
---|
65 | if (this.lstStrings.size() * 15 + 6 > getHeight()) {
|
---|
66 | getScrollBar().setSize(getScrollBar().getMaxSize() * getHeight() / (this.lstStrings.size() * 15 + 6));
|
---|
67 | } else {
|
---|
68 | getScrollBar().setSize(getScrollBar().getMaxSize());
|
---|
69 | }
|
---|
70 | getScrollBar().setPosition(getScrollBar().getMaxSize() - getScrollBar().getSize());
|
---|
71 | }
|
---|
72 |
|
---|
73 | private void changeTextStart(int increment) {
|
---|
74 | setTextStart(getTextStart() + increment);
|
---|
75 | if (this.lstStrings.size() * 15 + 6 > getHeight() && getTextStart() >= this.lstStrings.size() * 15 + 6 - getHeight()) {
|
---|
76 | setTextStart(this.lstStrings.size() * 15 + 6 - getHeight());
|
---|
77 | getScrollBar().setPosition(getScrollBar().getMaxSize() - getScrollBar().getSize());
|
---|
78 | } else if (getTextStart() < 0 || this.lstStrings.size() * 15 + 6 <= getHeight()) {
|
---|
79 | setTextStart(0);
|
---|
80 | getScrollBar().setPosition(0);
|
---|
81 | } else {
|
---|
82 | getScrollBar().setPosition(getTextStart() * getScrollBar().getMaxSize() / (this.lstStrings.size() * 15 + 6));
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | private void splitString() {
|
---|
87 | String drawnString = getText();
|
---|
88 | ArrayList<String> lstTemp = new ArrayList<String>();
|
---|
89 | do {
|
---|
90 | int x = 0, lastSpace = -1;
|
---|
91 | while (x < drawnString.length() && this.metrics.stringWidth(drawnString.substring(0, x + 1)) <= getWidth() - 10 && !drawnString.substring(x, x + 1).equals("\n")) {
|
---|
92 | if (drawnString.charAt(x) == ' ') {
|
---|
93 | lastSpace = x;
|
---|
94 | }
|
---|
95 | x++;
|
---|
96 | }
|
---|
97 | int xReal = x;
|
---|
98 | if (lastSpace > 0 && drawnString.length() > x) {
|
---|
99 | x = lastSpace + 1;
|
---|
100 | }
|
---|
101 | if (drawnString.length() > xReal && drawnString.substring(xReal, xReal + 1).equals("\n")) {
|
---|
102 | lstTemp.add(drawnString.substring(0, xReal));
|
---|
103 | drawnString = drawnString.substring(xReal + 1);
|
---|
104 | } else {
|
---|
105 | lstTemp.add(drawnString.substring(0, x));
|
---|
106 | drawnString = drawnString.substring(x);
|
---|
107 | }
|
---|
108 | } while (this.metrics.stringWidth(drawnString) > 0);
|
---|
109 | if (lstTemp.size() * 15 - getHeight() + 6 > 0) {
|
---|
110 | setTextStart(lstTemp.size() * 15 - getHeight() + 6);
|
---|
111 | } else {
|
---|
112 | setTextStart(0);
|
---|
113 | }
|
---|
114 | this.lstStrings = lstTemp;
|
---|
115 | }
|
---|
116 |
|
---|
117 | public void draw(Graphics g) {
|
---|
118 | GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
---|
119 | GraphicsDevice device = env.getDefaultScreenDevice();
|
---|
120 | GraphicsConfiguration gc = device.getDefaultConfiguration();
|
---|
121 | BufferedImage source = gc.createCompatibleImage(getWidth(), getHeight());
|
---|
122 | Graphics2D srcGraphics = source.createGraphics();
|
---|
123 | if (isSelected() && System.currentTimeMillis() - getLastCursorChange() > getBlinkInterval()) {
|
---|
124 | if (getCursorState() == 0) {
|
---|
125 | setCursorState(1);
|
---|
126 | } else {
|
---|
127 | setCursorState(0);
|
---|
128 | }
|
---|
129 | setLastCursorChange(System.currentTimeMillis());
|
---|
130 | }
|
---|
131 | srcGraphics.setColor(Color.green);
|
---|
132 | srcGraphics.setFont(getFont());
|
---|
133 | int x;
|
---|
134 | for (x = 0; x < this.lstStrings.size(); x++) {
|
---|
135 | srcGraphics.drawString(this.lstStrings.get(x), 5, this.metrics.getHeight() + x * 15 - getTextStart());
|
---|
136 | }
|
---|
137 | x--;
|
---|
138 | if (isSelected() && getCursorState() == 1) {
|
---|
139 | srcGraphics.drawLine(this.metrics.stringWidth(this.lstStrings.get(x)) + 6, 5 + x * 15 - getTextStart(), this.metrics.stringWidth(this.lstStrings.get(x)) + 6, this.metrics.getHeight() + x * 15 - getTextStart());
|
---|
140 | }
|
---|
141 | g.setColor(Color.green);
|
---|
142 | g.setFont(getFont());
|
---|
143 | g.drawImage(source, getX(), getY(), null);
|
---|
144 | g.drawString(getLabel(), getX() - this.metrics.stringWidth(getLabel()) - 10, getY() + (getHeight() + this.metrics.getHeight()) / 2 - 2);
|
---|
145 | g.setColor(Color.red);
|
---|
146 | if (!this.noBorder) {
|
---|
147 | g.drawRect(getX(), getY(), getWidth(), getHeight());
|
---|
148 | }
|
---|
149 | if (getScrollBar() != null) {
|
---|
150 | getScrollBar().draw(g);
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|