-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
179 lines (154 loc) · 4.68 KB
/
Node.java
File metadata and controls
179 lines (154 loc) · 4.68 KB
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
import java.awt.Point;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
public class Node {
private Gate gate;
private Point2D.Double location;
public ArrayList<Node> connectionsInput1 = new ArrayList<Node>();
public ArrayList<Node> connectionsInput2 = new ArrayList<Node>();
private boolean input1 = false;
private boolean input2 = false;
private boolean output = false;
private boolean nextOutput = false;
private double width = 10;
private double height = 10;
public static double panX;
public static double panY;
public Node(Point2D.Double location, Gate gate) {
this.location = location;
this.gate = gate;
}
//dimension scaler for zooming
public void scaleDimensions(double xScaler, double yScaler) {
width *= xScaler;
height *= yScaler;
}
//setters and getters
public void setWidth(int width) {
this.width = width;
}
public int getWidth() {
return (int)width;
}
public void setHeight(int height) {
this.height = height;
}
public int getHeight() {
return (int)height;
}
public void setPoint(Point2D.Double p) {
location = p;
}
public Point2D.Double getPoint() {
return location;
}
public void setGate(Gate g) {
gate = g;
}
public Gate getGate() {
return gate;
}
public void setConnection1(Node n) {
connectionsInput1.add(n);
}
public void setConnection2(Node n) {
connectionsInput2.add(n);
}
public void recieveinput1(boolean input) {
input1 = input;
}
public void recieveinput2(boolean input) {
input2 = input;
}
//evaluates the gate with the current inputs/outputs
public void evaluateGate() {
nextOutput = gate.evaluate(input1, input2);
}
public void nextOuputToCurrentOutput() {
output = nextOutput;
}
public boolean getOutput() {
return output;
}
//creates a matrix of the point
public double[] getPosMat() {
double[] ans = new double[2];
ans[0] = location.getX();
ans[1] = location.getY();
return ans;
}
public Point getPrintPos() {
Main.worldSpaceToScreenSpace.setWorld();
double[] ans = Main.worldSpaceToScreenSpace.translate(this.getPosMat());
// sets center
ans[0] += Main.p.getWidth()/2 + panX;
ans[1] += Main.p.getHeight()/2 + panY;
return new Point((int)ans[0], (int)ans[1]);
}
public static Point2D.Double getWorldPos(Point screenPoint) {
Main.worldSpaceToScreenSpace.setWorld();
double[] screenArray = {screenPoint.getX(), screenPoint.getY()};
// sets center
screenArray[0] -= Main.p.getWidth()/2 + panX;
screenArray[1] -= Main.p.getHeight()/2 + panY;
double[] ans = Main.worldSpaceToScreenSpace.inverseTranslate(screenArray);
return new Point2D.Double(ans[0], ans[1]);
}
//adds the nodeIn to the connectionsInput ArrayLists
public void addConnectionIn1(Node nodeIn) {
this.connectionsInput1.add(nodeIn);
}
public void addConnectionIn2(Node nodeIn) {
this.connectionsInput2.add(nodeIn);
}
//extracts the values of the inputs for each node in the input ArrayLists
public void extractIn1() {
for(Node i: connectionsInput1) {
if(i.getOutput() == true) {
input1 = true;
return;
}
}
}
public void extractIn2() {
for(Node i: connectionsInput2) {
if(i.getOutput() == true) {
input2 = true;
return;
}
}
}
public void clicked() {
gate.clicked();
}
public void setLastNodeClicked() {
Main.lastNodeClicked = this;
}
static class setNextInput1 extends AbstractAction {
public void actionPerformed(ActionEvent e) {
Main.nextInputChoice = 1;
}
}
static class setNextInput2 extends AbstractAction {
public void actionPerformed(ActionEvent e) {
Main.nextInputChoice = 2;
}
}
public void setConnection() {
if(Main.lastNodeClicked == null) {
Main.lastNodeClicked = this;
}
else {
if(Main.nextInputChoice == 1) {
addConnectionIn1(Main.lastNodeClicked);
}
else {
addConnectionIn2(Main.lastNodeClicked);
}
Main.lastNodeClicked = null;
}
Main.p.repaint();
}
}