Interface is now Driver, and Widget is now a set of interfaces with a struct per widget type. This should make it easier to add other types.
63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
package ui
|
|
|
|
import (
|
|
"code.ur.gs/lupine/ordoor/internal/menus"
|
|
)
|
|
|
|
func init() {
|
|
registerBuilder(menus.TypeCheckbox, registerCheckbox)
|
|
}
|
|
|
|
type checkbox struct {
|
|
button
|
|
|
|
valueImpl
|
|
}
|
|
|
|
// A checkbox has 3 sprites, and 3 states: unchecked, checked, disabled.
|
|
func registerCheckbox(d *Driver, r *menus.Record) error {
|
|
sprites, err := d.menu.Sprites(r.Share, 3) // unchecked, disabled, checked
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
checkbox := &checkbox{
|
|
button: button{
|
|
path: r.Path(),
|
|
baseSpr: sprites[0], // unchecked
|
|
clickSpr: sprites[2], // checked
|
|
frozenSpr: sprites[1],
|
|
hoverImpl: hoverImpl{text: r.Desc},
|
|
},
|
|
valueImpl: valueImpl{str: "0"},
|
|
}
|
|
|
|
d.clickables = append(d.clickables, checkbox)
|
|
d.freezables = append(d.freezables, checkbox)
|
|
d.hoverables = append(d.hoverables, checkbox)
|
|
d.paintables = append(d.paintables, checkbox)
|
|
d.valueables = append(d.valueables, checkbox)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *checkbox) registerMouseClick() {
|
|
if c.value() == "1" { // Click disables
|
|
c.setValue("0")
|
|
} else { // Click enables
|
|
c.setValue("1")
|
|
}
|
|
}
|
|
|
|
func (c *checkbox) regions(tick int) []region {
|
|
if c.isFrozen() {
|
|
return oneRegion(c.bounds().Min, c.frozenSpr.Image)
|
|
}
|
|
|
|
if c.value() == "1" {
|
|
return oneRegion(c.bounds().Min, c.clickSpr.Image)
|
|
}
|
|
|
|
return oneRegion(c.bounds().Min, c.baseSpr.Image)
|
|
}
|