starting to get a UI system working
parent
30e47e6f7f
commit
19a753cf99
@ -0,0 +1,43 @@
|
||||
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 }
|
@ -0,0 +1,34 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/jordanorelli/astro-domu/internal/math"
|
||||
)
|
||||
|
||||
type containerView struct {
|
||||
children []*node
|
||||
focussed int
|
||||
}
|
||||
|
||||
func (c *containerView) handleEvent(e tcell.Event) change {
|
||||
switch e.(type) {
|
||||
case *tcell.EventKey:
|
||||
return c.children[c.focussed].handleEvent(e)
|
||||
|
||||
default:
|
||||
// ui.Debug("screen saw unhandled event of type %T", e)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *containerView) draw(img canvas, st *state) {
|
||||
for _, n := range c.children {
|
||||
n.draw(cut(img, n.frame), st)
|
||||
}
|
||||
}
|
||||
|
||||
type node struct {
|
||||
view
|
||||
frame math.Rect
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
package app
|
||||
|
||||
type node struct {
|
||||
view
|
||||
children []*node
|
||||
}
|
Loading…
Reference in New Issue