Files
beatglow/app/widget/object.go
T
2026-10-29 17:29:41 +01:00

71 lines
1.6 KiB
Go

package widget
import (
"image"
"github.com/BeatGlow/beatglow/app"
"github.com/BeatGlow/display/draw"
)
type Object struct {
active bool
width int
height int
pos image.Point
style app.ObjectStyle
canvas draw.Image
changed bool
}
func MakeObject(ctx app.Context, position image.Point, width, height int, style app.ObjectStyle) Object {
return Object{
width: width,
height: height,
pos: position,
style: style,
canvas: app.BufferFor(ctx, width, height),
changed: true,
}
}
func (w *Object) Bounds() image.Rectangle {
return image.Rect(0, 0, w.width, w.height)
}
func (w *Object) Position() image.Point {
return w.pos
}
func (w *Object) IsActive() bool {
return w.active
}
func (w *Object) Render() draw.Image {
if w.changed {
// Clear entire buffer.
app.ImageClear(w.canvas)
var (
bounds = w.canvas.Bounds()
outer = w.style.Outer(w.canvas)
)
// Background draws into outer region.
if w.style.Background != nil {
app.ImageCopy(outer, w.style.Background)
} else {
app.ImageFill(outer, w.style.BackgroundColor)
}
// Draw border
draw.Box(w.canvas, image.Rect(0, 0, bounds.Max.X, w.style.Border.Top), w.style.BorderColor)
draw.Box(w.canvas, image.Rect(0, bounds.Max.Y-w.style.Border.Bottom, bounds.Max.X, bounds.Max.Y), w.style.BorderColor)
draw.Box(w.canvas, image.Rect(0, 0, w.style.Border.Left, bounds.Max.Y), w.style.BorderColor)
draw.Box(w.canvas, image.Rect(bounds.Max.X-w.style.Border.Right, 0, bounds.Max.X, bounds.Max.Y), w.style.BorderColor)
// After redrawing mark the object as unchanged.
w.changed = false
}
return w.canvas
}