From df458a74dea7fe2e5ea4830d2908612a39010ccd Mon Sep 17 00:00:00 2001 From: ohaiibuzzle <23693150+ohaiibuzzle@users.noreply.github.com> Date: Sun, 7 May 2023 10:49:48 +0700 Subject: [PATCH 1/6] fix: option to disable auto keymap disabling for buggy games --- PlayTools/Controls/PlayInput.swift | 18 +++++++++++------- PlayTools/Controls/Toucher.swift | 2 +- PlayTools/PlaySettings.swift | 3 +++ PlayTools/Utils/Toast.swift | 4 ++++ 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/PlayTools/Controls/PlayInput.swift b/PlayTools/Controls/PlayInput.swift index 79f4c789..6e64f6dc 100644 --- a/PlayTools/Controls/PlayInput.swift +++ b/PlayTools/Controls/PlayInput.swift @@ -155,13 +155,17 @@ class PlayKeyboard { public static func initialize() { let centre = NotificationCenter.default let main = OperationQueue.main - centre.addObserver(forName: UIApplication.keyboardDidHideNotification, object: nil, queue: main) { _ in - mode.keyboardMapped = true - Toucher.writeLog(logMessage: "virtual keyboard did hide") - } - centre.addObserver(forName: UIApplication.keyboardWillShowNotification, object: nil, queue: main) { _ in - mode.keyboardMapped = false - Toucher.writeLog(logMessage: "virtual keyboard will show") + + // Initialize observers to automatically hide keymappings + if PlaySettings.shared.noKMOnInput { + centre.addObserver(forName: UIApplication.keyboardDidHideNotification, object: nil, queue: main) { _ in + mode.keyboardMapped = true + Toucher.writeLog(logMessage: "virtual keyboard did hide") + } + centre.addObserver(forName: UIApplication.keyboardWillShowNotification, object: nil, queue: main) { _ in + mode.keyboardMapped = false + Toucher.writeLog(logMessage: "virtual keyboard will show") + } } AKInterface.shared!.setupKeyboard(keyboard: {keycode, pressed, isRepeat in if !mode.keyboardMapped { diff --git a/PlayTools/Controls/Toucher.swift b/PlayTools/Controls/Toucher.swift index a4dce82d..cc949669 100644 --- a/PlayTools/Controls/Toucher.swift +++ b/PlayTools/Controls/Toucher.swift @@ -10,7 +10,7 @@ class Toucher { static weak var keyWindow: UIWindow? static weak var keyView: UIView? // For debug only - static var logEnabled = false + static var logEnabled = true static var logFilePath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] + "/toucher.log" static private var logCount = 0 diff --git a/PlayTools/PlaySettings.swift b/PlayTools/PlaySettings.swift index 0fab777e..2f10eb37 100644 --- a/PlayTools/PlaySettings.swift +++ b/PlayTools/PlaySettings.swift @@ -71,6 +71,8 @@ let settings = PlaySettings.shared @objc lazy var customScaler = settingsData.customScaler @objc lazy var rootWorkDir = settingsData.rootWorkDir + + @objc lazy var noKMOnInput = settingsData.noKMOnInput } struct AppSettingsData: Codable { @@ -93,4 +95,5 @@ struct AppSettingsData: Codable { var inverseScreenValues = false var windowFixMethod = 0 var rootWorkDir = true + var noKMOnInput = false } diff --git a/PlayTools/Utils/Toast.swift b/PlayTools/Utils/Toast.swift index 1e69ee04..e8910884 100644 --- a/PlayTools/Utils/Toast.swift +++ b/PlayTools/Utils/Toast.swift @@ -83,6 +83,10 @@ class Toast { messageLabel.center.x = parent.center.x messageLabel.center.y = -messageLabel.frame.size.height / 2 + // Disable editing + messageLabel.isEditable = false + messageLabel.isSelectable = false + hintView.append(messageLabel) parent.addSubview(messageLabel) From 582171dada2d705c112ff58e5a2120a2ea5afbe2 Mon Sep 17 00:00:00 2001 From: ohaiibuzzle <23693150+ohaiibuzzle@users.noreply.github.com> Date: Sun, 7 May 2023 16:52:25 +0700 Subject: [PATCH 2/6] fix: disable keyboard mapping on editor mode change (thanks Xyct!) --- PlayTools/Controls/ControlMode.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/PlayTools/Controls/ControlMode.swift b/PlayTools/Controls/ControlMode.swift index a30ae3fc..f3af4d5e 100644 --- a/PlayTools/Controls/ControlMode.swift +++ b/PlayTools/Controls/ControlMode.swift @@ -47,6 +47,9 @@ public class ControlMode { } Toucher.writeLog(logMessage: "cursor show switched to \(show)") visible = show + if PlaySettings.shared.noKMOnInput { + keyboardMapped = false + } } } } From 1b039283342b568989c2f8e0ee02ba6a29bbcb60 Mon Sep 17 00:00:00 2001 From: ohaiibuzzle <23693150+ohaiibuzzle@users.noreply.github.com> Date: Sun, 7 May 2023 16:54:33 +0700 Subject: [PATCH 3/6] fix: disable logging on Toucher --- PlayTools/Controls/Toucher.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PlayTools/Controls/Toucher.swift b/PlayTools/Controls/Toucher.swift index cc949669..a4dce82d 100644 --- a/PlayTools/Controls/Toucher.swift +++ b/PlayTools/Controls/Toucher.swift @@ -10,7 +10,7 @@ class Toucher { static weak var keyWindow: UIWindow? static weak var keyView: UIView? // For debug only - static var logEnabled = true + static var logEnabled = false static var logFilePath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] + "/toucher.log" static private var logCount = 0 From e04a277054ad3d38a7bb8339843bdf020a84331f Mon Sep 17 00:00:00 2001 From: ohaiibuzzle <23693150+ohaiibuzzle@users.noreply.github.com> Date: Tue, 16 May 2023 01:17:15 +0700 Subject: [PATCH 4/6] fix: update after syncing with upstream --- PlayTools/Controls/PlayInput.swift | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/PlayTools/Controls/PlayInput.swift b/PlayTools/Controls/PlayInput.swift index d9866252..d98372a3 100644 --- a/PlayTools/Controls/PlayInput.swift +++ b/PlayTools/Controls/PlayInput.swift @@ -154,13 +154,15 @@ class PlayKeyboard { public static func initialize() { let centre = NotificationCenter.default let main = OperationQueue.main - centre.addObserver(forName: UIApplication.keyboardDidHideNotification, object: nil, queue: main) { _ in - mode.setMapping(true) - Toucher.writeLog(logMessage: "virtual keyboard did hide") - } - centre.addObserver(forName: UIApplication.keyboardWillShowNotification, object: nil, queue: main) { _ in - mode.setMapping(false) - Toucher.writeLog(logMessage: "virtual keyboard will show") + if PlaySettings.shared.noKMOnInput { + centre.addObserver(forName: UIApplication.keyboardDidHideNotification, object: nil, queue: main) { _ in + mode.setMapping(true) + Toucher.writeLog(logMessage: "virtual keyboard did hide") + } + centre.addObserver(forName: UIApplication.keyboardWillShowNotification, object: nil, queue: main) { _ in + mode.setMapping(false) + Toucher.writeLog(logMessage: "virtual keyboard will show") + } } AKInterface.shared!.setupKeyboard(keyboard: {keycode, pressed, isRepeat in if !mode.keyboardMapped { From 82af019ff275578c6c02070ce65b08d5314e07e0 Mon Sep 17 00:00:00 2001 From: OHaiiBuzzle <23693150+ohaiibuzzle@users.noreply.github.com> Date: Tue, 16 May 2023 15:09:51 +0700 Subject: [PATCH 5/6] fix: flipped enable condition for the editor Co-authored-by: Xyct <87l46110@gmail.com> --- PlayTools/Controls/ControlMode.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PlayTools/Controls/ControlMode.swift b/PlayTools/Controls/ControlMode.swift index a79338cf..ba3437d4 100644 --- a/PlayTools/Controls/ControlMode.swift +++ b/PlayTools/Controls/ControlMode.swift @@ -55,7 +55,7 @@ public class ControlMode { } Toucher.writeLog(logMessage: "cursor show switched to \(show)") visible = show - if PlaySettings.shared.noKMOnInput { + if !PlaySettings.shared.noKMOnInput { keyboardMapped = false } } From 20c8392c3d050f3168a3e049b92dd039a276685f Mon Sep 17 00:00:00 2001 From: Xyct <87l46110@gmail.com> Date: Thu, 18 May 2023 02:54:00 +0800 Subject: [PATCH 6/6] make noKMOnInput do the thing --- PlayTools/Controls/ControlMode.swift | 17 ++++++++++++++--- PlayTools/Controls/PlayInput.swift | 18 ++++++++++++++++-- PlayTools/Controls/PlayMice.swift | 4 ++++ 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/PlayTools/Controls/ControlMode.swift b/PlayTools/Controls/ControlMode.swift index ba3437d4..f99ed3c1 100644 --- a/PlayTools/Controls/ControlMode.swift +++ b/PlayTools/Controls/ControlMode.swift @@ -14,6 +14,7 @@ public class ControlMode { public var keyboardMapped = true public static func trySwap() -> Bool { + // this function is called from `AKPlugin` when `option` is pressed. if PlayInput.shouldLockCursor { mode.show(!mode.visible) return true @@ -24,16 +25,24 @@ public class ControlMode { func setMapping(_ mapped: Bool) { if mapped { + // `parseKeymap` and `invalidate` roughly do the opposite thing PlayInput.shared.parseKeymap() } else { - show(true) + // to avoid infinite recursion + if !visible { + show(true) + } PlayInput.shared.invalidate() } keyboardMapped = mapped } func show(_ show: Bool) { - if keyboardMapped { + // special cases where function of `option` should be temparorily disabled. + // if the new auto keymapping feature is enabled, it could cause problems to switch + // cursor show state while typing. + if (!PlaySettings.shared.noKMOnInput && !editor.editorMode) + || (PlaySettings.shared.noKMOnInput && keyboardMapped) { if show { if !visible { NotificationCenter.default.post(name: NSNotification.Name.playtoolsCursorWillShow, @@ -56,7 +65,9 @@ public class ControlMode { Toucher.writeLog(logMessage: "cursor show switched to \(show)") visible = show if !PlaySettings.shared.noKMOnInput { - keyboardMapped = false + // we want to set keymapping as the reverse of curosr show status, not always false. + // as well as do some logic along with it + setMapping(!show) } } } diff --git a/PlayTools/Controls/PlayInput.swift b/PlayTools/Controls/PlayInput.swift index d98372a3..754f2b51 100644 --- a/PlayTools/Controls/PlayInput.swift +++ b/PlayTools/Controls/PlayInput.swift @@ -16,12 +16,14 @@ class PlayInput { joystickHandler: [String: (CGFloat, CGFloat) -> Void] = [:] func invalidate() { + // this is called whenever keymapping disabled, to release all mapping resource for action in self.actions { action.invalidate() } } static public func registerButton(key: String, handler: @escaping (Bool) -> Void) { + // this function is called when setting up `button` type of mapping if "LMB" == key { PlayInput.shouldLockCursor = true } @@ -34,7 +36,13 @@ class PlayInput { func parseKeymap() { actions = [PlayMice.shared] PlayInput.buttonHandlers.removeAll(keepingCapacity: true) - PlayInput.shouldLockCursor = false + // `shouldLockCursor` is used to disable `option` toggle when there is no mouse mapping + // but in the case this new feature disabled, `option` should always function. + // this variable is initilized here to be checked for mouse mapping later. + // intialize it to the reverse of the new feature's enable state makes + // it always true if the new feature is disabled, as it won't be set false in + // any case anywhere else in this case. + PlayInput.shouldLockCursor = !PlaySettings.shared.noKMOnInput for button in keymap.keymapData.buttonModels { actions.append(ButtonAction(data: button)) } @@ -65,6 +73,8 @@ class PlayInput { Toucher.writeLog(logMessage: "editor opened? \(show)") if show { self.invalidate() + // there is no special reason to use GC API for editor, instead of NSEvents. + // just voider did this and I'm not changing it yet. if let keyboard = GCKeyboard.coalesced!.keyboardInput { keyboard.keyChangedHandler = { _, _, keyCode, pressed in PlayKeyboard.handleEditorEvent(keyCode: keyCode, pressed: pressed) @@ -163,6 +173,9 @@ class PlayKeyboard { mode.setMapping(false) Toucher.writeLog(logMessage: "virtual keyboard will show") } + } else { + // we want to initialize keymapping to false + mode.setMapping(false) } AKInterface.shared!.setupKeyboard(keyboard: {keycode, pressed, isRepeat in if !mode.keyboardMapped { @@ -174,7 +187,8 @@ class PlayKeyboard { } let mapped = PlayKeyboard.handleEvent(keycode, pressed) return mapped - }, + }, // passing the function to be called when `option` pressed. + // return `true` meaning this key press is consumed, `false` dispatching it to the App swapMode: ControlMode.trySwap) } } diff --git a/PlayTools/Controls/PlayMice.swift b/PlayTools/Controls/PlayMice.swift index 487090db..d56c9a19 100644 --- a/PlayTools/Controls/PlayMice.swift +++ b/PlayTools/Controls/PlayMice.swift @@ -38,6 +38,7 @@ public class PlayMice: Action { var fakedMousePressed: Bool {fakedMouseTouchPointId != nil} public func mouseMovementMapped() -> Bool { + // this is called from `parseKeymap` to set `shouldLockCursor`'s value for handler in [PlayInput.cameraMoveHandler, PlayInput.joystickHandler] where handler[PlayMice.elementName] != nil { return true @@ -220,6 +221,9 @@ class CameraAction: Action { func invalidate() { PlayInput.cameraMoveHandler.removeValue(forKey: key) + // when noKMOnInput is false, swipe/pan gesture handler would be invalidated when keymapping disabled. + // as it's just a temporary toggle, not fixing it. + // but should remove that toggle as long as new feature considered stable. PlayInput.cameraScaleHandler[PlayMice.elementName] = nil swipeMove.invalidate() swipeScale1.invalidate()