Skip to content

Commit

Permalink
Closed paths move location back to last unbroken segment
Browse files Browse the repository at this point in the history
  • Loading branch information
swhitty committed Aug 3, 2022
1 parent 937a1a9 commit abd0f23
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Examples/Sources/ViewController.swift
Expand Up @@ -65,7 +65,7 @@ class ViewController: UIViewController {

override func loadView() {
let imageView = UIImageView(frame: UIScreen.main.bounds)
imageView.image = Image(named: "stylesheet.svg", in: .samples)?.rasterize()
imageView.image = Image(named: "path-close.svg", in: .samples)?.rasterize()
imageView.contentMode = .scaleAspectFit
imageView.backgroundColor = .white
self.view = imageView
Expand Down
3 changes: 3 additions & 0 deletions Samples/key.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions Samples/path-close.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 12 additions & 10 deletions SwiftDraw/LayerTree.Path.swift
Expand Up @@ -70,17 +70,12 @@ extension LayerTree.Path {

return location
}

var lastStart: LayerTree.Point? {
let rev = segments.reversed().dropFirst()
guard
let closeIdx = rev.firstIndex(where: { $0.isClose }),
closeIdx != rev.startIndex else {
return segments.first?.location
}

let point = rev.index(before: closeIdx)
return rev[point].location
guard let index = segments.lastIndex(where:\.isMove) else {
return segments.first?.location
}
return segments[index].location
}
}

Expand All @@ -92,6 +87,13 @@ private extension LayerTree.Path.Segment {
}
return true
}

var isMove: Bool {
guard case .move = self else {
return false
}
return true
}

var location: LayerTree.Point? {
switch self {
Expand Down
6 changes: 6 additions & 0 deletions SwiftDrawTests/LayerTree.PathTests.swift
Expand Up @@ -42,6 +42,12 @@ final class LayerTreePathTests: XCTestCase {
func testLocation() {
let path = Path()
XCTAssertNil(path.location)

path.segments.append(move(10, 10))
XCTAssertEqual(path.location, Point(10, 10))

path.segments.append(line(20, 20))
XCTAssertEqual(path.location, Point(20, 20))

path.segments.append(move(110, 90))
XCTAssertEqual(path.location, Point(110, 90))
Expand Down

0 comments on commit abd0f23

Please sign in to comment.