Refactor inventorySelect to build it hierarchically
This commit is contained in:
42
internal/ui/menus.go
Normal file
42
internal/ui/menus.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user