2023-01-10 02:25:11 +00:00
|
|
|
package core
|
2023-01-09 06:03:19 +00:00
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "image/color"
|
2023-02-02 06:48:16 +00:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
2023-01-09 06:03:19 +00:00
|
|
|
|
|
|
|
// Core is a struct that implements some core functionality common to most
|
2023-01-09 18:06:55 +00:00
|
|
|
// widgets. It is meant to be embedded directly into a struct.
|
2023-01-09 06:03:19 +00:00
|
|
|
type Core struct {
|
2023-02-02 06:48:16 +00:00
|
|
|
canvas canvas.Canvas
|
2023-01-09 06:03:19 +00:00
|
|
|
|
|
|
|
metrics struct {
|
|
|
|
minimumWidth int
|
|
|
|
minimumHeight int
|
|
|
|
}
|
2023-01-09 18:06:55 +00:00
|
|
|
|
2023-02-08 05:22:40 +00:00
|
|
|
drawSizeChange func ()
|
2023-01-19 19:44:40 +00:00
|
|
|
onMinimumSizeChange func ()
|
2023-02-02 06:48:16 +00:00
|
|
|
onDamage func (region canvas.Canvas)
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 18:06:55 +00:00
|
|
|
// NewCore creates a new element core and its corresponding control.
|
2023-02-03 23:07:10 +00:00
|
|
|
func NewCore (
|
2023-02-08 05:22:40 +00:00
|
|
|
drawSizeChange func (),
|
2023-02-03 23:07:10 +00:00
|
|
|
) (
|
|
|
|
core *Core,
|
|
|
|
control CoreControl,
|
|
|
|
) {
|
2023-02-07 16:27:59 +00:00
|
|
|
core = &Core {
|
2023-02-08 05:22:40 +00:00
|
|
|
drawSizeChange: drawSizeChange,
|
2023-02-03 23:28:01 +00:00
|
|
|
}
|
2023-01-09 18:06:55 +00:00
|
|
|
control = CoreControl { core: core }
|
2023-01-09 06:03:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-13 06:49:33 +00:00
|
|
|
// Bounds fulfills the tomo.Element interface. This should not need to be
|
|
|
|
// overridden.
|
2023-01-16 04:48:41 +00:00
|
|
|
func (core *Core) Bounds () (bounds image.Rectangle) {
|
2023-01-31 19:29:35 +00:00
|
|
|
if core.canvas == nil { return }
|
2023-01-14 06:54:57 +00:00
|
|
|
return core.canvas.Bounds()
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 04:58:23 +00:00
|
|
|
// MinimumSize fulfils the tomo.Element interface. This should not need to be
|
|
|
|
// overridden.
|
2023-01-16 04:48:41 +00:00
|
|
|
func (core *Core) MinimumSize () (width, height int) {
|
|
|
|
return core.metrics.minimumWidth, core.metrics.minimumHeight
|
2023-01-11 20:46:48 +00:00
|
|
|
}
|
|
|
|
|
2023-01-31 19:29:35 +00:00
|
|
|
// DrawTo fulfills the tomo.Element interface. This should not need to be
|
|
|
|
// overridden.
|
2023-02-02 06:48:16 +00:00
|
|
|
func (core *Core) DrawTo (canvas canvas.Canvas) {
|
2023-01-31 19:29:35 +00:00
|
|
|
core.canvas = canvas
|
2023-02-08 05:22:40 +00:00
|
|
|
if core.drawSizeChange != nil {
|
|
|
|
core.drawSizeChange()
|
2023-01-31 21:18:10 +00:00
|
|
|
}
|
2023-01-31 19:29:35 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 19:44:40 +00:00
|
|
|
// OnDamage fulfils the tomo.Element interface. This should not need to be
|
2023-01-16 04:58:23 +00:00
|
|
|
// overridden.
|
2023-02-02 06:48:16 +00:00
|
|
|
func (core *Core) OnDamage (callback func (region canvas.Canvas)) {
|
2023-01-19 19:44:40 +00:00
|
|
|
core.onDamage = callback
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnMinimumSizeChange fulfils the tomo.Element interface. This should not need
|
|
|
|
// to be overridden.
|
|
|
|
func (core *Core) OnMinimumSizeChange (callback func ()) {
|
|
|
|
core.onMinimumSizeChange = callback
|
2023-01-11 20:46:48 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 05:02:15 +00:00
|
|
|
// CoreControl is a struct that can exert control over a Core struct. It can be
|
|
|
|
// used as a canvas. It must not be directly embedded into an element, but
|
|
|
|
// instead kept as a private member. When a Core struct is created, a
|
|
|
|
// corresponding CoreControl struct is linked to it and returned alongside it.
|
2023-01-09 18:06:55 +00:00
|
|
|
type CoreControl struct {
|
|
|
|
core *Core
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 06:49:33 +00:00
|
|
|
// ColorModel fulfills the draw.Image interface.
|
|
|
|
func (control CoreControl) ColorModel () (model color.Model) {
|
|
|
|
return color.RGBAModel
|
|
|
|
}
|
|
|
|
|
|
|
|
// At fulfills the draw.Image interface.
|
|
|
|
func (control CoreControl) At (x, y int) (pixel color.Color) {
|
|
|
|
if control.core.canvas == nil { return }
|
|
|
|
return control.core.canvas.At(x, y)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bounds fulfills the draw.Image interface.
|
|
|
|
func (control CoreControl) Bounds () (bounds image.Rectangle) {
|
|
|
|
if control.core.canvas == nil { return }
|
|
|
|
return control.core.canvas.Bounds()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set fulfills the draw.Image interface.
|
|
|
|
func (control CoreControl) Set (x, y int, c color.Color) () {
|
|
|
|
if control.core.canvas == nil { return }
|
|
|
|
control.core.canvas.Set(x, y, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Buffer fulfills the canvas.Canvas interface.
|
|
|
|
func (control CoreControl) Buffer () (data []color.RGBA, stride int) {
|
|
|
|
if control.core.canvas == nil { return }
|
|
|
|
return control.core.canvas.Buffer()
|
|
|
|
}
|
|
|
|
|
2023-01-16 04:58:23 +00:00
|
|
|
// HasImage returns true if the core has an allocated image buffer, and false if
|
|
|
|
// it doesn't.
|
|
|
|
func (control CoreControl) HasImage () (has bool) {
|
2023-01-31 21:18:10 +00:00
|
|
|
return control.core.canvas != nil && !control.core.canvas.Bounds().Empty()
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 19:44:40 +00:00
|
|
|
// DamageRegion pushes the selected region of pixels to the parent element. This
|
2023-01-16 04:58:23 +00:00
|
|
|
// does not need to be called when responding to a resize event.
|
2023-01-19 19:44:40 +00:00
|
|
|
func (control CoreControl) DamageRegion (bounds image.Rectangle) {
|
|
|
|
if control.core.onDamage != nil {
|
2023-02-13 06:49:33 +00:00
|
|
|
control.core.onDamage(canvas.Cut(control.core.canvas, bounds))
|
2023-01-19 19:44:40 +00:00
|
|
|
}
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 19:44:40 +00:00
|
|
|
// DamageAll pushes all pixels to the parent element. This does not need to be
|
2023-01-31 19:29:35 +00:00
|
|
|
// called when redrawing in response to a change in size.
|
2023-01-19 19:44:40 +00:00
|
|
|
func (control CoreControl) DamageAll () {
|
2023-01-31 21:18:10 +00:00
|
|
|
control.DamageRegion(control.core.Bounds())
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 04:58:23 +00:00
|
|
|
// SetMinimumSize sets the minimum size of this element, notifying the parent
|
|
|
|
// element in the process.
|
2023-01-09 18:06:55 +00:00
|
|
|
func (control CoreControl) SetMinimumSize (width, height int) {
|
|
|
|
core := control.core
|
2023-01-11 20:46:48 +00:00
|
|
|
if width == core.metrics.minimumWidth &&
|
|
|
|
height == core.metrics.minimumHeight {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
core.metrics.minimumWidth = width
|
|
|
|
core.metrics.minimumHeight = height
|
2023-01-19 19:44:40 +00:00
|
|
|
if control.core.onMinimumSizeChange != nil {
|
|
|
|
control.core.onMinimumSizeChange()
|
|
|
|
}
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 04:58:23 +00:00
|
|
|
// ConstrainSize contstrains the specified width and height to the minimum width
|
|
|
|
// and height, and returns wether or not anything ended up being constrained.
|
2023-01-09 18:06:55 +00:00
|
|
|
func (control CoreControl) ConstrainSize (
|
2023-01-09 06:03:19 +00:00
|
|
|
inWidth, inHeight int,
|
|
|
|
) (
|
|
|
|
outWidth, outHeight int,
|
|
|
|
constrained bool,
|
|
|
|
) {
|
2023-01-09 18:06:55 +00:00
|
|
|
core := control.core
|
2023-01-09 06:03:19 +00:00
|
|
|
outWidth = inWidth
|
|
|
|
outHeight = inHeight
|
|
|
|
if outWidth < core.metrics.minimumWidth {
|
|
|
|
outWidth = core.metrics.minimumWidth
|
|
|
|
constrained = true
|
|
|
|
}
|
|
|
|
if outHeight < core.metrics.minimumHeight {
|
|
|
|
outHeight = core.metrics.minimumHeight
|
|
|
|
constrained = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|