2019-03-05 20:52:18 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image/color"
|
|
|
|
|
|
|
|
"opslag.de/schobers/geom"
|
|
|
|
)
|
|
|
|
|
2019-07-06 05:42:11 +00:00
|
|
|
type ClickFn func(pos geom.PointF32, btn MouseButton)
|
|
|
|
type DragEndFn func(start, end geom.PointF32)
|
|
|
|
type DragMoveFn func(start, move geom.PointF32)
|
|
|
|
type DragStartFn func(start geom.PointF32)
|
|
|
|
type MouseEnterFn func()
|
|
|
|
type MouseLeaveFn func()
|
2019-03-05 20:52:18 +00:00
|
|
|
|
|
|
|
var _ Control = &ControlBase{}
|
|
|
|
|
|
|
|
type ControlBase struct {
|
|
|
|
bounds geom.RectangleF32
|
|
|
|
offset geom.PointF32
|
2019-04-11 21:38:32 +00:00
|
|
|
parent Control
|
2019-03-05 20:52:18 +00:00
|
|
|
dragStart *geom.PointF32
|
|
|
|
over bool
|
|
|
|
pressed bool
|
|
|
|
|
|
|
|
onClick ClickFn
|
|
|
|
onDragEnd DragEndFn
|
|
|
|
onDragMove DragMoveFn
|
|
|
|
onDragStart DragStartFn
|
|
|
|
|
2019-06-24 18:58:14 +00:00
|
|
|
Background color.Color
|
|
|
|
Font FontStyle
|
|
|
|
TextAlignment HorizontalAlignment
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 21:38:32 +00:00
|
|
|
func (c *ControlBase) Arrange(ctx Context, bounds geom.RectangleF32, offset geom.PointF32, parent Control) {
|
2019-03-05 20:52:18 +00:00
|
|
|
c.bounds = bounds
|
|
|
|
c.offset = offset
|
2019-04-11 21:38:32 +00:00
|
|
|
c.parent = parent
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 21:38:32 +00:00
|
|
|
func (c *ControlBase) Bounds() geom.RectangleF32 { return c.bounds }
|
2019-03-05 20:52:18 +00:00
|
|
|
|
2019-04-11 21:38:32 +00:00
|
|
|
func (c *ControlBase) DesiredSize(Context) geom.PointF32 { return geom.ZeroPtF32 }
|
2019-04-10 19:23:56 +00:00
|
|
|
|
2019-03-05 20:52:18 +00:00
|
|
|
func (c *ControlBase) Handle(ctx Context, e Event) {
|
|
|
|
var over = func(e MouseEvent) bool {
|
2019-04-11 21:38:32 +00:00
|
|
|
pos := e.Pos()
|
|
|
|
if !c.IsInBounds(pos) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
parent := c.Parent()
|
|
|
|
for parent != nil {
|
|
|
|
if !parent.IsInBounds(pos) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
parent = parent.Parent()
|
|
|
|
}
|
|
|
|
return true
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
switch e := e.(type) {
|
|
|
|
case *MouseMoveEvent:
|
2019-04-11 21:38:32 +00:00
|
|
|
c.over = over(e.MouseEvent)
|
2019-03-05 20:52:18 +00:00
|
|
|
if c.pressed {
|
|
|
|
if c.dragStart == nil {
|
2019-04-11 21:38:32 +00:00
|
|
|
var start = c.ToControlPosition(e.Pos())
|
2019-03-05 20:52:18 +00:00
|
|
|
c.dragStart = &start
|
|
|
|
if c.onDragStart != nil {
|
2019-07-06 05:42:11 +00:00
|
|
|
c.onDragStart(start)
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var start = *c.dragStart
|
2019-04-11 21:38:32 +00:00
|
|
|
var move = c.ToControlPosition(e.Pos())
|
2019-03-05 20:52:18 +00:00
|
|
|
if c.onDragMove != nil {
|
2019-07-06 05:42:11 +00:00
|
|
|
c.onDragMove(start, move)
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-24 19:56:52 +00:00
|
|
|
case *MouseLeaveEvent:
|
|
|
|
c.over = false
|
2019-03-05 20:52:18 +00:00
|
|
|
case *MouseButtonDownEvent:
|
2019-04-11 21:38:32 +00:00
|
|
|
c.over = over(e.MouseEvent)
|
|
|
|
if c.over && e.Button == MouseButtonLeft {
|
2019-03-05 20:52:18 +00:00
|
|
|
c.pressed = true
|
|
|
|
}
|
|
|
|
case *MouseButtonUpEvent:
|
2019-04-10 19:23:56 +00:00
|
|
|
if e.Button == MouseButtonLeft {
|
2019-03-05 20:52:18 +00:00
|
|
|
if c.dragStart != nil {
|
|
|
|
var start = *c.dragStart
|
2019-04-11 21:38:32 +00:00
|
|
|
var end = c.ToControlPosition(e.Pos())
|
2019-03-05 20:52:18 +00:00
|
|
|
c.dragStart = nil
|
|
|
|
if c.onDragEnd != nil {
|
2019-07-06 05:42:11 +00:00
|
|
|
c.onDragEnd(start, end)
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.pressed {
|
|
|
|
if c.onClick != nil {
|
2019-07-06 05:42:11 +00:00
|
|
|
c.onClick(c.ToControlPosition(e.Pos()), e.Button)
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
c.pressed = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-12 18:43:52 +00:00
|
|
|
func (c *ControlBase) FontColor(ctx Context) color.Color {
|
|
|
|
var text = c.Font.Color
|
|
|
|
if text == nil {
|
|
|
|
text = ctx.Style().Palette.Text
|
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ControlBase) FontName(ctx Context) string {
|
|
|
|
var name = c.Font.Name
|
|
|
|
if len(name) == 0 {
|
|
|
|
name = ctx.Style().Fonts.Default
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2019-04-11 21:38:32 +00:00
|
|
|
func (c *ControlBase) IsInBounds(p geom.PointF32) bool {
|
|
|
|
bounds := c.bounds
|
|
|
|
if bounds.Min.X < 0 {
|
|
|
|
bounds.Min.X = 0
|
|
|
|
}
|
|
|
|
if bounds.Min.Y < 0 {
|
|
|
|
bounds.Min.Y = 0
|
|
|
|
}
|
|
|
|
return c.ToControlPosition(p).In(c.bounds)
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:52:18 +00:00
|
|
|
func (c *ControlBase) IsOver() bool { return c.over }
|
|
|
|
|
2019-04-11 21:38:32 +00:00
|
|
|
func (c *ControlBase) Parent() Control { return c.parent }
|
|
|
|
|
2019-03-05 20:52:18 +00:00
|
|
|
func (c *ControlBase) IsPressed() bool { return c.pressed }
|
|
|
|
|
2019-03-05 21:42:57 +00:00
|
|
|
func (c *ControlBase) Offset() geom.PointF32 { return c.offset }
|
|
|
|
|
2019-04-11 21:38:32 +00:00
|
|
|
func (c *ControlBase) OnClick(fn ClickFn) { c.onClick = fn }
|
2019-03-05 20:52:18 +00:00
|
|
|
|
|
|
|
func (c *ControlBase) OnDragStart(fn DragStartFn) {
|
|
|
|
c.onDragStart = fn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ControlBase) OnDragMove(fn DragMoveFn) {
|
|
|
|
c.onDragMove = fn
|
|
|
|
}
|
|
|
|
|
2019-04-11 21:38:32 +00:00
|
|
|
func (c *ControlBase) OnDragEnd(fn DragEndFn) { c.onDragEnd = fn }
|
|
|
|
|
2019-04-11 21:27:25 +00:00
|
|
|
func (c *ControlBase) Render(Context) {}
|
2019-03-05 20:52:18 +00:00
|
|
|
|
|
|
|
func (c *ControlBase) RenderBackground(ctx Context) {
|
|
|
|
if c.Background != nil {
|
|
|
|
ctx.Renderer().FillRectangle(c.bounds, c.Background)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 21:27:25 +00:00
|
|
|
func (c *ControlBase) RenderOutline(ctx Context) {
|
|
|
|
style := ctx.Style()
|
|
|
|
width := style.Dimensions.OutlineWidth
|
|
|
|
color := style.Palette.Primary
|
|
|
|
if c.Font.Color != nil {
|
|
|
|
color = c.Font.Color
|
|
|
|
}
|
|
|
|
ctx.Renderer().Rectangle(c.bounds.Inset(.5*width), color, width)
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
2019-04-11 21:38:32 +00:00
|
|
|
|
|
|
|
func (c *ControlBase) ToControlPosition(p geom.PointF32) geom.PointF32 { return p.Sub(c.offset) }
|