tins2020/control.go
Sander Schobers b14f79a61a Lots of UI work.
Added more icons and placed buttons on the bars.
Implemented pause/run/fast.
2020-05-10 17:16:18 +02:00

51 lines
1.0 KiB
Go

package tins2020
import "github.com/veandco/go-sdl2/sdl"
type Control interface {
Init(*Context) error
Arrange(*Context, Rectangle)
Handle(*Context, sdl.Event)
Render(*Context)
}
type EventFn func(*Context)
type EmptyEventFn func()
func EmptyEvent(fn EmptyEventFn) EventFn {
return func(*Context) { fn() }
}
type ControlBase struct {
Bounds Rectangle
IsMouseOver bool
OnLeftMouseButtonClick EventFn
}
func (b *ControlBase) Arrange(ctx *Context, bounds Rectangle) { b.Bounds = bounds }
func (b *ControlBase) Init(*Context) error { return nil }
func (b *ControlBase) Handle(ctx *Context, event sdl.Event) {
switch e := event.(type) {
case *sdl.MouseMotionEvent:
b.IsMouseOver = b.Bounds.IsPointInside(e.X, e.Y)
case *sdl.MouseButtonEvent:
if b.IsMouseOver && e.Button == sdl.BUTTON_LEFT && e.Type == sdl.MOUSEBUTTONDOWN {
b.Invoke(ctx, b.OnLeftMouseButtonClick)
}
}
}
func (b *ControlBase) Invoke(ctx *Context, fn EventFn) {
if fn == nil {
return
}
fn(ctx)
}
func (b *ControlBase) Render(*Context) {}