Refactor inventorySelect to build it hierarchically

This commit is contained in:
2020-03-31 23:45:11 +01:00
parent 2ae3611d7f
commit c1925697c9
3 changed files with 52 additions and 33 deletions

42
internal/ui/menus.go Normal file
View File

@@ -0,0 +1,42 @@
package ui
import (
"code.ur.gs/lupine/ordoor/internal/menus"
)
func init() {
// These menu types don't need driving, so we can ignore them
registerBuilder(menus.TypeMenu, registerMenu)
registerBuilder(menus.TypeDragMenu, nil) // Menus are just containers
}
func registerMenu(d *Driver, r *menus.Record) ([]*menus.Record, error) {
// Group all inventory selects that share a menu together
var childrenLeft []*menus.Record
var inventorySelects []*inventorySelect
for _, child := range r.Children {
switch child.Type {
case menus.TypeInventorySelect:
is, err := registerInventorySelect(d, child)
if err != nil {
return nil, err
}
inventorySelects = append(inventorySelects, is)
default:
childrenLeft = append(childrenLeft, child)
}
}
if len(inventorySelects) > 0 {
inventorySelects[0].setValue("1") // Always start with one selected
for _, is := range inventorySelects {
is.others = inventorySelects
}
}
// Return all the unhandled children to be processed further
return childrenLeft, nil
}