// Copyright (c) 2026 Dominic Masters // // This software is released under the MIT License. // https://opensource.org/licenses/MIT // Label + a "< option >"-style value label, cycling a caller-provided // options array. Same directionInput()-consumes-LEFT/RIGHT shape as // Slider, stepping the selected index (with wraparound) instead of a // numeric value. class Dropdown extends UIElement { init() { this.label = new Label(); this.add(this.label); this.valueLabel = new Label(); this.valueLabel.x = 100; this.add(this.valueLabel); this.options = []; this._index = 0; this.highlighted = false; this.onChange = null; this._refresh(); } get text() { return this.label.text; } set text(value) { this.label.text = value; } get selectedIndex() { return this._index; } set selectedIndex(index) { if(this.options.length === 0) { this._index = 0; this._refresh(); return; } const count = this.options.length; this._index = ((index % count) + count) % count; this._refresh(); if(this.onChange) this.onChange(this._index, this.options[this._index]); } get selected() { return this.options[this._index]; } setHighlighted(highlighted) { this.highlighted = highlighted; this.label.color = highlighted ? Dropdown.COLOR_HIGHLIGHTED : Dropdown.COLOR_NORMAL; } directionInput(dx, dy) { if(dx === 0) return false; this.selectedIndex = this._index + dx; return true; } _refresh() { const value = this.options[this._index]; this.valueLabel.text = value !== undefined ? ('< ' + value + ' >') : '< >'; } } Dropdown.COLOR_NORMAL = { r: 255, g: 255, b: 255, a: 255 }; Dropdown.COLOR_HIGHLIGHTED = { r: 255, g: 0, b: 0, a: 255 }; module.exports = Dropdown;