source: lost-perception/gamegui/MultiTextbox.java@ ebd3538

Last change on this file since ebd3538 was ebd3538, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 6 years ago

Initial commit. This codebase for the Lost Perception game was created by decompiling code from a jar file.

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