-
Notifications
You must be signed in to change notification settings - Fork 0
/
manualDraw.js
executable file
·213 lines (180 loc) · 6.58 KB
/
manualDraw.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
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
let currentMousePos, prevMousePos;
let stringActive = false;
let editing = false;
let enableWrap = true;
let previousHoveredPegIndex = null;
window.addEventListener("mouseup", () => {
//if close enough to a peg, set that as the starting one
let nearestPegIndex = board.getNearestPegIndex(currentMousePos);
if (isMouseHoveringPeg(nearestPegIndex))
{
if (slDrawMode.value == "Manual") pegClickManual(nearestPegIndex);
else if (slDrawMode.value == "Pattern") pegClickPattern();
}
});
window.addEventListener("mousemove", (e) => {
prevMousePos.set(currentMousePos);
let canvasBounds = cnvMain.getBoundingClientRect();
let canvasPixelsPerScreenPixel = { x: canvasRes / canvasBounds.width, y: canvasRes / canvasBounds.height };
currentMousePos.x = (e.x - canvasBounds.x) * canvasPixelsPerScreenPixel.x - canvasCentreOffset.x;
currentMousePos.y = (e.y - canvasBounds.y) * canvasPixelsPerScreenPixel.y - canvasCentreOffset.y;
if (slDrawMode.value == "Manual")
{
if (stringActive)
{
if (enableWrap) board.resolveWraps(prevMousePos, currentMousePos);
requestDraw = true;
}
}
else if (slDrawMode.value == "Pattern")
{
let currentHoveredPegIndex = null;
let nearestPegIndex = board.getNearestPegIndex(currentMousePos);
if (isMouseHoveringPeg(nearestPegIndex))
{
currentHoveredPegIndex = nearestPegIndex;
}
if (currentHoveredPegIndex != null && currentHoveredPegIndex != previousHoveredPegIndex)
{
//create new hover preview
let clockwise = (slPatternDirection.value == "Clockwise");
let colour = colourInputToRGB(inpStringColour.value);
board.generatePatternPreview(currentHoveredPegIndex, patternInterval, patternCount, clockwise, colour);
requestDraw = true;
}
if (currentHoveredPegIndex == null && previousHoveredPegIndex != null) requestDraw = true;
previousHoveredPegIndex = currentHoveredPegIndex;
}
updateMouseCursor();
});
window.addEventListener("keydown", (e) => {
if (e.key === "Shift")
{
if (slShiftMode.value === "Hold") enableWrap = false;
requestDraw = true;
}
});
window.addEventListener("keyup", (e) => {
if (e.key === "Shift")
{
if (slShiftMode.value === "Hold") enableWrap = true;
else enableWrap = !enableWrap;
requestDraw = true;
}
if (e.key.toLowerCase() == "z")
{
let csc = board.getCurrentStringChain();
if (csc)
{
if (csc.getLength() > 1) csc.pop();
else
{
board.deleteStringChain(csc);
if (editing)
{
dvStringChainList.removeChild(editingListItem);
editingListItem = null;
}
stringActive = false;
editing = false;
enableUI()
updateMouseCursor();
}
requestDraw = true;
}
}
});
function pegClickManual(nearestPegIndex)
{
let currentStringChain = board.getCurrentStringChain();
if (stringActive)
{
if (nearestPegIndex === currentStringChain.getFirstPegIndex() && currentStringChain.getLength() === 1)
{
//unwrapped from all pegs, remove the chain
board.deleteStringChain(currentStringChain);
if (editing)
{
//remove this chain from the list of existing chains
dvStringChainList.removeChild(editingListItem);
editingListItem = null;
}
}
else
{
//finish the string here
if (currentStringChain.getLastPegIndex() != nearestPegIndex)
{
let wrapStarts = board.calculateWrapStarts(currentStringChain.getLastPegIndex(), currentStringChain.getLastPegIsClockwise(), nearestPegIndex);
let wrapEnd = board.calculateWrapEnd(currentStringChain.getLastPegIndex(), currentStringChain.getLastPegIsClockwise(), nearestPegIndex, true);
currentStringChain.setLastPegWrapEnd(wrapEnd);
currentStringChain.push(nearestPegIndex, true, wrapStarts.clockwise, null); //default to clockwise when clicked
}
if (!editing) addStringChainDiv(currentStringChain);
board.nextStringChainIndex();
}
board.activeStringChain = null;
board.updatePreRender(cnvMain, ctxMain);
stringActive = false;
editing = false;
enableUI()
requestDraw = true;
}
else
{
//begin string chain
board.newStringChain(nearestPegIndex, currentColour);
stringActive = true;
enableWrap = true;
updateMouseCursor();
disableUI();
requestDraw = true;
}
updateMouseCursor();
}
function pegClickPattern()
{
if (previousHoveredPegIndex != null)
{
board.applyPattern();
addStringChainDiv(board.patternPreviewChain);
board.updatePreRender(cnvMain, ctxMain);
requestDraw = true;
}
}
function updateMouseCursor()
{
let nearestPegIndex = board.getNearestPegIndex(currentMousePos);
if (isMouseHoveringPeg(nearestPegIndex))
{
cnvMain.style.cursor = "pointer";
}
else
{
if (stringActive) cnvMain.style.cursor = "crosshair";
else cnvMain.style.cursor = "default";
}
}
function signedTriangleArea(baseStart, baseEnd, peak)
{
let AB = new Vec2(baseEnd.x - baseStart.x, baseEnd.y - baseStart.y);
let AC = new Vec2(peak.x - baseStart.x, peak.y - baseStart.y);
let area = AB.cross(AC);
return area;
}
function isPointInTriangle(t1, t2, t3, p)
{
let a1 = signedTriangleArea(t1, t2, p);
let a2 = signedTriangleArea(t2, t3, p);
let a3 = signedTriangleArea(t1, t3, p);
let s1 = Math.sign(a1);
let s2 = Math.sign(a2);
let s3 = Math.sign(a3);
return { isWrapped: (s1 === s2 && s1 !== s3), isClockwise: s1 > 0 };
}
function isMouseHoveringPeg(pegIndex)
{
let pegPos = board.getPegPos(pegIndex);
return (pegPos.x - currentMousePos.x) * (pegPos.x - currentMousePos.x) +
(pegPos.y - currentMousePos.y) * (pegPos.y - currentMousePos.y) < board.pegRadius * board.pegRadius + 100; //bit of extra room for clicking
}