-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrols.js
149 lines (142 loc) · 4.55 KB
/
controls.js
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
function setEventListeners() {
//canvas
canvas.addEventListener("contextmenu", onRClick);
canvas.addEventListener("click", onClick);
canvas.addEventListener("mousedown", onMouseDown);
canvas.addEventListener("mousemove", onMouseMove);
addEventListener("mouseup", onMouseUp);
//keyboard
addEventListener("keydown", keyPressed);
addEventListener("keyup", keyRelease);
//buttons
addButton.forEach((button) => {
button.addEventListener("click", addBox);
});
removeButton.forEach((button) => {
button.addEventListener("click", removeBox);
});
gateButtons.forEach((button) => {
button.addEventListener("mousedown", spawn);
});
}
function keyPressed(event) {
if (!pressedKeys.includes(event.keyCode)) pressedKeys.push(event.keyCode);
}
function keyRelease(event) {
switch (event.keyCode) {
case keyCodes.plus:
handleGateInputs(0, currentSelectedGate);
break;
case keyCodes.minus:
handleGateInputs(1, currentSelectedGate);
break;
case keyCodes.delete:
deleteGate(currentSelectedGate);
break;
}
remove(event.keyCode, pressedKeys);
}
function onRClick(event) {
// let point = getCurrentSelection(event);
// if (point instanceof connectionPoint) if (point != null) point.disconnect();
}
function onClick(event) {
//check inputboxes
for (let i = INPUTBOXES.length - 1; i >= 0; i--) {
let box = INPUTBOXES[i];
let isIn = isInCircle(
event,
box.position.x,
box.position.y,
boolBox.radius
);
if (isIn) {
box.toggle();
}
}
}
function onMouseDown(event) {
pressedKeys.push(event.button); //adding mouse button to list
if (event.button === 0) {
//on Left Click
tempSelection = getCurrentSelection(event);
if (tempSelection instanceof gate) {
remove(tempSelection, GATES);
GATES.push(tempSelection);
currentSelectedGate = tempSelection; /////////
tempSelection.offset.x = event.offsetX - tempSelection.position.x;
tempSelection.offset.y = event.offsetY - tempSelection.position.y;
} else if (tempSelection instanceof outputPoint) {
currentConLineIndex =
tempSelection.connections.push(new connectionLine(tempSelection)) - 1;
}
// currentSelectedGate = GATES[GATES.length - 1];
} else if (event.button == 2) {
//on Right Click
let point = getCurrentSelection(event);
if (point instanceof connectionPoint) if (point != null) point.disconnect();
}
// root.style.setProperty("--cursor", "grabbing"); ////////////
}
function onMouseMove(event) {
if (pressedKeys.includes(0)) {
if (tempSelection instanceof gate) {
//Moving Gate
tempSelection.position.x = event.offsetX - tempSelection.offset.x;
tempSelection.position.y = event.offsetY - tempSelection.offset.y;
} else if (tempSelection instanceof outputPoint) {
//Moving Connection Line
tempSelection.connections[currentConLineIndex].end.position.x =
event.offsetX;
tempSelection.connections[currentConLineIndex].end.position.y =
event.offsetY;
snapable = isSnapable(event);
if (snapable && snapable.connection == null) {
//temp replace
tempSelection.connections[currentConLineIndex].end.position.x =
snapable.position.x;
tempSelection.connections[currentConLineIndex].end.position.y =
snapable.position.y;
}
}
}
if (pressedKeys.includes(2)) {
let point = getCurrentSelection(event);
if (point instanceof connectionPoint) if (point != null) point.disconnect();
}
// show label on hover
[...connectionPoints.input, ...connectionPoints.output].forEach((con) => {
let isIn = isInCircle(
event,
con.position.x,
con.position.y,
connectionPoint.radius
);
if (isIn) {
con.mouseOver.hover = true;
if (
pressedKeys.includes(keyCodes.control) ||
pressedKeys.includes(keyCodes.command)
) {
con.mouseOver.label = true;
}
} else con.mouseOver = { hover: false, label: false };
});
}
function onMouseUp(event) {
if (event.button === 0) {
if (tempSelection instanceof outputPoint) {
if (snapable && snapable.connection == null) {
snapable.connect(tempSelection.connections[currentConLineIndex]);
snapable = false;
} else {
tempSelection.disconnect(currentConLineIndex);
}
}
tempSelection = null;
}
remove(event.button, pressedKeys); //removing mouse button from list
//Mark gates for delete if out of frame
markForDelete();
// root.style.setProperty("--cursor", "grab"); ///////////
}