From 8b8a5fc109521b6e2bc432d86a7e65d72f740c73 Mon Sep 17 00:00:00 2001 From: Xyct <87l46110@gmail.com> Date: Wed, 14 Sep 2022 19:12:52 +0800 Subject: [PATCH 1/8] introduce GCController --- PlayTools/Controls/PlayInput.swift | 12 ++++++++++++ PlayTools/Keymap/KeyCodeNames.swift | 1 + PlayTools/Keymap/Keymapping.swift | 8 +++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/PlayTools/Controls/PlayInput.swift b/PlayTools/Controls/PlayInput.swift index fd93736c..2cb6f78e 100644 --- a/PlayTools/Controls/PlayInput.swift +++ b/PlayTools/Controls/PlayInput.swift @@ -65,6 +65,14 @@ final class PlayInput: NSObject { self.swapMode(pressed) } } + + if let controller = GCController.current?.extendedGamepad { + controller.valueChangedHandler = { gamepad, element in + // This is the index of controller buttons, which is String, not Int + let alias: String! = element.aliases.first + Toast.showOver(msg: alias) + } + } } static public func cmdPressed() -> Bool { @@ -123,6 +131,10 @@ final class PlayInput: NSObject { centre.addObserver(forName: NSNotification.Name.GCMouseDidConnect, object: nil, queue: main) { _ in PlayInput.shared.setup() } + + centre.addObserver(forName: NSNotification.Name.GCControllerDidConnect, object: nil, queue: main) { _ in + PlayInput.shared.setup() + } setup() // fix beep sound diff --git a/PlayTools/Keymap/KeyCodeNames.swift b/PlayTools/Keymap/KeyCodeNames.swift index 517ef0ca..ea417cdc 100644 --- a/PlayTools/Keymap/KeyCodeNames.swift +++ b/PlayTools/Keymap/KeyCodeNames.swift @@ -12,6 +12,7 @@ class KeyCodeNames { -13: "L2", -14: "R1", -15: "R2", + -16: "Controller", -1: "LMB", -2: "RMB", -3: "MMB", diff --git a/PlayTools/Keymap/Keymapping.swift b/PlayTools/Keymap/Keymapping.swift index 5aaa4486..49f60e69 100644 --- a/PlayTools/Keymap/Keymapping.swift +++ b/PlayTools/Keymap/Keymapping.swift @@ -70,9 +70,10 @@ struct KeyModelTransform: Codable { var xCoord: CGFloat var yCoord: CGFloat } - +// controller buttons are indexed with names struct Button: Codable { var keyCode: Int + var keyName: String var transform: KeyModelTransform } @@ -81,10 +82,15 @@ struct Joystick: Codable { var rightKeyCode: Int var downKeyCode: Int var leftKeyCode: Int + var upKeyName: String + var rightKeyName: String + var downKeyName: String + var leftKeyName: String var transform: KeyModelTransform } struct MouseArea: Codable { + var keyName: String var transform: KeyModelTransform } From 40b3a6ccf59fee4e7765c17055e2b9b25c9ad7fc Mon Sep 17 00:00:00 2001 From: Xyct <87l46110@gmail.com> Date: Wed, 14 Sep 2022 20:26:10 +0800 Subject: [PATCH 2/8] no need to map controller buttons to joystick --- PlayTools/Keymap/Keymapping.swift | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/PlayTools/Keymap/Keymapping.swift b/PlayTools/Keymap/Keymapping.swift index 49f60e69..c0031c05 100644 --- a/PlayTools/Keymap/Keymapping.swift +++ b/PlayTools/Keymap/Keymapping.swift @@ -75,6 +75,17 @@ struct Button: Codable { var keyCode: Int var keyName: String var transform: KeyModelTransform + + init(keyCode: Int, transform: KeyModelTransform) { + self.keyCode = keyCode + self.transform = transform + self.keyName = KeyCodeNames.keyCodes[keyCode] ?? "" + } + init(keyName: String, transform: KeyModelTransform) { + self.keyCode = -16 + self.transform = transform + self.keyName = keyName + } } struct Joystick: Codable { @@ -82,16 +93,20 @@ struct Joystick: Codable { var rightKeyCode: Int var downKeyCode: Int var leftKeyCode: Int - var upKeyName: String - var rightKeyName: String - var downKeyName: String - var leftKeyName: String var transform: KeyModelTransform } struct MouseArea: Codable { var keyName: String var transform: KeyModelTransform + init(transform: KeyModelTransform) { + self.transform = transform + self.keyName = "Mouse" + } + init(keyName: String, transform: KeyModelTransform) { + self.transform = transform + self.keyName = keyName + } } struct KeymappingData: Codable { From db196fee0b3ac4c07b4d07e736a96ab7ed54789f Mon Sep 17 00:00:00 2001 From: Xyct <87l46110@gmail.com> Date: Mon, 26 Sep 2022 02:43:09 +0800 Subject: [PATCH 3/8] display controller buttons in editor --- PlayTools/Controls/PlayAction.swift | 61 ++++++++++++++++-------- PlayTools/Controls/PlayInput.swift | 7 ++- PlayTools/Controls/PlayMice.swift | 4 +- PlayTools/Keymap/ControlModel.swift | 62 ++++++++++++++----------- PlayTools/Keymap/EditorController.swift | 10 +++- PlayTools/Keymap/KeyCodeNames.swift | 4 +- PlayTools/Keymap/Keymapping.swift | 8 +--- 7 files changed, 97 insertions(+), 59 deletions(-) diff --git a/PlayTools/Controls/PlayAction.swift b/PlayTools/Controls/PlayAction.swift index eb0b8a25..7d314d44 100644 --- a/PlayTools/Controls/PlayAction.swift +++ b/PlayTools/Controls/PlayAction.swift @@ -19,41 +19,64 @@ extension GCKeyboard { class ButtonAction: Action { func invalidate() { Toucher.touchcam(point: point, phase: UITouch.Phase.ended, tid: id) - if let keyboard = GCKeyboard.coalesced?.keyboardInput { - keyboard.button(forKeyCode: key)?.pressedChangedHandler = nil + if let gcKey = GCKeyboard.coalesced?.keyboardInput?.button(forKeyCode: keyCode) { + gcKey.pressedChangedHandler = nil + + } else if let gcControllerElement = GCController.current?.extendedGamepad?.elements[keyName] { + + if let gcControllerButton = gcControllerElement as? GCControllerButtonInput { + gcControllerButton.pressedChangedHandler = nil + } + } } - let key: GCKeyCode + let keyCode: GCKeyCode + let keyName: String let keyid: Int let point: CGPoint var id: Int - init(id: Int, keyid: Int, key: GCKeyCode, point: CGPoint) { + private func getChangedHandler(handler: ((T1, Float, Bool) -> Void)?) -> (T1, Float, Bool) -> Void { + return { button, value, pressed in + if !mode.visible && !PlayInput.cmdPressed() { + self.update(pressed: pressed) + } + if let previous = handler { + previous(button, value, pressed) + } + } + } + + init(id: Int, keyid: Int, keyCode: GCKeyCode, keyName: String, point: CGPoint) { self.keyid = keyid - self.key = key + self.keyCode = keyCode + self.keyName = keyName self.point = point self.id = id - if let keyboard = GCKeyboard.coalesced?.keyboardInput { - if !PlayMice.shared.setMiceButtons(keyid, action: self) { - let handler = keyboard.button(forKeyCode: key)?.pressedChangedHandler - keyboard.button(forKeyCode: key)?.pressedChangedHandler = { button, value, pressed in - if !mode.visible && !PlayInput.cmdPressed() { - self.update(pressed: pressed) - } - if let previous = handler { - previous(button, value, pressed) - } - } + if PlayMice.shared.setMiceButtons(keyid, action: self) { + // No more work to do for mouse buttons + } else if let gcKey = GCKeyboard.coalesced?.keyboardInput?.button(forKeyCode: keyCode) { + let handler = gcKey.pressedChangedHandler + gcKey.pressedChangedHandler = getChangedHandler(handler: handler) + + } else if let gcControllerElement = GCController.current?.extendedGamepad?.elements[keyName] { + + if let gcControllerButton = gcControllerElement as? GCControllerButtonInput { + let handler = gcControllerButton.pressedChangedHandler + gcControllerButton.pressedChangedHandler = getChangedHandler(handler: handler) } + } } convenience init(id: Int, data: Button) { + let keyCode = GCKeyCode(rawValue: CFIndex(data.keyCode)) self.init( id: id, keyid: data.keyCode, - key: GCKeyCode(rawValue: CFIndex(data.keyCode)), + keyCode: keyCode, + keyName: data.keyName, point: CGPoint( x: data.transform.xCoord.absoluteX, y: data.transform.yCoord.absoluteY)) @@ -73,9 +96,9 @@ class DraggableButtonAction: ButtonAction { var releasePoint: CGPoint - override init(id: Int, keyid: Int, key: GCKeyCode, point: CGPoint) { + override init(id: Int, keyid: Int, keyCode: GCKeyCode, keyName: String, point: CGPoint) { self.releasePoint = point - super.init(id: id, keyid: keyid, key: key, point: point) + super.init(id: id, keyid: keyid, keyCode: keyCode, keyName: keyName, point: point) if settings.mouseMapping { PlayMice.shared.setupMouseMovedHandler() } diff --git a/PlayTools/Controls/PlayInput.swift b/PlayTools/Controls/PlayInput.swift index 2cb6f78e..f5285a11 100644 --- a/PlayTools/Controls/PlayInput.swift +++ b/PlayTools/Controls/PlayInput.swift @@ -49,7 +49,7 @@ final class PlayInput: NSObject { && !PlayInput.cmdPressed() && !PlayInput.FORBIDDEN.contains(keyCode) && self.isSafeToBind(keyboard) { - EditorController.shared.setKeyCode(keyCode.rawValue) + EditorController.shared.setKey(keyCode.rawValue) } } keyboard.button(forKeyCode: GCKeyCode(rawValue: 227))?.pressedChangedHandler = { _, _, pressed in @@ -71,6 +71,9 @@ final class PlayInput: NSObject { // This is the index of controller buttons, which is String, not Int let alias: String! = element.aliases.first Toast.showOver(msg: alias) + if editor.editorMode { + EditorController.shared.setKey(alias) + } } } } @@ -131,7 +134,7 @@ final class PlayInput: NSObject { centre.addObserver(forName: NSNotification.Name.GCMouseDidConnect, object: nil, queue: main) { _ in PlayInput.shared.setup() } - + centre.addObserver(forName: NSNotification.Name.GCControllerDidConnect, object: nil, queue: main) { _ in PlayInput.shared.setup() } diff --git a/PlayTools/Controls/PlayMice.swift b/PlayTools/Controls/PlayMice.swift index 4065db42..375d755e 100644 --- a/PlayTools/Controls/PlayMice.swift +++ b/PlayTools/Controls/PlayMice.swift @@ -100,9 +100,9 @@ typealias ResponseBlockBool = @convention(block) (_ event: Any) -> Bool return nil } else if EditorController.shared.editorMode { if _up == 8 { - EditorController.shared.setKeyCode(-2) + EditorController.shared.setKey(-2) } else if _up == 33554432 { - EditorController.shared.setKeyCode(-3) + EditorController.shared.setKey(-3) } } return event diff --git a/PlayTools/Keymap/ControlModel.swift b/PlayTools/Keymap/ControlModel.swift index 1385242a..19bffba6 100644 --- a/PlayTools/Keymap/ControlModel.swift +++ b/PlayTools/Keymap/ControlModel.swift @@ -2,6 +2,7 @@ import GameController @objc class ControlData: NSObject { var keyCodes: [Int] + var keyName: String var size: CGFloat var xCoord: CGFloat var yCoord: CGFloat @@ -9,6 +10,7 @@ import GameController init(keyCodes: [Int], size: CGFloat, xCoord: CGFloat, yCoord: CGFloat, parent: ControlModel?) { self.keyCodes = keyCodes + self.keyName = KeyCodeNames.keyCodes[keyCodes[0]] ?? "Btn" self.size = size self.xCoord = xCoord self.yCoord = yCoord @@ -17,6 +19,7 @@ import GameController init(keyCodes: [Int], size: CGFloat, xCoord: CGFloat, yCoord: CGFloat, sensitivity: CGFloat) { self.keyCodes = keyCodes + self.keyName = KeyCodeNames.keyCodes[keyCodes[0]] ?? "Btn" self.size = size self.xCoord = xCoord self.yCoord = yCoord @@ -24,6 +27,7 @@ import GameController init(keyCodes: [Int], parent: ControlModel) { self.keyCodes = keyCodes + self.keyName = KeyCodeNames.keyCodes[keyCodes[0]] ?? "Btn" self.size = parent.data.size / 3 self.xCoord = 0 self.yCoord = 0 @@ -32,6 +36,7 @@ import GameController init(size: CGFloat, xCoord: CGFloat, yCoord: CGFloat) { self.keyCodes = [0] + self.keyName = "Mouse" self.size = size self.xCoord = xCoord self.yCoord = yCoord @@ -40,6 +45,7 @@ import GameController init(keyCodes: [Int], size: CGFloat, xCoord: CGFloat, yCoord: CGFloat) { self.keyCodes = keyCodes + self.keyName = KeyCodeNames.keyCodes[keyCodes[0]] ?? "Btn" self.size = size self.xCoord = xCoord self.yCoord = yCoord @@ -95,7 +101,15 @@ class ControlModel { update() } - func setKeyCodes(keys: [Int]) {} + func setKey(codes: [Int], name: String) {} + + func setKey(codes: [Int]) { + self.setKey(codes: codes, name: KeyCodeNames.keyCodes[codes[0]] ?? "Btn") + } + + func setKey(name: String) { + self.setKey(codes: [-10], name: name) + } } class ButtonModel: ControlModel { @@ -107,12 +121,12 @@ class ButtonModel: ControlModel { func save() -> Button { Button( - keyCode: data.keyCodes[0], + keyCode: data.keyCodes[0], keyName: data.keyName, transform: KeyModelTransform(size: data.size, xCoord: data.xCoord, yCoord: data.yCoord)) } override func update() { - self.setKeyCodes(keys: data.keyCodes) + self.setKey(codes: data.keyCodes, name: data.keyName) button.setWidth(width: data.size.absoluteSize) button.setHeight(height: data.size.absoluteSize) button.setX(xCoord: data.xCoord.absoluteX) @@ -137,20 +151,17 @@ class ButtonModel: ControlModel { } } - override func setKeyCodes(keys: [Int]) { - data.keyCodes = keys - if let title = KeyCodeNames.keyCodes[keys[0]] { - button.setTitle(title, for: UIControl.State.normal) - } else { - button.setTitle("Btn", for: UIControl.State.normal) - } + override func setKey(codes: [Int], name: String) { + data.keyCodes = codes + data.keyName = name + button.setTitle(data.keyName, for: UIControl.State.normal) } } class JoystickButtonModel: ControlModel { override init(data: ControlData) { super.init(data: data) - self.setKeyCodes(keys: data.keyCodes) + self.setKey(codes: data.keyCodes) data.parent?.button.addSubview(button) } @@ -158,13 +169,10 @@ class JoystickButtonModel: ControlModel { data.parent?.button.removeFromSuperview() } - override func setKeyCodes(keys: [Int]) { - data.keyCodes = keys - if let title = KeyCodeNames.keyCodes[keys[0]] { - button.setTitle(title, for: UIControl.State.normal) - } else { - button.setTitle("Btn", for: UIControl.State.normal) - } + override func setKey(codes: [Int], name: String) { + data.keyCodes = codes + data.keyName = name + button.setTitle(data.keyName, for: UIControl.State.normal) } override func move(deltaY: CGFloat, deltaX: CGFloat) { @@ -194,12 +202,13 @@ class DraggableButtonModel: MouseAreaModel { var childButton: JoystickButtonModel? func save() -> Button { - return Button(keyCode: childButton!.data.keyCodes[0], + return Button(keyCode: childButton!.data.keyCodes[0], keyName: data.keyName, transform: KeyModelTransform(size: data.size, xCoord: data.xCoord, yCoord: data.yCoord)) } - override func setKeyCodes(keys: [Int]) { - childButton!.setKeyCodes(keys: keys) + override func setKey(codes: [Int], name: String) { + childButton!.setKey(codes: codes) + self.data.keyName = name } override func focus(_ focus: Bool) { super.focus(focus) @@ -229,6 +238,7 @@ class JoystickModel: ControlModel { rightKeyCode: joystickButtons[3].data.keyCodes[0], downKeyCode: joystickButtons[1].data.keyCodes[0], leftKeyCode: joystickButtons[2].data.keyCodes[0], + keyName: self.data.keyName, transform: KeyModelTransform(size: data.size, xCoord: data.xCoord, yCoord: data.yCoord)) } @@ -244,6 +254,7 @@ class JoystickModel: ControlModel { button.setY(yCoord: data.yCoord.absoluteY) button.layer.cornerRadius = 0.5 * button.bounds.size.width button.clipsToBounds = true + self.setKey(name: data.keyName) if data.keyCodes.count == 4 && joystickButtons.count == 0 { for keyCode in data.keyCodes { joystickButtons.append(JoystickButtonModel(data: ControlData(keyCodes: [keyCode], parent: self))) @@ -270,9 +281,8 @@ class JoystickModel: ControlModel { } } - override func setKeyCodes(keys: [Int]) { - // I'm trying to be an easter egg - Toast.showOver(msg: "U~w~U") + override func setKey(codes: [Int], name: String) { + self.data.keyName = name } override func resize(down: Bool) { @@ -326,9 +336,9 @@ class MouseAreaModel: ControlModel { button.clipsToBounds = true } - override func setKeyCodes(keys: [Int]) { + override func setKey(codes: [Int]) { EditorController.shared.removeControl() - EditorController.shared.addDraggableButton(CGPoint(x: data.xCoord, y: data.yCoord), keys[0]) + EditorController.shared.addDraggableButton(CGPoint(x: data.xCoord, y: data.yCoord), codes[0]) } override init(data: ControlData) { diff --git a/PlayTools/Keymap/EditorController.swift b/PlayTools/Keymap/EditorController.swift index 1933951c..0c2f305a 100644 --- a/PlayTools/Keymap/EditorController.swift +++ b/PlayTools/Keymap/EditorController.swift @@ -75,9 +75,15 @@ final class EditorController: NSObject { var editorMode: Bool { !(editorWindow?.isHidden ?? true)} - public func setKeyCode(_ key: Int) { + public func setKey(_ code: Int) { if editorMode { - focusedControl?.setKeyCodes(keys: [key]) + focusedControl?.setKey(codes: [code]) + } + } + + public func setKey(_ name: String) { + if editorMode { + focusedControl?.setKey(name: name) } } diff --git a/PlayTools/Keymap/KeyCodeNames.swift b/PlayTools/Keymap/KeyCodeNames.swift index ea417cdc..9dbaeec1 100644 --- a/PlayTools/Keymap/KeyCodeNames.swift +++ b/PlayTools/Keymap/KeyCodeNames.swift @@ -6,13 +6,13 @@ class KeyCodeNames { -7: "cY", -8: "dU", -9: "dD", - -10: "dR", +// -10: "dR", + -10: "Controller", -11: "dL", -12: "L1", -13: "L2", -14: "R1", -15: "R2", - -16: "Controller", -1: "LMB", -2: "RMB", -3: "MMB", diff --git a/PlayTools/Keymap/Keymapping.swift b/PlayTools/Keymap/Keymapping.swift index c0031c05..7832efbc 100644 --- a/PlayTools/Keymap/Keymapping.swift +++ b/PlayTools/Keymap/Keymapping.swift @@ -76,14 +76,9 @@ struct Button: Codable { var keyName: String var transform: KeyModelTransform - init(keyCode: Int, transform: KeyModelTransform) { + init(keyCode: Int, keyName: String, transform: KeyModelTransform) { self.keyCode = keyCode self.transform = transform - self.keyName = KeyCodeNames.keyCodes[keyCode] ?? "" - } - init(keyName: String, transform: KeyModelTransform) { - self.keyCode = -16 - self.transform = transform self.keyName = keyName } } @@ -93,6 +88,7 @@ struct Joystick: Codable { var rightKeyCode: Int var downKeyCode: Int var leftKeyCode: Int + var keyName: String = "Keyboard" var transform: KeyModelTransform } From 79df64d7676385408fa47f0183c0ecd0b1999bf9 Mon Sep 17 00:00:00 2001 From: Xyct <87l46110@gmail.com> Date: Tue, 27 Sep 2022 14:01:21 +0800 Subject: [PATCH 4/8] editor aware of key name --- PlayTools/Keymap/ControlModel.swift | 40 ++++++-------- PlayTools/Keymap/EditorController.swift | 70 +++++++++++-------------- PlayTools/Keymap/Keymapping.swift | 6 --- 3 files changed, 48 insertions(+), 68 deletions(-) diff --git a/PlayTools/Keymap/ControlModel.swift b/PlayTools/Keymap/ControlModel.swift index 19bffba6..7368c019 100644 --- a/PlayTools/Keymap/ControlModel.swift +++ b/PlayTools/Keymap/ControlModel.swift @@ -8,25 +8,20 @@ import GameController var yCoord: CGFloat var parent: ControlModel? - init(keyCodes: [Int], size: CGFloat, xCoord: CGFloat, yCoord: CGFloat, parent: ControlModel?) { + init(keyCodes: [Int], keyName: String, size: CGFloat, + xCoord: CGFloat, yCoord: CGFloat, parent: ControlModel? = nil) { self.keyCodes = keyCodes - self.keyName = KeyCodeNames.keyCodes[keyCodes[0]] ?? "Btn" + self.keyName = keyName self.size = size self.xCoord = xCoord self.yCoord = yCoord self.parent = parent } - init(keyCodes: [Int], size: CGFloat, xCoord: CGFloat, yCoord: CGFloat, sensitivity: CGFloat) { - self.keyCodes = keyCodes - self.keyName = KeyCodeNames.keyCodes[keyCodes[0]] ?? "Btn" - self.size = size - self.xCoord = xCoord - self.yCoord = yCoord - } - init(keyCodes: [Int], parent: ControlModel) { self.keyCodes = keyCodes + // For now, not support binding controller key + // Support for that is left for later to concern self.keyName = KeyCodeNames.keyCodes[keyCodes[0]] ?? "Btn" self.size = parent.data.size / 3 self.xCoord = 0 @@ -34,18 +29,9 @@ import GameController self.parent = parent } - init(size: CGFloat, xCoord: CGFloat, yCoord: CGFloat) { + init(keyName: String, size: CGFloat, xCoord: CGFloat, yCoord: CGFloat) { self.keyCodes = [0] - self.keyName = "Mouse" - self.size = size - self.xCoord = xCoord - self.yCoord = yCoord - self.parent = nil - } - - init(keyCodes: [Int], size: CGFloat, xCoord: CGFloat, yCoord: CGFloat) { - self.keyCodes = keyCodes - self.keyName = KeyCodeNames.keyCodes[keyCodes[0]] ?? "Btn" + self.keyName = keyName self.size = size self.xCoord = xCoord self.yCoord = yCoord @@ -161,6 +147,8 @@ class ButtonModel: ControlModel { class JoystickButtonModel: ControlModel { override init(data: ControlData) { super.init(data: data) + // joystick buttons cannot be mapped to controller keys. + // Instead, map a real joystick to the joystick as a whole. self.setKey(codes: data.keyCodes) data.parent?.button.addSubview(button) } @@ -202,7 +190,7 @@ class DraggableButtonModel: MouseAreaModel { var childButton: JoystickButtonModel? func save() -> Button { - return Button(keyCode: childButton!.data.keyCodes[0], keyName: data.keyName, + return Button(keyCode: childButton!.data.keyCodes[0], keyName: childButton!.data.keyName, transform: KeyModelTransform(size: data.size, xCoord: data.xCoord, yCoord: data.yCoord)) } @@ -219,7 +207,10 @@ class DraggableButtonModel: MouseAreaModel { override func update() { super.update() if childButton == nil { - childButton = JoystickButtonModel(data: ControlData(keyCodes: [data.keyCodes[0]], parent: self)) + // temporarily, cannot map controller keys to draggable buttons + // `data.keyName` is the key for the move area, not that of the button key. + childButton = JoystickButtonModel(data: ControlData( + keyCodes: [data.keyCodes[0]], parent: self)) } let btn = button.subviews[0] let buttonSize = data.size.absoluteSize / 3 @@ -257,7 +248,8 @@ class JoystickModel: ControlModel { self.setKey(name: data.keyName) if data.keyCodes.count == 4 && joystickButtons.count == 0 { for keyCode in data.keyCodes { - joystickButtons.append(JoystickButtonModel(data: ControlData(keyCodes: [keyCode], parent: self))) + joystickButtons.append(JoystickButtonModel(data: ControlData( + keyCodes: [keyCode], parent: self))) } } changeButtonsSize() diff --git a/PlayTools/Keymap/EditorController.swift b/PlayTools/Keymap/EditorController.swift index 0c2f305a..d95185a6 100644 --- a/PlayTools/Keymap/EditorController.swift +++ b/PlayTools/Keymap/EditorController.swift @@ -96,24 +96,25 @@ final class EditorController: NSObject { for button in keymap.keymapData.buttonModels { let ctrl = ButtonModel(data: ControlData( keyCodes: [button.keyCode], + keyName: button.keyName, size: button.transform.size, xCoord: button.transform.xCoord, - yCoord: button.transform.yCoord, - parent: nil)) + yCoord: button.transform.yCoord)) addControlToView(control: ctrl) } for button in keymap.keymapData.draggableButtonModels { let ctrl = DraggableButtonModel(data: ControlData( keyCodes: [button.keyCode], + keyName: button.keyName, size: button.transform.size, xCoord: button.transform.xCoord, - yCoord: button.transform.yCoord, - parent: nil)) + yCoord: button.transform.yCoord)) addControlToView(control: ctrl) } for mouse in keymap.keymapData.mouseAreaModel { let ctrl = MouseAreaModel(data: ControlData( + keyName: mouse.keyName, size: mouse.transform.size, xCoord: mouse.transform.xCoord, yCoord: mouse.transform.yCoord)) @@ -122,6 +123,7 @@ final class EditorController: NSObject { for joystick in keymap.keymapData.joystickModel { let ctrl = JoystickModel(data: ControlData( keyCodes: [joystick.upKeyCode, joystick.downKeyCode, joystick.leftKeyCode, joystick.rightKeyCode], + keyName: joystick.keyName, size: joystick.transform.size, xCoord: joystick.transform.xCoord, yCoord: joystick.transform.yCoord)) @@ -157,66 +159,58 @@ final class EditorController: NSObject { GCKeyCode.keyS.rawValue, GCKeyCode.keyA.rawValue, GCKeyCode.keyD.rawValue], + keyName: "Keyboard", size: 20, xCoord: center.x.relativeX, yCoord: center.y.relativeY))) } } - @objc public func addButton(_ toPoint: CGPoint) { + private func addButton(keyCode: Int, point: CGPoint) { if editorMode { - addControlToView(control: ButtonModel(data: ControlData(keyCodes: [-1], - size: 5, - xCoord: toPoint.x.relativeX, - yCoord: toPoint.y.relativeY, - parent: nil))) + addControlToView(control: ButtonModel(data: ControlData( + keyCodes: [keyCode], + keyName: KeyCodeNames.keyCodes[keyCode] ?? "Btn", + size: 5, + xCoord: point.x.relativeX, + yCoord: point.y.relativeY))) } } + @objc public func addButton(_ toPoint: CGPoint) { + self.addLMB(toPoint) + } + @objc public func addRMB(_ toPoint: CGPoint) { - if editorMode { - addControlToView(control: ButtonModel(data: ControlData(keyCodes: [-2], - size: 5, - xCoord: toPoint.x.relativeX, - yCoord: toPoint.y.relativeY, - parent: nil))) - } + self.addButton(keyCode: -2, point: toPoint) } @objc public func addLMB(_ toPoint: CGPoint) { - if editorMode { - addControlToView(control: ButtonModel(data: ControlData(keyCodes: [-1], - size: 5, - xCoord: toPoint.x.relativeX, - yCoord: toPoint.y.relativeY, - parent: nil))) - } + self.addButton(keyCode: -1, point: toPoint) } @objc public func addMMB(_ toPoint: CGPoint) { - if editorMode { - addControlToView(control: ButtonModel(data: ControlData(keyCodes: [-3], - size: 5, - xCoord: toPoint.x.relativeX, - yCoord: toPoint.y.relativeY, - parent: nil))) - } + self.addButton(keyCode: -3, point: toPoint) } @objc public func addMouseArea(_ center: CGPoint) { if editorMode { - addControlToView(control: MouseAreaModel(data: ControlData(size: 25, - xCoord: center.x.relativeX, - yCoord: center.y.relativeY))) + addControlToView(control: MouseAreaModel(data: ControlData( + keyName: "Mouse", + size: 25, + xCoord: center.x.relativeX, + yCoord: center.y.relativeY))) } } @objc public func addDraggableButton(_ center: CGPoint, _ keyCode: Int) { if editorMode { - addControlToView(control: DraggableButtonModel(data: ControlData(keyCodes: [keyCode], - size: 15, - xCoord: center.x, - yCoord: center.y))) + addControlToView(control: DraggableButtonModel(data: ControlData( + keyCodes: [keyCode], + keyName: KeyCodeNames.keyCodes[keyCode] ?? "Btn", + size: 15, + xCoord: center.x, + yCoord: center.y))) } } diff --git a/PlayTools/Keymap/Keymapping.swift b/PlayTools/Keymap/Keymapping.swift index 7832efbc..48427b38 100644 --- a/PlayTools/Keymap/Keymapping.swift +++ b/PlayTools/Keymap/Keymapping.swift @@ -75,12 +75,6 @@ struct Button: Codable { var keyCode: Int var keyName: String var transform: KeyModelTransform - - init(keyCode: Int, keyName: String, transform: KeyModelTransform) { - self.keyCode = keyCode - self.transform = transform - self.keyName = keyName - } } struct Joystick: Codable { From 4f466783ffefc26224e0ef84571c41ae75aef4fc Mon Sep 17 00:00:00 2001 From: Xyct <87l46110@gmail.com> Date: Sun, 2 Oct 2022 17:55:43 +0800 Subject: [PATCH 5/8] map thumbsticks to mouse area & draggable button --- PlayTools/Controls/PlayAction.swift | 4 +- PlayTools/Controls/PlayInput.swift | 21 ++++- PlayTools/Controls/PlayMice.swift | 75 ++++++++++++------ PlayTools/Keymap/ControlModel.swift | 100 ++++++++++++------------ PlayTools/Keymap/EditorController.swift | 7 +- PlayTools/Keymap/KeyCodeNames.swift | 2 + 6 files changed, 128 insertions(+), 81 deletions(-) diff --git a/PlayTools/Controls/PlayAction.swift b/PlayTools/Controls/PlayAction.swift index 9f983fc7..e4ee9199 100644 --- a/PlayTools/Controls/PlayAction.swift +++ b/PlayTools/Controls/PlayAction.swift @@ -96,9 +96,7 @@ class DraggableButtonAction: ButtonAction { override init(id: Int, keyCode: GCKeyCode, keyName: String, point: CGPoint) { self.releasePoint = point super.init(id: id, keyCode: keyCode, keyName: keyName, point: point) - if settings.mouseMapping { - PlayMice.shared.setupMouseMovedHandler() - } + _ = PlayMice.shared.setupThumbstickChangedHandler(name: keyName) } override func update(pressed: Bool) { diff --git a/PlayTools/Controls/PlayInput.swift b/PlayTools/Controls/PlayInput.swift index 180ada8f..7ebe24b4 100644 --- a/PlayTools/Controls/PlayInput.swift +++ b/PlayTools/Controls/PlayInput.swift @@ -17,7 +17,7 @@ class PlayInput { } } - func setup() { + func parseKeymap() { actions = [] // ID 1 is left for mouse area var counter = 2 @@ -31,8 +31,8 @@ class PlayInput { counter += 1 } - if settings.mouseMapping { - for mouse in keymap.keymapData.mouseAreaModel { + for mouse in keymap.keymapData.mouseAreaModel { + if mouse.keyName.hasSuffix("stick") || settings.mouseMapping { PlayMice.shared.setup(mouse) counter += 1 } @@ -42,7 +42,10 @@ class PlayInput { actions.append(JoystickAction(id: counter, data: joystick)) counter += 1 } + } + func setup() { + parseKeymap() if let keyboard = GCKeyboard.coalesced?.keyboardInput { keyboard.keyChangedHandler = { _, _, keyCode, _ in if editor.editorMode @@ -70,12 +73,22 @@ class PlayInput { controller.valueChangedHandler = { gamepad, element in // This is the index of controller buttons, which is String, not Int let alias: String! = element.aliases.first - Toast.showOver(msg: alias) +// Toast.showOver(msg: alias) if editor.editorMode { EditorController.shared.setKey(alias) } } } + for mouse in GCMouse.mice() { + mouse.mouseInput?.mouseMovedHandler = { _, deltaX, deltaY in + if editor.editorMode { + EditorController.shared.setKey("Mouse") + } else { + PlayMice.shared.handleMouseMoved(deltaX: deltaX, deltaY: deltaY) + } + } + } + } static public func cmdPressed() -> Bool { diff --git a/PlayTools/Controls/PlayMice.swift b/PlayTools/Controls/PlayMice.swift index e69efb29..301aed7f 100644 --- a/PlayTools/Controls/PlayMice.swift +++ b/PlayTools/Controls/PlayMice.swift @@ -20,13 +20,13 @@ public class PlayMice { setupMouseButton(_up: 8, _down: 16) setupMouseButton(_up: 33554432, _down: 67108864) PlayMice.isInit = true - if acceptMouseEvents { - setupMouseMovedHandler() - } + } } var fakedMousePressed = false + private var thumbstickVelocity: CGVector = CGVector.zero + private var byThumbstick = false, byMouse = false public var cursorPos: CGPoint { var point = CGPoint(x: 0, y: 0) @@ -55,27 +55,59 @@ public class PlayMice { } func setup(_ data: MouseArea) { + if setupThumbstickChangedHandler(name: data.keyName) { + byThumbstick = true +// Toast.showOver(msg: "thumbstick setup") + } else { + byMouse = true + } camera = CameraControl( centerX: data.transform.xCoord.absoluteX, centerY: data.transform.yCoord.absoluteY) - setupMouseMovedHandler() } - public func setupMouseMovedHandler() { - for mouse in GCMouse.mice() { - mouse.mouseInput?.mouseMovedHandler = { _, deltaX, deltaY in - if !mode.visible { - if let draggableButton = DraggableButtonAction.activeButton { - draggableButton.onMouseMoved(deltaX: CGFloat(deltaX), deltaY: CGFloat(deltaY)) - } else { - self.camera?.updated(CGFloat(deltaX), CGFloat(deltaY)) - } - if self.acceptMouseEvents && self.fakedMousePressed { - Toucher.touchcam(point: self.cursorPos, phase: UITouch.Phase.moved, tid: 1) - } + public func setupThumbstickChangedHandler(name: String) -> Bool { + if let thumbstick = GCController.current?.extendedGamepad?.elements[name] as? GCControllerDirectionPad { + thumbstick.valueChangedHandler = { _, deltaX, deltaY in + if self.thumbstickVelocity.dx.isZero && self.thumbstickVelocity.dy.isZero { + DispatchQueue.main.async(execute: self.thumbstickPoll) } -// Toast.showOver(msg: "\(self.cursorPos)") + self.thumbstickVelocity.dx = CGFloat(deltaX * 8) + self.thumbstickVelocity.dy = CGFloat(deltaY * 8) +// Toast.showOver(msg: "thumbstick") + } + return true + } + return false + } + + private func thumbstickPoll() { +// DispatchQueue.main.async { +// Toast.showOver(msg: "polling") +// } + if !thumbstickVelocity.dx.isZero || !thumbstickVelocity.dy.isZero { + if let draggableButton = DraggableButtonAction.activeButton { + draggableButton.onMouseMoved(deltaX: thumbstickVelocity.dx, deltaY: thumbstickVelocity.dy) + } else if byThumbstick { + self.camera?.updated(thumbstickVelocity.dx, thumbstickVelocity.dy) } + DispatchQueue.main.asyncAfter( + deadline: DispatchTime.now() + 0.017, execute: self.thumbstickPoll) + } + } + + public func handleMouseMoved(deltaX: Float, deltaY: Float) { + if self.acceptMouseEvents && self.fakedMousePressed { + Toucher.touchcam(point: self.cursorPos, phase: UITouch.Phase.moved, tid: 1) + } + if mode.visible { + return + } + if let draggableButton = DraggableButtonAction.activeButton { + draggableButton.onMouseMoved(deltaX: CGFloat(deltaX), deltaY: CGFloat(deltaY)) + + } else if byMouse { + self.camera?.updated(CGFloat(deltaX), CGFloat(deltaY)) } } @@ -85,6 +117,8 @@ public class PlayMice { // } camera?.stop() camera = nil + byMouse = false + byThumbstick = false mouseActions.keys.forEach { key in mouseActions[key] = [] } @@ -112,10 +146,8 @@ public class PlayMice { } else if actionIndex == 33554432 { EditorController.shared.setKey(-3) } - return true - } else { - return true } + return true } if self.acceptMouseEvents { if state { @@ -130,15 +162,14 @@ public class PlayMice { self.fakedMousePressed = true return false } - return true } else { if self.fakedMousePressed { self.fakedMousePressed = false Toucher.touchcam(point: self.cursorPos, phase: UITouch.Phase.ended, tid: 1) return false } - return true } + return true } if !mode.visible { self.mouseActions[actionIndex]!.forEach({ buttonAction in diff --git a/PlayTools/Keymap/ControlModel.swift b/PlayTools/Keymap/ControlModel.swift index 01308f99..2e7265ac 100644 --- a/PlayTools/Keymap/ControlModel.swift +++ b/PlayTools/Keymap/ControlModel.swift @@ -18,11 +18,15 @@ class ControlData { self.parent = parent } - init(keyCodes: [Int], parent: ControlModel) { + convenience init(keyCodes: [Int], parent: ControlModel) { + self.init(keyCodes: keyCodes, keyName: KeyCodeNames.keyCodes[keyCodes[0]] ?? "Btn", parent: parent) + } + + init(keyCodes: [Int], keyName: String, parent: ControlModel) { self.keyCodes = keyCodes // For now, not support binding controller key // Support for that is left for later to concern - self.keyName = KeyCodeNames.keyCodes[keyCodes[0]] ?? "Btn" + self.keyName = keyName self.size = parent.data.size / 3 self.xCoord = 0 self.yCoord = 0 @@ -50,7 +54,16 @@ class ControlModel { func update() {} - func focus(_ focus: Bool) {} + func focus(_ focus: Bool) { + if focus { + button.layer.borderWidth = 3 + button.layer.borderColor = UIColor.systemPink.cgColor + button.setNeedsDisplay() + } else { + button.layer.borderWidth = 0 + button.setNeedsDisplay() + } + } func unfocusChildren() {} @@ -82,7 +95,7 @@ class ControlModel { } func resize(down: Bool) { - let mod = down ? 0.9 : 1.1 + let mod = down ? 0.9 : 1/0.9 data.size = (button.frame.width * CGFloat(mod)).relativeSize update() } @@ -94,7 +107,7 @@ class ControlModel { } func setKey(name: String) { - self.setKey(codes: [-10], name: name) + self.setKey(codes: [KeyCodeNames.defaultCode], name: name) } } @@ -120,23 +133,12 @@ class ButtonModel: ControlModel { button.layer.cornerRadius = 0.5 * button.bounds.size.width button.clipsToBounds = true button.titleLabel?.minimumScaleFactor = 0.01 - button.titleLabel?.numberOfLines = 1 + button.titleLabel?.numberOfLines = 2 button.titleLabel?.adjustsFontSizeToFitWidth = true button.titleLabel?.textAlignment = .center button.contentEdgeInsets = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2) } - override func focus(_ focus: Bool) { - if focus { - button.layer.borderWidth = 3 - button.layer.borderColor = UIColor.systemPink.cgColor - button.setNeedsDisplay() - } else { - button.layer.borderWidth = 0 - button.setNeedsDisplay() - } - } - override func setKey(codes: [Int], name: String) { data.keyCodes = codes data.keyName = name @@ -147,8 +149,6 @@ class ButtonModel: ControlModel { class JoystickButtonModel: ControlModel { override init(data: ControlData) { super.init(data: data) - // joystick buttons cannot be mapped to controller keys. - // Instead, map a real joystick to the joystick as a whole. self.setKey(codes: data.keyCodes) data.parent?.button.addSubview(button) } @@ -176,13 +176,8 @@ class JoystickButtonModel: ControlModel { override func focus(_ focus: Bool) { if focus { data.parent?.unfocusChildren() - button.layer.borderWidth = 3 - button.layer.borderColor = UIColor.systemPink.cgColor - button.setNeedsDisplay() - } else { - button.layer.borderWidth = 0 - button.setNeedsDisplay() } + super.focus(focus) } } @@ -190,13 +185,18 @@ class DraggableButtonModel: MouseAreaModel { var childButton: JoystickButtonModel? func save() -> Button { - return Button(keyCode: childButton!.data.keyCodes[0], keyName: childButton!.data.keyName, + return Button(keyCode: childButton!.data.keyCodes[0], keyName: data.keyName, transform: KeyModelTransform(size: data.size, xCoord: data.xCoord, yCoord: data.yCoord)) } override func setKey(codes: [Int], name: String) { - childButton!.setKey(codes: codes) - self.data.keyName = name + let code = codes[0] + if code == KeyCodeNames.defaultCode { + self.data.keyName = name + button.setTitle(data.keyName, for: UIControl.State.normal) + } else { + childButton!.setKey(codes: codes) + } } override func focus(_ focus: Bool) { super.focus(focus) @@ -206,13 +206,14 @@ class DraggableButtonModel: MouseAreaModel { } override func update() { super.update() + self.button.titleEdgeInsets = UIEdgeInsets(top: data.size.absoluteSize / 2, left: 0, bottom: 0, right: 0) if childButton == nil { // temporarily, cannot map controller keys to draggable buttons // `data.keyName` is the key for the move area, not that of the button key. childButton = JoystickButtonModel(data: ControlData( keyCodes: [data.keyCodes[0]], parent: self)) } - let btn = button.subviews[0] + let btn = childButton!.button let buttonSize = data.size.absoluteSize / 3 let coord = (button.frame.width - buttonSize) / 2 btn.frame = CGRect(x: coord, y: coord, width: buttonSize, height: buttonSize) @@ -248,6 +249,8 @@ class JoystickModel: ControlModel { self.setKey(name: data.keyName) if data.keyCodes.count == 4 && joystickButtons.count == 0 { for keyCode in data.keyCodes { + // joystick buttons cannot be mapped to controller keys. + // Instead, map a real joystick to the joystick as a whole. joystickButtons.append(JoystickButtonModel(data: ControlData( keyCodes: [keyCode], parent: self))) } @@ -256,13 +259,8 @@ class JoystickModel: ControlModel { } override func focus(_ focus: Bool) { - if focus { - button.layer.borderWidth = 3 - button.layer.borderColor = UIColor.systemPink.cgColor - button.setNeedsDisplay() - } else { - button.layer.borderWidth = 0 - button.setNeedsDisplay() + super.focus(focus) + if !focus { unfocusChildren() } } @@ -275,6 +273,7 @@ class JoystickModel: ControlModel { override func setKey(codes: [Int], name: String) { self.data.keyName = name +// button.setTitle(data.keyName, for: UIControl.State.normal) } override func resize(down: Bool) { @@ -305,18 +304,8 @@ class JoystickModel: ControlModel { class MouseAreaModel: ControlModel { func save() -> MouseArea { - MouseArea(transform: KeyModelTransform(size: data.size, xCoord: data.xCoord, yCoord: data.yCoord)) - } - - override func focus(_ focus: Bool) { - if focus { - button.layer.borderWidth = 3 - button.layer.borderColor = UIColor.systemPink.cgColor - button.setNeedsDisplay() - } else { - button.layer.borderWidth = 0 - button.setNeedsDisplay() - } + MouseArea(keyName: data.keyName, + transform: KeyModelTransform(size: data.size, xCoord: data.xCoord, yCoord: data.yCoord)) } override func update() { @@ -326,11 +315,22 @@ class MouseAreaModel: ControlModel { button.setY(yCoord: data.yCoord.absoluteY) button.layer.cornerRadius = 0.5 * button.bounds.size.width button.clipsToBounds = true + setKey(name: data.keyName) } - override func setKey(codes: [Int]) { + private func setDraggableButton(code: Int) { EditorController.shared.removeControl() - EditorController.shared.addDraggableButton(CGPoint(x: data.xCoord, y: data.yCoord), codes[0]) + EditorController.shared.addDraggableButton(CGPoint(x: data.xCoord, y: data.yCoord), code) + } + + override func setKey(codes: [Int], name: String) { + let code = codes[0] + if code == KeyCodeNames.defaultCode { + self.data.keyName = name + } else { + self.setDraggableButton(code: code) + } + button.setTitle(data.keyName, for: UIControl.State.normal) } override init(data: ControlData) { diff --git a/PlayTools/Keymap/EditorController.swift b/PlayTools/Keymap/EditorController.swift index 78f29904..aa8307da 100644 --- a/PlayTools/Keymap/EditorController.swift +++ b/PlayTools/Keymap/EditorController.swift @@ -83,7 +83,10 @@ class EditorController { public func setKey(_ name: String) { if editorMode { - focusedControl?.setKey(name: name) + if name != "Mouse" || focusedControl as? MouseAreaModel != nil + || focusedControl as? JoystickModel != nil { + focusedControl?.setKey(name: name) + } } } @@ -207,7 +210,7 @@ class EditorController { if editorMode { addControlToView(control: DraggableButtonModel(data: ControlData( keyCodes: [keyCode], - keyName: KeyCodeNames.keyCodes[keyCode] ?? "Btn", + keyName: "Mouse", size: 15, xCoord: center.x, yCoord: center.y))) diff --git a/PlayTools/Keymap/KeyCodeNames.swift b/PlayTools/Keymap/KeyCodeNames.swift index 9dbaeec1..8df24d67 100644 --- a/PlayTools/Keymap/KeyCodeNames.swift +++ b/PlayTools/Keymap/KeyCodeNames.swift @@ -1,4 +1,6 @@ class KeyCodeNames { + public static let defaultCode = -10 + public static let keyCodes = [ -4: "cA", -5: "cX", From eeac38fdf33c13f128d0285291cde051062fe46e Mon Sep 17 00:00:00 2001 From: Xyct <87l46110@gmail.com> Date: Tue, 4 Oct 2022 15:32:28 +0800 Subject: [PATCH 6/8] map thumbstick mouse to joystick --- PlayTools/Controls/PlayAction.swift | 134 +++++++++++++++++------- PlayTools/Controls/PlayInput.swift | 24 +++-- PlayTools/Controls/PlayMice.swift | 105 +++++++++++-------- PlayTools/Keymap/ControlModel.swift | 23 ++-- PlayTools/Keymap/EditorController.swift | 35 ++++--- 5 files changed, 200 insertions(+), 121 deletions(-) diff --git a/PlayTools/Controls/PlayAction.swift b/PlayTools/Controls/PlayAction.swift index e4ee9199..402531a3 100644 --- a/PlayTools/Controls/PlayAction.swift +++ b/PlayTools/Controls/PlayAction.swift @@ -65,6 +65,8 @@ class ButtonAction: Action { gcControllerButton.pressedChangedHandler = getChangedHandler(handler: handler) } + } else { + Toast.showOver(msg: "failed to map button at point \(point)") } } @@ -89,8 +91,6 @@ class ButtonAction: Action { } class DraggableButtonAction: ButtonAction { - static public var activeButton: DraggableButtonAction? - var releasePoint: CGPoint override init(id: Int, keyCode: GCKeyCode, keyName: String, point: CGPoint) { @@ -103,26 +103,81 @@ class DraggableButtonAction: ButtonAction { if pressed { Toucher.touchcam(point: point, phase: UITouch.Phase.began, tid: id) self.releasePoint = point - DraggableButtonAction.activeButton = self + PlayMice.shared.draggableHandler[keyName] = self.onMouseMoved } else { - DraggableButtonAction.activeButton = nil + PlayMice.shared.draggableHandler.removeValue(forKey: keyName) Toucher.touchcam(point: releasePoint, phase: UITouch.Phase.ended, tid: id) } } override func invalidate() { - DraggableButtonAction.activeButton = nil + PlayMice.shared.draggableHandler.removeValue(forKey: keyName) PlayMice.shared.stop() super.invalidate() } func onMouseMoved(deltaX: CGFloat, deltaY: CGFloat) { - self.releasePoint.x += deltaX * CGFloat(PlaySettings.shared.sensitivity) - self.releasePoint.y -= deltaY * CGFloat(PlaySettings.shared.sensitivity) + self.releasePoint.x += deltaX + self.releasePoint.y -= deltaY Toucher.touchcam(point: self.releasePoint, phase: UITouch.Phase.moved, tid: id) } } +class ConcreteJoystickAction: Action { + var key: String + var center: CGPoint + var position: CGPoint! + var id: Int + var sensitivity: CGFloat + var begun = false + + init(id: Int, data: Joystick) { + self.id = id + self.center = CGPoint( + x: data.transform.xCoord.absoluteX, + y: data.transform.yCoord.absoluteY) + self.key = data.keyName + position = center + self.sensitivity = data.transform.size.absoluteSize / 2 + if PlayMice.shared.setupThumbstickChangedHandler(name: key) { + PlayMice.shared.joystickHandler[key] = thumbstickUpdate + } else { + PlayMice.shared.joystickHandler[key] = mouseUpdate + } + } + + func update(_ point: CGPoint) { + let dis = (center.x - point.x).magnitude + (center.y - point.y).magnitude + if dis < 16 { + if begun { + begun = false + Toucher.touchcam(point: point, phase: UITouch.Phase.ended, tid: id) + } + } else if !begun { + begun = true + Toucher.touchcam(point: point, phase: UITouch.Phase.began, tid: id) + } else { + Toucher.touchcam(point: point, phase: UITouch.Phase.moved, tid: id) + } + } + + func thumbstickUpdate(_ deltaX: CGFloat, _ deltaY: CGFloat) { + let pos = CGPoint(x: center.x + deltaX * sensitivity, + y: center.y - deltaY * sensitivity) + self.update(pos) + } + + func mouseUpdate(_ deltaX: CGFloat, _ deltaY: CGFloat) { + position.x += deltaX + position.y -= deltaY + self.update(position) + } + + func invalidate() { + PlayMice.shared.joystickHandler.removeValue(forKey: key) + } +} + class JoystickAction: Action { let keys: [GCKeyCode] let center: CGPoint @@ -174,39 +229,40 @@ class JoystickAction: Action { } func update() { - if !mode.visible { - var touch = center - var start = center - if GCKeyboard.pressed(key: keys[0]) { - touch.y -= shift / 3 - } else if GCKeyboard.pressed(key: keys[1]) { - touch.y += shift / 3 - } - if GCKeyboard.pressed(key: keys[2]) { - touch.x -= shift / 3 - } else if GCKeyboard.pressed(key: keys[3]) { - touch.x += shift / 3 - } - if moving { - if touch.equalTo(center) { - moving = false - Toucher.touchcam(point: touch, phase: UITouch.Phase.ended, tid: id) - } else { - Toucher.touchcam(point: touch, phase: UITouch.Phase.moved, tid: id) - } + if mode.visible { + return + } + var touch = center + var start = center + if GCKeyboard.pressed(key: keys[0]) { + touch.y -= shift / 3 + } else if GCKeyboard.pressed(key: keys[1]) { + touch.y += shift / 3 + } + if GCKeyboard.pressed(key: keys[2]) { + touch.x -= shift / 3 + } else if GCKeyboard.pressed(key: keys[3]) { + touch.x += shift / 3 + } + if moving { + if touch.equalTo(center) { + moving = false + Toucher.touchcam(point: touch, phase: UITouch.Phase.ended, tid: id) } else { - if !touch.equalTo(center) { - start.x += (touch.x - start.x) / 8 - start.y += (touch.y - start.y) / 8 - moving = true - Toucher.touchcam(point: start, phase: UITouch.Phase.began, tid: id) - Toucher.touchQueue.asyncAfter(deadline: .now() + 0.04) { - if self.moving { - Toucher.touchcam(point: touch, phase: UITouch.Phase.moved, tid: self.id) - } - } - } + Toucher.touchcam(point: touch, phase: UITouch.Phase.moved, tid: id) } - } + } else { + if !touch.equalTo(center) { + start.x += (touch.x - start.x) / 8 + start.y += (touch.y - start.y) / 8 + moving = true + Toucher.touchcam(point: start, phase: UITouch.Phase.began, tid: id) + Toucher.touchQueue.asyncAfter(deadline: .now() + 0.04) { + if self.moving { + Toucher.touchcam(point: touch, phase: UITouch.Phase.moved, tid: self.id) + } // end if + } // end closure + } // end if + } // end else } } diff --git a/PlayTools/Controls/PlayInput.swift b/PlayTools/Controls/PlayInput.swift index 7ebe24b4..77a4f769 100644 --- a/PlayTools/Controls/PlayInput.swift +++ b/PlayTools/Controls/PlayInput.swift @@ -19,8 +19,8 @@ class PlayInput { func parseKeymap() { actions = [] - // ID 1 is left for mouse area - var counter = 2 + // ID starts from 1 + var counter = 1 for button in keymap.keymapData.buttonModels { actions.append(ButtonAction(id: counter, data: button)) counter += 1 @@ -32,14 +32,18 @@ class PlayInput { } for mouse in keymap.keymapData.mouseAreaModel { - if mouse.keyName.hasSuffix("stick") || settings.mouseMapping { - PlayMice.shared.setup(mouse) + if mouse.keyName.hasSuffix("tick") || settings.mouseMapping { + actions.append(CameraAction(id: counter, data: mouse)) counter += 1 } } for joystick in keymap.keymapData.joystickModel { - actions.append(JoystickAction(id: counter, data: joystick)) + if joystick.keyName.contains(Character("u")) { + actions.append(ConcreteJoystickAction(id: counter, data: joystick)) + } else { + actions.append(JoystickAction(id: counter, data: joystick)) + } counter += 1 } } @@ -70,7 +74,7 @@ class PlayInput { } if let controller = GCController.current?.extendedGamepad { - controller.valueChangedHandler = { gamepad, element in + controller.valueChangedHandler = { _, element in // This is the index of controller buttons, which is String, not Int let alias: String! = element.aliases.first // Toast.showOver(msg: alias) @@ -97,11 +101,9 @@ class PlayInput { private func isSafeToBind(_ input: GCKeyboardInput) -> Bool { var result = true - for forbidden in PlayInput.FORBIDDEN { - if input.button(forKeyCode: forbidden)?.isPressed ?? false { - result = false - break - } + for forbidden in PlayInput.FORBIDDEN where input.button(forKeyCode: forbidden)?.isPressed ?? false { + result = false + break } return result } diff --git a/PlayTools/Controls/PlayMice.swift b/PlayTools/Controls/PlayMice.swift index 301aed7f..69519f8e 100644 --- a/PlayTools/Controls/PlayMice.swift +++ b/PlayTools/Controls/PlayMice.swift @@ -9,9 +9,9 @@ import GameController public class PlayMice { public static let shared = PlayMice() + public static let elementName = "Mouse" private static var isInit = false - private var camera: CameraControl? private var acceptMouseEvents = !PlaySettings.shared.mouseMapping public init() { @@ -20,13 +20,14 @@ public class PlayMice { setupMouseButton(_up: 8, _down: 16) setupMouseButton(_up: 33554432, _down: 67108864) PlayMice.isInit = true - } } var fakedMousePressed = false private var thumbstickVelocity: CGVector = CGVector.zero - private var byThumbstick = false, byMouse = false + public var draggableHandler: [String: (CGFloat, CGFloat) -> Void] = [:], + cameraHandler: [String: (CGFloat, CGFloat) -> Void] = [:], + joystickHandler: [String: (CGFloat, CGFloat) -> Void] = [:] public var cursorPos: CGPoint { var point = CGPoint(x: 0, y: 0) @@ -54,45 +55,43 @@ public class PlayMice { return point } - func setup(_ data: MouseArea) { - if setupThumbstickChangedHandler(name: data.keyName) { - byThumbstick = true -// Toast.showOver(msg: "thumbstick setup") - } else { - byMouse = true - } - camera = CameraControl( - centerX: data.transform.xCoord.absoluteX, - centerY: data.transform.yCoord.absoluteY) - } - public func setupThumbstickChangedHandler(name: String) -> Bool { if let thumbstick = GCController.current?.extendedGamepad?.elements[name] as? GCControllerDirectionPad { thumbstick.valueChangedHandler = { _, deltaX, deltaY in if self.thumbstickVelocity.dx.isZero && self.thumbstickVelocity.dy.isZero { - DispatchQueue.main.async(execute: self.thumbstickPoll) + DispatchQueue.main.async(execute: self.thumbstickPoll(name)) } self.thumbstickVelocity.dx = CGFloat(deltaX * 8) self.thumbstickVelocity.dy = CGFloat(deltaY * 8) // Toast.showOver(msg: "thumbstick") + if let joystickUpdate = self.joystickHandler[name] { + joystickUpdate(self.thumbstickVelocity.dx, self.thumbstickVelocity.dy) + } } return true } return false } - private func thumbstickPoll() { + private func thumbstickPoll(_ name: String) -> () -> Void { // DispatchQueue.main.async { // Toast.showOver(msg: "polling") // } - if !thumbstickVelocity.dx.isZero || !thumbstickVelocity.dy.isZero { - if let draggableButton = DraggableButtonAction.activeButton { - draggableButton.onMouseMoved(deltaX: thumbstickVelocity.dx, deltaY: thumbstickVelocity.dy) - } else if byThumbstick { - self.camera?.updated(thumbstickVelocity.dx, thumbstickVelocity.dy) + return { + if !self.thumbstickVelocity.dx.isZero || !self.thumbstickVelocity.dy.isZero { + var captured = false + if let draggableUpdate = self.draggableHandler[name] { + draggableUpdate(self.thumbstickVelocity.dx, self.thumbstickVelocity.dy) + captured = true + } + if !captured { + if let cameraUpdate = self.cameraHandler[name] { + cameraUpdate(self.thumbstickVelocity.dx, self.thumbstickVelocity.dy) + } + } + DispatchQueue.main.asyncAfter( + deadline: DispatchTime.now() + 0.017, execute: self.thumbstickPoll(name)) } - DispatchQueue.main.asyncAfter( - deadline: DispatchTime.now() + 0.017, execute: self.thumbstickPoll) } } @@ -103,22 +102,28 @@ public class PlayMice { if mode.visible { return } - if let draggableButton = DraggableButtonAction.activeButton { - draggableButton.onMouseMoved(deltaX: CGFloat(deltaX), deltaY: CGFloat(deltaY)) - - } else if byMouse { - self.camera?.updated(CGFloat(deltaX), CGFloat(deltaY)) + let cgDx = CGFloat(deltaX) * CGFloat(PlaySettings.shared.sensitivity), + cgDy = CGFloat(deltaY) * CGFloat(PlaySettings.shared.sensitivity) + for name in ["", PlayMice.elementName] { + if let draggableUpdate = self.draggableHandler[name] { + draggableUpdate(cgDx, cgDy) + return + } + } + for name in ["", PlayMice.elementName] { + if let cameraUpdate = self.cameraHandler[name] { + cameraUpdate(cgDx, cgDy) + } + if let joystickUpdate = self.joystickHandler[name] { + joystickUpdate(cgDx, cgDy) + } } } public func stop() { -// for mouse in GCMouse.mice() { -// mouse.mouseInput?.mouseMovedHandler = nil -// } - camera?.stop() - camera = nil - byMouse = false - byThumbstick = false +// draggableJobs.removeAll() +// acceleratedJobs.removeAll() +// movedJobs.removeAll() mouseActions.keys.forEach { key in mouseActions[key] = [] } @@ -191,17 +196,26 @@ public class PlayMice { } } -final class CameraControl { - +class CameraAction: Action { var center: CGPoint = CGPoint.zero var location: CGPoint = CGPoint.zero - + var key: String! + var id: Int! init(centerX: CGFloat = screen.width / 2, centerY: CGFloat = screen.height / 2) { self.center = CGPoint(x: centerX, y: centerY) // in rare cases the cooldown reset task is lost by the dispatch queue self.cooldown = false } + convenience init(id: Int, data: MouseArea) { + self.init( + centerX: data.transform.xCoord.absoluteX, + centerY: data.transform.yCoord.absoluteY) + self.id = id + self.key = data.keyName + _ = PlayMice.shared.setupThumbstickChangedHandler(name: key) + PlayMice.shared.cameraHandler[key] = self.updated + } var isMoving = false func delay(_ delay: Double, closure: @escaping () -> Void) { @@ -258,7 +272,7 @@ final class CameraControl { location = center counter = 0 stationaryCount = 0 - Toucher.touchcam(point: self.center, phase: UITouch.Phase.began, tid: 1) + Toucher.touchcam(point: self.center, phase: UITouch.Phase.began, tid: id) delay(0.1, closure: checkEnded) } @@ -274,9 +288,9 @@ final class CameraControl { } movingFast = true } - self.location.x += deltaX * CGFloat(PlaySettings.shared.sensitivity) - self.location.y -= deltaY * CGFloat(PlaySettings.shared.sensitivity) - Toucher.touchcam(point: self.location, phase: UITouch.Phase.moved, tid: 1) + self.location.x += deltaX + self.location.y -= deltaY + Toucher.touchcam(point: self.location, phase: UITouch.Phase.moved, tid: id) stationaryCount = 0 } @@ -284,7 +298,7 @@ final class CameraControl { if !self.isMoving { return } - Toucher.touchcam(point: self.location, phase: UITouch.Phase.ended, tid: 1) + Toucher.touchcam(point: self.location, phase: UITouch.Phase.ended, tid: id) self.isMoving = false // ending and beginning too frequently leads to the beginning event not recognized // so let the beginning event wait some time @@ -295,7 +309,8 @@ final class CameraControl { cooldown = true } - func stop() { + func invalidate() { + PlayMice.shared.cameraHandler.removeValue(forKey: key) self.doLiftOff() } } diff --git a/PlayTools/Keymap/ControlModel.swift b/PlayTools/Keymap/ControlModel.swift index 2e7265ac..5d3a6805 100644 --- a/PlayTools/Keymap/ControlModel.swift +++ b/PlayTools/Keymap/ControlModel.swift @@ -246,7 +246,6 @@ class JoystickModel: ControlModel { button.setY(yCoord: data.yCoord.absoluteY) button.layer.cornerRadius = 0.3 * button.bounds.size.width button.clipsToBounds = true - self.setKey(name: data.keyName) if data.keyCodes.count == 4 && joystickButtons.count == 0 { for keyCode in data.keyCodes { // joystick buttons cannot be mapped to controller keys. @@ -255,6 +254,7 @@ class JoystickModel: ControlModel { keyCodes: [keyCode], parent: self))) } } + self.setKey(name: data.keyName) changeButtonsSize() } @@ -272,14 +272,19 @@ class JoystickModel: ControlModel { } override func setKey(codes: [Int], name: String) { - self.data.keyName = name -// button.setTitle(data.keyName, for: UIControl.State.normal) - } - - override func resize(down: Bool) { - let mod = down ? 0.9 : 1.1 - data.size = (button.frame.width * CGFloat(mod)).relativeSize - update() + if codes[0] == KeyCodeNames.defaultCode && name.contains(Character("s")) { + self.data.keyName = name + button.setTitle(data.keyName, for: UIControl.State.normal) + for btn in joystickButtons { + btn.button.isHidden = true + } + } else { + self.data.keyName = "Keyboard" + button.setTitle("", for: UIControl.State.normal) + for btn in joystickButtons { + btn.button.isHidden = false + } + } } func changeButtonsSize() { diff --git a/PlayTools/Keymap/EditorController.swift b/PlayTools/Keymap/EditorController.swift index aa8307da..a23dfc88 100644 --- a/PlayTools/Keymap/EditorController.swift +++ b/PlayTools/Keymap/EditorController.swift @@ -84,7 +84,8 @@ class EditorController { public func setKey(_ name: String) { if editorMode { if name != "Mouse" || focusedControl as? MouseAreaModel != nil - || focusedControl as? JoystickModel != nil { + || focusedControl as? JoystickModel != nil + || focusedControl as? DraggableButtonModel != nil { focusedControl?.setKey(name: name) } } @@ -96,15 +97,6 @@ class EditorController { } func showButtons() { - for button in keymap.keymapData.buttonModels { - let ctrl = ButtonModel(data: ControlData( - keyCodes: [button.keyCode], - keyName: button.keyName, - size: button.transform.size, - xCoord: button.transform.xCoord, - yCoord: button.transform.yCoord)) - addControlToView(control: ctrl) - } for button in keymap.keymapData.draggableButtonModels { let ctrl = DraggableButtonModel(data: ControlData( keyCodes: [button.keyCode], @@ -114,6 +106,15 @@ class EditorController { yCoord: button.transform.yCoord)) addControlToView(control: ctrl) } + for joystick in keymap.keymapData.joystickModel { + let ctrl = JoystickModel(data: ControlData( + keyCodes: [joystick.upKeyCode, joystick.downKeyCode, joystick.leftKeyCode, joystick.rightKeyCode], + keyName: joystick.keyName, + size: joystick.transform.size, + xCoord: joystick.transform.xCoord, + yCoord: joystick.transform.yCoord)) + addControlToView(control: ctrl) + } for mouse in keymap.keymapData.mouseAreaModel { let ctrl = MouseAreaModel(data: ControlData( @@ -123,13 +124,13 @@ class EditorController { yCoord: mouse.transform.yCoord)) addControlToView(control: ctrl) } - for joystick in keymap.keymapData.joystickModel { - let ctrl = JoystickModel(data: ControlData( - keyCodes: [joystick.upKeyCode, joystick.downKeyCode, joystick.leftKeyCode, joystick.rightKeyCode], - keyName: joystick.keyName, - size: joystick.transform.size, - xCoord: joystick.transform.xCoord, - yCoord: joystick.transform.yCoord)) + for button in keymap.keymapData.buttonModels { + let ctrl = ButtonModel(data: ControlData( + keyCodes: [button.keyCode], + keyName: button.keyName, + size: button.transform.size, + xCoord: button.transform.xCoord, + yCoord: button.transform.yCoord)) addControlToView(control: ctrl) } } From aa46eb91b378d4eb70b50c6d1f69c273c8de1006 Mon Sep 17 00:00:00 2001 From: Xyct <87l46110@gmail.com> Date: Wed, 5 Oct 2022 14:47:51 +0800 Subject: [PATCH 7/8] thumbstick performance optimization --- PlayTools/Controls/PlayAction.swift | 2 +- PlayTools/Controls/PlayMice.swift | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/PlayTools/Controls/PlayAction.swift b/PlayTools/Controls/PlayAction.swift index 402531a3..e1db21ce 100644 --- a/PlayTools/Controls/PlayAction.swift +++ b/PlayTools/Controls/PlayAction.swift @@ -138,7 +138,7 @@ class ConcreteJoystickAction: Action { y: data.transform.yCoord.absoluteY) self.key = data.keyName position = center - self.sensitivity = data.transform.size.absoluteSize / 2 + self.sensitivity = data.transform.size.absoluteSize / 4 if PlayMice.shared.setupThumbstickChangedHandler(name: key) { PlayMice.shared.joystickHandler[key] = thumbstickUpdate } else { diff --git a/PlayTools/Controls/PlayMice.swift b/PlayTools/Controls/PlayMice.swift index 69519f8e..bc470aed 100644 --- a/PlayTools/Controls/PlayMice.swift +++ b/PlayTools/Controls/PlayMice.swift @@ -26,7 +26,7 @@ public class PlayMice { var fakedMousePressed = false private var thumbstickVelocity: CGVector = CGVector.zero public var draggableHandler: [String: (CGFloat, CGFloat) -> Void] = [:], - cameraHandler: [String: (CGFloat, CGFloat) -> Void] = [:], + cameraHandler: [String: (CGFloat, CGFloat) -> Void] = [:], joystickHandler: [String: (CGFloat, CGFloat) -> Void] = [:] public var cursorPos: CGPoint { @@ -59,7 +59,9 @@ public class PlayMice { if let thumbstick = GCController.current?.extendedGamepad?.elements[name] as? GCControllerDirectionPad { thumbstick.valueChangedHandler = { _, deltaX, deltaY in if self.thumbstickVelocity.dx.isZero && self.thumbstickVelocity.dy.isZero { - DispatchQueue.main.async(execute: self.thumbstickPoll(name)) + if let closure = self.thumbstickPoll(name) { + DispatchQueue.main.async(execute: closure) + } } self.thumbstickVelocity.dx = CGFloat(deltaX * 8) self.thumbstickVelocity.dy = CGFloat(deltaY * 8) @@ -73,10 +75,15 @@ public class PlayMice { return false } - private func thumbstickPoll(_ name: String) -> () -> Void { + private func thumbstickPoll(_ name: String) -> (() -> Void)? { // DispatchQueue.main.async { // Toast.showOver(msg: "polling") // } + let draggableUpdate = self.draggableHandler[name] + let cameraUpdate = self.cameraHandler[name] + if draggableUpdate == nil && cameraUpdate == nil { + return nil + } return { if !self.thumbstickVelocity.dx.isZero || !self.thumbstickVelocity.dy.isZero { var captured = false @@ -89,8 +96,10 @@ public class PlayMice { cameraUpdate(self.thumbstickVelocity.dx, self.thumbstickVelocity.dy) } } - DispatchQueue.main.asyncAfter( - deadline: DispatchTime.now() + 0.017, execute: self.thumbstickPoll(name)) + if let closure = self.thumbstickPoll(name) { + DispatchQueue.main.asyncAfter( + deadline: DispatchTime.now() + 0.017, execute: closure) + } } } } From a059b0d241d428a2a98352bde020c02df00e5e08 Mon Sep 17 00:00:00 2001 From: Xyct <87l46110@gmail.com> Date: Thu, 6 Oct 2022 20:31:10 +0800 Subject: [PATCH 8/8] change mouse keybind logic --- PlayTools/Controls/PlayInput.swift | 5 +++-- PlayTools/Controls/PlayMice.swift | 12 ++++++++---- PlayTools/Keymap/ControlModel.swift | 16 ++++++++++++---- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/PlayTools/Controls/PlayInput.swift b/PlayTools/Controls/PlayInput.swift index 77a4f769..58e761e5 100644 --- a/PlayTools/Controls/PlayInput.swift +++ b/PlayTools/Controls/PlayInput.swift @@ -39,9 +39,10 @@ class PlayInput { } for joystick in keymap.keymapData.joystickModel { + // Left Thumbstick, Right Thumbstick, Mouse if joystick.keyName.contains(Character("u")) { actions.append(ConcreteJoystickAction(id: counter, data: joystick)) - } else { + } else { // Keyboard actions.append(JoystickAction(id: counter, data: joystick)) } counter += 1 @@ -86,7 +87,7 @@ class PlayInput { for mouse in GCMouse.mice() { mouse.mouseInput?.mouseMovedHandler = { _, deltaX, deltaY in if editor.editorMode { - EditorController.shared.setKey("Mouse") +// EditorController.shared.setKey("Mouse") } else { PlayMice.shared.handleMouseMoved(deltaX: deltaX, deltaY: deltaY) } diff --git a/PlayTools/Controls/PlayMice.swift b/PlayTools/Controls/PlayMice.swift index bc470aed..ae4a73f9 100644 --- a/PlayTools/Controls/PlayMice.swift +++ b/PlayTools/Controls/PlayMice.swift @@ -55,16 +55,20 @@ public class PlayMice { return point } + static private func isVectorSignificant(_ vector: CGVector) -> Bool { + return vector.dx.magnitude + vector.dy.magnitude > 0.2 + } + public func setupThumbstickChangedHandler(name: String) -> Bool { if let thumbstick = GCController.current?.extendedGamepad?.elements[name] as? GCControllerDirectionPad { thumbstick.valueChangedHandler = { _, deltaX, deltaY in - if self.thumbstickVelocity.dx.isZero && self.thumbstickVelocity.dy.isZero { + if !PlayMice.isVectorSignificant(self.thumbstickVelocity) { if let closure = self.thumbstickPoll(name) { DispatchQueue.main.async(execute: closure) } } - self.thumbstickVelocity.dx = CGFloat(deltaX * 8) - self.thumbstickVelocity.dy = CGFloat(deltaY * 8) + self.thumbstickVelocity.dx = CGFloat(deltaX * 6) + self.thumbstickVelocity.dy = CGFloat(deltaY * 6) // Toast.showOver(msg: "thumbstick") if let joystickUpdate = self.joystickHandler[name] { joystickUpdate(self.thumbstickVelocity.dx, self.thumbstickVelocity.dy) @@ -85,7 +89,7 @@ public class PlayMice { return nil } return { - if !self.thumbstickVelocity.dx.isZero || !self.thumbstickVelocity.dy.isZero { + if PlayMice.isVectorSignificant(self.thumbstickVelocity) { var captured = false if let draggableUpdate = self.draggableHandler[name] { draggableUpdate(self.thumbstickVelocity.dx, self.thumbstickVelocity.dy) diff --git a/PlayTools/Keymap/ControlModel.swift b/PlayTools/Keymap/ControlModel.swift index 5d3a6805..47df4273 100644 --- a/PlayTools/Keymap/ControlModel.swift +++ b/PlayTools/Keymap/ControlModel.swift @@ -272,8 +272,12 @@ class JoystickModel: ControlModel { } override func setKey(codes: [Int], name: String) { - if codes[0] == KeyCodeNames.defaultCode && name.contains(Character("s")) { - self.data.keyName = name + if codes[0] < 0 && name != "Keyboard" { + if name.hasSuffix("tick") { + self.data.keyName = name + } else { + self.data.keyName = "Mouse" + } button.setTitle(data.keyName, for: UIControl.State.normal) for btn in joystickButtons { btn.button.isHidden = true @@ -330,8 +334,12 @@ class MouseAreaModel: ControlModel { override func setKey(codes: [Int], name: String) { let code = codes[0] - if code == KeyCodeNames.defaultCode { - self.data.keyName = name + if code < 0 { + if name.hasSuffix("tick") { + self.data.keyName = name + } else { + self.data.keyName = "Mouse" + } } else { self.setDraggableButton(code: code) }