doodle/pkg/modal/alert.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

75 lines
1.3 KiB
Go

package modal
import (
"fmt"
"git.kirsle.net/apps/doodle/pkg/balance"
"git.kirsle.net/apps/doodle/pkg/log"
"git.kirsle.net/go/ui"
)
// Alert pops up an alert box modal.
func Alert(message string, args ...interface{}) *Modal {
if !ready {
panic("modal.Alert(): not ready")
} else if current != nil {
return current
}
// Reset the supervisor.
supervisor = ui.NewSupervisor()
m := &Modal{
title: "Alert",
message: fmt.Sprintf(message, args...),
}
m.window = makeAlert(m)
center(m.window)
current = m
return m
}
// alertWindow creates the ui.Window for the Alert modal.
func makeAlert(m *Modal) *ui.Window {
win := ui.NewWindow("Alert")
_, title := win.TitleBar()
title.TextVariable = &m.title
msgFrame := ui.NewFrame("Alert 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,
})
button := ui.NewButton("Ok Button", ui.NewLabel(ui.Label{
Text: "Ok",
Font: balance.MenuFont,
}))
button.Handle(ui.Click, func(ev ui.EventData) error {
log.Info("clicked!")
m.Dismiss(true)
return nil
})
win.Pack(button, ui.Pack{
Side: ui.N,
PadY: 4,
})
button.Compute(engine)
supervisor.Add(button)
win.Compute(engine)
win.Supervise(supervisor)
return win
}