Skip to content

Commit

Permalink
Merge branch 'master' into feat/controller-mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
XuYicong committed Nov 9, 2022
2 parents a059b0d + d8783b0 commit aca900f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 51 deletions.
49 changes: 12 additions & 37 deletions PlayTools/Controls/PlayMice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,11 @@ public class PlayMice {
}

public func handleMouseMoved(deltaX: Float, deltaY: Float) {
if self.acceptMouseEvents && self.fakedMousePressed {
Toucher.touchcam(point: self.cursorPos, phase: UITouch.Phase.moved, tid: 1)
if self.acceptMouseEvents {
if self.fakedMousePressed {
Toucher.touchcam(point: self.cursorPos, phase: UITouch.Phase.moved, tid: 1)
}
return
}
if mode.visible {
return
Expand Down Expand Up @@ -236,40 +239,22 @@ class CameraAction: Action {
Toucher.touchQueue.asyncAfter(deadline: when, execute: closure)
}

// if max speed of this touch is high
var movingFast = false
// like sequence but resets when touch begins. Used to calc touch duration
var counter = 0
// if should wait before beginning next touch
var cooldown = false
// if the touch point had been prevented from lifting off because of moving slow
var idled = false
// in how many tests has this been identified as stationary
var stationaryCount = 0
let stationaryThreshold = 2

@objc func checkEnded() {
// if been stationary for enough time
if self.stationaryCount < self.stationaryThreshold {
if self.stationaryCount < self.stationaryThreshold || (self.stationaryCount < 20 - self.counter) {
self.stationaryCount += 1
self.delay(0.1, closure: checkEnded)
self.delay(0.04, closure: checkEnded)
return
}
// and slow touch lasts for sufficient time
if self.movingFast || self.counter > 64 {
self.doLiftOff()
} else {
self.idled = true
// idle for at most 4 seconds
self.delay(4) {
if self.stationaryCount < self.stationaryThreshold {
self.stationaryCount += 1
self.delay(0.1, closure: self.checkEnded)
return
}
self.doLiftOff()
}
}
self.doLiftOff()
}

@objc func updated(_ deltaX: CGFloat, _ deltaY: CGFloat) {
Expand All @@ -280,26 +265,16 @@ class CameraAction: Action {
counter += 1
if !isMoving {
isMoving = true
movingFast = false
idled = false
location = center
counter = 0
stationaryCount = 0
Toucher.touchcam(point: self.center, phase: UITouch.Phase.began, tid: id)

delay(0.1, closure: checkEnded)
delay(0.01, closure: checkEnded)
}
// if not moving fast, regard the user fine-tuning the camera(e.g. aiming)
// so hold the touch for longer to avoid cold startup
if deltaX.magnitude + deltaY.magnitude > 12 {
// if we had mistaken this as player aiming
if self.idled {
// Toast.showOver(msg: "idled")
// since not aiming, re-touch to re-gain control
self.doLiftOff()
return
}
movingFast = true
if self.counter == 120 {
self.doLiftOff()
return
}
self.location.x += deltaX
self.location.y -= deltaY
Expand Down
12 changes: 4 additions & 8 deletions PlayTools/Keymap/DragElementsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class KeymapHolder: CircleMenuDelegate {
frame: CGRect(x: 0, y: 0, width: 50, height: 50),
normalIcon: "xmark.circle.fill",
selectedIcon: "xmark.circle.fill",
buttonsCount: 6,
buttonsCount: 4,
duration: 0.25,
distance: 80)
menu?.delegate = self
Expand Down Expand Up @@ -56,12 +56,8 @@ class KeymapHolder: CircleMenuDelegate {
EditorController.shared.addJoystick(globalPoint!)
case 2:
EditorController.shared.addMouseArea(globalPoint!)
case 3:
EditorController.shared.addRMB(globalPoint!)
case 4:
EditorController.shared.addLMB(globalPoint!)
default:
EditorController.shared.addMMB(globalPoint!)
EditorController.shared.addMouseJoystick(globalPoint!)
}
hideWithAnimation()
}
Expand All @@ -78,8 +74,8 @@ class KeymapHolder: CircleMenuDelegate {
"circle.circle",
"dpad",
"arrow.up.and.down.and.arrow.left.and.right",
"rb.rectangle.roundedbottom.fill",
"lb.rectangle.roundedbottom",
// "rb.rectangle.roundedbottom.fill",
// "lb.rectangle.roundedbottom",
"computermouse"
]
}
17 changes: 11 additions & 6 deletions PlayTools/Keymap/EditorController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,21 @@ class EditorController {
self.addLMB(toPoint)
}

public func addRMB(_ toPoint: CGPoint) {
self.addButton(keyCode: -2, point: toPoint)
}

public func addLMB(_ toPoint: CGPoint) {
self.addButton(keyCode: -1, point: toPoint)
}

public func addMMB(_ toPoint: CGPoint) {
self.addButton(keyCode: -3, point: toPoint)
public func addMouseJoystick(_ center: CGPoint) {
if editorMode {
addControlToView(control: JoystickModel(data: ControlData(keyCodes: [GCKeyCode.keyW.rawValue,
GCKeyCode.keyS.rawValue,
GCKeyCode.keyA.rawValue,
GCKeyCode.keyD.rawValue],
keyName: "Mouse",
size: 20,
xCoord: center.x.relativeX,
yCoord: center.y.relativeY)))
}
}

public func addMouseArea(_ center: CGPoint) {
Expand Down

0 comments on commit aca900f

Please sign in to comment.