zntg/ui/state.go

72 lines
1.3 KiB
Go

package ui
import (
"log"
"time"
"opslag.de/schobers/geom"
"opslag.de/schobers/zntg/allg5"
)
type State interface {
Enter(Context) error
Leave(Context) error
Update(Context, time.Duration) (State, error)
Handle(Context, allg5.Event) error
Render(Context) error
}
type StateBase struct {
Control Control
ChangeTo State
}
func (s *StateBase) Enter(ctx Context) error {
if nil != s.Control {
err := s.Control.Created(ctx, nil)
if nil != err {
log.Println("error creating control")
s.Control = nil
}
}
return nil
}
func (s *StateBase) Leave(ctx Context) error {
if nil != s.Control {
s.Control.Destroyed(ctx)
}
return nil
}
func (s *StateBase) Update(ctx Context, dt time.Duration) (State, error) {
if nil != s.Control {
s.Control.Update(ctx, dt)
}
return s.ChangeTo, nil
}
func (s *StateBase) Handle(ctx Context, ev allg5.Event) error {
if nil != s.Control {
s.Control.Handle(ctx, ev)
}
return nil
}
func Arrange(ctx Context, c Control, rect geom.RectangleF32) {
if cont, ok := c.(Container); ok {
cont.Arrange(ctx, rect)
} else {
c.SetRect(rect)
}
}
func (s *StateBase) Render(ctx Context) error {
if nil != s.Control {
var disp = ctx.Display()
Arrange(ctx, s.Control, geom.RectF32(0, 0, float32(disp.Width()), float32(disp.Height())))
s.Control.Render(ctx)
}
return nil
}