-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #146 from XuYicong/debug-overlay
Add a touch point visualization mechanism to help debugging
- Loading branch information
Showing
8 changed files
with
303 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
PlayTools/Controls/Backend/DebugOverlay/DebugController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// DebugController.swift | ||
// PlayTools | ||
// | ||
// Created by 许沂聪 on 2024/5/28. | ||
// | ||
|
||
import Foundation | ||
|
||
class DebugController { | ||
static let instance = DebugController() | ||
private init() { | ||
|
||
} | ||
|
||
private var debugView = DebugContainer.instance | ||
|
||
public func toggleDebugOverlay() { | ||
let window = screen.keyWindow | ||
let controller = window!.rootViewController | ||
let view = controller!.view | ||
if debugView.superview == nil { | ||
view!.addSubview(debugView) | ||
view!.bringSubviewToFront(debugView) | ||
debugView.isHidden = false | ||
debugView.isUserInteractionEnabled = false | ||
PlayInput.touchQueue.async { | ||
DebugModel.instance.enabled = true | ||
} | ||
} else { | ||
debugView.removeFromSuperview() | ||
PlayInput.touchQueue.async { | ||
DebugModel.instance.enabled = false | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// DebugModel.swift | ||
// PlayTools | ||
// | ||
// Created by 许沂聪 on 2024/5/28. | ||
// | ||
|
||
import Foundation | ||
|
||
class DebugModel { | ||
static let instance = DebugModel() | ||
private init() { | ||
touches = [] | ||
} | ||
struct TouchPoint { | ||
var point: CGPoint | ||
var phase: UITouch.Phase | ||
var description: String | ||
} | ||
|
||
public var touches: [TouchPoint] | ||
public var enabled = false | ||
public func record(point: CGPoint, phase: UITouch.Phase, tid: Int, description: String) { | ||
// If debug screen not enabled, do not record | ||
if !enabled { | ||
return | ||
} | ||
// Run in main thread, because `touches` is not thread safe | ||
DispatchQueue.main.async { | ||
while self.touches.count < tid { | ||
// report error | ||
self.touches.append(TouchPoint( | ||
point: CGPoint(x: 100, y: 100), | ||
phase: UITouch.Phase.cancelled, | ||
description: "Error recording debug info: point id exceeds record array" | ||
)) | ||
} | ||
if self.touches.count == tid { | ||
self.touches.append(TouchPoint(point: point, phase: phase, description: description)) | ||
} else { | ||
self.touches[tid] = TouchPoint(point: point, phase: phase, description: description) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.