2023-03-16 12:22:56 -06:00
|
|
|
package containers
|
2023-01-10 14:39:37 -07:00
|
|
|
|
2023-01-10 15:34:40 -07:00
|
|
|
import "image"
|
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/input"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
2023-01-10 14:39:37 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
2023-03-30 23:06:29 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/default/theme"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/default/config"
|
2023-01-10 14:39:37 -07:00
|
|
|
|
2023-01-12 14:02:33 -07:00
|
|
|
// Container is an element capable of containg other elements, and arranging
|
|
|
|
// them in a layout.
|
2023-01-10 14:39:37 -07:00
|
|
|
type Container struct {
|
|
|
|
*core.Core
|
2023-03-03 23:20:23 -07:00
|
|
|
*core.Propagator
|
2023-01-10 14:39:37 -07:00
|
|
|
core core.CoreControl
|
|
|
|
|
2023-03-30 21:19:04 -06:00
|
|
|
layout tomo.Layout
|
|
|
|
children []tomo.LayoutEntry
|
2023-01-30 15:01:47 -07:00
|
|
|
warping bool
|
2023-01-19 14:49:34 -07:00
|
|
|
|
2023-02-08 12:36:14 -07:00
|
|
|
config config.Wrapped
|
|
|
|
theme theme.Wrapped
|
2023-02-07 22:22:40 -07:00
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
onFocusRequest func () (granted bool)
|
2023-02-01 23:48:16 -07:00
|
|
|
onFocusMotionRequest func (input.KeynavDirection) (granted bool)
|
2023-01-10 14:39:37 -07:00
|
|
|
}
|
|
|
|
|
2023-01-12 14:02:33 -07:00
|
|
|
// NewContainer creates a new container.
|
2023-03-30 21:19:04 -06:00
|
|
|
func NewContainer (layout tomo.Layout) (element *Container) {
|
2023-02-08 12:36:14 -07:00
|
|
|
element = &Container { }
|
2023-03-30 23:06:29 -06:00
|
|
|
element.theme.Case = tomo.C("tomo", "container")
|
2023-03-14 23:41:23 -06:00
|
|
|
element.Core, element.core = core.NewCore(element, element.redoAll)
|
|
|
|
element.Propagator = core.NewPropagator(element, element.core)
|
2023-01-10 14:39:37 -07:00
|
|
|
element.SetLayout(layout)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-12 14:02:33 -07:00
|
|
|
// SetLayout sets the layout of this container.
|
2023-03-30 21:19:04 -06:00
|
|
|
func (element *Container) SetLayout (layout tomo.Layout) {
|
2023-01-10 14:39:37 -07:00
|
|
|
element.layout = layout
|
2023-03-16 23:52:26 -06:00
|
|
|
element.updateMinimumSize()
|
2023-01-10 15:34:40 -07:00
|
|
|
if element.core.HasImage() {
|
2023-01-31 12:54:43 -07:00
|
|
|
element.redoAll()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-10 15:34:40 -07:00
|
|
|
}
|
2023-01-10 14:39:37 -07:00
|
|
|
}
|
|
|
|
|
2023-01-12 14:02:33 -07:00
|
|
|
// Adopt adds a new child element to the container. If expand is set to true,
|
|
|
|
// the element will expand (instead of contract to its minimum size), in
|
|
|
|
// whatever way is defined by the current layout.
|
2023-03-30 21:19:04 -06:00
|
|
|
func (element *Container) Adopt (child tomo.Element, expand bool) {
|
|
|
|
if child0, ok := child.(tomo.Themeable); ok {
|
2023-02-08 12:36:14 -07:00
|
|
|
child0.SetTheme(element.theme.Theme)
|
|
|
|
}
|
2023-03-30 21:19:04 -06:00
|
|
|
if child0, ok := child.(tomo.Configurable); ok {
|
2023-02-08 12:36:14 -07:00
|
|
|
child0.SetConfig(element.config.Config)
|
|
|
|
}
|
2023-03-14 23:41:23 -06:00
|
|
|
child.SetParent(element)
|
2023-01-19 14:49:34 -07:00
|
|
|
|
|
|
|
// add child
|
2023-03-30 21:19:04 -06:00
|
|
|
element.children = append (element.children, tomo.LayoutEntry {
|
2023-01-10 14:39:37 -07:00
|
|
|
Element: child,
|
2023-01-10 15:34:40 -07:00
|
|
|
Expand: expand,
|
2023-01-10 14:39:37 -07:00
|
|
|
})
|
|
|
|
|
2023-01-19 14:49:34 -07:00
|
|
|
// refresh stale data
|
2023-01-10 14:39:37 -07:00
|
|
|
element.updateMinimumSize()
|
2023-01-12 14:02:33 -07:00
|
|
|
if element.core.HasImage() && !element.warping {
|
2023-01-31 12:54:43 -07:00
|
|
|
element.redoAll()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-12 14:02:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warp runs the specified callback, deferring all layout and rendering updates
|
|
|
|
// until the callback has finished executing. This allows for aplications to
|
|
|
|
// perform batch gui updates without flickering and stuff.
|
|
|
|
func (element *Container) Warp (callback func ()) {
|
|
|
|
if element.warping {
|
|
|
|
callback()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
element.warping = true
|
|
|
|
callback()
|
|
|
|
element.warping = false
|
|
|
|
|
|
|
|
// TODO: create some sort of task list so we don't do a full recalculate
|
|
|
|
// and redraw every time, because although that is the most likely use
|
|
|
|
// case, it is not the only one.
|
2023-01-10 15:34:40 -07:00
|
|
|
if element.core.HasImage() {
|
2023-01-31 12:54:43 -07:00
|
|
|
element.redoAll()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-10 15:34:40 -07:00
|
|
|
}
|
2023-01-10 14:39:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Disown removes the given child from the container if it is contained within
|
|
|
|
// it.
|
2023-03-30 21:19:04 -06:00
|
|
|
func (element *Container) Disown (child tomo.Element) {
|
2023-01-10 14:39:37 -07:00
|
|
|
for index, entry := range element.children {
|
|
|
|
if entry.Element == child {
|
2023-01-19 14:49:34 -07:00
|
|
|
element.clearChildEventHandlers(entry.Element)
|
2023-01-10 14:39:37 -07:00
|
|
|
element.children = append (
|
|
|
|
element.children[:index],
|
|
|
|
element.children[index + 1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
element.updateMinimumSize()
|
2023-01-12 14:02:33 -07:00
|
|
|
if element.core.HasImage() && !element.warping {
|
2023-01-31 12:54:43 -07:00
|
|
|
element.redoAll()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-30 21:19:04 -06:00
|
|
|
func (element *Container) clearChildEventHandlers (child tomo.Element) {
|
2023-03-14 23:41:23 -06:00
|
|
|
child.DrawTo(nil, image.Rectangle { }, nil)
|
|
|
|
child.SetParent(nil)
|
|
|
|
|
2023-03-30 21:19:04 -06:00
|
|
|
if child, ok := child.(tomo.Focusable); ok {
|
2023-03-14 23:41:23 -06:00
|
|
|
if child.Focused() {
|
|
|
|
child.HandleUnfocus()
|
2023-01-19 14:49:34 -07:00
|
|
|
}
|
|
|
|
}
|
2023-01-10 14:39:37 -07:00
|
|
|
}
|
|
|
|
|
2023-01-10 16:07:51 -07:00
|
|
|
// DisownAll removes all child elements from the container at once.
|
|
|
|
func (element *Container) DisownAll () {
|
2023-03-04 08:44:45 -07:00
|
|
|
for _, entry := range element.children {
|
|
|
|
element.clearChildEventHandlers(entry.Element)
|
|
|
|
}
|
2023-01-10 16:07:51 -07:00
|
|
|
element.children = nil
|
|
|
|
|
|
|
|
element.updateMinimumSize()
|
2023-01-12 14:02:33 -07:00
|
|
|
if element.core.HasImage() && !element.warping {
|
2023-01-31 12:54:43 -07:00
|
|
|
element.redoAll()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-10 16:07:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 14:39:37 -07:00
|
|
|
// Children returns a slice containing this element's children.
|
2023-03-30 21:19:04 -06:00
|
|
|
func (element *Container) Children () (children []tomo.Element) {
|
|
|
|
children = make([]tomo.Element, len(element.children))
|
2023-01-10 14:39:37 -07:00
|
|
|
for index, entry := range element.children {
|
|
|
|
children[index] = entry.Element
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// CountChildren returns the amount of children contained within this element.
|
|
|
|
func (element *Container) CountChildren () (count int) {
|
|
|
|
return len(element.children)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Child returns the child at the specified index. If the index is out of
|
|
|
|
// bounds, this method will return nil.
|
2023-03-30 21:19:04 -06:00
|
|
|
func (element *Container) Child (index int) (child tomo.Element) {
|
2023-01-10 14:39:37 -07:00
|
|
|
if index < 0 || index > len(element.children) { return }
|
|
|
|
return element.children[index].Element
|
|
|
|
}
|
|
|
|
|
2023-01-10 15:34:40 -07:00
|
|
|
// ChildAt returns the child that contains the specified x and y coordinates. If
|
|
|
|
// there are no children at the coordinates, this method will return nil.
|
2023-03-30 21:19:04 -06:00
|
|
|
func (element *Container) ChildAt (point image.Point) (child tomo.Element) {
|
2023-01-10 15:34:40 -07:00
|
|
|
for _, entry := range element.children {
|
2023-01-31 12:54:43 -07:00
|
|
|
if point.In(entry.Bounds) {
|
2023-01-10 15:34:40 -07:00
|
|
|
child = entry.Element
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-31 12:54:43 -07:00
|
|
|
func (element *Container) redoAll () {
|
2023-02-08 23:30:14 -07:00
|
|
|
if !element.core.HasImage() { return }
|
2023-03-09 20:23:09 -07:00
|
|
|
|
|
|
|
// remove child canvasses so that any operations done in here will not
|
|
|
|
// cause a child to draw to a wack ass canvas.
|
|
|
|
for _, entry := range element.children {
|
2023-03-14 23:41:23 -06:00
|
|
|
entry.DrawTo(nil, entry.Bounds, nil)
|
2023-03-09 20:23:09 -07:00
|
|
|
}
|
|
|
|
|
2023-01-31 12:54:43 -07:00
|
|
|
// do a layout
|
2023-02-09 00:04:58 -07:00
|
|
|
element.doLayout()
|
2023-01-31 12:54:43 -07:00
|
|
|
|
|
|
|
// draw a background
|
2023-02-11 19:17:43 -07:00
|
|
|
rocks := make([]image.Rectangle, len(element.children))
|
|
|
|
for index, entry := range element.children {
|
|
|
|
rocks[index] = entry.Bounds
|
|
|
|
}
|
2023-02-07 22:22:40 -07:00
|
|
|
pattern := element.theme.Pattern (
|
2023-03-30 23:06:29 -06:00
|
|
|
tomo.PatternBackground,
|
|
|
|
tomo.State { })
|
2023-03-11 23:15:36 -07:00
|
|
|
artist.DrawShatter(element.core, pattern, element.Bounds(), rocks...)
|
2023-01-31 12:54:43 -07:00
|
|
|
|
2023-01-31 16:04:12 -07:00
|
|
|
// cut our canvas up and give peices to child elements
|
2023-01-31 12:54:43 -07:00
|
|
|
for _, entry := range element.children {
|
2023-03-11 23:15:36 -07:00
|
|
|
entry.DrawTo (
|
|
|
|
canvas.Cut(element.core, entry.Bounds),
|
2023-03-14 23:41:23 -06:00
|
|
|
entry.Bounds, func (region image.Rectangle) {
|
|
|
|
element.core.DamageRegion(region)
|
|
|
|
})
|
2023-01-31 12:54:43 -07:00
|
|
|
}
|
2023-01-16 09:54:02 -07:00
|
|
|
}
|
2023-01-10 15:34:40 -07:00
|
|
|
|
2023-03-31 01:25:46 -06:00
|
|
|
func (element *Container) Window () tomo.Window {
|
|
|
|
return element.core.Window()
|
|
|
|
}
|
|
|
|
|
2023-03-14 23:41:23 -06:00
|
|
|
// NotifyMinimumSizeChange notifies the container that the minimum size of a
|
|
|
|
// child element has changed.
|
2023-03-30 21:19:04 -06:00
|
|
|
func (element *Container) NotifyMinimumSizeChange (child tomo.Element) {
|
2023-03-14 23:41:23 -06:00
|
|
|
element.updateMinimumSize()
|
|
|
|
element.redoAll()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
|
2023-02-07 22:22:40 -07:00
|
|
|
// SetTheme sets the element's theme.
|
2023-03-30 23:06:29 -06:00
|
|
|
func (element *Container) SetTheme (new tomo.Theme) {
|
2023-02-08 12:36:14 -07:00
|
|
|
if new == element.theme.Theme { return }
|
|
|
|
element.theme.Theme = new
|
2023-03-03 23:20:23 -07:00
|
|
|
element.Propagator.SetTheme(new)
|
2023-02-07 22:22:40 -07:00
|
|
|
element.updateMinimumSize()
|
2023-02-07 09:27:59 -07:00
|
|
|
element.redoAll()
|
|
|
|
}
|
|
|
|
|
2023-02-07 22:22:40 -07:00
|
|
|
// SetConfig sets the element's configuration.
|
2023-03-30 23:06:29 -06:00
|
|
|
func (element *Container) SetConfig (new tomo.Config) {
|
2023-02-08 12:36:14 -07:00
|
|
|
if new == element.config.Config { return }
|
2023-03-03 23:20:23 -07:00
|
|
|
element.Propagator.SetConfig(new)
|
2023-02-07 22:22:40 -07:00
|
|
|
element.updateMinimumSize()
|
2023-02-07 09:27:59 -07:00
|
|
|
element.redoAll()
|
|
|
|
}
|
|
|
|
|
2023-01-10 14:39:37 -07:00
|
|
|
func (element *Container) updateMinimumSize () {
|
2023-03-30 23:06:29 -06:00
|
|
|
margin := element.theme.Margin(tomo.PatternBackground)
|
|
|
|
padding := element.theme.Padding(tomo.PatternBackground)
|
2023-03-03 23:20:23 -07:00
|
|
|
width, height := element.layout.MinimumSize (
|
2023-03-03 23:42:14 -07:00
|
|
|
element.children, margin, padding)
|
2023-01-16 21:34:17 -07:00
|
|
|
element.core.SetMinimumSize(width, height)
|
2023-01-10 14:39:37 -07:00
|
|
|
}
|
|
|
|
|
2023-02-09 00:04:58 -07:00
|
|
|
func (element *Container) doLayout () {
|
2023-03-30 23:06:29 -06:00
|
|
|
margin := element.theme.Margin(tomo.PatternBackground)
|
|
|
|
padding := element.theme.Padding(tomo.PatternBackground)
|
2023-02-01 23:48:16 -07:00
|
|
|
element.layout.Arrange (
|
2023-03-03 23:42:14 -07:00
|
|
|
element.children, margin,
|
|
|
|
padding, element.Bounds())
|
2023-01-10 14:39:37 -07:00
|
|
|
}
|