Files
8bitMyGoUI/data/propertygrid.go
T
ZacharyZhang-NY 03680980da Add data components 151-175; clear MyGo row highlights
Lists, tables, trees, grids, inventories and records in data/.
Outline and GridView build on ui.List: ui.Outline strokes its arrows
and ui.GridView spaces and tints items. The pixel theme keeps MyGo's
row highlights clear so scrolled rows stay on whole dots; off lists
take a veil against the wheel. Adds Canvas.Silhouette, SwitchOptions
Label and exports Rarity.Slot.
2026-10-11 22:29:07 -04:00

64 lines
1.9 KiB
Go

package data
import (
"fmt"
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/input"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
// Property is one row; set exactly one value.
type Property struct {
Name string
Text *string
Number *float64
Switch *bool
// Min and Max bound Number as NumberInput's do.
Min, Max float64
}
type PropertyGridOptions struct {
// NameWidth is the names' column in dots; zero means 80.
NameWidth int
Disabled bool
}
type PropertyGridResult struct {
Element *ui.Element
Changed bool
}
// PropertyGrid edits named text, number and switch values.
func PropertyGrid(c *ui.Context, text string, props []Property, o PropertyGridOptions) PropertyGridResult {
k := pixel.K(c)
nw := pixel.Or("PropertyGrid NameWidth", o.NameWidth, 80)
col := ui.Column(c).Gap(k.D(2)).Role(ui.RoleGroup).Label(text)
res := PropertyGridResult{Element: col}
col.Children(func() {
for _, p := range props {
ui.Row(c).Gap(k.D(4)).AlignItems(ui.Center).Children(func() {
ui.Box(c).Width(k.D(nw)).Shrink(0).Children(func() {
pixel.Text(c, p.Name, pixel.TextOptions{Slot: pixel.Faint})
})
switch {
case p.Text != nil && p.Number == nil && p.Switch == nil:
res.Changed = input.TextInput(c, p.Name, p.Text, input.TextInputOptions{Disabled: o.Disabled}).Changed || res.Changed
case p.Number != nil && p.Text == nil && p.Switch == nil:
res.Changed = input.NumberInput(c, p.Name, p.Number, input.NumberInputOptions{Min: p.Min, Max: p.Max, Disabled: o.Disabled}).Changed || res.Changed
case p.Switch != nil && p.Text == nil && p.Number == nil:
res.Changed = input.Switch(c, "", p.Switch, input.SwitchOptions{Label: p.Name, Disabled: o.Disabled}).Changed || res.Changed
default:
panic(fmt.Sprintf("data: Property %q needs exactly one value", p.Name))
}
})
}
})
if res.Changed {
slog.Debug("8bit: property grid", "label", text)
}
return res
}