2023-01-09 19:25:11 -07:00
|
|
|
package core
|
2023-01-08 23:03:19 -07:00
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "image/color"
|
2023-03-30 21:19:04 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo"
|
2023-02-01 23:48:16 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
2023-04-02 20:02:55 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
2023-04-02 20:46:38 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/shatter"
|
2023-01-08 23:03:19 -07:00
|
|
|
|
|
|
|
// Core is a struct that implements some core functionality common to most
|
2023-01-09 11:06:55 -07:00
|
|
|
// widgets. It is meant to be embedded directly into a struct.
|
2023-01-08 23:03:19 -07:00
|
|
|
type Core struct {
|
2023-02-01 23:48:16 -07:00
|
|
|
canvas canvas.Canvas
|
2023-03-11 22:17:35 -07:00
|
|
|
bounds image.Rectangle
|
2023-03-30 21:19:04 -06:00
|
|
|
parent tomo.Parent
|
|
|
|
outer tomo.Element
|
2023-01-08 23:03:19 -07:00
|
|
|
|
|
|
|
metrics struct {
|
|
|
|
minimumWidth int
|
|
|
|
minimumHeight int
|
|
|
|
}
|
2023-01-09 11:06:55 -07:00
|
|
|
|
2023-03-14 16:30:32 -06:00
|
|
|
drawSizeChange func ()
|
|
|
|
onDamage func (region image.Rectangle)
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
2023-03-14 16:30:32 -06:00
|
|
|
// NewCore creates a new element core and its corresponding control given the
|
|
|
|
// element that it will be a part of. If outer is nil, this function will return
|
|
|
|
// nil.
|
2023-02-03 16:07:10 -07:00
|
|
|
func NewCore (
|
2023-03-30 21:19:04 -06:00
|
|
|
outer tomo.Element,
|
2023-02-07 22:22:40 -07:00
|
|
|
drawSizeChange func (),
|
2023-02-03 16:07:10 -07:00
|
|
|
) (
|
|
|
|
core *Core,
|
|
|
|
control CoreControl,
|
|
|
|
) {
|
2023-03-14 16:30:32 -06:00
|
|
|
if outer == nil { return }
|
2023-02-07 09:27:59 -07:00
|
|
|
core = &Core {
|
2023-03-14 16:30:32 -06:00
|
|
|
outer: outer,
|
2023-02-07 22:22:40 -07:00
|
|
|
drawSizeChange: drawSizeChange,
|
2023-02-03 16:28:01 -07:00
|
|
|
}
|
2023-01-09 11:06:55 -07:00
|
|
|
control = CoreControl { core: core }
|
2023-01-08 23:03:19 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-12 23:49:33 -07:00
|
|
|
// Bounds fulfills the tomo.Element interface. This should not need to be
|
|
|
|
// overridden.
|
2023-01-15 21:48:41 -07:00
|
|
|
func (core *Core) Bounds () (bounds image.Rectangle) {
|
2023-01-31 12:29:35 -07:00
|
|
|
if core.canvas == nil { return }
|
2023-03-11 22:17:35 -07:00
|
|
|
return core.bounds
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
2023-01-15 21:58:23 -07:00
|
|
|
// MinimumSize fulfils the tomo.Element interface. This should not need to be
|
|
|
|
// overridden.
|
2023-01-15 21:48:41 -07:00
|
|
|
func (core *Core) MinimumSize () (width, height int) {
|
|
|
|
return core.metrics.minimumWidth, core.metrics.minimumHeight
|
2023-01-11 13:46:48 -07:00
|
|
|
}
|
|
|
|
|
2023-03-14 16:30:32 -06:00
|
|
|
// MinimumSize fulfils the tomo.Element interface. This should not need to be
|
|
|
|
// overridden, unless you want to detect when the element is parented or
|
|
|
|
// unparented.
|
2023-03-30 21:19:04 -06:00
|
|
|
func (core *Core) SetParent (parent tomo.Parent) {
|
2023-03-14 23:46:58 -06:00
|
|
|
if parent != nil && core.parent != nil {
|
2023-03-14 16:30:32 -06:00
|
|
|
panic("core.SetParent: element already has a parent")
|
|
|
|
}
|
|
|
|
|
|
|
|
core.parent = parent
|
|
|
|
}
|
|
|
|
|
2023-01-31 12:29:35 -07:00
|
|
|
// DrawTo fulfills the tomo.Element interface. This should not need to be
|
|
|
|
// overridden.
|
2023-03-14 16:30:32 -06:00
|
|
|
func (core *Core) DrawTo (
|
|
|
|
canvas canvas.Canvas,
|
|
|
|
bounds image.Rectangle,
|
|
|
|
onDamage func (region image.Rectangle),
|
|
|
|
) {
|
|
|
|
core.canvas = canvas
|
|
|
|
core.bounds = bounds
|
|
|
|
core.onDamage = onDamage
|
2023-03-09 20:23:09 -07:00
|
|
|
if core.drawSizeChange != nil && core.canvas != nil {
|
2023-02-07 22:22:40 -07:00
|
|
|
core.drawSizeChange()
|
2023-01-31 14:18:10 -07:00
|
|
|
}
|
2023-01-31 12:29:35 -07:00
|
|
|
}
|
|
|
|
|
2023-01-15 22:02:15 -07: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 11:06:55 -07:00
|
|
|
type CoreControl struct {
|
|
|
|
core *Core
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
2023-02-12 23:49:33 -07: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-03-14 16:30:32 -06:00
|
|
|
// Parent returns the element's parent.
|
2023-03-30 21:19:04 -06:00
|
|
|
func (control CoreControl) Parent () tomo.Parent {
|
2023-03-14 16:30:32 -06:00
|
|
|
return control.core.parent
|
|
|
|
}
|
|
|
|
|
2023-04-02 20:02:55 -06:00
|
|
|
// DrawBackground fills the element's canvas with the parent's background
|
|
|
|
// pattern, if the parent supports it. If it is not supported, the fallback
|
|
|
|
// pattern will be used instead.
|
|
|
|
func (control CoreControl) DrawBackground (fallback artist.Pattern) {
|
|
|
|
control.DrawBackgroundBounds(fallback, control.Bounds())
|
|
|
|
}
|
|
|
|
|
|
|
|
// DrawBackgroundBounds is like DrawBackground, but it takes in a bounding
|
|
|
|
// rectangle instead of using the element's bounds.
|
|
|
|
func (control CoreControl) DrawBackgroundBounds (
|
|
|
|
fallback artist.Pattern,
|
|
|
|
bounds image.Rectangle,
|
|
|
|
) {
|
|
|
|
parent, ok := control.Parent().(tomo.BackgroundParent)
|
|
|
|
if ok {
|
|
|
|
parent.DrawBackground(bounds)
|
|
|
|
} else if fallback != nil {
|
2023-04-02 20:16:12 -06:00
|
|
|
fallback.Draw(canvas.Cut(control, bounds), control.Bounds())
|
2023-04-02 20:02:55 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-02 20:46:38 -06:00
|
|
|
// DrawBackgroundBoundsShatter is like DrawBackgroundBounds, but uses the
|
|
|
|
// shattering algorithm to avoid drawing in areas specified by rocks.
|
|
|
|
func (control CoreControl) DrawBackgroundBoundsShatter (
|
|
|
|
fallback artist.Pattern,
|
|
|
|
bounds image.Rectangle,
|
|
|
|
rocks ...image.Rectangle,
|
|
|
|
) {
|
|
|
|
tiles := shatter.Shatter(bounds, rocks...)
|
|
|
|
for _, tile := range tiles {
|
|
|
|
control.DrawBackgroundBounds(fallback, tile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 01:25:46 -06:00
|
|
|
// Window returns the window containing the element.
|
|
|
|
func (control CoreControl) Window () tomo.Window {
|
|
|
|
parent := control.Parent()
|
|
|
|
if parent == nil {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return parent.Window()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-14 16:30:32 -06:00
|
|
|
// Outer returns the outer element given when the control was constructed.
|
2023-03-30 21:19:04 -06:00
|
|
|
func (control CoreControl) Outer () tomo.Element {
|
2023-03-14 16:30:32 -06:00
|
|
|
return control.core.outer
|
|
|
|
}
|
|
|
|
|
2023-01-15 21:58:23 -07: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 14:18:10 -07:00
|
|
|
return control.core.canvas != nil && !control.core.canvas.Bounds().Empty()
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
2023-01-19 12:44:40 -07:00
|
|
|
// DamageRegion pushes the selected region of pixels to the parent element. This
|
2023-01-15 21:58:23 -07:00
|
|
|
// does not need to be called when responding to a resize event.
|
2023-02-16 20:41:07 -07:00
|
|
|
func (control CoreControl) DamageRegion (regions ...image.Rectangle) {
|
2023-02-14 00:14:52 -07:00
|
|
|
if control.core.canvas == nil { return }
|
2023-01-19 12:44:40 -07:00
|
|
|
if control.core.onDamage != nil {
|
2023-02-16 20:41:07 -07:00
|
|
|
for _, region := range regions {
|
2023-03-14 16:30:32 -06:00
|
|
|
control.core.onDamage(region)
|
2023-02-16 20:41:07 -07:00
|
|
|
}
|
2023-01-19 12:44:40 -07:00
|
|
|
}
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
2023-01-19 12:44:40 -07:00
|
|
|
// DamageAll pushes all pixels to the parent element. This does not need to be
|
2023-01-31 12:29:35 -07:00
|
|
|
// called when redrawing in response to a change in size.
|
2023-01-19 12:44:40 -07:00
|
|
|
func (control CoreControl) DamageAll () {
|
2023-01-31 14:18:10 -07:00
|
|
|
control.DamageRegion(control.core.Bounds())
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
2023-01-15 21:58:23 -07:00
|
|
|
// SetMinimumSize sets the minimum size of this element, notifying the parent
|
|
|
|
// element in the process.
|
2023-01-09 11:06:55 -07:00
|
|
|
func (control CoreControl) SetMinimumSize (width, height int) {
|
|
|
|
core := control.core
|
2023-01-11 13:46:48 -07:00
|
|
|
if width == core.metrics.minimumWidth &&
|
|
|
|
height == core.metrics.minimumHeight {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
core.metrics.minimumWidth = width
|
|
|
|
core.metrics.minimumHeight = height
|
2023-03-14 16:30:32 -06:00
|
|
|
if control.core.parent != nil {
|
|
|
|
control.core.parent.NotifyMinimumSizeChange(control.core.outer)
|
2023-01-19 12:44:40 -07:00
|
|
|
}
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
2023-01-15 21:58:23 -07: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 11:06:55 -07:00
|
|
|
func (control CoreControl) ConstrainSize (
|
2023-01-08 23:03:19 -07:00
|
|
|
inWidth, inHeight int,
|
|
|
|
) (
|
|
|
|
outWidth, outHeight int,
|
|
|
|
constrained bool,
|
|
|
|
) {
|
2023-01-09 11:06:55 -07:00
|
|
|
core := control.core
|
2023-01-08 23:03:19 -07: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
|
|
|
|
}
|