Skip to content

Commit

Permalink
Merge pull request #9 from yukiny0811/touchevents
Browse files Browse the repository at this point in the history
Add handlers for touch events (iOS)
  • Loading branch information
yukiny0811 authored Jan 14, 2024
2 parents b256285 + baf576a commit f48ebeb
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
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
}

0 comments on commit f48ebeb

Please sign in to comment.