Browse Source

【注释】手绘签名未支持从触控板写字

liujiajie 10 months ago
parent
commit
2d3763ab84

+ 142 - 1
PDF Office/PDF Master/Class/PDFWindowController/Side/RightSide/Signature/KMDrawView.swift

@@ -27,7 +27,53 @@ private var _points: [CGPoint] = [CGPoint](repeating: .zero, count: 5)
         }
     }
     private var bezierPath: NSBezierPath = NSBezierPath()
-    var isAcceptsTouch: Bool = false
+    var isAcceptsTouch: Bool = false {
+        willSet{
+            
+        }
+        didSet{
+            self.acceptsTouchEvents = isAcceptsTouch
+            let screenHeight = CGDisplayPixelsHigh(CGMainDisplayID())
+            let frameToWindow = self.convert(self.bounds, to: nil)
+            var frameToScreen = self.window!.convertToScreen(frameToWindow)
+            if isAcceptsTouch {
+                // If the mouse cursor is not already hidden,
+                if !self.cursorIsHidden {
+                    frameToScreen.origin.x += 5
+                    frameToScreen.origin.y = CGFloat(screenHeight) - frameToScreen.origin.y - 5
+                    CGWarpMouseCursorPosition(frameToScreen.origin)
+                    
+                    // Detach the mouse cursor from the mouse
+                    // hardware so that moving the mouse (or a
+                    // single finger) will not move the cursor
+                    CGAssociateMouseAndMouseCursorPosition(0)
+                    
+                    // Hide the mouse cursor
+                    NSCursor.hide()
+                    
+                    // Remember that we detached and hid the
+                    // mouse cursor
+                    self.cursorIsHidden = true
+                }
+            } else {
+                frameToScreen.origin.y = CGFloat(screenHeight) - frameToScreen.origin.y
+                CGWarpMouseCursorPosition(frameToScreen.origin)
+                
+                // Attach the mouse cursor to the mouse
+                // hardware so that moving the mouse (or a
+                // single finger) will move the cursor
+                CGAssociateMouseAndMouseCursorPosition(1)
+                
+                // Show the mouse cursor
+                NSCursor.unhide()
+                
+                // Remember that we attached and unhid the
+                // mouse cursor so that the next touch that
+                // begins will detach and hide it
+                self.cursorIsHidden = false
+            }
+        }
+    }
     private var cursorIsHidden: Bool = false
     private var mouseIsInView: Bool = false
     private var activeTouch: NSTouch?
@@ -163,6 +209,101 @@ private var _points: [CGPoint] = [CGPoint](repeating: .zero, count: 5)
         }
     }
     
+    override func touchesBegan(with event: NSEvent) {
+        super.touchesBegan(with: event)
+        let touches = event.touches(matching: .began, in: self)
+        self.activeTouch = touches.first
+        
+        guard let activeTouch = self.activeTouch else { return }
+        
+        var point = activeTouch.normalizedPosition
+        point.x *= self.bounds.size.width
+        point.y *= self.bounds.size.height
+        _index = 0
+        _points[0] = point
+        self.needsDisplay = true
+        
+        if !self.cursorIsHidden {
+            CGAssociateMouseAndMouseCursorPosition(0)
+            NSCursor.hide()
+            self.cursorIsHidden = true
+        }
+        
+        if let changeDrawCallback = changeDrawCallback {
+            changeDrawCallback(true)
+        }
+    }
+    
+    override func touchesMoved(with event: NSEvent) {
+        super.touchesMoved(with: event)
+        
+        let touches = event.touches(matching: .moved, in: self)
+        
+        var isTouch = false
+        for touch in touches {
+            if touch.identity.isEqual(self.activeTouch?.identity) {
+                isTouch = true
+                self.activeTouch = touch
+            }
+        }
+        
+        guard isTouch else { return }
+        
+        var point = self.activeTouch!.normalizedPosition
+        point.x *= self.bounds.size.width
+        point.y *= self.bounds.size.height
+        
+        _index += 1
+        _points[_index] = point
+        
+        if _index == 4 {
+            _points[3] = CGPoint(x: (_points[2].x + _points[4].x)/2.0,
+                                 y: (_points[2].y + _points[4].y)/2.0)
+            
+            bezierPath.move(to: _points[0])
+            bezierPath.curve(to: _points[3], controlPoint1: _points[1], controlPoint2: _points[2])
+            
+            _points[0] = _points[3]
+            _points[1] = _points[4]
+            _index = 1
+            self.needsDisplay = true
+        }
+    }
+    
+    override func touchesEnded(with event: NSEvent) {
+        super.touchesEnded(with: event)
+        
+        let touches = event.touches(matching: .moved, in: self)
+        
+        for touch in touches {
+            if touch.identity.isEqual(self.activeTouch?.identity) {
+                self.activeTouch = nil
+            }
+        }
+        
+        if _index < 4 {
+            for i in 0..<_index {
+                bezierPath.move(to: _points[i])
+            }
+            self.needsDisplay = true
+        }
+        
+        // var image = signatureImage()
+        // if let changeDrawCallback = changeDrawCallback {
+        //     changeDrawCallback(image)
+        // }
+    }
+    
+    override func touchesCancelled(with event: NSEvent) {
+        super.touchesCancelled(with: event)
+        
+        for i in 0..<_index {
+            bezierPath.move(to: _points[i])
+        }
+        activeTouch = nil
+        self.needsDisplay = true
+    }
+    
     override func mouseEntered(with event: NSEvent) {
         window?.makeFirstResponder(self)
         // self.mouseIsInView = true