doodle/pkg/modal/confirm.go
Noah Petherbridge 336a949ed0 Global UI Popup Modals
* Adds global modal support in the pkg/modal/ package. It has easy
  Alert() and Confirm() methods to prompt the user before calling a
  callback function on affirmative response.
* Modals have global app state: they're processed in the main loop in
  pkg/doodle.go similar to the global command shell.
* When a modal is active, a semitransparent black frame covers the
  screen (gameplay loop paused, last game frame rendered below) and the
  modal window appears on top.
* The developer console retains higher priority than the modal system
  and always renders on top.
* Editor Mode: track when the level pixels have been modified, and
  confirm the user about unsaved changes when they attempt to close the
  level (New, Open, Close, etc.)
* Global: the Escape key no longer immediately shuts down the game, but
  will confirm the user's intent via a modal.
* File->Quit in the Editor Mode also invokes the confirm shutdown modal.
2020-11-15 18:02:35 -08:00

92 lines
1.6 KiB
Go

package modal
import (
"fmt"
"git.kirsle.net/apps/doodle/pkg/balance"
"git.kirsle.net/go/ui"
)
// Confirm pops up an Ok/Cancel modal.
func Confirm(message string, args ...interface{}) *Modal {
if !ready {
panic("modal.Confirm(): not ready")
} else if current != nil {
return current
}
// Reset the supervisor.
supervisor = ui.NewSupervisor()
m := &Modal{
title: "Confirm",
message: fmt.Sprintf(message, args...),
}
m.window = makeConfirm(m)
center(m.window)
current = m
return m
}
// makeConfirm creates the ui.Window for the Confirm modal.
func makeConfirm(m *Modal) *ui.Window {
win := ui.NewWindow("Confirm")
_, title := win.TitleBar()
title.TextVariable = &m.title
msgFrame := ui.NewFrame("Confirm Message")
win.Pack(msgFrame, ui.Pack{
Side: ui.N,
})
msg := ui.NewLabel(ui.Label{
TextVariable: &m.message,
Font: balance.UIFont,
})
msgFrame.Pack(msg, ui.Pack{
Side: ui.N,
})
// Ok/Cancel button bar.
btnBar := ui.NewFrame("Button Bar")
msgFrame.Pack(btnBar, ui.Pack{
Side: ui.N,
PadY: 4,
})
for _, btn := range []struct {
Label string
F func(ui.EventData) error
}{
{"Ok", func(ev ui.EventData) error {
m.Dismiss(true)
return nil
}},
{"Cancel", func(ev ui.EventData) error {
m.Dismiss(false)
return nil
}},
} {
btn := btn
button := ui.NewButton(btn.Label+"Button", ui.NewLabel(ui.Label{
Text: btn.Label,
Font: balance.MenuFont,
}))
button.Handle(ui.Click, btn.F)
button.Compute(engine)
supervisor.Add(button)
btnBar.Pack(button, ui.Pack{
Side: ui.W,
PadX: 2,
})
}
win.Compute(engine)
win.Supervise(supervisor)
return win
}