Skip to content

Commit 99a7970

Browse files
committed
Fixed input device user defaults
1 parent bb453c4 commit 99a7970

File tree

1 file changed

+41
-25
lines changed

1 file changed

+41
-25
lines changed

GUI/Controller/MyControllerToolbar.swift

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,58 @@ struct InputDevice {
1818

1919
extension MyController {
2020

21+
func connectPort1(device: Int) {
22+
23+
let cpd: ControlPortDevice =
24+
device == InputDevice.none ? CPD_NONE :
25+
device == InputDevice.mouse ? CPD_MOUSE : CPD_JOYSTICK
26+
27+
amiga.controlPort1.connect(cpd)
28+
inputDevice1 = device
29+
UserDefaults.standard.set(device, forKey: Keys.inputDevice1)
30+
}
31+
32+
func connectPort2(device: Int) {
33+
34+
let cpd: ControlPortDevice =
35+
device == InputDevice.none ? CPD_NONE :
36+
device == InputDevice.mouse ? CPD_MOUSE : CPD_JOYSTICK
37+
38+
amiga.controlPort2.connect(cpd)
39+
inputDevice2 = device
40+
UserDefaults.standard.set(device, forKey: Keys.inputDevice2)
41+
}
42+
43+
/*
2144
func connectPort(port: Int, device: Int) {
2245

2346
assert(port == 1 || port == 2)
2447

2548
let cpd: ControlPortDevice =
26-
device == InputDevice.none ? CPD_NONE :
27-
device == InputDevice.mouse ? CPD_MOUSE : CPD_JOYSTICK
49+
device == InputDevice.none ? CPD_NONE :
50+
device == InputDevice.mouse ? CPD_MOUSE : CPD_JOYSTICK
2851

2952
port == 1 ? amiga.controlPort1.connect(cpd) : amiga.controlPort2.connect(cpd)
3053
}
54+
*/
3155

3256
@IBAction func port1Action(_ sender: NSPopUpButton) {
3357

3458
setPort1(sender.selectedTag())
3559
}
3660

3761
func setPort1(_ value: Int) {
38-
39-
// Remember selection
40-
inputDevice1 = value
41-
62+
63+
let newDevice1 = value
64+
var newDevice2 = inputDevice2
65+
4266
// Avoid double mappings
43-
inputDevice2 = (inputDevice1 == inputDevice2) ? InputDevice.none : inputDevice2
67+
if newDevice1 == newDevice2 { newDevice2 = InputDevice.none }
4468

45-
// Connect devices
46-
connectPort(port: 1, device: inputDevice1)
47-
connectPort(port: 2, device: inputDevice2)
69+
// Update ports
70+
if newDevice1 != inputDevice1 { connectPort1(device: newDevice1) }
71+
if newDevice2 != inputDevice2 { connectPort2(device: newDevice2) }
4872

49-
// Save the port settings in the user defaults
50-
UserDefaults.standard.set(inputDevice1, forKey: Keys.inputDevice1)
51-
UserDefaults.standard.set(inputDevice2, forKey: Keys.inputDevice2)
52-
5373
toolbar.validateVisibleItems()
5474
}
5575

@@ -60,20 +80,16 @@ extension MyController {
6080

6181
func setPort2(_ value: Int) {
6282

63-
// Remember selection
64-
inputDevice2 = value
65-
83+
var newDevice1 = inputDevice1
84+
let newDevice2 = value
85+
6686
// Avoid double mappings
67-
inputDevice1 = (inputDevice1 == inputDevice2) ? InputDevice.none : inputDevice1
87+
if newDevice1 == newDevice2 { newDevice1 = InputDevice.none }
6888

69-
// Connect devices
70-
connectPort(port: 1, device: inputDevice1)
71-
connectPort(port: 2, device: inputDevice2)
89+
// Update ports
90+
if newDevice1 != inputDevice1 { connectPort1(device: newDevice1) }
91+
if newDevice2 != inputDevice2 { connectPort2(device: newDevice2) }
7292

73-
// Save the port settings in the user defaults
74-
UserDefaults.standard.set(inputDevice1, forKey: Keys.inputDevice1)
75-
UserDefaults.standard.set(inputDevice2, forKey: Keys.inputDevice2)
76-
7793
toolbar.validateVisibleItems()
7894
}
7995

0 commit comments

Comments
 (0)