Files
ordoor/internal/ui/list_box.go

232 lines
5.3 KiB
Go
Raw Normal View History

2020-04-01 01:38:42 +01:00
package ui
import (
"fmt"
2020-04-01 19:45:57 +01:00
"image"
2020-04-01 01:38:42 +01:00
2020-04-01 19:45:57 +01:00
"code.ur.gs/lupine/ordoor/internal/assetstore"
2020-04-01 01:38:42 +01:00
"code.ur.gs/lupine/ordoor/internal/menus"
)
func init() {
registerBuilder(menus.TypeLineKbd, ownedByMenu)
registerBuilder(menus.TypeLineBriefing, ownedByMenu)
registerBuilder(menus.TypeThumb, ownedByMenu)
registerBuilder(menus.TypeListBoxUp, ownedByMenu)
registerBuilder(menus.TypeListBoxDown, ownedByMenu)
}
// listBox is a TListBox in VCL terms. It has a number of lines of text, one of
// which may be selected, and a slider with up and down buttons to scroll if the
// options in the box exceed its viewing capacity.
//
// TODO: multi-select functionality? Is it needed?
type listBox struct {
upBtn *button
downBtn *button
2020-04-01 19:45:57 +01:00
// FIXME: can we share code between slider and this element?
thumbBase *assetstore.Sprite // Bounds are given by this
thumbImg *assetstore.Sprite // This is displayed at offset * (height / steps)
2020-04-01 01:38:42 +01:00
base *noninteractive // The menu itself has a sprite to display
lines []*noninteractive // We display to these
// The list box acts as a window onto these
strings []string
// The start of our window
offset int
}
func registerListBox(d *Driver, menu *menus.Record) ([]*menus.Record, error) {
var upBtn *menus.Record
var downBtn *menus.Record
2020-04-01 19:45:57 +01:00
var thumb *menus.Record
2020-04-01 01:38:42 +01:00
var items []*menus.Record
var otherChildren []*menus.Record
2020-04-01 01:38:42 +01:00
for _, rec := range menu.Children {
switch rec.Type {
case menus.TypeListBoxUp:
if upBtn != nil {
return nil, fmt.Errorf("Duplicate up buttons in menu %v", menu.Locator())
}
upBtn = rec
case menus.TypeListBoxDown:
if downBtn != nil {
return nil, fmt.Errorf("Duplicate down buttons in menu %v", menu.Locator())
}
downBtn = rec
case menus.TypeLineKbd, menus.TypeLineBriefing:
items = append(items, rec)
case menus.TypeThumb:
2020-04-01 19:45:57 +01:00
if thumb != nil {
2020-04-01 01:38:42 +01:00
return nil, fmt.Errorf("Duplicate thumbs in menu %v", menu.Locator())
}
2020-04-01 19:45:57 +01:00
thumb = rec
2020-04-01 01:38:42 +01:00
default:
// e.g. maingame:18.12 includes a button that is not part of the box
otherChildren = append(otherChildren, rec)
2020-04-01 01:38:42 +01:00
}
}
2020-04-01 19:45:57 +01:00
if len(items) == 0 || thumb == nil || upBtn == nil || downBtn == nil {
2020-04-01 01:38:42 +01:00
return nil, fmt.Errorf("Missing items in menu %v", menu.Locator())
}
// Now build the wonderful thing
2020-04-01 19:45:57 +01:00
baseElem, err := registerNoninteractive(d, menu)
if err != nil {
return nil, err
}
upSprId := upBtn.SpriteId[0]
if upSprId == -1 {
upSprId = upBtn.Share
}
elemUp, err := registerButton(d, upBtn, upSprId)
2020-04-01 01:38:42 +01:00
if err != nil {
return nil, err
}
dnSprId := downBtn.SpriteId[0]
if dnSprId == -1 {
dnSprId = downBtn.Share
}
elemDown, err := registerButton(d, downBtn, dnSprId)
2020-04-01 01:38:42 +01:00
if err != nil {
return nil, err
}
thumbBaseSpr, err := d.menu.Sprite(menu.ObjectIdx, thumb.Share)
2020-04-01 19:45:57 +01:00
if err != nil {
return nil, err
}
thumbSprId := thumb.SpriteId[0]
if thumbSprId == -1 {
thumbSprId = thumb.Share
}
thumbImgSpr, err := d.menu.Sprite(menu.ObjectIdx, thumbSprId)
2020-04-01 19:45:57 +01:00
if err != nil {
return nil, err
}
2020-04-01 01:38:42 +01:00
element := &listBox{
2020-04-01 19:45:57 +01:00
base: baseElem,
// TODO: upBtn needs to be frozen when offset == 0; downBtn when offset == max
2020-04-01 01:38:42 +01:00
upBtn: elemUp,
downBtn: elemDown,
2020-04-01 19:45:57 +01:00
// TODO: need to be able to drag the thumb
thumbBase: thumbBaseSpr,
thumbImg: thumbImgSpr,
2020-04-01 01:38:42 +01:00
}
// Internal wiring-up
elemUp.onClick(element.up)
elemDown.onClick(element.down)
// FIXME: Test data for now
for i := 0; i < 50; i++ {
element.strings = append(element.strings, fmt.Sprintf("FOO %v", i))
}
2020-04-01 19:45:57 +01:00
// Register everything. Since we're a composite of other controls, they are
// mostly self-registered at the moment.
d.paintables = append(d.paintables, element)
// FIXME: we should be able to freeze/unfreeze as a group.
// HURK: These need to be registered after the other elements so they are
// drawn in the correct order to be visible
for _, rec := range items {
ni, err := registerNoninteractive(d, rec)
if err != nil {
return nil, err
}
// TODO: pick the correct font
ni.label = &label{
align: AlignModeLeft,
font: d.menu.Font(0),
rect: ni.rect,
}
element.lines = append(element.lines, ni)
}
element.refresh()
2020-04-01 01:38:42 +01:00
return otherChildren, nil
2020-04-01 01:38:42 +01:00
}
func (l *listBox) SetStrings(to []string) {
if len(to) < len(l.strings) {
l.offset = 0 // FIXME: unconditional? Trim to max?
}
l.strings = to
l.refresh()
}
// TODO: Selected returns the index and value of the selected item
func (l *listBox) Selected() (int, string) {
return 0, ""
}
func (l *listBox) up() {
if l.offset <= 0 {
return
}
l.offset -= 1
l.refresh()
}
func (l *listBox) down() {
if l.offset > len(l.strings)-len(l.lines) {
return
}
l.offset += 1
l.refresh()
}
func (l *listBox) refresh() {
for i, ni := range l.lines {
// FIXME: noninteractive isn't set up for dynamic text yet. Need to
// generate textImg on demand instead of once at start.
2020-04-01 19:45:57 +01:00
if ni.label != nil {
ni.label.text = ""
if len(l.strings) > l.offset+i {
ni.label.text = l.strings[l.offset+i]
}
2020-04-01 01:38:42 +01:00
}
}
2020-04-01 19:45:57 +01:00
}
func (l *listBox) thumbPos() image.Point {
pos := l.thumbImg.Rect.Min
if len(l.strings) == 0 {
return pos
}
pixPerLine := (l.thumbBase.Rect.Dy()) / (len(l.strings) - len(l.lines))
pos.Y += pixPerLine * l.offset
return pos
}
func (l *listBox) regions(tick int) []region {
// Draw the slider at the appropriate point
out := oneRegion(l.thumbBase.Rect.Min, l.thumbBase.Image)
out = append(out, oneRegion(l.thumbPos(), l.thumbImg.Image)...)
2020-04-01 01:38:42 +01:00
2020-04-01 19:45:57 +01:00
return out
2020-04-01 01:38:42 +01:00
}