tins2020/dialogs.go

99 lines
2.0 KiB
Go

package tins2020
import (
"opslag.de/schobers/zntg/ui"
)
type Dialogs struct {
ui.Proxy
intro ui.Overlay
research ui.Overlay
settings ui.Overlay
nothing ui.Control
closed ui.Events
opened ui.Events
}
const dialogsOverlayName = "dialogs"
func NewDialogs(game *Game) *Dialogs {
intro := NewIntro()
research := NewResearch(game)
settings := NewLargeDialog("Settings", &ui.Label{})
dialogs := &Dialogs{
intro: intro,
settings: settings,
research: research,
nothing: &ui.ControlBase{},
}
intro.CloseRequested().AddHandlerEmpty(dialogs.Close)
research.CloseRequested().AddHandlerEmpty(dialogs.Close)
settings.CloseRequested().AddHandlerEmpty(dialogs.Close)
return dialogs
}
func (d *Dialogs) Init(ctx ui.Context) {
overlays := ctx.Overlays()
overlays.AddOnTop(dialogsOverlayName, d, false)
d.Content = d.nothing
}
func (d *Dialogs) showDialog(ctx ui.Context, control ui.Control) {
if control == nil {
ctx.Overlays().Hide(dialogsOverlayName)
d.closed.Notify(ctx, control)
d.Content = d.nothing
} else {
d.Content = control
ctx.Overlays().Show(dialogsOverlayName)
d.opened.Notify(ctx, control)
}
}
func (d *Dialogs) Close(ctx ui.Context) {
d.showDialog(ctx, nil)
}
func (d *Dialogs) DialogClosed() ui.EventHandler { return &d.closed }
func (d *Dialogs) DialogOpened() ui.EventHandler { return &d.opened }
func (d *Dialogs) Hidden() {
d.Proxy.Hidden()
}
func (d *Dialogs) AskConfirmation(ctx ui.Context, caption, question string, confirm, cancel ui.EventFn) {
dialog := newConfirmationDialog(caption, question)
dialog.userDecided.AddHandler(func(ctx ui.Context, state interface{}) {
decision := state.(bool)
if decision {
confirm(ctx, nil)
} else {
cancel(ctx, nil)
}
d.Close(ctx)
})
d.showDialog(ctx, dialog)
}
func (d *Dialogs) ShowIntro(ctx ui.Context) {
d.showDialog(ctx, d.intro)
}
func (d *Dialogs) Shown() {
d.Proxy.Shown()
}
func (d *Dialogs) ShowResearch(ctx ui.Context) {
d.showDialog(ctx, d.research)
}
func (d *Dialogs) ShowSettings(ctx ui.Context) {
d.showDialog(ctx, d.settings)
}