2018-08-03 06:46:10 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2018-09-22 14:23:26 +00:00
|
|
|
"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)
|
2018-09-22 14:23:26 +00:00
|
|
|
Handle(Context, allg5.Event)
|
2018-08-03 06:46:10 +00:00
|
|
|
DesiredSize(Context) geom.PointF
|
2018-09-09 19:07:53 +00:00
|
|
|
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
|
2018-09-22 14:23:26 +00:00
|
|
|
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) {}
|
|
|
|
|
2018-09-22 14:23:26 +00:00
|
|
|
func (c *ControlBase) Handle(ctx Context, ev allg5.Event) {
|
2018-08-03 06:46:10 +00:00
|
|
|
switch e := ev.(type) {
|
2018-09-22 14:23:26 +00:00
|
|
|
case *allg5.MouseMoveEvent:
|
2018-08-03 06:46:10 +00:00
|
|
|
c.IsOver = c.IsInRect(float64(e.X), float64(e.Y))
|
2018-09-22 14:23:26 +00:00
|
|
|
case *allg5.MouseButtonDownEvent:
|
2018-08-03 06:46:10 +00:00
|
|
|
if c.IsOver {
|
|
|
|
c.IsPressed = true
|
|
|
|
}
|
2018-09-22 14:23:26 +00:00
|
|
|
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 {
|
2018-09-22 14:23:26 +00:00
|
|
|
allg5.DrawFilledRectangle(min.X, min.Y, max.X, max.Y, *c.Background)
|
2018-08-03 06:46:10 +00:00
|
|
|
}
|
|
|
|
if ctx.Debug().IsEnabled() {
|
2018-09-22 14:23:26 +00:00
|
|
|
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)
|
|
|
|
}
|