77 lines
1.9 KiB
Go
77 lines
1.9 KiB
Go
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
|
|
}
|