Added Context to Handle() methods.

This commit is contained in:
Sander Schobers 2020-04-10 11:00:27 +02:00
parent b431e44110
commit 14a0614ced
11 changed files with 44 additions and 32 deletions

View File

@ -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()
}
}

View File

@ -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

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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)
}
}
}

View File

@ -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:

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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)
}
}

View File

@ -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) {

View File

@ -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())