Removed Resize, added DrawTo

This commit is contained in:
Sasha Koshka 2023-01-31 14:29:35 -05:00
parent 0c5cc6ff74
commit 537d69b491
2 changed files with 20 additions and 28 deletions

View File

@ -15,9 +15,10 @@ type Element interface {
// instead of the offending dimension(s).
MinimumSize () (width, height int)
// Resize resizes the element. This should only be called by the
// element's parent.
Resize (width, height int)
// DrawTo sets this element's canvas. This should only be called by the
// parent element. This is typically a region of the parent element's
// canvas.
DrawTo (canvas Canvas)
// OnDamage sets a function to be called when an area of the element is
// drawn on and should be pushed to the screen.

View File

@ -7,21 +7,21 @@ import "git.tebibyte.media/sashakoshka/tomo"
// Core is a struct that implements some core functionality common to most
// widgets. It is meant to be embedded directly into a struct.
type Core struct {
canvas tomo.BasicCanvas
parent tomo.Element
canvas tomo.Canvas
metrics struct {
minimumWidth int
minimumHeight int
}
drawSizeChange func ()
onMinimumSizeChange func ()
onDamage func (region tomo.Canvas)
}
// NewCore creates a new element core and its corresponding control.
func NewCore (parent tomo.Element) (core *Core, control CoreControl) {
core = &Core { parent: parent }
func NewCore (drawSizeChange func ()) (core *Core, control CoreControl) {
core = &Core { drawSizeChange: drawSizeChange }
control = CoreControl { core: core }
return
}
@ -33,21 +33,25 @@ func (core *Core) ColorModel () (model color.Model) {
// ColorModel fulfills the draw.Image interface.
func (core *Core) At (x, y int) (pixel color.Color) {
if core.canvas == nil { return }
return core.canvas.At(x, y)
}
// ColorModel fulfills the draw.Image interface.
func (core *Core) Bounds () (bounds image.Rectangle) {
if core.canvas == nil { return }
return core.canvas.Bounds()
}
// ColorModel fulfills the draw.Image interface.
func (core *Core) Set (x, y int, c color.Color) () {
if core.canvas == nil { return }
core.canvas.Set(x, y, c)
}
// Buffer fulfills the tomo.Canvas interface.
func (core *Core) Buffer () (data []color.RGBA, stride int) {
if core.canvas == nil { return }
return core.canvas.Buffer()
}
@ -57,6 +61,12 @@ func (core *Core) MinimumSize () (width, height int) {
return core.metrics.minimumWidth, core.metrics.minimumHeight
}
// DrawTo fulfills the tomo.Element interface. This should not need to be
// overridden.
func (core *Core) DrawTo (canvas tomo.Canvas) {
core.canvas = canvas
}
// OnDamage fulfils the tomo.Element interface. This should not need to be
// overridden.
func (core *Core) OnDamage (callback func (region tomo.Canvas)) {
@ -81,7 +91,7 @@ type CoreControl struct {
// HasImage returns true if the core has an allocated image buffer, and false if
// it doesn't.
func (control CoreControl) HasImage () (has bool) {
return !control.Bounds().Empty()
return control.core.canvas != nil
}
// DamageRegion pushes the selected region of pixels to the parent element. This
@ -93,18 +103,11 @@ func (control CoreControl) DamageRegion (bounds image.Rectangle) {
}
// DamageAll pushes all pixels to the parent element. This does not need to be
// called when responding to a resize event.
// called when redrawing in response to a change in size.
func (control CoreControl) DamageAll () {
control.DamageRegion(control.Bounds())
}
// AllocateCanvas resizes the canvas, constraining the width and height so that
// they are not less than the specified minimum width and height.
func (control *CoreControl) AllocateCanvas (width, height int) {
control.core.canvas = tomo.NewBasicCanvas(width, height)
control.BasicCanvas = control.core.canvas
}
// SetMinimumSize sets the minimum size of this element, notifying the parent
// element in the process.
func (control CoreControl) SetMinimumSize (width, height int) {
@ -119,18 +122,6 @@ func (control CoreControl) SetMinimumSize (width, height int) {
if control.core.onMinimumSizeChange != nil {
control.core.onMinimumSizeChange()
}
// if there is an image buffer, and the current size is less
// than this new minimum size, send core.parent a resize event.
if control.HasImage() {
bounds := control.Bounds()
imageWidth,
imageHeight,
constrained := control.ConstrainSize(bounds.Dx(), bounds.Dy())
if constrained {
core.parent.Resize(imageWidth, imageHeight)
}
}
}
// ConstrainSize contstrains the specified width and height to the minimum width