2017-11-04 21:29:15 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mediocregopher/ginger/gim/geo"
|
|
|
|
"github.com/mediocregopher/ginger/gim/terminal"
|
|
|
|
)
|
|
|
|
|
2018-03-03 17:32:40 +00:00
|
|
|
type line struct {
|
2018-03-04 14:35:01 +00:00
|
|
|
from, to *box
|
|
|
|
fromI, toI int
|
2018-06-02 04:35:12 +00:00
|
|
|
body string
|
2018-03-03 17:32:40 +00:00
|
|
|
}
|
2017-11-04 21:29:15 +00:00
|
|
|
|
2017-11-23 19:19:32 +00:00
|
|
|
// given the "primary" direction the line should be headed, picks a possible
|
|
|
|
// secondary one which may be used to detour along the path in order to reach
|
|
|
|
// the destination (in the case that the two boxes are diagonal from each other)
|
2018-03-04 14:35:01 +00:00
|
|
|
func secondaryDir(flowDir, start, end geo.XY) geo.XY {
|
|
|
|
var perpDir geo.XY
|
|
|
|
perpDir[0], perpDir[1] = flowDir[1], flowDir[0]
|
|
|
|
return end.Sub(start).Mul(perpDir.Abs()).Unit()
|
2017-11-23 19:19:32 +00:00
|
|
|
}
|
|
|
|
|
2018-06-03 08:26:42 +00:00
|
|
|
func (l line) draw(buf *terminal.Buffer, flowDir, secFlowDir geo.XY) {
|
2018-03-03 17:32:40 +00:00
|
|
|
from, to := *(l.from), *(l.to)
|
2017-11-23 19:19:32 +00:00
|
|
|
|
2018-03-04 14:35:01 +00:00
|
|
|
start := from.rect().Edge(flowDir, secFlowDir)[0].Add(secFlowDir.Scale(l.fromI*2 + 1))
|
|
|
|
end := to.rect().Edge(flowDir.Inv(), secFlowDir)[0].Add(secFlowDir.Scale(l.toI*2 + 1))
|
|
|
|
dirSec := secondaryDir(flowDir, start, end)
|
2017-11-19 21:39:56 +00:00
|
|
|
mid := start.Midpoint(end, rounder)
|
2017-11-04 21:29:15 +00:00
|
|
|
|
|
|
|
along := func(xy, dir geo.XY) int {
|
|
|
|
if dir[0] != 0 {
|
|
|
|
return xy[0]
|
|
|
|
}
|
|
|
|
return xy[1]
|
|
|
|
}
|
|
|
|
|
2018-06-02 04:35:12 +00:00
|
|
|
// collect the points along the line into an array
|
2017-11-04 21:29:15 +00:00
|
|
|
var pts []geo.XY
|
2018-03-03 17:32:40 +00:00
|
|
|
midPrim := along(mid, flowDir)
|
2017-11-04 21:29:15 +00:00
|
|
|
endSec := along(end, dirSec)
|
|
|
|
for curr := start; curr != end; {
|
|
|
|
pts = append(pts, curr)
|
2018-03-03 17:32:40 +00:00
|
|
|
if prim := along(curr, flowDir); prim == midPrim {
|
2017-11-04 21:29:15 +00:00
|
|
|
if sec := along(curr, dirSec); sec != endSec {
|
|
|
|
curr = curr.Add(dirSec)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2018-03-03 17:32:40 +00:00
|
|
|
curr = curr.Add(flowDir)
|
2017-11-04 21:29:15 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 04:35:12 +00:00
|
|
|
// draw each point
|
2017-11-04 21:29:15 +00:00
|
|
|
for i, pt := range pts {
|
2018-06-03 08:26:42 +00:00
|
|
|
var r rune
|
2017-11-04 21:29:15 +00:00
|
|
|
switch {
|
|
|
|
case i == 0:
|
2018-06-06 06:30:39 +00:00
|
|
|
r = terminal.SingleLine.Perpendicular(flowDir)
|
2017-11-04 21:29:15 +00:00
|
|
|
case i == len(pts)-1:
|
2018-06-06 06:30:39 +00:00
|
|
|
r = terminal.SingleLine.Arrow(flowDir)
|
2017-11-04 21:29:15 +00:00
|
|
|
default:
|
|
|
|
prev, next := pts[i-1], pts[i+1]
|
2018-06-03 08:26:42 +00:00
|
|
|
r = terminal.SingleLine.Segment(prev.Sub(pt), next.Sub(pt))
|
2017-11-04 21:29:15 +00:00
|
|
|
}
|
2018-06-03 08:26:42 +00:00
|
|
|
buf.SetPos(pt)
|
|
|
|
buf.WriteRune(r)
|
2017-11-04 21:29:15 +00:00
|
|
|
}
|
2018-06-02 04:35:12 +00:00
|
|
|
|
|
|
|
// draw the body
|
|
|
|
if l.body != "" {
|
|
|
|
bodyPos := mid.Add(geo.Left.Scale(len(l.body) / 2))
|
2018-06-03 08:26:42 +00:00
|
|
|
buf.SetPos(bodyPos)
|
|
|
|
buf.WriteString(l.body)
|
2018-06-02 04:35:12 +00:00
|
|
|
}
|
2017-11-04 21:29:15 +00:00
|
|
|
}
|