Skip to content

Commit

Permalink
Merge pull request #112 from XuYicong/batch-release
Browse files Browse the repository at this point in the history
Fully disables input mapping when typing
  • Loading branch information
JoseMoreville authored May 8, 2023
2 parents 84c9030 + 6abed8e commit b07d385
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
26 changes: 17 additions & 9 deletions PlayTools/Controls/ControlMode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,35 @@ public class ControlMode {
return false
}

func setMapping(_ mapped: Bool) {
if mapped {
PlayInput.shared.parseKeymap()
} else {
show(true)
PlayInput.shared.invalidate()
}
keyboardMapped = mapped
}

func show(_ show: Bool) {
if !editor.editorMode {
if keyboardMapped {
if show {
if !visible {
NotificationCenter.default.post(name: NSNotification.Name.playtoolsKeymappingWillDisable,
NotificationCenter.default.post(name: NSNotification.Name.playtoolsCursorWillShow,
object: nil, userInfo: [:])
if screen.fullscreen {
screen.switchDock(true)
}
AKInterface.shared!.unhideCursor()
// PlayInput.shared.invalidate()
}
} else {
if visible {
NotificationCenter.default.post(name: NSNotification.Name.playtoolsKeymappingWillEnable,
NotificationCenter.default.post(name: NSNotification.Name.playtoolsCursorWillHide,
object: nil, userInfo: [:])
AKInterface.shared!.hideCursor()
if screen.fullscreen {
screen.switchDock(false)
}
// PlayInput.shared.setup()
}
}
Toucher.writeLog(logMessage: "cursor show switched to \(show)")
Expand All @@ -52,9 +60,9 @@ public class ControlMode {
}

extension NSNotification.Name {
public static let playtoolsKeymappingWillEnable: NSNotification.Name
= NSNotification.Name("playtools.keymappingWillEnable")
public static let playtoolsCursorWillHide: NSNotification.Name
= NSNotification.Name("playtools.cursorWillHide")

public static let playtoolsKeymappingWillDisable: NSNotification.Name
= NSNotification.Name("playtools.keymappingWillDisable")
public static let playtoolsCursorWillShow: NSNotification.Name
= NSNotification.Name("playtools.cursorWillShow")
}
11 changes: 5 additions & 6 deletions PlayTools/Controls/PlayInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class PlayInput {
}

func parseKeymap() {
actions = []
PlayInput.shouldLockCursor = false
actions = [PlayMice.shared]
PlayInput.buttonHandlers.removeAll(keepingCapacity: true)
PlayInput.shouldLockCursor = false
for button in keymap.keymapData.buttonModels {
actions.append(ButtonAction(data: button))
}
Expand All @@ -61,11 +61,10 @@ class PlayInput {
}

public func toggleEditor(show: Bool) {
mode.keyboardMapped = !show
mode.setMapping(!show)
Toucher.writeLog(logMessage: "editor opened? \(show)")
if show {
self.invalidate()
mode.show(show)
if let keyboard = GCKeyboard.coalesced!.keyboardInput {
keyboard.keyChangedHandler = { _, _, keyCode, pressed in
PlayKeyboard.handleEditorEvent(keyCode: keyCode, pressed: pressed)
Expand Down Expand Up @@ -156,11 +155,11 @@ class PlayKeyboard {
let centre = NotificationCenter.default
let main = OperationQueue.main
centre.addObserver(forName: UIApplication.keyboardDidHideNotification, object: nil, queue: main) { _ in
mode.keyboardMapped = true
mode.setMapping(true)
Toucher.writeLog(logMessage: "virtual keyboard did hide")
}
centre.addObserver(forName: UIApplication.keyboardWillShowNotification, object: nil, queue: main) { _ in
mode.keyboardMapped = false
mode.setMapping(false)
Toucher.writeLog(logMessage: "virtual keyboard will show")
}
AKInterface.shared!.setupKeyboard(keyboard: {keycode, pressed, isRepeat in
Expand Down
9 changes: 8 additions & 1 deletion PlayTools/Controls/PlayMice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import Foundation

public class PlayMice {
public class PlayMice: Action {

public static let shared = PlayMice()
public static let elementName = "Mouse"
Expand Down Expand Up @@ -139,6 +139,7 @@ public class PlayMice {
}
guard let curPos = self.cursorPos() else { return true }
PlayInput.touchQueue.async(qos: .userInteractive, execute: {
// considering cases where cursor becomes hidden while holding left button
if self.fakedMousePressed {
Toucher.touchcam(point: curPos, phase: UITouch.Phase.ended, tid: &self.fakedMouseTouchPointId)
return
Expand All @@ -149,6 +150,7 @@ public class PlayMice {
tid: &self.fakedMouseTouchPointId)
return
}
// considering cases where cursor becomes visible while holding left button
if let handlers = PlayInput.buttonHandlers["LMB"] {
for handler in handlers {
handler(pressed)
Expand All @@ -159,6 +161,11 @@ public class PlayMice {
return false
}
}
// For all other actions, this is a destructor. should release held resources.
func invalidate() {
Toucher.touchcam(point: self.cursorPos() ?? CGPoint(x: 10, y: 10),
phase: UITouch.Phase.ended, tid: &self.fakedMouseTouchPointId)
}
}

class CameraAction: Action {
Expand Down
2 changes: 1 addition & 1 deletion PlayTools/Keymap/EditorController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class EditorController {
tableName: "Playtools",
value: "Click a button to edit its position or key bind\n"
+ "Click an empty area to open input menu", comment: "")],
notification: NSNotification.Name.playtoolsKeymappingWillEnable)
notification: NSNotification.Name.playtoolsCursorWillHide)
}
// Toast.showOver(msg: "\(UIApplication.shared.windows.count)")
lock.unlock()
Expand Down
8 changes: 4 additions & 4 deletions PlayTools/Utils/Toast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class Toast {
}
var token2: NSObjectProtocol?
let center = NotificationCenter.default
token2 = center.addObserver(forName: NSNotification.Name.playtoolsKeymappingWillDisable,
token2 = center.addObserver(forName: NSNotification.Name.playtoolsCursorWillShow,
object: nil, queue: OperationQueue.main) { _ in
center.removeObserver(token2!)
UserDefaults.standard.set(thisUse, forKey: persistenceKeyname)
Expand All @@ -151,10 +151,10 @@ class Toast {
tableName: "Playtools",
value: "to enable mouse mapping", comment: "")],
timeout: 10,
notification: NSNotification.Name.playtoolsKeymappingWillEnable)
notification: NSNotification.Name.playtoolsCursorWillHide)
let center = NotificationCenter.default
var token: NSObjectProtocol?
token = center.addObserver(forName: NSNotification.Name.playtoolsKeymappingWillEnable,
token = center.addObserver(forName: NSNotification.Name.playtoolsCursorWillHide,
object: nil, queue: OperationQueue.main) { _ in
center.removeObserver(token!)
Toast.showHint(title: NSLocalizedString("hint.showCursor.title",
Expand All @@ -168,7 +168,7 @@ class Toast {
tableName: "Playtools",
value: "to unlock cursor", comment: "")],
timeout: 10,
notification: NSNotification.Name.playtoolsKeymappingWillDisable)
notification: NSNotification.Name.playtoolsCursorWillShow)
}
}

Expand Down

0 comments on commit b07d385

Please sign in to comment.