-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircuit.pde
136 lines (130 loc) · 3.41 KB
/
Circuit.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
public abstract class Circuit {
private int x, y;//world space coordinates
private int w, h;
private boolean[] inputs, outputs;//number of node sockets going in and out of the node
private Spline[] splines;
private String name;
private boolean updated;
int inputOffset, outputOffset;
public Circuit(int x, int y, int w, int h, int in, int out, String name) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.inputs = new boolean[in];
this.outputs = new boolean[out];
this.splines = new Spline[in];
inputOffset = h/(in+1);
outputOffset = h/(out+1);
this.name = name;
updated = false;
}
public void display() {
stroke(NODE_STROKE_COLOR);
fill(NODE_FILL_COLOR);
rect(x, y, w, h, NODE_SMOOTHNESS);
fill(NODE_TEXT_COLOR);
textAlign(CENTER);
text(name, x+w/2, y+NODE_TEXT_SIZE-2);
line(x, y+NODE_TEXT_SIZE, x+w, y+NODE_TEXT_SIZE);
int sy = y+inputOffset;
for (int i=0; i<this.inputs.length; i++) {
if (this.inputs[i]) {
fill(SPLINE_ELECTRICITY_COLOR);
stroke(SPLINE_ELECTRICITY_COLOR);
} else {
fill(NODE_STROKE_COLOR);
stroke(NODE_STROKE_COLOR);
}
circle(x, sy, NODE_SOCKET_SIZE);
if (this.splines[i] != null) {
splines[i].display(x, sy);
}
sy+=inputOffset;
}
sy = y+outputOffset;
for (int i=0; i<this.outputs.length; i++) {
if (this.outputs[i]) {
fill(SPLINE_ELECTRICITY_COLOR);
stroke(SPLINE_ELECTRICITY_COLOR);
} else {
fill(NODE_STROKE_COLOR);
stroke(NODE_STROKE_COLOR);
}
circle(x+w, sy, NODE_SOCKET_SIZE);
sy+=outputOffset;
}
}
public void highlight() {
fill(NODE_SELECTED_COLOR);
noStroke();
rect(x, y, w, h, NODE_SMOOTHNESS);
}
public boolean inNode(int x, int y) {
return ((x>=this.x) && (x<=(this.x+this.w)) && (this.y<=y) && y<=(this.y+this.h));
}
public void addSpline(int outId, int outsocketId, int id) {
this.splines[id] = new Spline(outId, outsocketId, getInput(id));
}
public boolean getOutput(int index) {
return this.outputs[index];
}
public void setOutput(int index, boolean value) {
this.outputs[index] = value;
}
public abstract void calcOutput( int index);
public void calcInput(int index) {
Spline s = this.splines[index];
this.inputs[index] = (s == null)? false : s.calcState();
}
public boolean getInput(int index) {
return this.inputs[index];
}
public void update() {
for (int i=0; i<inputs.length; i++) {
calcInput(i);
}
for (int i=0; i<outputs.length; i++) {
calcOutput(i);
}
}
public void recInput(int index) {
//println(this.name,"called recInput");
Spline s = this.splines[index];
this.inputs[index] = (s == null)? false : s.recCalcState();
}
public void recUpdate() {
for (int i=0; i<inputs.length; i++) {
recInput(i);
}
for (int i=0; i<outputs.length; i++) {
calcOutput(i);
}
}
public boolean recOutput(int index) {
if (!updated) {
recUpdate();
updated = true;
}
return getOutput(index);
}
public boolean isUpdated() {
return updated;
}
public void setXY(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public int getWidth() {
return this.w;
}
public int getHeight() {
return this.h;
}
}