direct-draw #6

Merged
sashakoshka merged 10 commits from direct-draw into main 2023-02-01 04:05:26 +00:00
2 changed files with 20 additions and 28 deletions
Showing only changes of commit 537d69b491 - Show all commits

View File

@ -15,9 +15,10 @@ type Element interface {
// instead of the offending dimension(s). // instead of the offending dimension(s).
MinimumSize () (width, height int) MinimumSize () (width, height int)
// Resize resizes the element. This should only be called by the // DrawTo sets this element's canvas. This should only be called by the
// element's parent. // parent element. This is typically a region of the parent element's
Resize (width, height int) // canvas.
DrawTo (canvas Canvas)
// OnDamage sets a function to be called when an area of the element is // OnDamage sets a function to be called when an area of the element is
// drawn on and should be pushed to the screen. // 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 // Core is a struct that implements some core functionality common to most
// widgets. It is meant to be embedded directly into a struct. // widgets. It is meant to be embedded directly into a struct.
type Core struct { type Core struct {
canvas tomo.BasicCanvas canvas tomo.Canvas
parent tomo.Element
metrics struct { metrics struct {
minimumWidth int minimumWidth int
minimumHeight int minimumHeight int
} }
drawSizeChange func ()
onMinimumSizeChange func () onMinimumSizeChange func ()
onDamage func (region tomo.Canvas) onDamage func (region tomo.Canvas)
} }
// NewCore creates a new element core and its corresponding control. // NewCore creates a new element core and its corresponding control.
func NewCore (parent tomo.Element) (core *Core, control CoreControl) { func NewCore (drawSizeChange func ()) (core *Core, control CoreControl) {
core = &Core { parent: parent } core = &Core { drawSizeChange: drawSizeChange }
control = CoreControl { core: core } control = CoreControl { core: core }
return return
} }
@ -33,21 +33,25 @@ func (core *Core) ColorModel () (model color.Model) {
// ColorModel fulfills the draw.Image interface. // ColorModel fulfills the draw.Image interface.
func (core *Core) At (x, y int) (pixel color.Color) { func (core *Core) At (x, y int) (pixel color.Color) {
if core.canvas == nil { return }
return core.canvas.At(x, y) return core.canvas.At(x, y)
} }
// ColorModel fulfills the draw.Image interface. // ColorModel fulfills the draw.Image interface.
func (core *Core) Bounds () (bounds image.Rectangle) { func (core *Core) Bounds () (bounds image.Rectangle) {
if core.canvas == nil { return }
return core.canvas.Bounds() return core.canvas.Bounds()
} }
// ColorModel fulfills the draw.Image interface. // ColorModel fulfills the draw.Image interface.
func (core *Core) Set (x, y int, c color.Color) () { func (core *Core) Set (x, y int, c color.Color) () {
if core.canvas == nil { return }
core.canvas.Set(x, y, c) core.canvas.Set(x, y, c)
} }
// Buffer fulfills the tomo.Canvas interface. // Buffer fulfills the tomo.Canvas interface.
func (core *Core) Buffer () (data []color.RGBA, stride int) { func (core *Core) Buffer () (data []color.RGBA, stride int) {
if core.canvas == nil { return }
return core.canvas.Buffer() return core.canvas.Buffer()
} }
@ -57,6 +61,12 @@ func (core *Core) MinimumSize () (width, height int) {
return core.metrics.minimumWidth, core.metrics.minimumHeight 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 // OnDamage fulfils the tomo.Element interface. This should not need to be
// overridden. // overridden.
func (core *Core) OnDamage (callback func (region tomo.Canvas)) { 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 // HasImage returns true if the core has an allocated image buffer, and false if
// it doesn't. // it doesn't.
func (control CoreControl) HasImage () (has bool) { 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 // 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 // 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 () { func (control CoreControl) DamageAll () {
control.DamageRegion(control.Bounds()) 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 // SetMinimumSize sets the minimum size of this element, notifying the parent
// element in the process. // element in the process.
func (control CoreControl) SetMinimumSize (width, height int) { func (control CoreControl) SetMinimumSize (width, height int) {
@ -119,18 +122,6 @@ func (control CoreControl) SetMinimumSize (width, height int) {
if control.core.onMinimumSizeChange != nil { if control.core.onMinimumSizeChange != nil {
control.core.onMinimumSizeChange() 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 // ConstrainSize contstrains the specified width and height to the minimum width