Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller Mapping - Part 1 #39

Merged
merged 11 commits into from
Nov 13, 2022
Prev Previous commit
Next Next commit
map thumbstick mouse to joystick
XuYicong committed Oct 4, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit eeac38fdf33c13f128d0285291cde051062fe46e
134 changes: 95 additions & 39 deletions PlayTools/Controls/PlayAction.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
24 changes: 13 additions & 11 deletions PlayTools/Controls/PlayInput.swift
Original file line number Diff line number Diff line change
@@ -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
}
Loading