Move inventorySelect to its own file

This commit is contained in:
2020-04-01 00:03:43 +01:00
parent c1925697c9
commit 31da40e772
2 changed files with 64 additions and 55 deletions

View File

@@ -0,0 +1,64 @@
package ui
import (
"code.ur.gs/lupine/ordoor/internal/menus"
)
func init() {
registerBuilder(menus.TypeInventorySelect, ownedByMenu)
}
// An inventory select is a sort of radio button. If 2 share the same menu,
// selecting one deselects the other. Otherwise, they act like checkboxes.
//
// TODO: wrap all the behaviour in a single struct to make it easier to drive
type inventorySelect struct {
checkbox
parentPath string
others []*inventorySelect
}
// Called from the menu, which fills "others" for us
func registerInventorySelect(d *Driver, r *menus.Record) (*inventorySelect, error) {
sprites, err := d.menu.Sprites(r.Share, 3) // unchecked, checked, disabled
if err != nil {
return nil, err
}
element := &inventorySelect{
checkbox: checkbox{
button: button{
path: r.Path(),
baseSpr: sprites[0], // unchecked
clickSpr: sprites[1], // checked
frozenSpr: sprites[2], // disabled
hoverImpl: hoverImpl{text: r.Text},
},
valueImpl: valueImpl{str: "0"},
},
}
d.clickables = append(d.clickables, element)
d.freezables = append(d.freezables, element)
d.hoverables = append(d.hoverables, element)
d.paintables = append(d.paintables, element)
d.valueables = append(d.valueables, element)
return element, nil
}
func (i *inventorySelect) registerMouseClick() {
// Do nothing if we're already selected
if i.value() == "1" {
return
}
// Turn us on, turn everyone else off
for _, other := range i.others {
other.setValue("0")
}
i.setValue("1")
}