mirror of
https://github.com/exyte/Macaw.git
synced 2024-09-11 05:05:23 +03:00
#561: Built-in zoom support (Fix OSX build)
This commit is contained in:
parent
84602c48a0
commit
916240f118
@ -26,70 +26,34 @@ open class MView: UIView, Touchable {
|
||||
|
||||
open override func touchesBegan(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
super.touchesBegan(touches, with: event)
|
||||
|
||||
let touchPoints = touches.map { touch -> MTouchEvent in
|
||||
let location = touch.location(in: self)
|
||||
let id = Int(bitPattern: Unmanaged.passUnretained(touch).toOpaque())
|
||||
|
||||
return MTouchEvent(x: Double(location.x), y: Double(location.y), id: id)
|
||||
}
|
||||
|
||||
mTouchesBegan(touchPoints)
|
||||
mTouchesBegan(touches, with: event)
|
||||
}
|
||||
|
||||
open override func touchesMoved(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
super.touchesMoved(touches, with: event)
|
||||
|
||||
let touchPoints = touches.map { touch -> MTouchEvent in
|
||||
let location = touch.location(in: self)
|
||||
let id = Int(bitPattern: Unmanaged.passUnretained(touch).toOpaque())
|
||||
|
||||
return MTouchEvent(x: Double(location.x), y: Double(location.y), id: id)
|
||||
}
|
||||
|
||||
self.mTouchesMoved(touchPoints)
|
||||
mTouchesMoved(touches, with: event)
|
||||
}
|
||||
|
||||
open override func touchesEnded(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
super.touchesEnded(touches, with: event)
|
||||
|
||||
let touchPoints = touches.map { touch -> MTouchEvent in
|
||||
let location = touch.location(in: self)
|
||||
let id = Int(bitPattern: Unmanaged.passUnretained(touch).toOpaque())
|
||||
|
||||
return MTouchEvent(x: Double(location.x), y: Double(location.y), id: id)
|
||||
}
|
||||
|
||||
mTouchesEnded(touchPoints)
|
||||
mTouchesEnded(touches, with: event)
|
||||
}
|
||||
|
||||
override open func touchesCancelled(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
super.touchesCancelled(touches, with: event)
|
||||
|
||||
let touchPoints = touches.map { touch -> MTouchEvent in
|
||||
let location = touch.location(in: self)
|
||||
let id = Int(bitPattern: Unmanaged.passUnretained(touch).toOpaque())
|
||||
|
||||
return MTouchEvent(x: Double(location.x), y: Double(location.y), id: id)
|
||||
mTouchesCancelled(touches, with: event)
|
||||
}
|
||||
|
||||
mTouchesCancelled(touchPoints)
|
||||
func mTouchesBegan(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
}
|
||||
|
||||
func mTouchesBegan(_ touches: [MTouchEvent]) {
|
||||
|
||||
func mTouchesMoved(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
}
|
||||
|
||||
func mTouchesMoved(_ touches: [MTouchEvent]) {
|
||||
|
||||
func mTouchesEnded(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
}
|
||||
|
||||
func mTouchesEnded(_ touches: [MTouchEvent]) {
|
||||
|
||||
}
|
||||
|
||||
func mTouchesCancelled(_ touches: [MTouchEvent]) {
|
||||
|
||||
func mTouchesCancelled(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -38,7 +38,6 @@ open class MView: NSView, Touchable {
|
||||
super.init(coder: coder)
|
||||
|
||||
self.wantsLayer = true
|
||||
setupMouse()
|
||||
}
|
||||
|
||||
open override var isFlipped: Bool {
|
||||
@ -84,114 +83,35 @@ open class MView: NSView, Touchable {
|
||||
// MARK: - Touch pad
|
||||
open override func touchesBegan(with event: NSEvent) {
|
||||
super.touchesBegan(with: event)
|
||||
|
||||
let touchPoints = event.touches(matching: .any, in: self).map { touch -> MTouchEvent in
|
||||
let location = touch.location(in: self)
|
||||
let id = Int(bitPattern: Unmanaged.passUnretained(touch).toOpaque())
|
||||
|
||||
return MTouchEvent(x: Double(location.x), y: Double(location.y), id: id)
|
||||
}
|
||||
|
||||
mTouchesBegan(touchPoints)
|
||||
mTouchesBegan(event.touches(matching: .any, in: self), with: event)
|
||||
}
|
||||
|
||||
open override func touchesEnded(with event: NSEvent) {
|
||||
super.touchesEnded(with: event)
|
||||
|
||||
let touchPoints = event.touches(matching: .any, in: self).map { touch -> MTouchEvent in
|
||||
let location = touch.location(in: self)
|
||||
let id = Int(bitPattern: Unmanaged.passUnretained(touch).toOpaque())
|
||||
|
||||
return MTouchEvent(x: Double(location.x), y: Double(location.y), id: id)
|
||||
}
|
||||
|
||||
mTouchesEnded(touchPoints)
|
||||
mTouchesEnded(event.touches(matching: .any, in: self), with: event)
|
||||
}
|
||||
|
||||
open override func touchesMoved(with event: NSEvent) {
|
||||
super.touchesMoved(with: event)
|
||||
|
||||
let touchPoints = event.touches(matching: .any, in: self).map { touch -> MTouchEvent in
|
||||
let location = touch.location(in: self)
|
||||
let id = Int(bitPattern: Unmanaged.passUnretained(touch).toOpaque())
|
||||
|
||||
return MTouchEvent(x: Double(location.x), y: Double(location.y), id: id)
|
||||
}
|
||||
|
||||
mTouchesMoved(touchPoints)
|
||||
mTouchesMoved(event.touches(matching: .any, in: self), with: event)
|
||||
}
|
||||
|
||||
open override func touchesCancelled(with event: NSEvent) {
|
||||
super.touchesCancelled(with: event)
|
||||
|
||||
let touchPoints = event.touches(matching: .any, in: self).map { touch -> MTouchEvent in
|
||||
let location = touch.location(in: self)
|
||||
let id = Int(bitPattern: Unmanaged.passUnretained(touch).toOpaque())
|
||||
|
||||
return MTouchEvent(x: Double(location.x), y: Double(location.y), id: id)
|
||||
}
|
||||
|
||||
mTouchesCancelled(touchPoints)
|
||||
}
|
||||
|
||||
// MARK: - Mouse
|
||||
private func setupMouse() {
|
||||
subscribeForMouseDown()
|
||||
subscribeForMouseUp()
|
||||
subscribeForMouseDragged()
|
||||
}
|
||||
|
||||
private func subscribeForMouseDown() {
|
||||
NSEvent.addLocalMonitorForEvents(matching: .leftMouseDown) { [weak self] event -> NSEvent? in
|
||||
self?.handleInput(event: event) { touches in
|
||||
self?.mTouchesBegan(touches)
|
||||
}
|
||||
return event
|
||||
}
|
||||
}
|
||||
|
||||
private func subscribeForMouseUp() {
|
||||
NSEvent.addLocalMonitorForEvents(matching: .leftMouseUp) { [weak self] event -> NSEvent? in
|
||||
self?.handleInput(event: event) { touches in
|
||||
self?.mTouchesEnded(touches)
|
||||
}
|
||||
return event
|
||||
}
|
||||
}
|
||||
|
||||
private func subscribeForMouseDragged() {
|
||||
NSEvent.addLocalMonitorForEvents(matching: .leftMouseDragged) { [weak self] event -> NSEvent? in
|
||||
self?.handleInput(event: event) { touches in
|
||||
self?.mTouchesMoved(touches)
|
||||
}
|
||||
return event
|
||||
}
|
||||
}
|
||||
|
||||
private func handleInput(event: NSEvent, handler: (_ touches: [MTouchEvent]) -> Void ) {
|
||||
let location = self.convert(event.locationInWindow, to: .none)
|
||||
let touchPoint = MTouchEvent(x: Double(location.x), y: Double(location.y), id: 0)
|
||||
|
||||
handler([touchPoint])
|
||||
|
||||
return
|
||||
mTouchesCancelled(event.touches(matching: .any, in: self), with: event)
|
||||
}
|
||||
|
||||
// MARK: - Touchable
|
||||
func mTouchesBegan(_ touches: [MTouchEvent]) {
|
||||
|
||||
func mTouchesBegan(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
}
|
||||
|
||||
func mTouchesMoved(_ touches: [MTouchEvent]) {
|
||||
|
||||
func mTouchesMoved(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
}
|
||||
|
||||
func mTouchesEnded(_ touches: [MTouchEvent]) {
|
||||
|
||||
func mTouchesEnded(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
}
|
||||
|
||||
func mTouchesCancelled(_ touches: [MTouchEvent]) {
|
||||
|
||||
func mTouchesCancelled(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
@ -249,29 +249,11 @@ open class MacawView: MView, MGestureRecognizerDelegate {
|
||||
return .none
|
||||
}
|
||||
|
||||
open override func touchesBegan(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
super.touchesBegan(touches, with: event)
|
||||
zoom.touchesBegan(touches)
|
||||
}
|
||||
|
||||
open override func touchesMoved(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
super.touchesMoved(touches, with: event)
|
||||
zoom.touchesMoved(touches)
|
||||
}
|
||||
|
||||
open override func touchesEnded(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
super.touchesEnded(touches, with: event)
|
||||
zoom.touchesEnded(touches)
|
||||
}
|
||||
|
||||
open override func touchesCancelled(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
super.touchesCancelled(touches, with: event)
|
||||
zoom.touchesEnded(touches)
|
||||
}
|
||||
|
||||
// MARK: - Touches
|
||||
override func mTouchesBegan(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
zoom.touchesBegan(touches)
|
||||
|
||||
override func mTouchesBegan(_ touches: [MTouchEvent]) {
|
||||
let touchPoints = convert(touches: touches)
|
||||
if !self.node.shouldCheckForPressed() &&
|
||||
!self.node.shouldCheckForMoved() &&
|
||||
!self.node.shouldCheckForReleased () {
|
||||
@ -282,7 +264,7 @@ open class MacawView: MView, MGestureRecognizerDelegate {
|
||||
return
|
||||
}
|
||||
|
||||
for touch in touches {
|
||||
for touch in touchPoints {
|
||||
let location = CGPoint(x: touch.x, y: touch.y)
|
||||
var nodePath = doFindNode(location: location)
|
||||
|
||||
@ -314,7 +296,8 @@ open class MacawView: MView, MGestureRecognizerDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
override func mTouchesMoved(_ touches: [MTouchEvent]) {
|
||||
override func mTouchesMoved(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
zoom.touchesMoved(touches)
|
||||
if !self.node.shouldCheckForMoved() {
|
||||
return
|
||||
}
|
||||
@ -323,6 +306,7 @@ open class MacawView: MView, MGestureRecognizerDelegate {
|
||||
return
|
||||
}
|
||||
|
||||
let touchPoints = convert(touches: touches)
|
||||
touchesOfNode.keys.forEach { currentNode in
|
||||
guard let initialTouches = touchesOfNode[currentNode] else {
|
||||
return
|
||||
@ -330,10 +314,10 @@ open class MacawView: MView, MGestureRecognizerDelegate {
|
||||
|
||||
var points = [TouchPoint]()
|
||||
for initialTouch in initialTouches {
|
||||
guard let currentIndex = touches.firstIndex(of: initialTouch) else {
|
||||
guard let currentIndex = touchPoints.firstIndex(of: initialTouch) else {
|
||||
continue
|
||||
}
|
||||
let currentTouch = touches[currentIndex]
|
||||
let currentTouch = touchPoints[currentIndex]
|
||||
guard let nodePath = touchesMap[currentTouch]?.first else {
|
||||
continue
|
||||
}
|
||||
@ -349,20 +333,30 @@ open class MacawView: MView, MGestureRecognizerDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
override func mTouchesCancelled(_ touches: [MTouchEvent]) {
|
||||
override func mTouchesCancelled(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
touchesEnded(touches: touches)
|
||||
}
|
||||
|
||||
override func mTouchesEnded(_ touches: [MTouchEvent]) {
|
||||
override func mTouchesEnded(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
touchesEnded(touches: touches)
|
||||
}
|
||||
|
||||
private func touchesEnded(touches: [MTouchEvent]) {
|
||||
private func convert(touches: Set<MTouch>) -> [MTouchEvent] {
|
||||
return touches.map { touch -> MTouchEvent in
|
||||
let location = touch.location(in: self)
|
||||
let id = Int(bitPattern: Unmanaged.passUnretained(touch).toOpaque())
|
||||
return MTouchEvent(x: Double(location.x), y: Double(location.y), id: id)
|
||||
}
|
||||
}
|
||||
|
||||
private func touchesEnded(touches: Set<MTouch>) {
|
||||
zoom.touchesEnded(touches)
|
||||
guard let _ = renderer else {
|
||||
return
|
||||
}
|
||||
|
||||
for touch in touches {
|
||||
let touchPoints = convert(touches: touches)
|
||||
for touch in touchPoints {
|
||||
|
||||
touchesMap[touch]?.forEach { nodePath in
|
||||
|
||||
|
@ -30,7 +30,9 @@ open class MacawZoom {
|
||||
trackScale = scale
|
||||
trackRotate = rotate
|
||||
if scale || rotate {
|
||||
#if os(iOS)
|
||||
view.isMultipleTouchEnabled = true
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@ -66,11 +68,6 @@ open class MacawZoom {
|
||||
|
||||
func touchesEnded(_ touches: Set<MTouch>) {
|
||||
cleanTouches()
|
||||
if let touch = touches.first {
|
||||
if touches.count == 1 && touch.tapCount == 2 && touch.timestamp + 0.3 >= CACurrentMediaTime() {
|
||||
set(offset: .zero, scale: 1, angle: 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@discardableResult private func cleanTouches() -> ZoomData? {
|
||||
|
@ -19,8 +19,8 @@ class MTouchEvent: Hashable {
|
||||
}
|
||||
|
||||
protocol Touchable {
|
||||
func mTouchesBegan(_ touches: [MTouchEvent])
|
||||
func mTouchesMoved(_ touches: [MTouchEvent])
|
||||
func mTouchesEnded(_ touches: [MTouchEvent])
|
||||
func mTouchesCancelled(_ touches: [MTouchEvent])
|
||||
func mTouchesBegan(_ touches: Set<MTouch>, with event: MEvent?)
|
||||
func mTouchesMoved(_ touches: Set<MTouch>, with event: MEvent?)
|
||||
func mTouchesEnded(_ touches: Set<MTouch>, with event: MEvent?)
|
||||
func mTouchesCancelled(_ touches: Set<MTouch>, with event: MEvent?)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user