diff --git a/Memola/Canvas/Contexts/GraphicContext.swift b/Memola/Canvas/Contexts/GraphicContext.swift index 142d852..e004576 100644 --- a/Memola/Canvas/Contexts/GraphicContext.swift +++ b/Memola/Canvas/Contexts/GraphicContext.swift @@ -154,11 +154,12 @@ extension GraphicContext { func endStroke(at point: CGPoint) { guard currentPoint != nil, let currentStroke else { return } currentStroke.finish(at: point) - currentStroke.saveQuads(to: currentStroke.quads.endIndex) - withPersistence(\.backgroundContext) { context in + withPersistence(\.backgroundContext) { [currentStroke] context in + guard let stroke = currentStroke.stroke(as: PenStroke.self) else { return } + stroke.object?.bounds = stroke.bounds try context.saveIfNeeded() - if let stroke = currentStroke.stroke(as: PenStroke.self)?.object { - context.refresh(stroke, mergeChanges: false) + if let object = stroke.object { + context.refresh(object, mergeChanges: false) } } previousStroke = currentStroke diff --git a/Memola/Canvas/Geometries/Stroke/Core/Stroke.swift b/Memola/Canvas/Geometries/Stroke/Core/Stroke.swift index 968ab44..6829343 100644 --- a/Memola/Canvas/Geometries/Stroke/Core/Stroke.swift +++ b/Memola/Canvas/Geometries/Stroke/Core/Stroke.swift @@ -18,8 +18,6 @@ protocol Stroke: AnyObject, Drawable, Hashable, Equatable { var quads: [Quad] { get set } var penStyle: any PenStyle { get set } - var batchIndex: Int { get set } - var quadIndex: Int { get set } var keyPoints: [CGPoint] { get set } var movingAverage: MovingAverage { get set } @@ -32,8 +30,6 @@ protocol Stroke: AnyObject, Drawable, Hashable, Equatable { func finish(at point: CGPoint) func addQuad(at point: CGPoint, rotation: CGFloat, shape: QuadShape) - func removeQuads(from index: Int) - func saveQuads(to index: Int) } extension Stroke { @@ -78,11 +74,6 @@ extension Stroke { ) quads.append(quad) } - - func removeQuads(from index: Int) { - let dropCount = quads.endIndex - max(1, index) - quads.removeLast(dropCount) - } } extension Stroke { diff --git a/Memola/Canvas/Geometries/Stroke/Generators/SolidPointStrokeGenerator.swift b/Memola/Canvas/Geometries/Stroke/Generators/SolidPointStrokeGenerator.swift index bddd937..01bdce5 100644 --- a/Memola/Canvas/Geometries/Stroke/Generators/SolidPointStrokeGenerator.swift +++ b/Memola/Canvas/Geometries/Stroke/Generators/SolidPointStrokeGenerator.swift @@ -29,9 +29,6 @@ struct SolidPointStrokeGenerator: StrokeGenerator { let control = CGPoint.middle(p1: start, p2: end) addCurve(from: start, to: end, by: control, on: stroke) case 3: - let quadIndex = stroke.quadIndex + 1 - stroke.removeQuads(from: quadIndex) - stroke.saveQuads(to: quadIndex) let index = stroke.keyPoints.endIndex - 1 var start = stroke.keyPoints[index - 2] var end = CGPoint.middle(p1: stroke.keyPoints[index - 2], p2: stroke.keyPoints[index - 1]) @@ -42,7 +39,7 @@ struct SolidPointStrokeGenerator: StrokeGenerator { end = CGPoint.middle(p1: stroke.keyPoints[index - 1], p2: stroke.keyPoints[index]) addCurve(from: start, to: end, by: control, on: stroke) default: - smoothOutPath(on: stroke) + adjustKeyPoint(on: stroke) let index = stroke.keyPoints.endIndex - 1 let start = CGPoint.middle(p1: stroke.keyPoints[index - 2], p2: stroke.keyPoints[index - 1]) let control = stroke.keyPoints[index - 1] @@ -60,29 +57,6 @@ struct SolidPointStrokeGenerator: StrokeGenerator { } } - private func smoothOutPath(on stroke: any Stroke) { - let quadIndex = stroke.quadIndex + 1 - stroke.removeQuads(from: quadIndex) - stroke.saveQuads(to: quadIndex) - adjustKeyPoint(on: stroke) - switch stroke.keyPoints.endIndex { - case 4: - let index = stroke.keyPoints.endIndex - 2 - let start = stroke.keyPoints[index - 2] - let end = CGPoint.middle(p1: stroke.keyPoints[index - 2], p2: stroke.keyPoints[index - 1]) - let control = CGPoint.middle(p1: start, p2: end) - addCurve(from: start, to: end, by: control, on: stroke) - fallthrough - default: - let index = stroke.keyPoints.endIndex - 2 - let start = CGPoint.middle(p1: stroke.keyPoints[index - 2], p2: stroke.keyPoints[index - 1]) - let control = stroke.keyPoints[index - 1] - let end = CGPoint.middle(p1: stroke.keyPoints[index - 1], p2: stroke.keyPoints[index]) - addCurve(from: start, to: end, by: control, on: stroke) - } - stroke.quadIndex = stroke.quads.endIndex - 1 - } - private func adjustKeyPoint(on stroke: any Stroke) { let index = stroke.keyPoints.endIndex - 1 let prev = stroke.keyPoints[index - 1] diff --git a/Memola/Canvas/Geometries/Stroke/Strokes/EraserStroke.swift b/Memola/Canvas/Geometries/Stroke/Strokes/EraserStroke.swift index d36e55d..011ece5 100644 --- a/Memola/Canvas/Geometries/Stroke/Strokes/EraserStroke.swift +++ b/Memola/Canvas/Geometries/Stroke/Strokes/EraserStroke.swift @@ -18,8 +18,6 @@ final class EraserStroke: Stroke, @unchecked Sendable { var quads: [Quad] var penStyle: any PenStyle - var batchIndex: Int = 0 - var quadIndex: Int = -1 var keyPoints: [CGPoint] = [] var movingAverage: MovingAverage = MovingAverage(windowSize: 3) @@ -43,8 +41,4 @@ final class EraserStroke: Stroke, @unchecked Sendable { self.quads = quads self.penStyle = style.penStyle } - - func saveQuads(to index: Int) { - - } } diff --git a/Memola/Canvas/Geometries/Stroke/Strokes/PenStroke.swift b/Memola/Canvas/Geometries/Stroke/Strokes/PenStroke.swift index 5c8153b..c8f9d70 100644 --- a/Memola/Canvas/Geometries/Stroke/Strokes/PenStroke.swift +++ b/Memola/Canvas/Geometries/Stroke/Strokes/PenStroke.swift @@ -19,8 +19,6 @@ final class PenStroke: Stroke, @unchecked Sendable { var quads: [Quad] var penStyle: any PenStyle - var batchIndex: Int = 0 - var quadIndex: Int = -1 var keyPoints: [CGPoint] = [] var movingAverage: MovingAverage = MovingAverage(windowSize: 3) @@ -71,30 +69,31 @@ final class PenStroke: Stroke, @unchecked Sendable { } } - func saveQuads(to index: Int) { - let quads = Array(quads[batchIndex..