107 lines
2.2 KiB
Go
107 lines
2.2 KiB
Go
|
package ui
|
||
|
|
||
|
import (
|
||
|
"time"
|
||
|
|
||
|
"opslag.de/schobers/galleg/allegro5"
|
||
|
"opslag.de/schobers/geom"
|
||
|
)
|
||
|
|
||
|
type Container interface {
|
||
|
Control
|
||
|
Children() []Control
|
||
|
Arrange(Context, geom.RectangleF)
|
||
|
}
|
||
|
|
||
|
func NewContainer(parent Container, children ...Control) *ContainerBase {
|
||
|
return &ContainerBase{ControlBase{}, false, parent, children}
|
||
|
}
|
||
|
|
||
|
type ContainerBase struct {
|
||
|
ControlBase
|
||
|
created bool
|
||
|
parent Container
|
||
|
children []Control
|
||
|
}
|
||
|
|
||
|
func (c *ContainerBase) Children() []Control {
|
||
|
return c.children
|
||
|
}
|
||
|
|
||
|
func (c *ContainerBase) Arrange(ctx Context, rect geom.RectangleF) {
|
||
|
c.ControlBase.SetRect(rect)
|
||
|
for _, child := range c.children {
|
||
|
Arrange(ctx, child, rect)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (c *ContainerBase) Append(child Control) {
|
||
|
if c.created {
|
||
|
panic("error append, must use append create when control is already created")
|
||
|
}
|
||
|
c.children = append(c.children, child)
|
||
|
}
|
||
|
|
||
|
func (c *ContainerBase) AppendCreate(ctx Context, child Control) error {
|
||
|
if !c.created {
|
||
|
panic("error append create, must use append when control is not yet created")
|
||
|
}
|
||
|
c.children = append(c.children, child)
|
||
|
return child.Created(ctx, c)
|
||
|
}
|
||
|
|
||
|
func (c *ContainerBase) RemoveFn(ctx Context, pred func(Control) bool) {
|
||
|
var i = 0
|
||
|
for i < len(c.children) {
|
||
|
if pred(c.children[i]) {
|
||
|
c.children[i].Destroyed(ctx)
|
||
|
c.children = c.children[:i+copy(c.children[i:], c.children[i+1:])]
|
||
|
} else {
|
||
|
i++
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (c *ContainerBase) Created(ctx Context, p Container) error {
|
||
|
if c.created {
|
||
|
panic("already created")
|
||
|
}
|
||
|
c.parent = p
|
||
|
for _, child := range c.children {
|
||
|
err := child.Created(ctx, p)
|
||
|
if nil != err {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
c.created = true
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (c *ContainerBase) Destroyed(ctx Context) {
|
||
|
if !c.created {
|
||
|
panic("not yet created or already destroyed")
|
||
|
}
|
||
|
for _, child := range c.children {
|
||
|
child.Destroyed(ctx)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (c *ContainerBase) Update(ctx Context, dt time.Duration) {
|
||
|
for _, child := range c.children {
|
||
|
child.Update(ctx, dt)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (c *ContainerBase) Handle(ctx Context, ev allegro5.Event) {
|
||
|
for _, child := range c.children {
|
||
|
child.Handle(ctx, ev)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (c *ContainerBase) Render(ctx Context) {
|
||
|
for _, child := range c.children {
|
||
|
child.Render(ctx)
|
||
|
}
|
||
|
c.ControlBase.Render(ctx)
|
||
|
}
|