tins2020/control.go
Sander Schobers ad7c149baf Changed appearance of tooltips a bit.
- Bit smaller text, background half transparent.
2020-05-14 12:59:46 +02:00

83 lines
1.7 KiB
Go

package tins2020
import "github.com/veandco/go-sdl2/sdl"
type Control interface {
Init(*Context) error
Arrange(*Context, Rectangle)
Handle(*Context, sdl.Event) bool
Render(*Context)
}
type EventContextFn func(*Context)
type EventFn func()
type EventInterfaceFn func(interface{})
func EmptyEvent(fn EventFn) EventContextFn {
return func(*Context) { fn() }
}
type ControlBase struct {
Bounds Rectangle
FontName string
Foreground sdl.Color
IsDisabled bool
IsMouseOver bool
OnLeftMouseButtonClick EventContextFn
}
func (c *ControlBase) ActualForeground() sdl.Color {
var none sdl.Color
if c.Foreground == none {
return MustHexColor("#ffffff")
}
return c.Foreground
}
func (c *ControlBase) ActualFont(ctx *Context) *Font {
name := c.ActualFontName()
return ctx.Fonts.Font(name)
}
func (c *ControlBase) ActualFontName() string {
if c.FontName == "" {
return "default"
}
return c.FontName
}
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) bool {
switch e := event.(type) {
case *sdl.MouseMotionEvent:
b.IsMouseOver = b.Bounds.IsPointInside(e.X, e.Y)
case *sdl.MouseButtonEvent:
if !b.IsDisabled && b.IsMouseOver && e.Button == sdl.BUTTON_LEFT && e.Type == sdl.MOUSEBUTTONDOWN {
return b.Invoke(ctx, b.OnLeftMouseButtonClick)
}
case *sdl.WindowEvent:
if e.Event == sdl.WINDOWEVENT_LEAVE {
b.IsMouseOver = false
}
}
return false
}
func (b *ControlBase) Invoke(ctx *Context, fn EventContextFn) bool {
if fn == nil {
return false
}
fn(ctx)
return true
}
func (b *ControlBase) Render(*Context) {}