1
1
mirror of https://github.com/exyte/Macaw.git synced 2024-08-15 16:10:39 +03:00

#561: Built-in zoom support (Fix OSX build)

This commit is contained in:
Yuri Strot 2019-04-08 19:27:57 +07:00
parent 84602c48a0
commit 916240f118
7 changed files with 49 additions and 174 deletions

View File

@ -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(touchPoints)
mTouchesCancelled(touches, with: event)
}
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?) {
}
}

View File

@ -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<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

View File

@ -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
}

View File

@ -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)
}

View File

@ -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

View File

@ -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? {

View File

@ -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?)
}