I'm trying to make a first person game where moving the mouse changes the camera angle, similar to in Roblox in first person mode or in Minecraft. How to do that? I'm asking how to do basic functionality so there is no code to provide. I just need to know how to hide and lock the cursor position in Swift or more specifically Metal or Cocoa.
You can subclass NSView, add a tracking area for mouse entered and exited, override NSView mouseEntered and mouseExited methods and just hide and unhide the cursor there:
import Cocoa
class HiddenCursorView: NSView {
override func awakeFromNib() {
addTrackingArea(NSTrackingArea(rect: bounds, options: [.activeAlways, .mouseEnteredAndExited], owner: self, userInfo: nil))
}
override func mouseEntered(with event: NSEvent) {
NSCursor.hide()
}
@objc override func mouseExited(with event: NSEvent) {
NSCursor.unhide()
}
}