-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddPath.js
114 lines (103 loc) · 2.33 KB
/
AddPath.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
var AddPath = (function() {
var hovering;
var previewSelfLoop;
var dragging;
var draggingTo;
var leftHome;
return {
Setup: Setup,
Teardown: Teardown,
Draw: Draw
};
function Setup() {
Events.On('MouseMove', MouseMove);
Events.On('MouseDown', MouseDown);
}
function Teardown() {
Events.Off('MouseMove', MouseMove);
Events.Off('MouseDown', MouseDown);
SetHovering(null);
SetPreviewSelfLoop(null);
SetDragging(null);
leftHome = false;
}
function MouseMove(point) {
if (dragging) {
draggingTo = point;
}
var state = GetStateAt(point);
if (state) {
SetHovering(state);
SetCursor('crosshair');
if (leftHome && state == dragging) {
SetPreviewSelfLoop(state);
}
} else {
SetHovering(null);
SetPreviewSelfLoop(null);
leftHome = true;
}
}
function MouseDown(point) {
var state = GetStateAt(point);
leftHome = false;
if (state) {
SetDragging(state);
Events.One('MouseUp', function(point) {
AddPath(point);
SetDragging(null);
});
}
}
function HasPath(source, destination) {
for (var i=0; i < source.paths.length; i++) {
var path = source.paths[i];
if (path.destination == destination) return true;
}
return false;
}
function AddPath(point) {
var destination = GetStateAt(point);
if (dragging && destination && leftHome) {
if (!HasPath(dragging, destination)) {
var path = new Path();
path.source = dragging;
path.destination = destination;
dragging.paths.push(path);
SelectPath.Select(path);
}
}
}
function SetHovering(state) {
if (hovering) hovering.hover = false;
hovering = state;
if (hovering) {
hovering.hover = true;
}
}
function SetDragging(state) {
if (dragging) dragging.drag = false;
dragging = state;
if (dragging) {
dragging.drag = true;
} else {
draggingTo = null;
}
}
function SetPreviewSelfLoop(state) {
if (previewSelfLoop) previewSelfLoop.previewSelfLoop = false;
previewSelfLoop = state;
if (previewSelfLoop) {
previewSelfLoop.previewSelfLoop = true;
}
}
function Draw(ctx) {
if (dragging && draggingTo) {
var point = dragging.point;
ctx.beginPath();
ctx.moveTo(point.x, point.y);
ctx.lineTo(draggingTo.x, draggingTo.y);
ctx.stroke();
}
}
})();