-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterface.js
72 lines (54 loc) · 1.97 KB
/
Interface.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
"use strict";
var ControlPointTool = BasicTool.extend(function() {
this.typename = "ControlPointTool"; // intended for debugging
this.create_controlpoint = function(socket, pos) {
if (this.gizmos[socket] || this.ties[socket]) {
console.error("Attempt to add a controlpoint at a used socket"); return;
}
this.create_output(socket);
this.gizmos[socket].pos = [Cplx.create(pos[0],0),Cplx.create(pos[1],0)];
}
this.recalculate = function() { }
this.create_output_gizmo = function(socket) { return ControlPoint.create(); }
this.find_closest = function(pos) {
var i_best=-1, d_best = Infinity;
for (var i=0; i<this.gizmos.length; i++) {
var d = this.gizmos[i].distance_to_c(pos);
if (d < d_best) { d_best = d; i_best = i; }
}
return [i_best, d_best];
}
this.get_state = function() {
var state = [];
for (var i=0; i<this.gizmos.length; i++) {
var gizmo = this.gizmos[i];
if (gizmo) state[i] = gizmo.dup();
}
return state;
}
this.restore_state = function(state) {
for (var i=0; i<state.length; i++) {
var pos = state[i];
if (pos) this.gizmos[i].pos = pos;
}
}
this.randomize = function() {
for (var i=0; i<this.gizmos.length; i++) {
var gizmo = this.gizmos[i];
if (gizmo) gizmo.pos = [Cplx.random(), Cplx.random()];
}
}
});
//TODO bug: get_gizmo reports connected gizmos, while its contract is to return only gizmos owned
//by this tool. This leads to problems in Tool.remove_output.
//Perhaps solution is to override get_output rather than get_gizmo?
var InterfaceTool = BasicTool.extend(function() {
this.typename = "InterfaceTool"; // intended for debugging
this.create_output = function() {} // we never own any gizmos
this.max_output_socket = function() { return this.max_input_socket(); }
this.get_output = function(socket) {
var tie = this.get_tie(socket);
return tie ? tie[0].get_output(tie[1]) : this.listen(socket);
}
this.recalculate = function() {}
});