-
Notifications
You must be signed in to change notification settings - Fork 0
/
CFile.pde
268 lines (230 loc) · 5.45 KB
/
CFile.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
262
263
264
265
266
267
public class CFile
{
private String name;
private FileType type;
public ObjectInputState inputState;
private ArrayList<CFile> files;
private color col;
private color lineCol;
private Body body;
private BodyDef bd;
private float r;
public CFile(String name, float x, float y, BodyType type)
{
this.name = name;
files = new ArrayList<CFile>();
this.type = FileType.ASCII;
inputState = ObjectInputState.NONE;
r = (10/2);
// Define a body.
bd = new BodyDef();
// set its position.
bd.position = box2d.coordPixelsToWorld(x, y);
// set the body type.
bd.type = type;
body = box2d.world.createBody(bd);
// make the body'd shape a circle.
CircleShape cs = new CircleShape();
cs.m_radius = box2d.scalarPixelsToWorld(r);
// create a fixture def.
FixtureDef fd = new FixtureDef();
fd.shape = cs;
// Paraeters that affect physics.
fd.density = 1;
fd.friction = 0.1;
fd.restitution = 0.5;
// attach fixture to body.
body.createFixture(fd);
body.setLinearVelocity(new Vec2(random(-1, 1), random(1, 2)));
col = color(175);
lineCol = color(255);
}
// This function removes the particle from the box2d world
private void killBody()
{
box2d.destroyBody(body);
}
// Is the particle ready for deletion?
private boolean done()
{
// Let's find the screen position of the particle
Vec2 pos = box2d.getBodyPixelCoord(body);
// Is it off the bottom of the screen?
if (pos.y > height+r*2) {
killBody();
return true;
}
return false;
}
private void pickColor()
{
// check for file type.
switch (this.type)
{
case ASCII:
{
// set the normal color of an ascii file.
col = color(0xffB2B2B2);
return;
}
case FOLDER:
{
col = color(0xffff0000);
return;
}
default:
{
return;
}
}
}
private void checkInputState()
{
switch (this.inputState)
{
case NONE:
{
// use this so we can reset the input state.
lineCol = color(255);
noStroke();
return;
}
case HOVER:
{
lineCol = color(0xff00FFDD);
stroke(color(0xff96FF00));
strokeWeight(3);
fill(col);
text(name, r*2, -r*2);
for (CFile file : files)
{
file.inputState = ObjectInputState.HOVER;
}
return;
}
default:
{
return;
}
}
}
public void display()
{
// We look at each body and get its screen position
Vec2 pos = box2d.getBodyPixelCoord(body);
ArrayList<CFile> tmpFiles = new ArrayList(files);
for (CFile f : tmpFiles)
{
// draw a line from the center of the parent
// to the children.
pushMatrix();
stroke(lineCol);
strokeWeight(1);
Vec2 childPos = box2d.getBodyPixelCoord(f.body);
line(pos.x, pos.y, childPos.x, childPos.y);
popMatrix();
f.display();
}
pushMatrix();
translate(pos.x, pos.y);
pickColor();
checkInputState();
fill(col);
ellipse(0, 0, r*2, r*2);
popMatrix();
}
public boolean exists(String fileName)
{
boolean flag = false;
for (CFile file : files)
{
if (match(file.name(), fileName) != null)
{
flag = true;
break;
}
}
return flag;
}
public void addFile(CFile f)
{
// create the joint between parent and
// the file we are added. This joint will
// act like a spring keeping the file from
// wandering far from its parent.
DistanceJointDef djd = new DistanceJointDef();
djd.bodyA = body;
djd.bodyB = f.body;
// equilibrium length
djd.length = box2d.scalarPixelsToWorld(50.0f);
// spring properties
djd.frequencyHz = 2.5;
djd.dampingRatio = 0.9;
DistanceJoint dj = (DistanceJoint)box2d.world.createJoint(djd);
files.add(f);
// since we added a file to this file
// we will label it as a folder.
type = FileType.FOLDER;
}
private void applyForce(Vec2 v)
{
body.applyForce(v, body.getWorldCenter());
}
public void push(CFile b)
{
float G = 1.15;
Vec2 pos = body.getWorldCenter();
Vec2 boxPos = b.body.getWorldCenter();
// vector pointing from this object to the other box.
Vec2 force = boxPos.sub(pos);
float distance = force.length();
// keep force within bounds.
distance = constrain(distance, 1, box2d.scalarPixelsToWorld(20.0f));
force.normalize();
float strength = (G * body.m_mass * b.body.m_mass) / (distance * distance);
force.mulLocal(strength);
b.applyForce(force);
}
public boolean contains(float x, float y)
{
Vec2 worldPoint = box2d.coordPixelsToWorld(x, y);
Fixture f = body.getFixtureList();
boolean inside = f.testPoint(worldPoint);
return inside;
}
/*
** Getters and Setters for private variables.
*/
public String name() {
return name;
}
public int numChildren() {return files.size();}
public Iterator fileIt() {
return files.iterator();
}
public Body body() {
return body;
}
public void col(color c) {
col = c;
}
public String typeAsString()
{
// check for file type.
switch (this.type)
{
case ASCII:
{
return "ascii";
}
case FOLDER:
{
return "folder";
}
default:
{
return "";
}
}
}
}