Move inventorySelect to its own file
This commit is contained in:
64
internal/ui/inventory_select.go
Normal file
64
internal/ui/inventory_select.go
Normal 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")
|
||||
}
|
Reference in New Issue
Block a user