tins2020/largedialog.go

109 lines
2.1 KiB
Go
Raw Normal View History

package tins2020
import "github.com/veandco/go-sdl2/sdl"
type DialogBase struct {
Container
content Proxy
2020-05-11 12:41:42 +00:00
onShow *Events
close EventFn
}
type Dialog interface {
CloseDialog()
2020-05-11 12:41:42 +00:00
OnShow() EventHandler
ShowDialog(*Context, EventFn)
}
func (d *DialogBase) CloseDialog() {
close := d.close
if close != nil {
close()
}
}
2020-05-11 12:41:42 +00:00
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
}
2020-05-11 12:41:42 +00:00
func (d *DialogBase) ShowDialog(ctx *Context, close EventFn) {
d.close = close
2020-05-11 12:41:42 +00:00
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, Rect(bounds.X, bounds.Y, bounds.W, titleHeight))
d.close.Arrange(ctx, Rect(bounds.W-64, 0, 64, 64))
d.content.Arrange(ctx, Rect(bounds.X+titleHeight, 96, bounds.W-2*titleHeight, bounds.H-titleHeight))
}
func (d *LargeDialog) Init(ctx *Context) error {
2020-05-14 06:29:23 +00:00
d.title.Text = "Botanim"
d.title.FontName = "title"
d.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)
}
2020-05-11 12:41:42 +00:00
func (d *LargeDialog) SetCaption(s string) { d.title.Text = s }