| 1 | InputSettings* inputSettings = nullptr; |
| 2 | |
| 3 | InputSettings::InputSettings() { |
| 4 | focusLabel.setText("When Focus is Lost:"); |
| 5 | focusPause.setText("Pause Emulation"); |
| 6 | focusAllow.setText("Allow Input"); |
| 7 | inputList.setHeaderText({"Name", "Mapping"}); |
| 8 | inputList.setHeaderVisible(); |
| 9 | resetButton.setText("Reset"); |
| 10 | eraseButton.setText("Erase"); |
| 11 | |
| 12 | append(focusLayout, {~0, 0}, 5); |
| 13 | focusLayout.append(focusLabel, {0, 0}, 5); |
| 14 | focusLayout.append(focusPause, {0, 0}, 5); |
| 15 | focusLayout.append(focusAllow, {0, 0}); |
| 16 | append(selectionLayout, {~0, 0}, 5); |
| 17 | selectionLayout.append(systemList, {~0, 0}, 5); |
| 18 | selectionLayout.append(portList, {~0, 0}, 5); |
| 19 | selectionLayout.append(deviceList, {~0, 0}); |
| 20 | append(inputList, {~0, ~0}, 5); |
| 21 | append(controlLayout, {~0, 0}); |
| 22 | controlLayout.append(assign[0], {100, 0}, 5); |
| 23 | controlLayout.append(assign[1], {100, 0}, 5); |
| 24 | controlLayout.append(assign[2], {100, 0}, 5); |
| 25 | controlLayout.append(spacer, {~0, 0}); |
| 26 | controlLayout.append(resetButton, {80, 0}, 5); |
| 27 | controlLayout.append(eraseButton, {80, 0}); |
| 28 | |
| 29 | for(auto& emulator : program->emulator) { |
| 30 | systemList.append(emulator->information.name); |
| 31 | } |
| 32 | |
| 33 | focusPause.setChecked(config->input.focus.pause); |
| 34 | focusAllow.setChecked(config->input.focus.allow); |
| 35 | focusAllow.setEnabled(!config->input.focus.pause); |
| 36 | |
| 37 | focusPause.onToggle = [&] { config->input.focus.pause = focusPause.checked(); focusAllow.setEnabled(!focusPause.checked()); }; |
| 38 | focusAllow.onToggle = [&] { config->input.focus.allow = focusAllow.checked(); }; |
| 39 | systemList.onChange = {&InputSettings::systemChanged, this}; |
| 40 | portList.onChange = {&InputSettings::portChanged, this}; |
| 41 | deviceList.onChange = {&InputSettings::deviceChanged, this}; |
| 42 | inputList.onChange = {&InputSettings::synchronize, this}; |
| 43 | inputList.onActivate = {&InputSettings::assignInput, this}; |
| 44 | assign[0].onActivate = [&] { assignMouseInput(0); }; |
| 45 | assign[1].onActivate = [&] { assignMouseInput(1); }; |
| 46 | assign[2].onActivate = [&] { assignMouseInput(2); }; |
| 47 | resetButton.onActivate = {&InputSettings::resetInput, this}; |
| 48 | eraseButton.onActivate = {&InputSettings::eraseInput, this}; |
| 49 | |
| 50 | systemChanged(); |
| 51 | } |
| 52 | |
| 53 | void InputSettings::synchronize() { |
| 54 | if(inputList.selected() == false) { |
| 55 | assign[0].setVisible(false); |
| 56 | assign[1].setVisible(false); |
| 57 | assign[2].setVisible(false); |
| 58 | } else { |
| 59 | unsigned number = activeDevice().order[inputList.selection()]; |
| 60 | auto& input = activeDevice().input[number]; |
| 61 | auto selectedInput = inputManager->inputMap[input.guid]; |
| 62 | |
| 63 | if(dynamic_cast<DigitalInput*>(selectedInput)) { |
| 64 | assign[0].setText("Mouse Left"); |
| 65 | assign[1].setText("Mouse Middle"); |
| 66 | assign[2].setText("Mouse Right"); |
| 67 | assign[0].setVisible(true); |
| 68 | assign[1].setVisible(true); |
| 69 | assign[2].setVisible(true); |
| 70 | } |
| 71 | |
| 72 | if(dynamic_cast<RelativeInput*>(selectedInput)) { |
| 73 | assign[0].setText("Mouse X-axis"); |
| 74 | assign[1].setText("Mouse Y-axis"); |
| 75 | assign[0].setVisible(true); |
| 76 | assign[1].setVisible(true); |
| 77 | assign[2].setVisible(false); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | eraseButton.setEnabled(inputList.selected()); |
| 82 | } |
| 83 | |
| 84 | Emulator::Interface& InputSettings::activeSystem() { |
| 85 | return *program->emulator[systemList.selection()]; |
| 86 | } |
| 87 | |
| 88 | Emulator::Interface::Port& InputSettings::activePort() { |
| 89 | return activeSystem().port[portList.selection()]; |
| 90 | } |
| 91 | |
| 92 | Emulator::Interface::Device& InputSettings::activeDevice() { |
| 93 | return activePort().device[deviceList.selection()]; |
| 94 | } |
| 95 | |
| 96 | void InputSettings::systemChanged() { |
| 97 | portList.reset(); |
| 98 | for(auto& port : activeSystem().port) { |
| 99 | portList.append(port.name); |
| 100 | } |
| 101 | portChanged(); |
| 102 | } |
| 103 | |
| 104 | void InputSettings::portChanged() { |
| 105 | deviceList.reset(); |
| 106 | for(auto& device : activePort().device) { |
| 107 | deviceList.append(device.name); |
| 108 | } |
| 109 | deviceChanged(); |
| 110 | } |
| 111 | |
| 112 | void InputSettings::deviceChanged() { |
| 113 | inputList.reset(); |
| 114 | for(unsigned number : activeDevice().order) inputList.append({"", ""}); |
| 115 | inputChanged(); |
| 116 | synchronize(); |
| 117 | } |
| 118 | |
| 119 | void InputSettings::inputChanged() { |
| 120 | unsigned index = 0; |
| 121 | for(unsigned number : activeDevice().order) { |
| 122 | auto& input = activeDevice().input[number]; |
| 123 | auto abstract = inputManager->inputMap(input.guid); |
| 124 | string mapping = inputManager->sanitize(abstract->mapping, " or "); |
| 125 | inputList.setText(index++, {input.name, mapping}); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void InputSettings::resetInput() { |
| 130 | if(MessageWindow().setParent(*settings).setText("All inputs will be erased. Are you sure you want to do this?") |
| 131 | .question() == MessageWindow::Response::No) return; |
| 132 | |
| 133 | auto& device = activeDevice(); |
| 134 | unsigned length = device.input.size(); |
| 135 | for(unsigned n = 0; n < length; n++) { |
| 136 | activeInput = inputManager->inputMap[device.input[n].guid]; |
| 137 | inputEvent(hidNull, 0, 0, 0, 1); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void InputSettings::eraseInput() { |
| 142 | unsigned number = activeDevice().order[inputList.selection()]; |
| 143 | auto& input = activeDevice().input[number]; |
| 144 | activeInput = inputManager->inputMap[input.guid]; |
| 145 | inputEvent(hidNull, 0, 0, 0, 1); |
| 146 | } |
| 147 | |
| 148 | void InputSettings::assignInput() { |
| 149 | unsigned number = activeDevice().order[inputList.selection()]; |
| 150 | auto& input = activeDevice().input[number]; |
| 151 | activeInput = inputManager->inputMap[input.guid]; |
| 152 | |
| 153 | settings->setStatusText({"Set assignment for [", activeDevice().name, "::", input.name, "] ..."}); |
| 154 | settings->layout.setEnabled(false); |
| 155 | setEnabled(false); |
| 156 | } |
| 157 | |
| 158 | void InputSettings::assignMouseInput(unsigned n) { |
| 159 | unsigned number = activeDevice().order[inputList.selection()]; |
| 160 | auto& input = activeDevice().input[number]; |
| 161 | activeInput = inputManager->inputMap[input.guid]; |
| 162 | |
| 163 | if(dynamic_cast<DigitalInput*>(activeInput)) { |
| 164 | if(auto hidMouse = inputManager->findMouse()) { |
| 165 | return inputEvent(*hidMouse, HID::Mouse::GroupID::Button, n, 0, 1, true); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if(dynamic_cast<RelativeInput*>(activeInput)) { |
| 170 | if(auto hidMouse = inputManager->findMouse()) { |
| 171 | return inputEvent(*hidMouse, HID::Mouse::GroupID::Axis, n, 0, +32767, true); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | void InputSettings::inputEvent(HID::Device& device, unsigned group, unsigned input, int16_t oldValue, int16_t newValue, bool allowMouseInput) { |
| 177 | if(activeInput == nullptr) return; |
| 178 | if(allowMouseInput == false && device.isMouse()) return; |
| 179 | if(activeInput->bind(device, group, input, oldValue, newValue) == false) return; |
| 180 | |
| 181 | activeInput = nullptr; |
| 182 | inputChanged(); |
| 183 | settings->setStatusText(""); |
| 184 | settings->layout.setEnabled(true); |
| 185 | setEnabled(true); |
| 186 | synchronize(); |
| 187 | } |