From 916240f118690cfdab278095c7f606358a6a000e Mon Sep 17 00:00:00 2001 From: Yuri Strot Date: Mon, 8 Apr 2019 19:27:57 +0700 Subject: [PATCH] #561: Built-in zoom support (Fix OSX build) --- Source/platform/iOS/MView_iOS.swift | 52 ++----------- Source/platform/macOS/MView_macOS.swift | 98 +++---------------------- Source/svg/SVGConstants.swift | 4 +- Source/svg/SVGParser.swift | 2 +- Source/views/MacawView.swift | 52 ++++++------- Source/views/MacawZoom.swift | 7 +- Source/views/Touchable.swift | 8 +- 7 files changed, 49 insertions(+), 174 deletions(-) diff --git a/Source/platform/iOS/MView_iOS.swift b/Source/platform/iOS/MView_iOS.swift index b97c7006..61d8d32e 100644 --- a/Source/platform/iOS/MView_iOS.swift +++ b/Source/platform/iOS/MView_iOS.swift @@ -26,70 +26,34 @@ open class MView: UIView, Touchable { open override func touchesBegan(_ touches: Set, 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, 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, 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, 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(touchPoints) + mTouchesCancelled(touches, with: event) } - func mTouchesBegan(_ touches: [MTouchEvent]) { - + func mTouchesBegan(_ touches: Set, with event: MEvent?) { } - func mTouchesMoved(_ touches: [MTouchEvent]) { - + func mTouchesMoved(_ touches: Set, with event: MEvent?) { } - func mTouchesEnded(_ touches: [MTouchEvent]) { - + func mTouchesEnded(_ touches: Set, with event: MEvent?) { } - func mTouchesCancelled(_ touches: [MTouchEvent]) { - + func mTouchesCancelled(_ touches: Set, with event: MEvent?) { } } diff --git a/Source/platform/macOS/MView_macOS.swift b/Source/platform/macOS/MView_macOS.swift index b2bfd231..141346b7 100644 --- a/Source/platform/macOS/MView_macOS.swift +++ b/Source/platform/macOS/MView_macOS.swift @@ -38,7 +38,6 @@ open class MView: NSView, Touchable { super.init(coder: coder) self.wantsLayer = true - setupMouse() } open override var isFlipped: Bool { @@ -80,118 +79,39 @@ open class MView: NSView, Touchable { func layoutSubviews() { super.resizeSubviews(withOldSize: self.bounds.size) } - + // 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, with event: MEvent?) { } - func mTouchesMoved(_ touches: [MTouchEvent]) { - + func mTouchesMoved(_ touches: Set, with event: MEvent?) { } - func mTouchesEnded(_ touches: [MTouchEvent]) { - + func mTouchesEnded(_ touches: Set, with event: MEvent?) { } - func mTouchesCancelled(_ touches: [MTouchEvent]) { - + func mTouchesCancelled(_ touches: Set, with event: MEvent?) { } } #endif diff --git a/Source/svg/SVGConstants.swift b/Source/svg/SVGConstants.swift index 4ebe0822..5111644d 100644 --- a/Source/svg/SVGConstants.swift +++ b/Source/svg/SVGConstants.swift @@ -292,7 +292,7 @@ open class SVGConstants { "yellow": 0xffff00, "yellowgreen": 0x9acd32 ] - + #if os(iOS) public static let systemColorList = [ "AppWorkspace": 0xffffff, @@ -334,7 +334,7 @@ open class SVGConstants { "WindowText": 0x242424 ] #endif - + public static func valueToColor(_ color: Int) -> String? { return SVGConstants.colorList.filter { _, v -> Bool in v == color }.map { k, _ -> String in k }.first } diff --git a/Source/svg/SVGParser.swift b/Source/svg/SVGParser.swift index d92b6ead..2b21bd02 100644 --- a/Source/svg/SVGParser.swift +++ b/Source/svg/SVGParser.swift @@ -595,7 +595,7 @@ open class SVGParser { let red = CGFloat((rgbValue >> 16) & 0xff) let green = CGFloat((rgbValue >> 08) & 0xff) let blue = CGFloat((rgbValue >> 00) & 0xff) - + return Color.rgba(r: Int(red), g: Int(green), b: Int(blue), a: opacity) } diff --git a/Source/views/MacawView.swift b/Source/views/MacawView.swift index 01823fc0..1c21593a 100644 --- a/Source/views/MacawView.swift +++ b/Source/views/MacawView.swift @@ -249,29 +249,11 @@ open class MacawView: MView, MGestureRecognizerDelegate { return .none } - open override func touchesBegan(_ touches: Set, with event: MEvent?) { - super.touchesBegan(touches, with: event) - zoom.touchesBegan(touches) - } - - open override func touchesMoved(_ touches: Set, with event: MEvent?) { - super.touchesMoved(touches, with: event) - zoom.touchesMoved(touches) - } - - open override func touchesEnded(_ touches: Set, with event: MEvent?) { - super.touchesEnded(touches, with: event) - zoom.touchesEnded(touches) - } - - open override func touchesCancelled(_ touches: Set, with event: MEvent?) { - super.touchesCancelled(touches, with: event) - zoom.touchesEnded(touches) - } - // MARK: - Touches + override func mTouchesBegan(_ touches: Set, 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, 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, with event: MEvent?) { touchesEnded(touches: touches) } - override func mTouchesEnded(_ touches: [MTouchEvent]) { + override func mTouchesEnded(_ touches: Set, with event: MEvent?) { touchesEnded(touches: touches) } - private func touchesEnded(touches: [MTouchEvent]) { + private func convert(touches: Set) -> [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) { + 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 diff --git a/Source/views/MacawZoom.swift b/Source/views/MacawZoom.swift index a420b11e..1831593b 100644 --- a/Source/views/MacawZoom.swift +++ b/Source/views/MacawZoom.swift @@ -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) { 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? { diff --git a/Source/views/Touchable.swift b/Source/views/Touchable.swift index a830e63d..f5229470 100644 --- a/Source/views/Touchable.swift +++ b/Source/views/Touchable.swift @@ -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, with event: MEvent?) + func mTouchesMoved(_ touches: Set, with event: MEvent?) + func mTouchesEnded(_ touches: Set, with event: MEvent?) + func mTouchesCancelled(_ touches: Set, with event: MEvent?) }