Sander Schobers
cdfb863ab0
Added Action{,s}. List of actions that can be used to defer cleanup code (see NewRenderer implementations). Added TextInputEvent (replaces the old KeyPressEvent) and added to new events KeyDown & KeyUp. Added VSync to NewRendererOptions. Removed IconScale from button. Added ImageSource interface that replaces the Image/Texture method on the Texture interface. This makes converting back a texture to an image optional (since this is atypical for a hardware texture for instance). Added new KeyModifier: OSCommand (Windows/Command key). Added KeyState that can keep the state of keys (pressed or not). Added KeyEnter, representing the Enter key. Changed signatures of CreateTexture methods in Renderer. Changed signatures of icon related method (removed factories). Basic example now depends on sdlgui.
135 lines
3.5 KiB
Go
135 lines
3.5 KiB
Go
package ui
|
|
|
|
import (
|
|
"opslag.de/schobers/geom"
|
|
)
|
|
|
|
type SelectedChangedFn func(selected bool)
|
|
|
|
type Checkbox struct {
|
|
ControlBase
|
|
|
|
onSelectedChanged SelectedChangedFn
|
|
|
|
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)
|
|
}
|
|
icon := c.getOrCreateNormalIcon(ctx)
|
|
_, iconWidth := ScaleToHeight(SizeOfTexture(icon), h)
|
|
w += pad + iconWidth
|
|
return geom.PtF32(w+pad, pad+h+pad)
|
|
}
|
|
|
|
func (c *Checkbox) icon(ctx Context) Texture {
|
|
if c.Selected {
|
|
return GetOrCreateIcon(ctx, "ui-default-checkbox-selected", c.selectedIcon)
|
|
} else if c.over {
|
|
return GetOrCreateIcon(ctx, "ui-default-checkbox-hover", c.hoverIcon)
|
|
}
|
|
return c.getOrCreateNormalIcon(ctx)
|
|
}
|
|
|
|
func (c *Checkbox) getOrCreateNormalIcon(ctx Context) Texture {
|
|
return GetOrCreateIcon(ctx, "ui-default-checkbox", c.normalIcon)
|
|
}
|
|
|
|
var checkBoxIconBorder = geom.PolF32(
|
|
geom.PtF32(48, 80),
|
|
geom.PtF32(400, 80),
|
|
geom.PtF32(400, 432),
|
|
geom.PtF32(48, 432),
|
|
)
|
|
|
|
var checkBoxCheckMark = geom.PointsF32{
|
|
geom.PtF32(96, 256),
|
|
geom.PtF32(180, 340),
|
|
geom.PtF32(340, 150),
|
|
}
|
|
|
|
func (c *Checkbox) hoverIcon(pt geom.PointF32) bool {
|
|
return (pt.DistanceToPolygon(checkBoxIconBorder) < 48 && !pt.InPolygon(checkBoxIconBorder)) || pt.DistanceToLines(checkBoxCheckMark) < 24
|
|
}
|
|
|
|
func (c *Checkbox) normalIcon(pt geom.PointF32) bool {
|
|
return pt.DistanceToPolygon(checkBoxIconBorder) < 48 && !pt.InPolygon(checkBoxIconBorder)
|
|
}
|
|
|
|
func (c *Checkbox) selectedIcon(pt geom.PointF32) bool {
|
|
if pt.DistanceToPolygon(checkBoxIconBorder) < 48 || pt.InPolygon(checkBoxIconBorder) {
|
|
return pt.DistanceToLines(checkBoxCheckMark) > 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
|
|
onSelectedChanged := c.onSelectedChanged
|
|
if onSelectedChanged != nil {
|
|
onSelectedChanged(c.Selected)
|
|
}
|
|
}
|
|
}
|
|
c.ControlBase.Handle(ctx, e)
|
|
if c.over {
|
|
ctx.Renderer().SetMouseCursor(MouseCursorPointer)
|
|
}
|
|
}
|
|
|
|
func (c *Checkbox) OnSelectedChanged(fn SelectedChangedFn) {
|
|
c.onSelectedChanged = fn
|
|
}
|
|
|
|
func (c *Checkbox) Render(ctx Context) {
|
|
c.RenderBackground(ctx)
|
|
|
|
var style = ctx.Style()
|
|
var palette = style.Palette
|
|
fore := c.FontColor(ctx)
|
|
bounds := c.bounds
|
|
|
|
var pad = style.Dimensions.TextPadding
|
|
bounds = bounds.Inset(pad)
|
|
boundsH := bounds.Dy()
|
|
pos := bounds.Min
|
|
icon := c.icon(ctx)
|
|
if icon != nil {
|
|
iconColor := fore
|
|
if c.Selected && c.Font.Color == nil {
|
|
iconColor = palette.Primary
|
|
}
|
|
scaledIcon, _ := ctx.Textures().ScaledHeight(icon, boundsH) // try to pre-scale icon
|
|
if scaledIcon == nil { // let the renderer scale
|
|
scaledIcon = icon
|
|
}
|
|
scale, iconWidth := ScaleToHeight(SizeOfTexture(scaledIcon), boundsH)
|
|
ctx.Renderer().DrawTextureOptions(scaledIcon, geom.PtF32(pos.X, pos.Y), DrawOptions{Tint: iconColor, Scale: scale})
|
|
pos.X += iconWidth + 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*(boundsH-font.Height())), fontName, fore, c.Text)
|
|
}
|
|
}
|