-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtils.pde
261 lines (226 loc) · 6.29 KB
/
Utils.pde
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
import javax.swing.JOptionPane;
HashMap<String,PImage> images = new HashMap<String,PImage>();
public void addImageAbsolute(String name, String path) {
images.put(name,loadImage(path));
}
public void addImage(String name, String path) {
String extension = "";
int i = path.lastIndexOf('.');
if (i <= 0) {
path = path+".png";
}
addImageAbsolute(name, "./"+path);
}
public void addImage(String name, String path, String fileType) {
addImage(name, path+"."+fileType);
}
public PImage getImage(String img) {
return images.get(img);
}
public static String combine (String path1, String path2) {
File file1 = new File(path1);
File file2 = new File(file1, path2);
return file2.getPath();
}
public void loadAllImages(String dir) {
File folder = new File(combine(dataPath(""),dir));
File[] files = folder.listFiles();
if (files!=null) {
for (File fileEntry : files) {
if (!fileEntry.isDirectory()) {
String fileName = fileEntry.getName();
if (fileName.toLowerCase().endsWith(".png") || fileName.toLowerCase().endsWith(".jpg")) {
String name = fileName.substring(0,fileName.lastIndexOf("."));
addImageAbsolute(name,fileEntry.getPath());
}
}
}
}
}
public PImage cropImage(String name, int x, int y, int w, int h) {
PImage img = getImage(name);
PGraphics g = createGraphics(w,h);
g.beginDraw();
g.image(img,-x,-y);
g.endDraw();
return g.get(0,0,w,h);
}
public void drawImage(PImage img, int x, int y, float scale, float angle) {
PVector size = imgToWorldCoords(img);
pushMatrix();
translate(toWorldX(x),toWorldY(y));
translate(size.x/2,size.y/2);
rotate(angle);
scale(scale);
translate(-size.x/2,-size.y/2);
image(img,0,0,size.x,size.y);
popMatrix();
}
public void drawImage(PImage img, int x, int y) {
drawImage(img,x,y,1,0);
}
public void drawImageCenter(PImage img, int x, int y) {
drawImage(img,int(x-img.width/2f),int(y-img.height/2f));
}
public void drawImage(String img, int x, int y, float angle) {
drawImage(getImage(img),x,y,1,angle);
}
public void drawImage(String img, int x, int y) {
drawImage(img,x,y,0);
}
public void drawImageCenter(String img, int x, int y) {
PImage actualImg = getImage(img);
drawImage(img,int(x-actualImg.width/2f),int(y-actualImg.height/2f));
}
public void drawImage(String img, int x, int y, float scale, float angle) {
drawImage(getImage(img),x,y,scale,angle);
}
class StackImg{
String img;
int x;
int y;
float delay;
float timer;
StackImg(String img, int x, int y, float delay) {
this.img = img;
this.x = x;
this.y = y;
this.timer = 0;
this.delay = delay;
}
}
ArrayList<StackImg> imageStack = new ArrayList<StackImg>();
public void drawImageDelay(String img, int x, int y, float delay) {
imageStack.add(new StackImg(img,x,y,delay));
}
public void drawImageStack(float time) {
ArrayList<StackImg> tmpImageStack = new ArrayList<StackImg>(imageStack);
for (StackImg s : tmpImageStack) {
if (s.timer<s.delay) {
drawImage(s.img,s.x,s.y);
s.timer+=time;
}
else {
imageStack.remove(s);
}
}
}
public PVector imgToWorldCoords(PImage img) {
return toWorldCoords(new PVector(img.width,img.height));
}
public PVector toWorldCoords(PVector coord) {
PVector vec = new PVector();
vec.x = toWorldX(int(coord.x));
vec.y = toWorldY(int(coord.y));
return vec;
}
public float toWorldX(int x) {
return (float(x)/float(gameWidth))*windowWidth;
}
public float toWorldY(int y) {
return (float(y)/float(gameHeight))*windowHeight;
}
public PVector toGameCoords(PVector coord) {
PVector vec = new PVector();
vec.x = toGameX(int(coord.x));
vec.y = toGameY(int(coord.y));
return vec;
}
public float toGameX(int x) {
return (float(x)/float(windowWidth))*gameWidth;
}
public float toGameY(int y) {
return (float(y)/float(windowHeight))*gameHeight;
}
HashMap<String, Boolean> keysDown = new HashMap<String, Boolean>();
HashMap<String, Boolean> wereKeysDown = new HashMap<String, Boolean>();
public void setMove(String k, boolean c, boolean b) {
if (c) {
switch (int(k)) {
case UP:
k = "up";
break;
case DOWN:
k = "down";
break;
case LEFT:
k = "left";
break;
case RIGHT:
k = "right";
break;
case ESC:
k = "esc";
break;
case CONTROL:
k = "control";
break;
}
}
keysDown.put(k, b);
}
public boolean isKeyDown(String g) {
boolean r = false;
if (keysDown.containsKey(g)) {
if (keysDown.get(g)) {
r = true;
}
} else {
r = false;
}
return r;
}
public boolean wasKeyDown(String g) {
boolean r = false;
if (wereKeysDown.containsKey(g)) {
if (wereKeysDown.get(g)) {
r = true;
}
} else {
r = false;
}
return r;
}
class GraphicRegion {
int margin;
int x;
int y;
int width;
int height;
int innerX;
int innerY;
int innerWidth;
int innerHeight;
GraphicRegion(int x, int y, int width, int height, int margin) {
this.x = x;
this.y = y;
this.margin = margin;
this.innerX = this.x + this.margin;
this.innerY = this.y + this.margin;
this.innerWidth = width;
this.innerHeight = height;
this.width = this.innerWidth + (this.margin * 2);
this.height = this.innerHeight + (this.margin * 2);
}
GraphicRegion(int x, int y, int width, int height) {
this(x, y, width, height, 0);
}
}
public int showConfirmDialog(String message, String title) {
return JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
}
public void showMessage(String message, String title, int messageType) {
JOptionPane.showMessageDialog(null, message, title, messageType);
}
public void showErrorMessage(String message, String title) {
showMessage(message, title, JOptionPane.ERROR_MESSAGE);
}
public void showErrorMessage(String message) {
showErrorMessage(message, "Error");
}
public void showInfoMessage(String message, String title) {
showMessage(message, title, JOptionPane.INFORMATION_MESSAGE);
}
public void showInfoMessage(String message) {
showInfoMessage(message, "Info");
}