Skip to content

Commit

Permalink
exyte#561: Built-in zoom support (Fix OSX build)
Browse files Browse the repository at this point in the history
  • Loading branch information
ystrot committed Apr 8, 2019
1 parent 84602c4 commit 916240f
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 174 deletions.
52 changes: 8 additions & 44 deletions Source/platform/iOS/MView_iOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?) {
}
}

Expand Down
98 changes: 9 additions & 89 deletions Source/platform/macOS/MView_macOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ open class MView: NSView, Touchable {
super.init(coder: coder)

self.wantsLayer = true
setupMouse()
}

open override var isFlipped: Bool {
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions Source/svg/SVGConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ open class SVGConstants {
"yellow": 0xffff00,
"yellowgreen": 0x9acd32
]

#if os(iOS)
public static let systemColorList = [
"AppWorkspace": 0xffffff,
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion Source/svg/SVGParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
52 changes: 23 additions & 29 deletions Source/views/MacawView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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)

Expand Down Expand Up @@ -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
}
Expand All @@ -323,17 +306,18 @@ open class MacawView: MView, MGestureRecognizerDelegate {
return
}

let touchPoints = convert(touches: touches)
touchesOfNode.keys.forEach { currentNode in
guard let initialTouches = touchesOfNode[currentNode] else {
return
}

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

Expand Down
7 changes: 2 additions & 5 deletions Source/views/MacawZoom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ open class MacawZoom {
trackScale = scale
trackRotate = rotate
if scale || rotate {
#if os(iOS)
view.isMultipleTouchEnabled = true
#endif
}
}

Expand Down Expand Up @@ -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? {
Expand Down
8 changes: 4 additions & 4 deletions Source/views/Touchable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
}

0 comments on commit 916240f

Please sign in to comment.