forked from riicchhaarrd/nodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignal.js
106 lines (95 loc) · 1.99 KB
/
signal.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
import {ctx} from "./nodes.js";
export default class signal {
constructor(node,label,type,x,y,rad)
{
this.node=node;
this.label=label;
this.type=type;
this.x=x;
this.y=y;
this.radius = rad;
this.state = "?";
this.link = null;
}
set_state(v)
{
this.state = v;
if(this.type == "output")
{
if(this.link != null)
{
this.link.set_state(v);
this.link.node.value_changed();
}
}
}
in_bounds(x, y)
{
let dx = x-(this.x+this.node.x);
let dy = y-(this.y+this.node.y);
let dist = Math.sqrt(dx*dx+dy*dy);
return dist < this.radius*2;
}
get_state()
{
if(this.type=="input")
{
if(this.link==null)
return "?";
return this.link.get_state();
}
return this.state;
}
get_state_text()
{
if(typeof this.get_state() == "number" && !Number.isSafeInteger(this.get_state()))
{
return this.get_state().toFixed(2);
}
return this.get_state();
}
move_relative(dx,dy)
{
this.x+=dx;
this.y+=dy;
}
draw(abs_x, abs_y, selected)
{
ctx.save();
ctx.fillStyle="#fff";
ctx.beginPath();
ctx.arc(abs_x + this.x, abs_y + this.y, this.radius, 0, 2 * Math.PI);
ctx.fill();
ctx.restore();
ctx.save();
ctx.font="14px arial";
ctx.strokeStyle=this.type=="input"?"green":"red";
ctx.fillStyle=this.type=="input"?"green":"red";
ctx.beginPath();
ctx.arc(abs_x + this.x, abs_y + this.y, this.radius, 0, 2 * Math.PI);
if(selected)
ctx.fill();
else
ctx.stroke();
ctx.fillText(this.get_state_text(), abs_x + this.x + 10, abs_y + this.y);
ctx.save();
ctx.fillText(this.label, abs_x+this.x+25,abs_y+this.y);
ctx.restore();
if(this.link != null)
{
ctx.beginPath();
ctx.moveTo(abs_x + this.x,abs_y + this.y);
let dx = this.link.x + this.link.node.x;
let dy = this.link.y + this.link.node.y;
ctx.bezierCurveTo(
(abs_x + this.x + dx) / 2, abs_y + this.y,
(abs_x + this.x + dx) / 2, dy,
dx,dy
);
//ctx.lineTo(this.link.x,this.link.y);
//ctx.closePath();
ctx.stroke();
}
ctx.restore();
}
}