diff --git a/alui/application.go b/alui/application.go index cdd0703..efd8840 100644 --- a/alui/application.go +++ b/alui/application.go @@ -96,10 +96,14 @@ func (a *Application) Run(main Control, init func(*UI) error) error { ctx := ui.Context() for { - allg5.ClearToColor(ctx.Palette.Icon) + allg5.ClearToColor(ctx.Palette.Background) ui.Render() disp.Flip() + if ctx.quit { + return nil + } + e := eq.Get() for e != nil { switch e := e.(type) { @@ -111,6 +115,9 @@ func (a *Application) Run(main Control, init func(*UI) error) error { } } ui.Handle(e) + if ctx.quit { + return nil + } e = eq.Get() } } diff --git a/alui/button.go b/alui/button.go index fcdd2ba..15a5481 100644 --- a/alui/button.go +++ b/alui/button.go @@ -14,7 +14,7 @@ type Button struct { TextAlign allg5.HorizontalAlignment } -func NewButton(text string, onClick func()) *Button { +func NewButton(text string, onClick func(*Context)) *Button { b := &Button{Text: text} b.OnClick = onClick return b diff --git a/alui/container.go b/alui/container.go index f672393..b6b7deb 100644 --- a/alui/container.go +++ b/alui/container.go @@ -33,10 +33,10 @@ func (c *Container) DesiredSize(ctx *Context) geom.PointF32 { return size } -func (c *Container) Handle(e allg5.Event) { - c.ControlBase.Handle(e) +func (c *Container) Handle(ctx *Context, e allg5.Event) { + c.ControlBase.Handle(ctx, e) for _, child := range c.Children { - child.Handle(e) + child.Handle(ctx, e) } } diff --git a/alui/context.go b/alui/context.go index 309f042..444af10 100644 --- a/alui/context.go +++ b/alui/context.go @@ -10,12 +10,16 @@ type Context struct { Fonts *Fonts Cursor allg5.MouseCursor Palette Palette + + quit bool } func (c *Context) DisplayBounds() geom.RectangleF32 { return geom.RectF32(0, 0, float32(c.Display.Width()), float32(c.Display.Height())) } +func (c *Context) Quit() { c.quit = true } + type State struct { Font string } diff --git a/alui/control.go b/alui/control.go index 7b57930..2cddb0c 100644 --- a/alui/control.go +++ b/alui/control.go @@ -8,7 +8,7 @@ import ( type Control interface { Bounds() geom.RectangleF32 - Handle(allg5.Event) + Handle(*Context, allg5.Event) DesiredSize(*Context) geom.PointF32 Layout(*Context, geom.RectangleF32) @@ -28,16 +28,16 @@ type ControlBase struct { Foreground *allg5.Color Background *allg5.Color - OnClick func() - OnEnter func() - OnLeave func() + OnClick func(*Context) + OnEnter func(*Context) + OnLeave func(*Context) } func MouseEventToPos(e allg5.MouseEvent) geom.PointF32 { return geom.PtF32(float32(e.X), float32(e.Y)) } func (b *ControlBase) DesiredSize(*Context) geom.PointF32 { return geom.ZeroPtF32 } -func (b *ControlBase) Handle(e allg5.Event) { +func (b *ControlBase) Handle(ctx *Context, e allg5.Event) { switch e := e.(type) { case *allg5.MouseMoveEvent: pos := MouseEventToPos(e.MouseEvent) @@ -46,11 +46,11 @@ func (b *ControlBase) Handle(e allg5.Event) { b.Over = over if over { if b.OnEnter != nil { - b.OnEnter() + b.OnEnter(ctx) } } else { if b.OnLeave != nil { - b.OnLeave() + b.OnLeave(ctx) } } } @@ -60,7 +60,7 @@ func (b *ControlBase) Handle(e allg5.Event) { } if e.Button == allg5.MouseButtonLeft { if b.OnClick != nil { - b.OnClick() + b.OnClick(ctx) } } } diff --git a/alui/menu.go b/alui/menu.go index 37a4587..14d8591 100644 --- a/alui/menu.go +++ b/alui/menu.go @@ -26,11 +26,11 @@ func (m *Menu) Activate(i int) { m.active = i % len(m.buttons) } -func (m *Menu) Add(text string, onClick func()) { +func (m *Menu) Add(text string, onClick func(*Context)) { idx := len(m.buttons) button := &Button{Text: text, TextAlign: allg5.AlignCenter} button.OnClick = onClick - button.OnEnter = func() { + button.OnEnter = func(*Context) { m.updateActiveButton(idx) } if idx == 0 { @@ -40,8 +40,8 @@ func (m *Menu) Add(text string, onClick func()) { m.AddChild(button) } -func (m *Menu) Handle(e allg5.Event) { - m.Column.Handle(e) +func (m *Menu) Handle(ctx *Context, e allg5.Event) { + m.Column.Handle(ctx, e) if len(m.buttons) == 0 { return @@ -63,7 +63,7 @@ func (m *Menu) Handle(e allg5.Event) { m.updateActiveButton((m.active + len(m.buttons) - 1) % len(m.buttons)) case allg5.KeyEnter: if onClick := m.buttons[m.active].OnClick; onClick != nil { - onClick() + onClick(ctx) } } case *allg5.MouseMoveEvent: diff --git a/alui/overlay.go b/alui/overlay.go index f7ae422..4efbc06 100644 --- a/alui/overlay.go +++ b/alui/overlay.go @@ -20,9 +20,9 @@ func (o *Overlay) DesiredSize(ctx *Context) geom.PointF32 { return geom.ZeroPtF32 } -func (o *Overlay) Handle(e allg5.Event) { +func (o *Overlay) Handle(ctx *Context, e allg5.Event) { if o.Visible { - o.Proxy.Handle(e) + o.Proxy.Handle(ctx, e) } } diff --git a/alui/palette.go b/alui/palette.go index d171ace..a09acd6 100644 --- a/alui/palette.go +++ b/alui/palette.go @@ -5,11 +5,12 @@ import ( ) type Palette struct { - Primary allg5.Color - Light allg5.Color - Dark allg5.Color - Text allg5.Color - TextLight allg5.Color - Icon allg5.Color - Accent allg5.Color + Background allg5.Color + Primary allg5.Color + Light allg5.Color + Dark allg5.Color + Text allg5.Color + TextLight allg5.Color + Icon allg5.Color + Accent allg5.Color } diff --git a/alui/proxy.go b/alui/proxy.go index aa5f98c..9ae7909 100644 --- a/alui/proxy.go +++ b/alui/proxy.go @@ -25,9 +25,9 @@ func (p *Proxy) DesiredSize(ctx *Context) geom.PointF32 { return geom.ZeroPtF32 } -func (p *Proxy) Handle(e allg5.Event) { +func (p *Proxy) Handle(ctx *Context, e allg5.Event) { if p.Target != nil { - p.Target.Handle(e) + p.Target.Handle(ctx, e) } } diff --git a/alui/stackpanel.go b/alui/stackpanel.go index 57fee2f..ce12f67 100644 --- a/alui/stackpanel.go +++ b/alui/stackpanel.go @@ -71,8 +71,8 @@ func (s *StackPanel) DesiredSize(ctx *Context) geom.PointF32 { return size } -func (s *StackPanel) Handle(e allg5.Event) { - s.Container.Handle(e) +func (s *StackPanel) Handle(ctx *Context, e allg5.Event) { + s.Container.Handle(ctx, e) } func (s *StackPanel) Layout(ctx *Context, bounds geom.RectangleF32) { diff --git a/alui/ui.go b/alui/ui.go index 168a998..8473cf3 100644 --- a/alui/ui.go +++ b/alui/ui.go @@ -33,7 +33,7 @@ func (ui *UI) layoutBounds(bounds geom.RectangleF32) { ui.main.Layout(ui.ctx, bounds) } -func (ui *UI) Handle(e allg5.Event) { ui.main.Handle(e) } +func (ui *UI) Handle(e allg5.Event) { ui.main.Handle(ui.ctx, e) } func (ui *UI) Render() { ui.RenderBounds(ui.ctx.DisplayBounds())