You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
960 B
Go
44 lines
960 B
Go
4 years ago
|
package app
|
||
|
|
||
|
import (
|
||
|
"github.com/gdamore/tcell/v2"
|
||
|
"github.com/jordanorelli/astro-domu/internal/math"
|
||
|
)
|
||
|
|
||
|
type canvas interface {
|
||
|
getTile(x, y int) tile
|
||
|
setTile(int, int, tile)
|
||
|
bounds() math.Rect
|
||
|
}
|
||
|
|
||
|
func writeString(img canvas, s string, start math.Vec, style tcell.Style) {
|
||
|
for i, r := range []rune(s) {
|
||
|
img.setTile(start.X+i, start.Y, tile{r: r, style: style})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func cut(img canvas, bounds math.Rect) canvas {
|
||
|
return §ion{img: img, frame: bounds}
|
||
|
}
|
||
|
|
||
|
type section struct {
|
||
|
img canvas
|
||
|
frame math.Rect
|
||
|
}
|
||
|
|
||
|
func (s *section) getTile(x, y int) tile {
|
||
|
if x < 0 || x >= s.frame.Width || y < 0 || y >= s.frame.Height {
|
||
|
return tile{}
|
||
|
}
|
||
|
return s.img.getTile(x+s.frame.Origin.X, y+s.frame.Origin.Y)
|
||
|
}
|
||
|
|
||
|
func (s *section) setTile(x, y int, t tile) {
|
||
|
if x < 0 || x >= s.frame.Width || y < 0 || y >= s.frame.Height {
|
||
|
return
|
||
|
}
|
||
|
s.img.setTile(x+s.frame.Origin.X, y+s.frame.Origin.Y, t)
|
||
|
}
|
||
|
|
||
|
func (s *section) bounds() math.Rect { return s.frame }
|