2018-06-08 02:01:55 +00:00
|
|
|
package view
|
2017-11-04 21:29:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-11-19 21:39:56 +00:00
|
|
|
"github.com/mediocregopher/ginger/gg"
|
2017-11-04 21:29:15 +00:00
|
|
|
"github.com/mediocregopher/ginger/gim/geo"
|
|
|
|
"github.com/mediocregopher/ginger/gim/terminal"
|
|
|
|
)
|
|
|
|
|
|
|
|
type box struct {
|
2017-11-19 21:39:56 +00:00
|
|
|
topLeft geo.XY
|
|
|
|
flowDir geo.XY
|
|
|
|
numIn, numOut int
|
2018-06-08 04:03:13 +00:00
|
|
|
buf *terminal.Buffer
|
2018-06-07 03:25:40 +00:00
|
|
|
bodyBuf *terminal.Buffer
|
2017-11-04 21:29:15 +00:00
|
|
|
}
|
|
|
|
|
2017-11-19 21:39:56 +00:00
|
|
|
func boxFromVertex(v *gg.Vertex, flowDir geo.XY) box {
|
|
|
|
b := box{
|
|
|
|
flowDir: flowDir,
|
|
|
|
numIn: len(v.In),
|
|
|
|
numOut: len(v.Out),
|
|
|
|
}
|
2018-01-21 15:39:25 +00:00
|
|
|
if v.VertexType == gg.ValueVertex {
|
2018-06-07 03:25:40 +00:00
|
|
|
b.bodyBuf = terminal.NewBuffer()
|
|
|
|
b.bodyBuf.WriteString(v.Value.V.(string))
|
2017-11-19 21:39:56 +00:00
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b box) rect() geo.Rect {
|
2018-06-07 03:25:40 +00:00
|
|
|
var bodyRect geo.Rect
|
|
|
|
if b.bodyBuf != nil {
|
|
|
|
bodyRect.Size = b.bodyBuf.Size().Add(geo.XY{2, 2})
|
2017-11-19 21:39:56 +00:00
|
|
|
}
|
2017-11-04 21:29:15 +00:00
|
|
|
|
2017-11-19 21:39:56 +00:00
|
|
|
var edgesRect geo.Rect
|
|
|
|
{
|
|
|
|
var neededByEdges int
|
|
|
|
if b.numIn > b.numOut {
|
|
|
|
neededByEdges = b.numIn*2 + 1
|
|
|
|
} else {
|
|
|
|
neededByEdges = b.numOut*2 + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
switch b.flowDir {
|
|
|
|
case geo.Left, geo.Right:
|
|
|
|
edgesRect.Size = geo.XY{2, neededByEdges}
|
2017-12-03 19:38:53 +00:00
|
|
|
case geo.Up, geo.Down:
|
|
|
|
edgesRect.Size = geo.XY{neededByEdges, 2}
|
2017-11-19 21:39:56 +00:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown flowDir: %#v", b.flowDir))
|
|
|
|
}
|
2017-11-04 21:29:15 +00:00
|
|
|
}
|
2017-11-19 21:39:56 +00:00
|
|
|
|
|
|
|
return bodyRect.Union(edgesRect).Translate(b.topLeft)
|
2017-11-04 21:29:15 +00:00
|
|
|
}
|
|
|
|
|
2018-06-03 08:26:42 +00:00
|
|
|
func (b box) draw(buf *terminal.Buffer) {
|
|
|
|
rect := b.rect()
|
|
|
|
buf.DrawRect(rect, terminal.SingleLine)
|
2018-06-07 03:25:40 +00:00
|
|
|
if b.bodyBuf != nil {
|
2018-06-08 00:17:49 +00:00
|
|
|
buf.DrawBufferCentered(rect.Center(), b.bodyBuf)
|
2018-06-07 03:25:40 +00:00
|
|
|
}
|
2017-11-04 21:29:15 +00:00
|
|
|
}
|