2018-08-03 06:46:10 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2018-09-22 14:23:26 +00:00
|
|
|
"opslag.de/schobers/zntg/allg5"
|
2018-08-03 06:46:10 +00:00
|
|
|
)
|
|
|
|
|
2018-09-22 14:23:26 +00:00
|
|
|
func shouldClose(ev allg5.Event) bool {
|
2018-08-03 06:46:10 +00:00
|
|
|
switch e := ev.(type) {
|
2018-09-22 14:23:26 +00:00
|
|
|
case *allg5.KeyCharEvent:
|
2018-08-03 06:46:10 +00:00
|
|
|
switch e.KeyCode {
|
2018-09-22 14:23:26 +00:00
|
|
|
case allg5.KeyEscape:
|
2018-08-03 06:46:10 +00:00
|
|
|
return true
|
2018-09-22 14:23:26 +00:00
|
|
|
case allg5.KeyF4:
|
|
|
|
if e.Modifiers&allg5.KeyModAlt == allg5.KeyModAlt {
|
2018-08-03 06:46:10 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2018-09-22 14:23:26 +00:00
|
|
|
case *allg5.DisplayCloseEvent:
|
2018-08-03 06:46:10 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-09-22 14:23:26 +00:00
|
|
|
func shouldToggleDebug(ev allg5.Event) bool {
|
2018-08-03 06:46:10 +00:00
|
|
|
switch e := ev.(type) {
|
2018-09-22 14:23:26 +00:00
|
|
|
case *allg5.KeyCharEvent:
|
2018-08-03 06:46:10 +00:00
|
|
|
switch e.KeyCode {
|
2018-09-22 14:23:26 +00:00
|
|
|
case allg5.KeyD:
|
|
|
|
return e.Modifiers&allg5.KeyModAlt == allg5.KeyModAlt
|
2018-08-03 06:46:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func switchState(ctx Context, curr State, s State) error {
|
|
|
|
var err error
|
|
|
|
err = curr.Leave(ctx)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.Enter(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Init() error {
|
2018-09-22 14:23:26 +00:00
|
|
|
return allg5.Init(allg5.InitAll)
|
2018-08-03 06:46:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-22 14:23:26 +00:00
|
|
|
func Run(w, h int, title string, s State, f Fonts, opts *allg5.NewDisplayOptions) error {
|
2018-08-03 06:46:10 +00:00
|
|
|
if nil == opts {
|
2018-09-22 14:23:26 +00:00
|
|
|
opts = &allg5.NewDisplayOptions{}
|
2018-08-03 06:46:10 +00:00
|
|
|
}
|
2018-09-22 14:23:26 +00:00
|
|
|
disp, err := allg5.NewDisplay(w, h, *opts)
|
2018-08-03 06:46:10 +00:00
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
disp.SetWindowTitle(title)
|
|
|
|
defer disp.Destroy()
|
|
|
|
|
2018-09-22 14:23:26 +00:00
|
|
|
evq, err := allg5.NewEventQueue()
|
2018-08-03 06:46:10 +00:00
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
evq.RegisterDisplay(disp)
|
|
|
|
evq.RegisterKeyboard()
|
|
|
|
evq.RegisterMouse()
|
|
|
|
defer evq.Destroy()
|
|
|
|
|
|
|
|
ctx := newContext(disp, f)
|
|
|
|
|
|
|
|
var state = s
|
|
|
|
err = state.Enter(ctx)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var t = time.Now()
|
|
|
|
main:
|
|
|
|
for nil != state {
|
|
|
|
for ev := evq.Get(); ev != nil; ev = evq.Get() {
|
|
|
|
if shouldClose(ev) {
|
|
|
|
break main
|
|
|
|
}
|
|
|
|
if shouldToggleDebug(ev) {
|
|
|
|
ctx.dbg.enbl = !ctx.dbg.enbl
|
|
|
|
}
|
|
|
|
err := state.Handle(ctx, ev)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var now = time.Now()
|
|
|
|
var dt = now.Sub(t)
|
|
|
|
var s, err = state.Update(ctx, dt)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t = now
|
|
|
|
if s != nil {
|
|
|
|
err = switchState(ctx, state, s)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
state = s
|
|
|
|
}
|
|
|
|
ctx.dbg.resetRainbow()
|
|
|
|
err = state.Render(ctx)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
disp.Flip()
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
err = state.Leave(ctx)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|