2019-04-11 19:23:51 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"opslag.de/schobers/geom"
|
|
|
|
)
|
|
|
|
|
2019-07-06 05:42:11 +00:00
|
|
|
type SelectedChangedFn func(selected bool)
|
2019-04-11 21:30:26 +00:00
|
|
|
|
2019-04-11 19:23:51 +00:00
|
|
|
type Checkbox struct {
|
|
|
|
ControlBase
|
|
|
|
|
2019-04-11 21:30:26 +00:00
|
|
|
onSelectedChanged SelectedChangedFn
|
|
|
|
|
2019-04-11 19:23:51 +00:00
|
|
|
Selected bool
|
|
|
|
Text string
|
|
|
|
}
|
|
|
|
|
|
|
|
func BuildCheckbox(text string, fn func(c *Checkbox)) *Checkbox {
|
|
|
|
var c = &Checkbox{Text: text}
|
|
|
|
if fn != nil {
|
|
|
|
fn(c)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Checkbox) desiredSize(ctx Context) geom.PointF32 {
|
|
|
|
var pad = ctx.Style().Dimensions.TextPadding
|
|
|
|
var font = ctx.Renderer().Font(c.FontName(ctx))
|
|
|
|
var w, h float32 = 0, font.Height()
|
|
|
|
if len(c.Text) != 0 {
|
|
|
|
w += pad + font.WidthOf(c.Text)
|
|
|
|
}
|
2020-05-12 21:03:43 +00:00
|
|
|
icon, _ := ctx.Textures().ScaledHeight(c.getOrCreateNormalIcon(ctx), h)
|
2019-04-11 19:23:51 +00:00
|
|
|
w += pad + icon.Width()
|
|
|
|
return geom.PtF32(w+pad, pad+h+pad)
|
|
|
|
}
|
|
|
|
|
2020-05-12 21:03:43 +00:00
|
|
|
func (c *Checkbox) icon(ctx Context) Texture {
|
2019-04-11 21:30:26 +00:00
|
|
|
if c.Selected {
|
2019-04-11 19:23:51 +00:00
|
|
|
return GetOrCreateIcon(ctx, "ui-default-checkbox-selected", c.selectedIcon)
|
2019-04-11 21:30:26 +00:00
|
|
|
} else if c.over {
|
|
|
|
return GetOrCreateIcon(ctx, "ui-default-checkbox-hover", c.hoverIcon)
|
2019-04-11 19:23:51 +00:00
|
|
|
}
|
2019-04-11 21:30:26 +00:00
|
|
|
return c.getOrCreateNormalIcon(ctx)
|
|
|
|
}
|
|
|
|
|
2020-05-12 21:03:43 +00:00
|
|
|
func (c *Checkbox) getOrCreateNormalIcon(ctx Context) Texture {
|
2019-04-11 19:23:51 +00:00
|
|
|
return GetOrCreateIcon(ctx, "ui-default-checkbox", c.normalIcon)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Checkbox) iconBorder() geom.PolygonF32 {
|
|
|
|
return geom.PolF32(
|
|
|
|
geom.PtF32(48, 80),
|
|
|
|
geom.PtF32(400, 80),
|
|
|
|
geom.PtF32(400, 432),
|
|
|
|
geom.PtF32(48, 432),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-04-11 21:30:26 +00:00
|
|
|
func (c *Checkbox) checkMark() geom.PointsF32 {
|
|
|
|
return geom.PointsF32{
|
|
|
|
geom.PtF32(96, 256),
|
|
|
|
geom.PtF32(180, 340),
|
|
|
|
geom.PtF32(340, 150),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 17:12:10 +00:00
|
|
|
func (c *Checkbox) hoverIcon() ImagePixelTestFn {
|
2019-04-11 21:30:26 +00:00
|
|
|
border := c.iconBorder()
|
|
|
|
check := c.checkMark()
|
|
|
|
return func(pt geom.PointF32) bool {
|
|
|
|
return (pt.DistanceToPolygon(border) < 48 && !pt.InPolygon(border)) || pt.DistanceToLines(check) < 24
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 17:12:10 +00:00
|
|
|
func (c *Checkbox) normalIcon() ImagePixelTestFn {
|
2019-04-11 19:23:51 +00:00
|
|
|
border := c.iconBorder()
|
|
|
|
return func(pt geom.PointF32) bool {
|
|
|
|
return pt.DistanceToPolygon(border) < 48 && !pt.InPolygon(border)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 17:12:10 +00:00
|
|
|
func (c *Checkbox) selectedIcon() ImagePixelTestFn {
|
2019-04-11 19:23:51 +00:00
|
|
|
border := c.iconBorder()
|
2019-04-11 21:30:26 +00:00
|
|
|
check := c.checkMark()
|
2019-04-11 19:23:51 +00:00
|
|
|
return func(pt geom.PointF32) bool {
|
|
|
|
if pt.DistanceToPolygon(border) < 48 || pt.InPolygon(border) {
|
|
|
|
return pt.DistanceToLines(check) > 24
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Checkbox) DesiredSize(ctx Context) geom.PointF32 { return c.desiredSize(ctx) }
|
|
|
|
|
|
|
|
func (c *Checkbox) Handle(ctx Context, e Event) {
|
|
|
|
switch e := e.(type) {
|
|
|
|
case *MouseButtonDownEvent:
|
|
|
|
if e.Button == MouseButtonLeft && c.over {
|
|
|
|
c.Selected = !c.Selected
|
2019-04-11 21:30:26 +00:00
|
|
|
onSelectedChanged := c.onSelectedChanged
|
|
|
|
if onSelectedChanged != nil {
|
2019-07-06 05:42:11 +00:00
|
|
|
onSelectedChanged(c.Selected)
|
2019-04-11 21:30:26 +00:00
|
|
|
}
|
2019-04-11 19:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
c.ControlBase.Handle(ctx, e)
|
|
|
|
if c.over {
|
|
|
|
ctx.Renderer().SetMouseCursor(MouseCursorPointer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 21:30:26 +00:00
|
|
|
func (c *Checkbox) OnSelectedChanged(fn SelectedChangedFn) {
|
|
|
|
c.onSelectedChanged = fn
|
|
|
|
}
|
|
|
|
|
2019-04-11 19:23:51 +00:00
|
|
|
func (c *Checkbox) Render(ctx Context) {
|
2019-04-11 21:30:26 +00:00
|
|
|
c.RenderBackground(ctx)
|
|
|
|
|
2019-04-11 19:23:51 +00:00
|
|
|
var style = ctx.Style()
|
|
|
|
var palette = style.Palette
|
|
|
|
fore := c.FontColor(ctx)
|
|
|
|
bounds := c.bounds
|
|
|
|
|
|
|
|
var pad = style.Dimensions.TextPadding
|
|
|
|
bounds = bounds.Inset(pad)
|
|
|
|
pos := bounds.Min
|
2020-05-12 21:03:43 +00:00
|
|
|
icon, _ := ctx.Textures().ScaledHeight(c.icon(ctx), bounds.Dy())
|
2019-04-11 19:23:51 +00:00
|
|
|
if icon != nil {
|
|
|
|
iconColor := fore
|
2019-04-11 21:30:26 +00:00
|
|
|
if c.Selected && c.Font.Color == nil {
|
2019-04-11 19:23:51 +00:00
|
|
|
iconColor = palette.Primary
|
|
|
|
}
|
2020-05-12 21:03:43 +00:00
|
|
|
ctx.Renderer().DrawTextureOptions(icon, geom.PtF32(pos.X, pos.Y+.5*(bounds.Dy()-icon.Height())), DrawOptions{Tint: iconColor})
|
2019-04-11 19:23:51 +00:00
|
|
|
pos.X += icon.Width() + pad
|
|
|
|
}
|
|
|
|
if len(c.Text) != 0 {
|
|
|
|
var fontName = c.FontName(ctx)
|
|
|
|
var font = ctx.Renderer().Font(fontName)
|
|
|
|
ctx.Renderer().Text(geom.PtF32(pos.X, pos.Y+.5*(bounds.Dy()-font.Height())), fontName, fore, c.Text)
|
|
|
|
}
|
|
|
|
}
|