|
| 1 | +/* |
| 2 | +Toolbox Aid |
| 3 | +David Quesenberry |
| 4 | +05/23/2026 |
| 5 | +InputComboState.js |
| 6 | +*/ |
| 7 | + |
| 8 | +export default class InputComboState { |
| 9 | + constructor({ requiredInputCount = 2 } = {}) { |
| 10 | + this.requiredInputCount = Math.max(2, Number(requiredInputCount) || 2); |
| 11 | + this.reset(); |
| 12 | + } |
| 13 | + |
| 14 | + begin({ actionLabel = 'selected action', deviceLabel = 'Combo' } = {}) { |
| 15 | + this.active = true; |
| 16 | + this.actionLabel = String(actionLabel || 'selected action'); |
| 17 | + this.deviceLabel = String(deviceLabel || 'Combo'); |
| 18 | + this.inputs = []; |
| 19 | + return { |
| 20 | + ok: true, |
| 21 | + waiting: true, |
| 22 | + message: `Combo capture: press any two keyboard, mouse, wheel, or game controller inputs for ${this.actionLabel}.`, |
| 23 | + engine: 'InputService ComboState' |
| 24 | + }; |
| 25 | + } |
| 26 | + |
| 27 | + reset() { |
| 28 | + this.active = false; |
| 29 | + this.actionLabel = ''; |
| 30 | + this.deviceLabel = 'Combo'; |
| 31 | + this.inputs = []; |
| 32 | + } |
| 33 | + |
| 34 | + record(input, { silentDuplicate = false } = {}) { |
| 35 | + if (!input || !input.binding) { |
| 36 | + return { |
| 37 | + ok: false, |
| 38 | + message: 'Combo capture requires a valid input from the engine input service.' |
| 39 | + }; |
| 40 | + } |
| 41 | + if (!this.active) { |
| 42 | + this.begin(); |
| 43 | + } |
| 44 | + if (this.inputs.some((candidate) => candidate.binding === input.binding)) { |
| 45 | + return { |
| 46 | + ok: false, |
| 47 | + duplicate: true, |
| 48 | + silent: silentDuplicate === true, |
| 49 | + message: 'Combo capture needs two different inputs.' |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + this.inputs.push({ ...input }); |
| 54 | + if (this.inputs.length < this.requiredInputCount) { |
| 55 | + return { |
| 56 | + ok: true, |
| 57 | + waiting: true, |
| 58 | + count: this.inputs.length, |
| 59 | + message: `Combo capture recorded ${comboCaptureInputLabel(input)}. Waiting for second input.` |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + const comboInput = this.createComboInput(); |
| 64 | + this.active = false; |
| 65 | + return { |
| 66 | + ok: true, |
| 67 | + complete: true, |
| 68 | + count: this.inputs.length, |
| 69 | + input: comboInput, |
| 70 | + message: `${comboInput.label} captured.` |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + createComboInput() { |
| 75 | + const parts = this.inputs.slice(0, this.requiredInputCount); |
| 76 | + const comboLabel = parts.map(comboInputLabel).join(' + '); |
| 77 | + return { |
| 78 | + source: parts.every((input) => input.source === parts[0].source) ? parts[0].source : 'keyboard', |
| 79 | + binding: `Combo:${parts.map((input) => input.binding).join('+')}`, |
| 80 | + displayLabelLines: ['Combo', comboLabel], |
| 81 | + label: `Combo ${comboLabel}`, |
| 82 | + title: parts.map((input) => input.title || input.label).join('\n+\n'), |
| 83 | + engine: 'InputService ComboState' |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + isBindingActive(binding, activeInputBindings) { |
| 88 | + const activeBindings = toBindingSet(activeInputBindings); |
| 89 | + const bindingText = String(binding || ''); |
| 90 | + if (activeBindings.has(bindingText)) { |
| 91 | + return true; |
| 92 | + } |
| 93 | + if (!bindingText.startsWith('Combo:')) { |
| 94 | + return false; |
| 95 | + } |
| 96 | + const parts = bindingText.slice('Combo:'.length).split('+').filter(Boolean); |
| 97 | + return parts.length > 0 && parts.every((part) => ( |
| 98 | + liveBindingCandidates(part).some((candidate) => activeBindings.has(candidate)) |
| 99 | + )); |
| 100 | + } |
| 101 | + |
| 102 | + decorateActions(actions = [], activeInputBindings = new Set()) { |
| 103 | + return actions.map((action) => ({ |
| 104 | + ...action, |
| 105 | + inputs: (action.inputs || []).map((input) => ({ |
| 106 | + ...input, |
| 107 | + isActionActive: this.isBindingActive(input.binding, activeInputBindings) |
| 108 | + })) |
| 109 | + })); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +function toBindingSet(activeInputBindings) { |
| 114 | + if (activeInputBindings instanceof Set) { |
| 115 | + return activeInputBindings; |
| 116 | + } |
| 117 | + if (Array.isArray(activeInputBindings)) { |
| 118 | + return new Set(activeInputBindings); |
| 119 | + } |
| 120 | + return new Set(); |
| 121 | +} |
| 122 | + |
| 123 | +function liveBindingCandidates(binding) { |
| 124 | + const bindingText = String(binding || ''); |
| 125 | + const candidates = [bindingText]; |
| 126 | + if (bindingText.startsWith('Pad')) { |
| 127 | + const [pad, control] = bindingText.split(':'); |
| 128 | + if (pad && control) { |
| 129 | + candidates.push(`${pad}:${control}`); |
| 130 | + } |
| 131 | + return candidates; |
| 132 | + } |
| 133 | + const baseBinding = bindingText.split(':')[0]; |
| 134 | + if (baseBinding && baseBinding !== bindingText) { |
| 135 | + candidates.push(baseBinding); |
| 136 | + } |
| 137 | + return candidates; |
| 138 | +} |
| 139 | + |
| 140 | +function comboInputLabel(input) { |
| 141 | + if (input.source === 'keyboard') { |
| 142 | + return keyboardComboLabel(input.binding); |
| 143 | + } |
| 144 | + if (input.source === 'mouse') { |
| 145 | + return input.label.replace(/^Mouse\s+/, 'Mouse '); |
| 146 | + } |
| 147 | + if (input.source === 'gamepad') { |
| 148 | + return input.label.replace(/^Game Controller\s+/, 'Game Controller '); |
| 149 | + } |
| 150 | + return input.label; |
| 151 | +} |
| 152 | + |
| 153 | +function comboCaptureInputLabel(input) { |
| 154 | + if (input.source === 'keyboard') { |
| 155 | + return `Keyboard ${input.binding}`; |
| 156 | + } |
| 157 | + return comboInputLabel(input); |
| 158 | +} |
| 159 | + |
| 160 | +function keyboardComboLabel(binding) { |
| 161 | + const namedKeys = { |
| 162 | + AltLeft: 'Alt', |
| 163 | + AltRight: 'Alt', |
| 164 | + Backspace: 'Backspace', |
| 165 | + ControlLeft: 'Ctrl', |
| 166 | + ControlRight: 'Ctrl', |
| 167 | + Delete: 'Delete', |
| 168 | + Enter: 'Enter', |
| 169 | + Escape: 'Esc', |
| 170 | + MetaLeft: 'Meta', |
| 171 | + MetaRight: 'Meta', |
| 172 | + ShiftLeft: 'Shift', |
| 173 | + ShiftRight: 'Shift', |
| 174 | + Space: 'Space', |
| 175 | + Tab: 'Tab' |
| 176 | + }; |
| 177 | + if (namedKeys[binding]) { |
| 178 | + return namedKeys[binding]; |
| 179 | + } |
| 180 | + if (/^Key[A-Z]$/.test(binding)) { |
| 181 | + return binding.slice(3); |
| 182 | + } |
| 183 | + if (/^Digit[0-9]$/.test(binding)) { |
| 184 | + return binding.slice(5); |
| 185 | + } |
| 186 | + return binding; |
| 187 | +} |
0 commit comments