This repository has been archived by the owner on Oct 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLanternaGUI.java
147 lines (125 loc) · 5.82 KB
/
LanternaGUI.java
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
package ldts.dino.gui;
import com.googlecode.lanterna.TerminalPosition;
import com.googlecode.lanterna.TerminalSize;
import com.googlecode.lanterna.TextColor;
import com.googlecode.lanterna.graphics.TextGraphics;
import com.googlecode.lanterna.input.KeyStroke;
import com.googlecode.lanterna.input.KeyType;
import com.googlecode.lanterna.screen.Screen;
import com.googlecode.lanterna.screen.TerminalScreen;
import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
import com.googlecode.lanterna.terminal.Terminal;
import com.googlecode.lanterna.terminal.swing.AWTTerminalFontConfiguration;
import ldts.dino.utils.Position;
import ldts.dino.utils.SoundManager;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
public class LanternaGUI implements GUI {
private final Screen screen;
public static final int WIDTH = 300, HEIGHT = 180, FONT_SIZE = 3;
private SpriteBuilder spb;
public LanternaGUI() throws IOException, URISyntaxException, FontFormatException {
AWTTerminalFontConfiguration fontConfig = loadSquareFont();
Terminal terminal = createTerminal(fontConfig);
this.screen = createScreen(terminal);
this.spb = new SpriteBuilder();
}
public LanternaGUI(Screen screen){
this.screen = screen;
}
private Screen createScreen(Terminal terminal) throws IOException {
final Screen screen;
screen = new TerminalScreen(terminal);
screen.setCursorPosition(null);
screen.startScreen();
screen.doResizeIfNecessary();
return screen;
}
private Terminal createTerminal(AWTTerminalFontConfiguration fontConfig) throws IOException {
TerminalSize terminalSize = new TerminalSize(WIDTH, HEIGHT + 1);
DefaultTerminalFactory terminalFactory = new DefaultTerminalFactory().setInitialTerminalSize(terminalSize);
terminalFactory.setForceAWTOverSwing(true);
terminalFactory.setTerminalEmulatorFontConfiguration(fontConfig);
return terminalFactory.createTerminal();
}
@Override
public ACTION getNextAction() throws IOException {
KeyStroke keyStroke = screen.pollInput();
if (keyStroke == null) return ACTION.NONE;
if (keyStroke.getKeyType() == KeyType.EOF) return ACTION.QUIT;
if (keyStroke.getKeyType() == KeyType.Character && keyStroke.getCharacter() == 'q' ||
keyStroke.getKeyType() == KeyType.Character && keyStroke.getCharacter() == 'Q') return ACTION.QUIT;
if (keyStroke.getKeyType() == KeyType.Character && keyStroke.getCharacter() == ' ') return ACTION.JUMP;
if (keyStroke.getKeyType() == KeyType.ArrowDown || (keyStroke.getKeyType() == KeyType.Character && keyStroke.getCharacter() == 'q' ||
keyStroke.getKeyType() == KeyType.Character && keyStroke.getCharacter() == 'Q')) return ACTION.DOWN;
if (keyStroke.getKeyType() == KeyType.ArrowUp) return ACTION.UP;
if (keyStroke.getKeyType() == KeyType.Enter) return ACTION.SELECT;
if (keyStroke.getKeyType() == KeyType.ArrowRight) return ACTION.BOMB;
if (keyStroke.getKeyType() == KeyType.Escape) return ACTION.BACK;
return ACTION.NONE;
}
@Override
public void clear() {
screen.clear();
}
@Override
public void refresh() throws IOException {
screen.refresh();
}
public void close() throws IOException {
screen.close();
}
@Override
public void drawImageFromFile(Position pos, String filename) {
BufferedImage image = spb.loadImage(filename);
drawImage(pos, image);
}
@Override
public void drawImage(Position pos, BufferedImage image) {
String pink = "#000000";
for (int x = 0; x < image.getWidth(); x++) {
for(int y = 0; y < image.getHeight(); y++) {
Color c = new Color(image.getRGB(x, y));
String color = "#" + Integer.toHexString(c.getRGB()).substring(2);
if (!color.equals(pink))
drawPixel(pos.offset(x, y), color);
}
}
}
@Override
public void drawPixel(Position pos, String color) {
TextGraphics tg = screen.newTextGraphics();
tg.setForegroundColor(TextColor.Factory.fromString(color));
tg.setBackgroundColor(TextColor.Factory.fromString(color));
tg.putString(pos.getX(), pos.getY(), " ");
}
public void paintBackground(String color) {
TextGraphics tg = screen.newTextGraphics();
tg.setBackgroundColor(TextColor.Factory.fromString(color));
tg.fillRectangle(new TerminalPosition(0, 0), new TerminalSize(WIDTH, HEIGHT + 1), ' ');
}
@Override
public void drawText(Position position, String text, int size, String color) {
BufferedImage textSprite = spb.loadText(new SpriteBuilder.TextDetails(text, size, color));
drawImage(position, textSprite);
}
@Override
public void drawArea(Position position, int width, int height, String color) {
TextGraphics tg = screen.newTextGraphics();
tg.setBackgroundColor(TextColor.Factory.fromString(color));
tg.fillRectangle(new TerminalPosition(position.getX(), position.getY()), new TerminalSize(width, height), ' ');
}
private AWTTerminalFontConfiguration loadSquareFont() throws URISyntaxException, FontFormatException, IOException {
URL resource = getClass().getClassLoader().getResource("fonts/square.ttf");
File fontFile = new File(resource.toURI());
Font font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);
Font loadedFont = font.deriveFont(Font.PLAIN, FONT_SIZE);
return AWTTerminalFontConfiguration.newInstance(loadedFont);
}
}