/
connection.go
648 lines (601 loc) · 12.7 KB
/
connection.go
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
// Copyright 2014 Gordon Klaus. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"github.com/gordonklaus/flux/go/types"
. "github.com/gordonklaus/flux/gui"
. "github.com/chsc/gogl/gl21"
"math"
"sort"
"strings"
)
type connection struct {
*ViewBase
AggregateMouser
blk *block
src *port
dst *port
feedback bool
focused, focusSrc bool
srcPt Point
dstPt Point
hidden bool
savedPort *port
editing bool
bad, wasBad bool
}
func newConnection() *connection {
c := &connection{}
c.ViewBase = NewView(c)
c.AggregateMouser = AggregateMouser{NewClickFocuser(c)}
return c
}
func (c *connection) connectable(src, dst *port) bool {
// Quietly disconnect c for the duration of this call to more accurately simulate the proposed scenario in which
// it has the new src and dst. In particular, assignableToAll must ignore the current state of this connection when
// reconnecting a generic input.
p, q := c.src, c.dst
c.src, c.dst = nil, nil
defer func() { c.src, c.dst = p, q }()
if src.out == dst.out {
return false
}
for _, c := range src.conns {
if c.dst == dst {
return false
}
}
t := src.obj.Type
u := dst.obj.Type
if t == nil {
return false
}
if (t == seqType) != (u == seqType) {
return false
}
if t == seqType {
return src.node.block() == dst.node.block() && src.node != dst.node && !precedes(src.node, dst.node) && !precedes(dst.node, src.node)
}
f := func(t types.Type) bool { return assignable(t, u) }
if n, ok := dst.node.(connectable); ok {
f = func(t types.Type) bool { return n.connectable(t, dst) && assignableToAll(t, dst) }
}
if !maybeIndirect(t, f) {
return false
}
// TODO: recursive func literals
// if f, ok := src.node.(*funcNode); ok && parentOrSelfInBlock(dst.node, f.funcblk) != nil {
// return true
// }
n1, n2 := src.node, dst.node
for b := n1.block(); ; n1, b = b.node, b.outer() {
if n := b.find(n2); n != nil {
n2 = n
break
}
}
if c.feedback {
for b := n1.block(); ; b = b.outer() {
if b == nil {
return false
}
if _, ok := b.node.(*loopNode); ok {
break
}
}
n1, n2 = n2, n1
} else if n1 == n2 {
return false
}
return !precedes(n2, n1)
}
func precedes(n1, n2 node) bool {
for _, dst := range dstsInBlock(n1) {
if dst == n2 || precedes(dst, n2) {
return true
}
}
return false
}
func assignable(t, u types.Type) bool {
if t == nil || u == nil {
return false
}
if t, ok := t.(*types.Basic); ok && t.Info&types.IsUntyped != 0 {
// TODO: consider representability of const values
switch u := underlying(u).(type) {
case *types.Interface:
return u.Empty()
case *types.Basic:
int := t.Info&types.IsInteger != 0
float := t.Info&types.IsFloat != 0
complex := t.Info&types.IsComplex != 0
switch {
case u.Info&types.IsBoolean != 0:
return t.Info&types.IsBoolean != 0
case u.Info&types.IsInteger != 0:
return int
case u.Info&types.IsFloat != 0:
return int || float
case u.Info&types.IsComplex != 0:
return int || float || complex
case u.Info&types.IsString != 0:
return t.Info&types.IsString != 0
}
}
return false
}
return types.IsAssignableTo(t, u)
}
type connectable interface {
connectable(t types.Type, dst *port) bool
}
// Returns true if t is assignable to or from all of the source types (or their indirections).
// Does not indirect t as connectable (via which this is meant to be called) already does so.
func assignableToAll(t types.Type, ins ...*port) bool {
for _, p := range ins {
for _, c := range p.conns {
if c.src != nil {
if !maybeIndirect(c.src.obj.Type, func(t2 types.Type) bool { return assignable(t, t2) || assignable(t2, t) }) {
return false
}
}
}
}
return true
}
// Returns one of the source types (or its indirection) to which all of the others (or their indirections) are assignable.
func inputType(ins ...*port) (T types.Type) {
assign := func(t, u types.Type) bool {
if T == nil || assignable(t, u) {
T = u
return true
}
return false
}
for _, p := range ins {
for _, c := range p.conns {
if c.src != nil {
maybeIndirect(T, func(t types.Type) bool {
return maybeIndirect(c.src.obj.Type, func(u types.Type) bool {
return assign(t, u) || assign(u, t)
})
})
}
}
}
if T != nil && !ins[0].node.(connectable).connectable(T, ins[0]) {
T, _ = indirect(T)
}
return
}
func maybeIndirect(t types.Type, f func(t types.Type) bool) bool {
p, ok := indirect(t)
return f(t) || ok && f(p)
}
func (c connection) connected() bool { return c.src != nil && c.dst != nil }
func (c *connection) disconnect() {
c.setSrc(nil)
c.setDst(nil)
}
func (c *connection) setSrc(src *port) {
txt := ""
if c.src != nil {
c.src.disconnect(c)
txt = c.src.conntxt.Text()
c.updateSrcTxt("")
}
if c.bad && src != nil && c.connectable(src, c.dst) {
c.bad = false
}
c.src = src
c.reblock()
if src != nil {
src.connect(c)
c.updateSrcTxt(txt)
}
if c.dst != nil {
c.dst.connsChanged()
}
c.reform()
}
func (c *connection) setDst(dst *port) {
if c.dst != nil {
c.dst.disconnect(c)
c.dst.connsChanged()
c.updateDstTxt()
}
if c.bad && dst != nil && c.connectable(c.src, dst) {
c.bad = false
}
c.dst = dst
c.reblock()
if dst != nil {
dst.connect(c)
c.dst.connsChanged()
c.updateDstTxt()
}
c.reform()
}
func (c *connection) reblock() {
var newblk *block
switch {
case c.src == nil && c.dst == nil:
return
case c.src == nil:
newblk = c.dst.node.block()
case c.dst == nil:
newblk = c.src.node.block()
default:
for b := c.src.node.block(); ; b = b.outer() {
if b.find(c.dst.node) != nil {
newblk = b
break
}
}
if c.feedback {
for {
n := newblk.node
newblk = n.block()
if _, ok := n.(*loopNode); ok {
break
}
}
}
}
newblk.addConn(c)
}
func (c *connection) reform() {
unconnectedOffset := Pt(0, -32)
if c.feedback {
unconnectedOffset.Y = 96
}
if c.src != nil {
c.srcPt = Map(Center(c.src), c.src, c.blk)
}
if c.dst != nil {
c.dstPt = Map(Center(c.dst), c.dst, c.blk)
} else {
c.dstPt = c.srcPt.Add(unconnectedOffset)
}
if c.src == nil {
c.srcPt = c.dstPt.Sub(unconnectedOffset)
}
rect := Rectangle{c.srcPt, c.dstPt}.Canon()
pos := rect.Min
// center the origin so that keyboard navigation works
center := rect.Center()
c.srcPt = c.srcPt.Sub(center)
c.dstPt = c.dstPt.Sub(center)
rect = rect.Sub(center)
c.Move(pos)
c.SetRect(rect)
}
func (c *connection) focus(focusSrc bool) {
c.focusSrc = focusSrc
SetKeyFocus(c)
Repaint(c)
if focusSrc {
panTo(c, c.srcPt)
} else {
panTo(c, c.dstPt)
}
}
func (c *connection) startEditing() {
if c.focusSrc {
if c.hidden {
return // TODO: alternately, edit all hidden connections simultaneously?
}
c.savedPort = c.src
} else {
c.savedPort = c.dst
}
c.wasBad = c.bad
c.editing = true
c.reform()
}
func (c *connection) cancelEditing() {
if c.editing {
if c.focusSrc {
c.setSrc(c.savedPort)
} else {
c.setDst(c.savedPort)
}
c.bad = c.wasBad
c.stopEditing()
}
}
func (c *connection) stopEditing() {
if c.editing {
c.editing = false
if c.connected() {
c.reform()
} else {
p := c.src
if c.focusSrc {
p = c.dst
}
c.blk.removeConn(c)
SetKeyFocus(p)
}
}
}
func (c *connection) toggleHidden() {
c.hidden = !c.hidden
rearrange(c.blk)
if c.hidden {
if srctxt := c.src.conntxt; srctxt.Text() == "" {
srctxt.TextChanged = func(string) {
for _, c := range c.src.conns {
if c.hidden {
c.updateDstTxt()
}
}
}
srctxt.Accept = func(name string) {
names := map[string]bool{"": true}
c.blk.outermost().walk(nil, nil, func(conn *connection) {
if conn.src != c.src {
names[conn.src.conntxt.Text()] = true
}
})
if names[name] {
SetKeyFocus(srctxt)
} else {
c.focus(c.focusSrc)
}
srctxt.TextChanged = nil
}
srctxt.Reject = func() {
c.toggleHidden()
c.focus(c.focusSrc)
srctxt.TextChanged = nil
}
SetKeyFocus(srctxt)
}
}
c.updateSrcTxt("")
c.updateDstTxt()
}
func (c *connection) updateSrcTxt(txt string) {
anyHidden := false
for _, c := range c.src.conns {
if c.hidden {
anyHidden = true
}
}
srctxt := c.src.conntxt
if !anyHidden {
srctxt.SetText("")
} else if txt != "" {
srctxt.SetText(txt)
}
srctxt.Move(Pt(-Width(srctxt)/2, -Height(srctxt)))
}
func (c *connection) updateDstTxt() {
if c.dst == nil {
return
}
nameset := map[string]bool{}
for _, c := range c.dst.conns {
if c.hidden {
nameset[c.src.conntxt.Text()] = true
}
}
names := []string{}
for n := range nameset {
names = append(names, n)
}
sort.StringSlice(names).Sort()
dsttxt := c.dst.conntxt
dsttxt.SetText(strings.Join(names, ","))
dsttxt.Move(Pt(-Width(dsttxt)/2, 0))
}
func (c *connection) TookKeyFocus() {
c.focused = true
Repaint(c)
}
func (c *connection) LostKeyFocus() {
c.cancelEditing()
c.focused = false
Repaint(c)
}
func (c *connection) KeyPress(event KeyEvent) {
if c.editing {
switch event.Key {
case KeyBackslash:
if c.src == nil || c.dst == nil {
c.feedback = !c.feedback
c.reform()
}
case KeyLeft, KeyRight, KeyDown, KeyUp:
b := c.blk.outermost()
ports := []View{}
p1 := c.src
if c.focusSrc {
p1 = c.dst
}
for _, n := range b.allNodes() {
p := n.inputs()
if c.focusSrc {
p = n.outputs()
}
for _, p2 := range p {
src, dst := p1, p2
if c.focusSrc {
src, dst = dst, src
}
if (src != c.src || dst != c.dst) && c.connectable(src, dst) {
ports = append(ports, p2)
}
}
}
pt := c.dstPt
if c.focusSrc {
pt = c.srcPt
}
v := nearestView(b, ports, Map(pt, c, b), event.Key)
if p, ok := v.(*port); ok {
if c.focusSrc {
c.setSrc(p)
panTo(c, c.srcPt)
} else {
c.setDst(p)
panTo(c, c.dstPt)
}
}
case KeyEnter:
c.stopEditing()
case KeyEscape:
c.cancelEditing()
}
return
}
if event.Alt {
switch event.Key {
case KeyLeft, KeyRight, KeyDown, KeyUp:
p := c.dst
if c.focusSrc {
p = c.src
}
c.blk.focusNearestView(p, event.Key)
return
}
}
switch event.Key {
case KeyUp:
if c.focusSrc {
SetKeyFocus(c.src)
} else {
c.focus(true)
}
case KeyDown:
if c.focusSrc {
c.focus(false)
} else {
SetKeyFocus(c.dst)
}
case KeyRight, KeyLeft:
p := c.dst
if c.focusSrc {
p = c.src
}
p.focusNextConn(c.dstPt.Sub(c.srcPt).Angle(), event.Key)
case KeyBackspace:
SetKeyFocus(c.src)
c.blk.removeConn(c)
case KeyDelete:
SetKeyFocus(c.dst)
c.blk.removeConn(c)
case KeyEnter:
c.startEditing()
case KeyEscape:
if c.focusSrc {
SetKeyFocus(c.src)
} else {
SetKeyFocus(c.dst)
}
default:
if event.Text == "_" {
if c.src.obj.Type != seqType {
c.toggleHidden()
}
} else {
c.ViewBase.KeyPress(event)
}
}
}
func (c *connection) Mouse(m MouseEvent) {
if m.Press {
if m.Pos.Sub(c.srcPt).Len() < 2*portSize {
c.focus(true)
c.startEditing()
} else if m.Pos.Sub(c.dstPt).Len() < 2*portSize {
c.focus(false)
c.startEditing()
}
}
if !c.editing {
return
}
p1 := c.src
if c.focusSrc {
p1 = c.dst
}
var p *port
b := c.blk.outermost()
if p2, ok := ViewAt(b, Map(m.Pos, c, b)).(*port); ok {
src, dst := p1, p2
if c.focusSrc {
src, dst = dst, src
}
if src == c.src && dst == c.dst || c.connectable(src, dst) {
p = p2
}
}
if c.focusSrc {
c.srcPt = m.Pos
c.setSrc(p)
} else {
c.dstPt = m.Pos
c.setDst(p)
}
if m.Release {
c.stopEditing()
}
}
func (c *connection) Paint() {
start, end := c.srcPt, c.dstPt
d := end.Sub(start)
mid := start.Add(d.Div(2))
if c.feedback {
mid.X = math.Max(start.X, end.X) + 128
}
off := Pt(0, math.Abs(d.Y/3))
p1 := start.Sub(off)
p2 := mid
p3 := end.Add(off)
pts := []Point{start, p1, p2, p3, end}
SetColor(lineColor)
SetLineWidth(3)
if c.src != nil && c.src.obj.Type == seqType || c.dst != nil && c.dst.obj.Type == seqType {
n := d.Len() / 3
d = d.Div(n)
p := start
for i := 0; i < int(n+.5); i += 2 {
q := p.Add(d)
DrawLine(p, q)
p = q.Add(d)
}
pts = []Point{start, end}
} else if !c.hidden {
DrawBezier(pts...)
}
if c.focused {
c2 := focusColor
if c.editing {
c2 = Color{1, .5, 0, .5}
}
c1 := c2
c1.A = 0
if c.focusSrc {
c1, c2 = c2, c1
}
ctrlPts := []Double{Double(c1.R), Double(c1.G), Double(c1.B), Double(c1.A), Double(c2.R), Double(c2.G), Double(c2.B), Double(c2.A)}
Map1d(MAP1_COLOR_4, 0, 1, 4, 2, &ctrlPts[0])
Enable(MAP1_COLOR_4)
SetLineWidth(7)
DrawBezier(pts...)
Disable(MAP1_COLOR_4)
}
if c.bad {
SetColor(Color{1, 0, 0, 1})
SetLineWidth(3)
p := Center(c)
d := Pt(6, 6)
DrawLine(p.Add(d), p.Sub(d))
d = Pt(6, -6)
DrawLine(p.Add(d), p.Sub(d))
}
}