2023-01-10 02:25:11 +00:00
|
|
|
package core
|
2023-01-09 06:03:19 +00:00
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "image/color"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo"
|
|
|
|
|
|
|
|
// 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-01-14 06:54:57 +00:00
|
|
|
canvas tomo.BasicCanvas
|
2023-01-09 06:03:19 +00:00
|
|
|
parent tomo.Element
|
|
|
|
|
|
|
|
metrics struct {
|
|
|
|
minimumWidth int
|
|
|
|
minimumHeight int
|
|
|
|
}
|
2023-01-09 18:06:55 +00:00
|
|
|
|
2023-01-10 16:51:46 +00:00
|
|
|
selectable bool
|
2023-01-11 20:46:48 +00:00
|
|
|
selected bool
|
2023-01-09 18:06:55 +00:00
|
|
|
hooks tomo.ParentHooks
|
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.
|
|
|
|
func NewCore (parent tomo.Element) (core *Core, control CoreControl) {
|
|
|
|
core = &Core { parent: parent }
|
|
|
|
control = CoreControl { core: core }
|
2023-01-09 06:03:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (core Core) ColorModel () (model color.Model) {
|
|
|
|
return color.RGBAModel
|
|
|
|
}
|
|
|
|
|
|
|
|
func (core Core) At (x, y int) (pixel color.Color) {
|
2023-01-14 06:54:57 +00:00
|
|
|
return core.canvas.At(x, y)
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-14 06:54:57 +00:00
|
|
|
func (core Core) Bounds () (bounds image.Rectangle) {
|
|
|
|
return core.canvas.Bounds()
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-14 06:54:57 +00:00
|
|
|
func (core Core) Set (x, y int, c color.Color) () {
|
|
|
|
core.canvas.Set(x, y, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (core Core) Buffer () (data []color.RGBA, stride int) {
|
|
|
|
return core.canvas.Buffer()
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 16:51:46 +00:00
|
|
|
func (core Core) Selectable () (selectable bool) {
|
|
|
|
return core.selectable
|
|
|
|
}
|
|
|
|
|
2023-01-11 20:46:48 +00:00
|
|
|
func (core Core) Selected () (selected bool) {
|
|
|
|
return core.selected
|
|
|
|
}
|
|
|
|
|
|
|
|
func (core Core) AdvanceSelection (direction int) (ok bool) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (core *Core) SetParentHooks (hooks tomo.ParentHooks) {
|
|
|
|
core.hooks = hooks
|
|
|
|
}
|
|
|
|
|
2023-01-09 18:06:55 +00:00
|
|
|
func (core Core) MinimumSize () (width, height int) {
|
|
|
|
return core.metrics.minimumWidth, core.metrics.minimumHeight
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 18:06:55 +00: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 {
|
2023-01-14 06:54:57 +00:00
|
|
|
tomo.BasicCanvas
|
2023-01-09 18:06:55 +00:00
|
|
|
core *Core
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-14 06:54:57 +00:00
|
|
|
func (control CoreControl) HasImage () (empty bool) {
|
|
|
|
return !control.Bounds().Empty()
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 03:30:14 +00:00
|
|
|
func (control CoreControl) Select () (granted bool) {
|
|
|
|
return control.core.hooks.RunSelectionRequest()
|
2023-01-09 20:14:36 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 20:46:48 +00:00
|
|
|
func (control CoreControl) SetSelected (selected bool) {
|
|
|
|
if !control.core.selectable { return }
|
|
|
|
control.core.selected = selected
|
|
|
|
}
|
|
|
|
|
|
|
|
func (control CoreControl) SetSelectable (selectable bool) {
|
|
|
|
if control.core.selectable == selectable { return }
|
|
|
|
control.core.selectable = selectable
|
|
|
|
if !selectable { control.core.selected = false }
|
|
|
|
control.core.hooks.RunSelectabilityChange(selectable)
|
|
|
|
}
|
|
|
|
|
2023-01-09 18:06:55 +00:00
|
|
|
func (control CoreControl) PushRegion (bounds image.Rectangle) {
|
2023-01-14 06:54:57 +00:00
|
|
|
control.core.hooks.RunDraw(tomo.Cut(control, bounds))
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 18:06:55 +00:00
|
|
|
func (control CoreControl) PushAll () {
|
|
|
|
control.PushRegion(control.Bounds())
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 20:14:36 +00:00
|
|
|
func (control *CoreControl) AllocateCanvas (width, height int) {
|
2023-01-09 18:06:55 +00:00
|
|
|
core := control.core
|
|
|
|
width, height, _ = control.ConstrainSize(width, height)
|
2023-01-14 06:54:57 +00:00
|
|
|
core.canvas = tomo.NewBasicCanvas(width, height)
|
|
|
|
control.BasicCanvas = core.canvas
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
core.hooks.RunMinimumSizeChange(width, height)
|
|
|
|
|
|
|
|
// if there is an image buffer, and the current size is less
|
|
|
|
// than this new minimum size, send core.parent a resize event.
|
2023-01-14 06:54:57 +00:00
|
|
|
bounds := control.Bounds()
|
|
|
|
imageWidth,
|
|
|
|
imageHeight,
|
|
|
|
constrained := control.ConstrainSize (
|
|
|
|
bounds.Dx(),
|
|
|
|
bounds.Dy())
|
|
|
|
if constrained {
|
|
|
|
core.parent.Handle (tomo.EventResize {
|
|
|
|
Width: imageWidth,
|
|
|
|
Height: imageHeight,
|
|
|
|
})
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|