-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathUtils.pde
118 lines (101 loc) · 2.83 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
import java.awt.Graphics2D;
import java.awt.Shape;
import java.util.jar.*;
/* timing utilities */
float lastTime;
float stopWatch()
{
float x = lastTime;
lastTime = millis();
return lastTime - x;
}
/* graphics utilities */
Graphics2D g2;
Shape[] clipStack;
int clipIdx;
void setupG2D()
{
g2 = ((PGraphicsJava2D)g).g2;
clipStack = new Shape[32];
clipIdx = 0;
}
/* I can't believe this is not part of the Processing API! */
void clipRect(int x, int y, int w, int h)
{
g2.clipRect(x, y, w, h);
}
void clipRect(float x, float y, float w, float h)
{
g2.clipRect((int)x, (int)y, (int)w, (int)h);
}
void noClip()
{
g2.setClip(null);
}
void pushClip()
{
clipStack[clipIdx++] = g2.getClip();
}
void popClip()
{
g2.setClip(clipStack[--clipIdx]);
clipStack[clipIdx] = null;
}
/* about paths:
inside IDE: sketchPath: /Users/camillo/UIC/CS424 visualization/p3/cs424p3
dataPath(""): /Users/camillo/UIC/CS424 visualization/p3/cs424p3/data/
application: sketchPath: /Users/camillo/UIC/CS424 visualization/p3/cs424p3/application.macosx
dataPath(""): /Users/camillo/UIC/CS424 visualization/p3/cs424p3/application.macosx/data/
applet: sketchPath: null
dataPath(""): null
*/
/* data utilities */
String[] listDataSubdir(String subdir)
{
String[] items = null;
/* dataPath does not work for application, either. it's only useful in the IDE */
/* if (sketchPath != null) {
println(dataPath(subdir));
File dir = new File(dataPath(subdir));
items = dir.list();
println(items);
} else */
ClassLoader cl = getClass().getClassLoader();
URL url = cl.getResource("META-INF/MANIFEST.MF"); /* just a random file that's known to exist */
if (url == null) { /* running in IDE */
File dir = new File(dataPath(subdir));
items = dir.list();
} else try { /* applet OR application */
JarURLConnection conn = (JarURLConnection)url.openConnection();
JarFile jar = conn.getJarFile();
Enumeration e = jar.entries();
String re = "data/" + subdir + "/(.*)";
/* note that jars don't have directory entries, or at least Processing's don't */
Set<String> itemSet = new LinkedHashSet<String>();
while (e.hasMoreElements()) {
JarEntry entry = (JarEntry)e.nextElement();
String[] groups = match(entry.getName(), re);
if (groups == null) continue;
String[] comps = split(groups[1], "/");
itemSet.add(comps[0]);
}
/*Iterator it = itemSet.iterator();
while (it.hasNext()) {
println(it.next());
}*/
items = (String[])itemSet.toArray(new String[0]);
} catch (IOException e) {
println(e);
}
return items;
}
boolean dataFileExists(String path)
{
InputStream is = createInput(path);
if (is == null) return false;
else {
try { is.close(); }
catch (IOException e) {}
return true;
}
}