tins2020/largedialog.go

110 lines
2.2 KiB
Go
Raw Normal View History

package tins2020
import "github.com/veandco/go-sdl2/sdl"
type DialogBase struct {
Container
content Proxy
onShow *Events
close EventFn
}
type Dialog interface {
CloseDialog()
OnShow() EventHandler
ShowDialog(*Context, EventFn)
}
func (d *DialogBase) CloseDialog() {
close := d.close
if close != nil {
close()
}
}
func (d *DialogBase) Init(ctx *Context) error {
d.AddChild(&d.content)
return d.Container.Init(ctx)
}
func (d *DialogBase) OnShow() EventHandler {
if d.onShow == nil {
d.onShow = NewEvents()
}
return d.onShow
}
func (d *DialogBase) SetContent(control Control) {
d.content.Proxied = control
}
func (d *DialogBase) ShowDialog(ctx *Context, close EventFn) {
d.close = close
if d.onShow != nil {
d.onShow.Notify(ctx)
}
}
type LargeDialog struct {
DialogBase
title Label
close IconButton
}
func (d *LargeDialog) Arrange(ctx *Context, bounds Rectangle) {
const titleHeight = 64
d.ControlBase.Arrange(ctx, bounds)
d.title.Arrange(ctx, RectSize(bounds.X, bounds.Y, bounds.W, titleHeight))
d.close.Arrange(ctx, RectSize(bounds.W-64, 0, 64, 64))
d.content.Arrange(ctx, RectSize(bounds.X+titleHeight, 96, bounds.W-2*titleHeight, bounds.H-titleHeight))
}
func (d *LargeDialog) Init(ctx *Context) error {
d.title = Label{
Text: "Botanim",
FontName: "title",
Alignment: TextAlignmentCenter,
}
d.close = IconButton{
Icon: "control-cancel",
IconHover: HoverEffectColor,
IconWidth: 32,
}
d.close.OnLeftMouseButtonClick = EmptyEvent(d.CloseDialog)
d.AddChild(&d.title)
d.AddChild(&d.close)
return d.DialogBase.Init(ctx)
}
func (d *LargeDialog) Handle(ctx *Context, event sdl.Event) bool {
if d.DialogBase.Handle(ctx, event) {
return true
}
switch e := event.(type) {
case *sdl.KeyboardEvent:
if e.Type == sdl.KEYDOWN {
switch e.Keysym.Sym {
case sdl.K_ESCAPE:
d.CloseDialog()
return true
case sdl.K_RETURN:
d.CloseDialog()
return true
}
}
}
return false
}
func (d *LargeDialog) Render(ctx *Context) {
SetDrawColor(ctx.Renderer, MustHexColor("#356DAD"))
ctx.Renderer.FillRect(d.Bounds.SDLPtr())
d.DialogBase.Render(ctx)
}
func (d *LargeDialog) SetCaption(s string) { d.title.Text = s }