-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphicsTestApp.java
More file actions
315 lines (282 loc) · 15.9 KB
/
GraphicsTestApp.java
File metadata and controls
315 lines (282 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import ANSI_Escape_Code_Creator.ANSI_Escape_Code_Creator;
import static ANSI_Escape_Code_Creator.ANSI_Escape_Code_Creator_Static.*;
/**
* Testing app for graphical features
* @author erich (sudo-Eric)
*/
public class GraphicsTestApp {
static int SCREEN_WIDTH = 80;
static int SCREEN_HEIGHT = 25;
static String MAIN_DISPLAY = new ANSI_Escape_Code_Creator(){{
cursor_invisible();
erase_display().cursor_position(1,1).SGR_clear().SGR_bold().append("╔══════╤══════╤══════╤");
for (int i = 0; i < SCREEN_WIDTH-23; i++)
append('═');
append("╗\n").append('║').SGR_set_foreground_color("Red").append("HP:")
.SGR_default_foreground_color().cursor_forward(3)
.append('│').SGR_set_foreground_color("Blue").append("MP:")
.SGR_default_foreground_color().cursor_forward(3)
.append('│').SGR_set_foreground_color("Green").append("ST:")
.SGR_default_foreground_color().cursor_forward(3)
.append('│').cursor_forward(SCREEN_WIDTH-23).append("║\n")
.append("╟──────┴──────┴──────┤").cursor_forward(SCREEN_WIDTH-23).append("║\n");
for (int i = 0; i < SCREEN_HEIGHT - 8; i++)
append('║').cursor_forward(20).append('│').cursor_forward(SCREEN_WIDTH-23).append("║\n");
append('║').cursor_forward(20).append("├");
for (int i = 0; i < SCREEN_WIDTH-23; i++)
append('─');
append("╢\n");
for (int i = 0; i < 3; i++)
append('║').cursor_forward(20).append('│').cursor_forward(SCREEN_WIDTH-23).append("║\n");
append("╚════════════════════╧");
for (int i = 0; i < SCREEN_WIDTH-23; i++)
append('═');
append("╝\n").SGR_clear();
}}.toString();
static String INVENTORY_DISPLAY = new ANSI_Escape_Code_Creator(){{
int col = 5;
int row = 3;
cursor_invisible().cursor_position(++row,col).SGR_clear().SGR_bold()
.append("╔════════════════════════════════════════════════════════╤═════════════╗").cursor_position(++row,col)
.append("║ Inventory │").SGR_set_foreground_color("Yellow")
.append("Gold:").SGR_default_foreground_color().append(" ║").cursor_position(++row,col)
.append("╟──────────────────┬──────────────────┬──────────────────┤").SGR_set_foreground_color("Yellow").append('$')
.SGR_default_foreground_color().append(" ║").cursor_position(++row,col)
.append("║ │ │ ├─────────────╢").cursor_position(++row,col)
.append("║ │ │ │Description: ║").cursor_position(++row,col)
.append("║ │ │ │The health ║").cursor_position(++row,col)
.append("║ │ │ │potion will ║").cursor_position(++row,col)
.append("║ │ │ │heal the ║").cursor_position(++row,col)
.append("║ │ │ │player 10HP. ║").cursor_position(++row,col)
.append("║ │ │ │ ║").cursor_position(++row,col)
.append("║ │ │ │ ║").cursor_position(++row,col)
.append("║ │ │ │ ║").cursor_position(++row,col)
.append("║ │ │ │ ║").cursor_position(++row,col)
.append("║ │ │ │ ║").cursor_position(++row,col)
.append("║ │ │ │ ║").cursor_position(++row,col)
.append("║ │ │ │ ║").cursor_position(++row,col)
.append("║ │ │ ├─────────────╢").cursor_position(++row,col)
.append("║ │ │ │Page: ║").cursor_position(++row,col)
.append("╚══════════════════╧══════════════════╧══════════════════╧═════════════╝");
}}.toString();
public static void main(String[] args) throws InterruptedException {
try {
erase_display();
erase_display(3);
System.out.print(MAIN_DISPLAY);
cursor_visible();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
renderHealth(0);
renderMana(0);
renderStamina(0);
renderDescriptionText("This is an example of some text being displayed on the screen. " +
"This text is a single string in the program, but has been split into " +
"multiple lines to be able to fit onto the screen.\n\n" +
"The text renderer will also interoperate newline characters as a command " +
"to continue rendering on the next line.");
renderMenuList(new String[]{
"Attack goblin", "Flee goblin", "Inventory", "Map", "Spell book", "Menu",
"Test of a menu item that wraps around to the next line",
"Test of the line after"});
// for (int i = 0; i < 101; i++) {
// renderHealth(i);
// renderMana(i);
// renderStamina(i);
// Thread.sleep(50);
// }
renderDescriptionText("The text renderer will also clear the text rendering area when new text is " +
"rendered so that multiple messages do not overlap.\n\nThis should make it trivial to display " +
"new messages and description text to this area.");
renderMenuList(new String[]{
"Attack chest", "Inspect chest", "Open chest", "Inventory", "Map", "Spell book", "Menu",
"Test of a menu item that wraps around to the next line",
"Test of the line after"});
DummyItemClass[] inventory = {
new DummyItemClass("Health Potion", "The health potion will restore the player 10HP.", 1, 10),
new DummyItemClass("Mana Potion", "The mana potion will restore the player 10MP.", 1, 10),
new DummyItemClass("Stamina Potion", "The stamina potion will restore the player 10ST.", 1, 10),
new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(),
new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(),
new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(),
new DummyItemClass("Second Page Itm", "Item that begins the second page.", 1, 1),
new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(),
new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(),
new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(),
new DummyItemClass("Third Page Itm", "Item that begins the third page/", 1, 1),
new DummyItemClass(), new DummyItemClass(), new DummyItemClass()};
renderInventory(inventory, 0);
// for (int i = 0; i < inventory.length; i++) {
// renderInventory(inventory, i);
// Thread.sleep(200);
// }
cursor_position(SCREEN_HEIGHT + 1, 1);
} catch (Exception e) {
cursor_position(SCREEN_HEIGHT + 2, 1);
throw e;
}
}
private static void renderDescriptionText(String text) {
renderTextBlock(2, 23, SCREEN_HEIGHT - 5, SCREEN_WIDTH-1, text);
}
private static void renderMenuList(String[] items) {
final int width = 17;
int height = SCREEN_HEIGHT - 4;
int line = 4;
ANSI_Escape_Code_Creator gen = new ANSI_Escape_Code_Creator();
for (int i = 0; i < items.length; i++) {
String item = items[i];
int lastChar = 0;
int lastSpace = 0;
final int numberLine = line;
for (int j = 0; j < item.length(); j++) {
renderText(gen, line, 2, 3, "");
if (j - lastChar >= width || item.charAt(j) == '\n') {
if (item.charAt(j) == '\n')
lastSpace = j;
renderText(gen, line, 5, width, item.substring(lastChar, lastSpace));
lastChar = lastSpace + 1;
line++;
} else if (item.charAt(j) == ' ') {
lastSpace = j;
}
}
if (item.length() != lastChar) {
renderText(gen, line, 2, 3, "");
renderText(gen, line, 5, width, item.substring(lastChar));
line++;
}
renderText(gen, numberLine, 2, 3, String.format("%2d.", i+1));
}
for (int i = line; i < height + 4; i++) {
renderText(gen, i, 2, width + 3, "");
}
System.out.print(gen);
}
private static void renderTextBlock(int startRow, int startCol, int endRow, int endCol, String text) {
int width = endCol - startCol + 1;
int lastChar = 0;
int lastSpace = 0;
int line = startRow;
ANSI_Escape_Code_Creator gen = new ANSI_Escape_Code_Creator();
for (int i = 0; i < text.length(); i++) {
if (i - lastChar >= width || text.charAt(i) == '\n') {
if (text.charAt(i) == '\n')
lastSpace = i;
renderText(gen, line, startCol, width, text.substring(lastChar, lastSpace));
lastChar = lastSpace + 1;
line++;
} else if (text.charAt(i) == ' ') {
lastSpace = i;
}
}
if (text.length() != lastChar) {
renderText(gen, line, startCol, width, text.substring(lastChar));
line++;
}
for (int i = line; i < endRow + 1; i++) {
renderText(gen, i, startCol, width, "");
}
System.out.print(gen);
}
private static void renderInventory(DummyItemClass[] items, int selected) {
System.out.print(INVENTORY_DISPLAY);
final long gold = 1234567890L; // Where does gold come from?
renderGold(gold);
ANSI_Escape_Code_Creator gen = new ANSI_Escape_Code_Creator();
int pages = ((items.length - 1) / 21) + 1;
int page = (selected / 21) + 1;
int offset = (selected / 21) * 21;
int length = items.length - offset;
int i = offset;
renderText(gen, 21, 68, 8, String.format("%8s", String.format("%d/%d", page, pages)));
if (items.length == 0) {
renderText(gen, 21, 68, 8, String.format("%8s", String.format("%d/%d", 0, 0)));
System.out.print(gen);
return;
}
for (int col = 0; col < ((length - 1) / 7) + 1 && col < 3; col++) {
for (int row = 0; row < ((length - 1) % 7) + 1 || ((length - (col * 7)) / 7 != 0 && row < 7); row++) {
DummyItemClass item = items[((col * 7) + row) + offset];
boolean selectedItem = ((col * 7) + row == selected - offset);
renderText(gen, 7 + (2 * row), 6 + (19 * col), 18, String.format("%2d.%s", ++i, item.name), selectedItem);
renderText(gen, 8 + (2 * row), 14 + (19 * col), 10, String.format("x%4d/%4d", item.count, item.maxCount));
if (selectedItem) {
renderTextBlock(8, 63, 19, 75, item.description);
}
}
}
System.out.print(gen);
}
private static void renderText(ANSI_Escape_Code_Creator gen, int row, int col, int width, String line) {
renderText(gen, row, col, width, line, "White");
}
private static void renderText(ANSI_Escape_Code_Creator gen, int row, int col, int width, String line, String color) {
renderText(gen, row, col, width, line, color, false);
}
private static void renderText(ANSI_Escape_Code_Creator gen, int row, int col, int width, String line, boolean inverted) {
renderText(gen, row, col, width, line, "White", inverted);
}
private static void renderText(ANSI_Escape_Code_Creator gen, int row, int col, int width, String line, String color, boolean inverted) {
gen.cursor_invisible().cursor_position(row, col).SGR_set_foreground_color(color);
if (inverted)
gen.SGR_invert();
gen.append(line);
if (inverted)
gen.SGR_not_reversed();
for (int j = line.length(); j < width; j++)
gen.append(' ');
}
private static void renderNumber(int row, int col, int width, long number, String color, boolean alignRight) {
ANSI_Escape_Code_Creator gen = new ANSI_Escape_Code_Creator()
.cursor_invisible().cursor_position(row,col);
String str = Long.toString(number);
SGR_set_foreground_color(color, _4BIT_COLOR);
if (alignRight)
for (int i = 0; i < width - str.length(); i++)
gen.append(' ');
gen.append(str);
if (!alignRight)
for (int i = 0; i < width - str.length(); i++)
gen.append(' ');
gen.SGR_clear();
System.out.print(gen);
}
private static void renderHealth(int health) {
renderNumber(2, 5, 3, health, "Red", true);
}
private static void renderMana(int mana) {
renderNumber(2, 12, 3, mana, "Blue", true);
}
private static void renderStamina(int stamina) {
renderNumber(2, 19, 3, stamina, "Green", true);
}
private static void renderGold(long gold) {
renderNumber(6, 64, 12, gold, "Yellow", true);
}
}
/**
* This is a placeholder class for the items class.
* This is onl for testing and should not be used in the main game.
*/
class DummyItemClass {
String name;
String description;
int count;
int maxCount;
DummyItemClass() {
this.name = "Generic Item";
this.description = "This is a generic item.";
this.count = 1;
this.maxCount = 1;
}
DummyItemClass(String name, String description, int count, int maxCount) {
this.name = name;
this.description = description;
this.count = count;
this.maxCount = maxCount;
}
@Override
public String toString() {
return this.name + "\t" + this.count + " / " + this.maxCount;
}
}