-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPart.java
More file actions
98 lines (74 loc) · 2.66 KB
/
Part.java
File metadata and controls
98 lines (74 loc) · 2.66 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
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.util.ArrayList;
public abstract class Part {
public int x, y;
public int width, height;
public String name;
public ArrayList<Wire> inputs = new ArrayList<Wire>();
public ArrayList<Wire> outputs = new ArrayList<Wire>();
public int maxInputs;
public int maxOutputs;
public boolean state;
public Part(String name, int x, int y, int width, int height, int maxInputs, int maxOutputs) {
this.name = name;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.maxInputs = maxInputs;
this.maxOutputs = maxOutputs;
}
public Part(Part p){
this.name = p.name;
this.x = p.x;
this.y = p.y;
this.width = p.width;
this.height = p.height;
this.maxInputs = p.maxInputs;
this.maxOutputs = p.maxOutputs;
}
public Part clone() { return null; }
public boolean update() {
boolean[] in = new boolean[inputs.size()];
for (Wire w : inputs) {
in[inputs.indexOf(w)] = w.state;
}
boolean[] out = getBooleanOutputs(in);
boolean flag = false;
for (int i = 0; i < outputs.size(); i++) {
if (outputs.get(i).state != out[i])
flag = true;
outputs.get(i).state = out[i];
}
return flag;
}
public boolean[] getBooleanOutputs(boolean[] in) { return new boolean[outputs.size()]; }
public abstract void draw(Graphics2D g);
public void onClick() { }
public void onDoubleClick() { }
public void changeName() {
String newName = javax.swing.JOptionPane.showInputDialog("Change name", name);
if (newName != null && !newName.trim().isEmpty() && newName.length() <= 10) {
name = newName;
}
}
public int[] getInputNode(Wire w) { return new int[] {0, 0}; }
public int[] getOutputNode(Wire w) { return new int[] {0, 0}; }
public boolean canAddInput() {
return (inputs.size() < maxInputs || maxInputs == -1);
}
public boolean canAddOutput() {
return (outputs.size() < maxOutputs || maxOutputs == -1 || maxOutputs == 1);
}
public boolean inside(int x, int y){
return (x > this.x && x < this.x + this.width && y > this.y && y < this.y + this.height);
}
public void drawText(Graphics2D g) {
g.setColor(Main.nodeColor);
FontMetrics fm = g.getFontMetrics();
int x = this.x + (this.width - fm.stringWidth(name)) / 2;
int y = this.y + (this.height - fm.getHeight()) / 2 + fm.getAscent();
g.drawString(name, x, y);
}
}