/
display.go
86 lines (70 loc) · 1.87 KB
/
display.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
package main
import (
"image"
"gioui.org/f32"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
)
type NodeHud struct{}
func (hud *NodeHud) Layout(gtx *Context) {
for _, node := range gtx.Diagram.Nodes {
hud.LayoutNode(gtx, node)
}
}
func (hud *NodeHud) LayoutNode(gtx *Context, n *Node) {
b := gtx.Bounds(n.Box)
FillRect(gtx, b, n.Style.Fill)
FillRectBorder(gtx, b, float32(gtx.Transform.Dp), n.Style.Border)
}
type ConnectionHud struct{}
func (hud *ConnectionHud) Layout(gtx *Context) {
for _, conn := range gtx.Diagram.Connections {
hud.LayoutConnection(gtx, conn)
}
}
func (hud *ConnectionHud) LayoutConnection(gtx *Context, c *Connection) {
connectionWidth := gtx.PxPerUnit / 4
from := gtx.FPt(c.From.Position())
to := gtx.FPt(c.To.Position()).Sub(from)
curveOffset := f32.Point{X: float32(to.X) / 2}
path := func(ops *op.Ops) clip.PathSpec {
var p clip.Path
p.Begin(ops)
p.MoveTo(from)
if to.X != 0 {
p.Cube(curveOffset, to.Sub(curveOffset), to)
} else {
p.Line(to)
}
return p.End()
}
paint.FillShape(gtx.Ops, DefaultConnection.Border, clip.Stroke{
Path: path(gtx.Ops),
Width: float32(connectionWidth + gtx.Transform.Dp*2),
}.Op())
paint.FillShape(gtx.Ops, DefaultConnection.Fill, clip.Stroke{
Path: path(gtx.Ops),
Width: float32(connectionWidth),
}.Op())
}
type PortHud struct {
ShowAll *bool
}
func (hud *PortHud) Layout(gtx *Context) {
for _, node := range gtx.Diagram.Nodes {
if !(hud.ShowAll != nil && *hud.ShowAll) && !gtx.Diagram.Selection.Contains(node) {
continue
}
for _, port := range node.Ports {
hud.LayoutPort(gtx, port)
}
}
}
func (hud *PortHud) LayoutPort(gtx *Context, p *Port) {
pos := gtx.Pt(p.Position())
r := image.Rectangle{Min: pos, Max: pos}
b := r.Inset(-gtx.PxPerUnit / 4)
FillRect(gtx, b, DefaultPort.Fill)
FillRectBorder(gtx, b, float32(gtx.Transform.Dp), DefaultPort.Border)
}