| 1 | HotkeySettings* hotkeySettings = nullptr; |
| 2 | |
| 3 | HotkeySettings::HotkeySettings() { |
| 4 | inputList.setHeaderText({"Name", "Mapping"}); |
| 5 | inputList.setHeaderVisible(); |
| 6 | eraseButton.setText("Erase"); |
| 7 | |
| 8 | append(inputList, {~0, ~0}, 5); |
| 9 | append(controlLayout, {~0, 0}); |
| 10 | controlLayout.append(spacer, {~0, 0}); |
| 11 | controlLayout.append(eraseButton, {80, 0}); |
| 12 | |
| 13 | inputList.onChange = {&HotkeySettings::synchronize, this}; |
| 14 | inputList.onActivate = {&HotkeySettings::assignInput, this}; |
| 15 | eraseButton.onActivate = {&HotkeySettings::eraseInput, this}; |
| 16 | |
| 17 | for(auto& hotkey : inputManager->hotkeyMap) inputList.append({"", ""}); |
| 18 | refresh(); |
| 19 | } |
| 20 | |
| 21 | void HotkeySettings::synchronize() { |
| 22 | eraseButton.setEnabled(inputList.selected()); |
| 23 | } |
| 24 | |
| 25 | void HotkeySettings::refresh() { |
| 26 | unsigned index = 0; |
| 27 | for(auto& hotkey : inputManager->hotkeyMap) { |
| 28 | string mapping = inputManager->sanitize(hotkey->mapping, " and "); |
| 29 | inputList.setText(index++, {hotkey->name, mapping}); |
| 30 | } |
| 31 | synchronize(); |
| 32 | } |
| 33 | |
| 34 | void HotkeySettings::eraseInput() { |
| 35 | activeInput = inputManager->hotkeyMap[inputList.selection()]; |
| 36 | inputEvent(hidNull, 0, 0, 0, 1); |
| 37 | } |
| 38 | |
| 39 | void HotkeySettings::assignInput() { |
| 40 | activeInput = inputManager->hotkeyMap[inputList.selection()]; |
| 41 | |
| 42 | settings->setStatusText({"Set assignment for [", activeInput->name, "] ..."}); |
| 43 | settings->layout.setEnabled(false); |
| 44 | setEnabled(false); |
| 45 | } |
| 46 | |
| 47 | void HotkeySettings::inputEvent(HID::Device& device, unsigned group, unsigned input, int16_t oldValue, int16_t newValue) { |
| 48 | if(activeInput == nullptr) return; |
| 49 | if(device.isMouse()) return; |
| 50 | if(device.isJoypad() && group == HID::Joypad::GroupID::Axis) return; |
| 51 | if(activeInput->bind(device, group, input, oldValue, newValue) == false) return; |
| 52 | |
| 53 | activeInput = nullptr; |
| 54 | settings->setStatusText(""); |
| 55 | settings->layout.setEnabled(true); |
| 56 | setEnabled(true); |
| 57 | refresh(); |
| 58 | } |