zntg/ui/control.go

91 lines
1.8 KiB
Go
Raw Normal View History

2018-08-03 06:46:10 +00:00
package ui
import (
"time"
"opslag.de/schobers/zntg/allg5"
2018-08-03 06:46:10 +00:00
"opslag.de/schobers/geom"
)
type Control interface {
Created(Context, Container) error
Destroyed(Context)
Update(Context, time.Duration)
Handle(Context, allg5.Event)
2018-08-03 06:46:10 +00:00
DesiredSize(Context) geom.PointF
Rect() geom.RectangleF
2018-08-03 06:46:10 +00:00
SetRect(geom.RectangleF)
Render(Context)
}
type MouseClickFn func(Control)
var _ Control = &ControlBase{}
type ControlBase struct {
Parent Container
Bounds geom.RectangleF
Disabled bool
IsOver bool
IsPressed bool
OnClick MouseClickFn
MinSize geom.PointF
Background *allg5.Color
2018-08-03 06:46:10 +00:00
}
func (c *ControlBase) Created(_ Context, p Container) error {
c.Parent = p
return nil
}
func (c *ControlBase) Destroyed(Context) {}
func (c *ControlBase) Update(Context, time.Duration) {}
func (c *ControlBase) Handle(ctx Context, ev allg5.Event) {
2018-08-03 06:46:10 +00:00
switch e := ev.(type) {
case *allg5.MouseMoveEvent:
2018-08-03 06:46:10 +00:00
c.IsOver = c.IsInRect(float64(e.X), float64(e.Y))
case *allg5.MouseButtonDownEvent:
2018-08-03 06:46:10 +00:00
if c.IsOver {
c.IsPressed = true
}
case *allg5.MouseButtonUpEvent:
2018-08-03 06:46:10 +00:00
if c.IsPressed && c.IsOver {
var onClick = c.OnClick
if nil != onClick {
onClick(c)
}
}
c.IsPressed = false
}
}
func (c *ControlBase) DesiredSize(Context) geom.PointF {
return c.MinSize
}
func (c *ControlBase) SetRect(rect geom.RectangleF) {
c.Bounds = rect
}
func (c *ControlBase) Render(ctx Context) {
var min = c.Bounds.Min.To32()
var max = c.Bounds.Max.To32()
if nil != c.Background {
allg5.DrawFilledRectangle(min.X, min.Y, max.X, max.Y, *c.Background)
2018-08-03 06:46:10 +00:00
}
if ctx.Debug().IsEnabled() {
allg5.DrawRectangle(min.X, min.Y, max.X, max.Y, ctx.Debug().Rainbow(), 5)
2018-08-03 06:46:10 +00:00
}
}
func (c *ControlBase) Rect() geom.RectangleF {
return c.Bounds
}
func (c *ControlBase) IsInRect(x, y float64) bool {
return geom.PtF(x, y).In(c.Bounds)
}