Checkpoint
This commit is contained in:
125 files changed
+59376
No files matched your search
@@ -0,0 +1,147 @@
|
||||
package virtual
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"strings"
|
||||
|
||||
"git.maze.io/go/harmon/config"
|
||||
"git.maze.io/go/harmon/display"
|
||||
"git.maze.io/go/harmon/internal/pixel"
|
||||
)
|
||||
|
||||
func init() {
|
||||
for _, name := range []string{
|
||||
"row",
|
||||
//"append",
|
||||
//"horizontal",
|
||||
"col",
|
||||
"column",
|
||||
//"stack",
|
||||
//"vertical",
|
||||
} {
|
||||
display.Register(name, newRowOrColumn)
|
||||
}
|
||||
}
|
||||
|
||||
func newRowOrColumn(config *config.Display) (display.Display, error) {
|
||||
if config.Address == "" {
|
||||
return nil, fmt.Errorf("virtual display %q missing required addr", config.Name)
|
||||
}
|
||||
|
||||
var (
|
||||
displays []display.Display
|
||||
err error
|
||||
)
|
||||
for i, name := range strings.Split(config.Address, ",") {
|
||||
var d display.Display
|
||||
if d, err = display.Lookup(name); err != nil {
|
||||
return nil, fmt.Errorf("virtual display %q error loading %q: %w", config.Name, name, err)
|
||||
}
|
||||
displays = append(displays, d)
|
||||
|
||||
if i > 1 && !displays[i].Size().Eq(displays[i-1].Size()) {
|
||||
return nil, fmt.Errorf("virtual display %q requires displays with the same dimensions")
|
||||
}
|
||||
}
|
||||
if len(displays) == 1 {
|
||||
return displays[0], nil
|
||||
}
|
||||
|
||||
switch config.Type {
|
||||
case "row", "append", "horizontal":
|
||||
// row of displays
|
||||
return newList(displays, false), nil
|
||||
case "col", "column", "stack", "vertical":
|
||||
// column of displays
|
||||
return newList(displays, true), nil
|
||||
default:
|
||||
panic("unreachable")
|
||||
}
|
||||
}
|
||||
|
||||
// list is a virtual row or column of one or more displays.
|
||||
type list struct {
|
||||
displays []display.Display
|
||||
size image.Point // size of one display
|
||||
rect image.Rectangle // rect is the union of all displays
|
||||
vertical bool
|
||||
}
|
||||
|
||||
func newList(displays []display.Display, vertical bool) display.Display {
|
||||
var (
|
||||
rect image.Rectangle
|
||||
size image.Point
|
||||
)
|
||||
for i, display := range displays {
|
||||
if i == 0 {
|
||||
size = display.Size()
|
||||
rect = image.Rectangle{Max: size}
|
||||
} else if vertical {
|
||||
rect.Max.Y += size.Y
|
||||
} else {
|
||||
rect.Max.X += size.X
|
||||
}
|
||||
}
|
||||
return &list{
|
||||
displays: displays,
|
||||
size: size,
|
||||
rect: rect,
|
||||
vertical: vertical,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *list) String() string {
|
||||
if d.vertical {
|
||||
return "column"
|
||||
}
|
||||
return "row"
|
||||
}
|
||||
|
||||
func (d *list) ColorModel() color.Model { return d.displays[0].ColorModel() }
|
||||
func (d *list) Bounds() image.Rectangle { return d.rect }
|
||||
func (d *list) Halt() error { return nil }
|
||||
|
||||
func (d *list) displayAt(x, y int) (int, int, display.Display) {
|
||||
for _, display := range d.displays {
|
||||
if image.Pt(x, y).In(display.Bounds()) {
|
||||
return x, y, display
|
||||
}
|
||||
if d.vertical {
|
||||
y -= d.size.Y
|
||||
} else {
|
||||
x -= d.size.X
|
||||
}
|
||||
}
|
||||
return -1, -1, nil
|
||||
}
|
||||
|
||||
func (d *list) At(x, y int) color.Color {
|
||||
var display display.Display
|
||||
if x, y, display = d.displayAt(x, y); display != nil {
|
||||
return display.At(x, y)
|
||||
}
|
||||
return pixel.Off
|
||||
}
|
||||
|
||||
func (d *list) Set(x, y int, c color.Color) {
|
||||
var display display.Display
|
||||
if x, y, display = d.displayAt(x, y); display != nil {
|
||||
display.Set(x, y, c)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (d *list) Refresh() error {
|
||||
for _, display := range d.displays {
|
||||
if err := display.Refresh(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *list) Size() image.Point {
|
||||
return d.rect.Size()
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package virtual
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
|
||||
"git.maze.io/go/harmon/config"
|
||||
"git.maze.io/go/harmon/display"
|
||||
"git.maze.io/go/harmon/internal/pixel"
|
||||
)
|
||||
|
||||
func init() {
|
||||
display.Register("split", NewSplit)
|
||||
}
|
||||
|
||||
// Split is a view on another display.
|
||||
type Split struct {
|
||||
display display.Display
|
||||
bbox image.Rectangle // b are the normalized bounds
|
||||
rect image.Rectangle // r are the absolute bounds in d
|
||||
}
|
||||
|
||||
func NewSplit(config *config.Display) (display.Display, error) {
|
||||
if config.Address == "" {
|
||||
return nil, fmt.Errorf("virtual display %q missing required addr", config.Name)
|
||||
}
|
||||
|
||||
r, err := parseRect(config.Port)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("virtual display %q invalid port %q: %w", config.Name, config.Port, err)
|
||||
}
|
||||
|
||||
d, err := display.Lookup(config.Address)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("virtual display %q error loading %q: %w", config.Name, config.Address, err)
|
||||
}
|
||||
|
||||
return &Split{
|
||||
display: d,
|
||||
bbox: r.Sub(r.Min),
|
||||
rect: r,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (d *Split) String() string { return "split:" + d.display.String() }
|
||||
|
||||
func (d *Split) At(x, y int) color.Color {
|
||||
p := image.Pt(x, y).Add(d.rect.Min)
|
||||
if !p.In(d.rect) {
|
||||
return pixel.Off
|
||||
}
|
||||
return d.display.At(p.X, p.Y)
|
||||
}
|
||||
|
||||
func (d *Split) Set(x, y int, c color.Color) {
|
||||
p := image.Pt(x, y).Add(d.rect.Min)
|
||||
if !p.In(d.rect) {
|
||||
return
|
||||
}
|
||||
d.display.Set(p.X, p.Y, c)
|
||||
}
|
||||
|
||||
func (d *Split) Bounds() image.Rectangle { return d.bbox }
|
||||
func (d *Split) ColorModel() color.Model { return d.display.ColorModel() }
|
||||
func (d *Split) Halt() error { return nil }
|
||||
func (d *Split) Refresh() error { return d.display.Refresh() }
|
||||
func (d *Split) Size() image.Point { return d.rect.Size() }
|
||||
|
||||
func parseRect(s string) (image.Rectangle, error) {
|
||||
var r image.Rectangle
|
||||
if _, err := fmt.Sscanf(s, "%d,%d,%d,%d", &r.Min.X, &r.Min.Y, &r.Max.X, &r.Max.Y); err != nil {
|
||||
return r, err
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
Reference in new issue
Block a user