zntg/ui/state.go
Sander Schobers 9d4b097352 Added Rect method to Control interface.
Fixed ContentScrollbar.
Added horizontal alignment to label.
Removed some dimensional constants.
Added several controls:
- Margin;
- Button;
- Columns;
- Scroll;
- Wrapper (reusable layout control) that wraps around existing control.
2018-09-09 21:07:53 +02:00

72 lines
1.3 KiB
Go

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
}
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 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
}