zntg/ui/state.go

72 lines
1.3 KiB
Go
Raw Normal View History

2018-08-03 06:46:10 +00:00
package ui
import (
"log"
"time"
"opslag.de/schobers/galleg/allegro5"
"opslag.de/schobers/geom"
)
type State interface {
Enter(Context) error
Leave(Context) error
Update(Context, time.Duration) (State, error)
Handle(Context, allegro5.Event) error
Render(Context) error
}
type StateBase struct {
Control Control
ChangeTo State
2018-08-03 06:46:10 +00:00
}
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
2018-08-03 06:46:10 +00:00
}
func (s *StateBase) Handle(ctx Context, ev allegro5.Event) error {
if nil != s.Control {
s.Control.Handle(ctx, ev)
}
return nil
}
func Arrange(ctx Context, c Control, rect geom.RectangleF) {
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.RectF(0, 0, float64(disp.Width()), float64(disp.Height())))
s.Control.Render(ctx)
}
return nil
}