/
SnippetSave.java
119 lines (104 loc) · 3.76 KB
/
SnippetSave.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
package computerblocks.display.ui.menu.elements;
import java.util.ArrayList;
import computerblocks.display.*;
import computerblocks.display.ui.menu.*;
import computerblocks.display.ui.menu.elements.*;
import computerblocks.player.*;
import computerblocks.player.io.*;
import computerblocks.Grid;
import computerblocks.snippet.*;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import java.io.File;
import java.awt.image.*;
import java.awt.Image;
import java.io.IOException;
public class SnippetSave {
public double x, y, width, height;
private Image icon;
private String font = Fonts.pixelmix;
private int textSize = 20;
private String text = "New Snippet";
public SnippetSave(double x, double y, double width, double height) {
this.x = x;
this.y = y;
this.height = height;
this.width = width;
loadImage("icon.png");
}
public void loadImage(String header) {
try {
icon = ImageIO.read(new File("../assets/" + header));
} catch (IOException err) {
err.printStackTrace();
}
}
public void draw(Display display) {
display.color(Color.UI_BORDER);
display.rect(x, y + 4, width, height);
display.color(Color.CABLE);
display.rect(x, y, width, height);
display.image(icon, x, y, width, height);
}
public boolean pointOver(double x, double y) {
if (x > this.x && x < this.x + width &&
y > this.y && y < this.y + height) {
return true;
}
return false;
}
public void checkPress(Display display, Player player, SnippetTray snippetTray) {
if (pointOver(player.mouse.position.x, player.mouse.position.y)) {
if (player.mouse.down(Mouse.LEFT)) {
buttonPress(display, player, snippetTray);
}
display.color(new Color(255, 255, 255, 0.2f));
display.rect(x, y, width, height);
}
}
public void buttonPress(Display display, Player player, SnippetTray snippetTray) {
String saveName = "";
while (true) {
saveName = player.keyboard.keyStream(saveName, 20);
if (player.keyboard.down(Keyboard.ESC)) break;
display.reset(Color.BACKGROUND);
display.font(font, textSize);
if (!saveName.equals("")) {
if (snippetAlreadyExists(saveName, snippetTray)) {
display.color(Color.INVERTER);
String warningText = "WARINING! - THIS WILL REPLACE PREVIOUS SNIPPET";
display.text(warningText, (display.width / 2) - display.getStringWidth(warningText, font, textSize) / 2, (display.height / 2) + display.getFontHeight(font, textSize) / 2 + display.height / 8);
}
display.color(Color.UI_BORDER);
display.text(saveName, (display.width / 2) - display.getStringWidth(saveName, font, textSize) / 2, (display.height / 2) + display.getFontHeight(font, textSize) / 2);
} else {
display.color(new Color(Color.UI_BORDER, 0.25f));
display.text(text, (display.width / 2) - display.getStringWidth(text, font, textSize) / 2, (display.height / 2) + display.getFontHeight(font, textSize) / 2);
}
display.draw();
if (player.keyboard.enterDown) {
if (!saveName.equals("")) {
player.snippet.saveToFile("../saves/snippets/", saveName);
snippetTray.refreshSaveNames = true;
player.state = State.GAME;
player.placeTime = 0;
break;
} else {
player.snippet.saveToFile("../saves/snippets/", text);
snippetTray.refreshSaveNames = true;
player.state = State.GAME;
player.placeTime = 0;
break;
}
}
}
}
public boolean snippetAlreadyExists(String saveName, SnippetTray snippetTray) {
for (SnippetButton i : snippetTray.snippets) {
if (i.text.toLowerCase().equals(saveName.toLowerCase())) {
return true;
}
}
return false;
}
}