allg5/alui/control.go

74 lines
1.5 KiB
Go
Raw Normal View History

2020-03-08 09:10:41 +00:00
package alui
import (
"opslag.de/schobers/allg5"
"opslag.de/schobers/geom"
)
type Control interface {
Bounds() geom.RectangleF32
2020-04-10 09:00:27 +00:00
Handle(*Context, allg5.Event)
2020-03-08 09:10:41 +00:00
DesiredSize(*Context) geom.PointF32
Layout(*Context, geom.RectangleF32)
Render(*Context, geom.RectangleF32)
}
type Bounds struct {
value geom.RectangleF32
}
var _ Control = &ControlBase{}
type ControlBase struct {
_Bounds geom.RectangleF32
Over bool
Font string
Foreground *allg5.Color
Background *allg5.Color
2020-04-10 09:00:27 +00:00
OnClick func(*Context)
OnEnter func(*Context)
OnLeave func(*Context)
2020-03-08 09:10:41 +00:00
}
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 }
2020-04-10 09:00:27 +00:00
func (b *ControlBase) Handle(ctx *Context, e allg5.Event) {
2020-03-08 09:10:41 +00:00
switch e := e.(type) {
case *allg5.MouseMoveEvent:
pos := MouseEventToPos(e.MouseEvent)
over := pos.In(b._Bounds)
if over != b.Over {
b.Over = over
if over {
if b.OnEnter != nil {
2020-04-10 09:00:27 +00:00
b.OnEnter(ctx)
2020-03-08 09:10:41 +00:00
}
} else {
if b.OnLeave != nil {
2020-04-10 09:00:27 +00:00
b.OnLeave(ctx)
2020-03-08 09:10:41 +00:00
}
}
}
case *allg5.MouseButtonDownEvent:
if !b.Over {
break
}
if e.Button == allg5.MouseButtonLeft {
if b.OnClick != nil {
2020-04-10 09:00:27 +00:00
b.OnClick(ctx)
2020-03-08 09:10:41 +00:00
}
}
}
}
func (b *ControlBase) Layout(_ *Context, bounds geom.RectangleF32) { b._Bounds = bounds }
func (b *ControlBase) Render(*Context, geom.RectangleF32) {}
func (b *ControlBase) Bounds() geom.RectangleF32 { return b._Bounds }