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

Add handlers for touch events (iOS) #9

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Sources/EasyMetalShader/MetalPreLibrary/VertexInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,18 @@ public struct VertexInput {
self.input8 = input8
self.input9 = input9
}

public init(
input0: simd_float4,
input1: simd_float4
) {
self.input0 = input0
self.input1 = input1
}

public init(
input0: simd_float4
) {
self.input0 = input0
}
}
8 changes: 8 additions & 0 deletions Sources/EasyMetalShader/Pipelines/ShaderRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ open class ShaderRenderer: NSObject, MTKViewDelegate {
}

open func draw(view: MTKView, drawable: CAMetalDrawable) {}

#if os(iOS)
open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?, view: UIView?) {}
open func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?, view: UIView?) {}
open func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?, view: UIView?) {}
open func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?, view: UIView?) {}
open func onScroll(recognizer: UIPanGestureRecognizer?, view: UIView?) {}
#endif
}
42 changes: 42 additions & 0 deletions Sources/EasyMetalShader/Views/ShaderMTKView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public class ShaderMTKView: MTKView {
self.sampleCount = 1
self.clearDepth = 1.0

#if os(macOS)
self.layer?.isOpaque = false
#elseif os(iOS)
self.layer.isOpaque = false
#endif

#if os(macOS)
let options: NSTrackingArea.Options = [
.mouseMoved,
Expand All @@ -43,12 +49,30 @@ public class ShaderMTKView: MTKView {
let trackingArea = NSTrackingArea(rect: bounds, options: options, owner: self, userInfo: nil)
self.addTrackingArea(trackingArea)
#endif

#if os(iOS)
let scrollGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(onScroll))
scrollGestureRecognizer.allowedScrollTypesMask = .continuous
scrollGestureRecognizer.minimumNumberOfTouches = 2
scrollGestureRecognizer.maximumNumberOfTouches = 2
self.addGestureRecognizer(scrollGestureRecognizer)
#endif
}

required init(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

deinit {
#if os(iOS)
if let recognizers = self.gestureRecognizers {
for recognizer in recognizers {
self.removeGestureRecognizer(recognizer)
}
}
#endif
}

#if os(macOS)
public override func mouseDragged(with event: NSEvent) {
let mousePos = mousePos(event: event, viewFrame: self.superview!.frame)
Expand All @@ -64,4 +88,22 @@ public class ShaderMTKView: MTKView {
return simd_float2(Float(location.x), Float(location.y))
}
#endif

#if os(iOS)
public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
renderer.touchesBegan(touches, with: event, view: self)
}
public override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
renderer.touchesMoved(touches, with: event, view: self)
}
public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
renderer.touchesEnded(touches, with: event, view: self)
}
public override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
renderer.touchesCancelled(touches, with: event, view: self)
}
@objc func onScroll(recognizer: UIPanGestureRecognizer) {
renderer.onScroll(recognizer: recognizer, view: self)
}
#endif
}