This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/elements/core/core.go

149 lines
3.6 KiB
Go
Raw Normal View History

2023-01-09 19:25:11 -07:00
package core
2023-01-08 23:03:19 -07:00
import "image"
import "image/color"
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.
2023-01-08 23:03:19 -07:00
type Core struct {
canvas *image.RGBA
2023-01-08 23:03:19 -07:00
parent tomo.Element
metrics struct {
minimumWidth int
minimumHeight int
}
2023-01-10 09:51:46 -07:00
selectable bool
hooks tomo.ParentHooks
2023-01-08 23:03:19 -07:00
}
// NewCore creates a new element core and its corresponding control.
func NewCore (parent tomo.Element) (core *Core, control CoreControl) {
core = &Core { parent: parent }
control = CoreControl { core: core }
2023-01-08 23:03:19 -07:00
return
}
func (core Core) ColorModel () (model color.Model) {
return color.RGBAModel
}
func (core Core) At (x, y int) (pixel color.Color) {
if core.canvas == nil { return color.RGBA { } }
pixel = core.canvas.At(x, y)
2023-01-08 23:03:19 -07:00
return
}
func (core Core) RGBAAt (x, y int) (pixel color.RGBA) {
if core.canvas == nil { return color.RGBA { } }
pixel = core.canvas.RGBAAt(x, y)
2023-01-08 23:03:19 -07:00
return
}
func (core Core) Bounds () (bounds image.Rectangle) {
if core.canvas != nil { bounds = core.canvas.Bounds() }
2023-01-08 23:03:19 -07:00
return
}
func (core *Core) SetParentHooks (hooks tomo.ParentHooks) {
core.hooks = hooks
2023-01-08 23:03:19 -07:00
}
2023-01-10 09:51:46 -07:00
func (core Core) Selectable () (selectable bool) {
return core.selectable
}
func (core Core) MinimumSize () (width, height int) {
return core.metrics.minimumWidth, core.metrics.minimumHeight
2023-01-08 23:03:19 -07:00
}
// CoreControl is a struct that can exert control over a control struct. It can
// be used as a canvas. It must not be directly embedded into an element, but
// instead kept as a private member.
type CoreControl struct {
*image.RGBA
core *Core
2023-01-08 23:03:19 -07:00
}
func (control CoreControl) HasImage () (has bool) {
has = control.RGBA != nil
return
2023-01-08 23:03:19 -07:00
}
2023-01-09 13:14:36 -07:00
func (control CoreControl) Select () {
control.core.hooks.RunSelectionRequest()
}
func (control CoreControl) PushRegion (bounds image.Rectangle) {
2023-01-09 13:14:36 -07:00
control.core.hooks.RunDraw(control.SubImage(bounds).(*image.RGBA))
2023-01-08 23:03:19 -07:00
}
func (control CoreControl) PushAll () {
control.PushRegion(control.Bounds())
2023-01-08 23:03:19 -07:00
}
2023-01-09 13:14:36 -07:00
func (control *CoreControl) AllocateCanvas (width, height int) {
core := control.core
width, height, _ = control.ConstrainSize(width, height)
core.canvas = image.NewRGBA(image.Rect (0, 0, width, height))
control.RGBA = core.canvas
2023-01-08 23:03:19 -07:00
}
2023-01-10 09:51:46 -07:00
func (control CoreControl) SetSelectable (selectable bool) {
changed := control.core.selectable != selectable
control.core.selectable = selectable
if changed {
control.core.hooks.RunSelectabilityChange(selectable)
}
}
func (control CoreControl) SetMinimumSize (width, height int) {
core := control.core
2023-01-08 23:03:19 -07:00
if width != core.metrics.minimumWidth ||
height != core.metrics.minimumHeight {
core.metrics.minimumWidth = width
core.metrics.minimumHeight = height
core.hooks.RunMinimumSizeChange(width, height)
2023-01-08 23:03:19 -07:00
// 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()
2023-01-08 23:03:19 -07:00
imageWidth,
imageHeight,
constrained := control.ConstrainSize (
2023-01-08 23:03:19 -07:00
bounds.Dx(),
bounds.Dy())
if constrained {
core.parent.Handle (tomo.EventResize {
Width: imageWidth,
Height: imageHeight,
})
}
}
}
}
func (control CoreControl) ConstrainSize (
2023-01-08 23:03:19 -07:00
inWidth, inHeight int,
) (
outWidth, outHeight int,
constrained bool,
) {
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
}