Files
8bitMyGoUI/data/transfer.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

65 lines
1.9 KiB
Go

package data
import (
"log/slog"
"slices"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/icon"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/input"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type TransferOptions struct {
// Height is each list's in dots; zero means 120.
Height int
Disabled bool
}
type TransferResult struct {
Element *ui.Element
Changed bool
}
type transferKey struct{}
// Transfer moves chosen items between lists with → ←.
func Transfer(c *ui.Context, text string, left, right *[]string, o TransferOptions) TransferResult {
k := pixel.K(c)
row := ui.Row(c).Gap(k.D(4)).AlignItems(ui.Center).Role(ui.RoleGroup).Label(text)
sel := ui.Local(row, transferKey{}, func() [2]int { return [2]int{} })
res := TransferResult{Element: row}
// side lists one side's items, its choice kept in range.
side := func(name string, items []string, at *int) {
*at = max(0, min(*at, len(items)-1))
List(c, name, len(items), at, ListOptions{Height: o.Height, Disabled: o.Disabled}, func(i int) {
pixel.Text(c, items[i], pixel.TextOptions{})
}).Element.Width(k.D(100))
}
shift := func(from, to *[]string, at int) {
if at < 0 || at >= len(*from) {
return
}
*to = append(*to, (*from)[at])
*from = slices.Delete(*from, at, at+1)
res.Changed = true
}
row.Children(func() {
side(text+" "+k.Msg("left"), *left, &sel[0])
ui.Column(c).Gap(k.D(4)).Children(func() {
if input.IconButton(c, icon.ArrowRight, k.Msg("right"), input.IconButtonOptions{Disabled: o.Disabled || len(*left) == 0}).Clicked {
shift(left, right, sel[0])
}
if input.IconButton(c, icon.ArrowLeft, k.Msg("left"), input.IconButtonOptions{Disabled: o.Disabled || len(*right) == 0}).Clicked {
shift(right, left, sel[1])
}
})
side(text+" "+k.Msg("right"), *right, &sel[1])
})
if res.Changed {
slog.Debug("8bit: transfer", "label", text, "left", len(*left), "right", len(*right))
}
return res
}