refactor: use Stroke protocol annotation in place of PenStroke

This commit is contained in:
dscyrescotti
2024-05-25 20:08:23 +07:00
parent dccf8388bc
commit fc6639b514
7 changed files with 22 additions and 15 deletions

View File

@@ -11,11 +11,12 @@ import CoreData
import Foundation import Foundation
final class GraphicContext: @unchecked Sendable { final class GraphicContext: @unchecked Sendable {
var strokes: [PenStroke] = [] var strokes: [any Stroke] = []
var object: GraphicContextObject? var object: GraphicContextObject?
var currentStroke: (any Stroke)?
var previousStroke: (any Stroke)?
var currentStroke: PenStroke?
var previousStroke: PenStroke?
var currentPoint: CGPoint? var currentPoint: CGPoint?
var renderType: RenderType = .finished var renderType: RenderType = .finished
var vertices: [ViewPortVertex] = [] var vertices: [ViewPortVertex] = []
@@ -40,7 +41,7 @@ final class GraphicContext: @unchecked Sendable {
guard !strokes.isEmpty else { return } guard !strokes.isEmpty else { return }
let stroke = strokes.removeLast() let stroke = strokes.removeLast()
withPersistence(\.backgroundContext) { [stroke] context in withPersistence(\.backgroundContext) { [stroke] context in
stroke.object?.graphicContext = nil stroke.stroke(as: PenStroke.self)?.object?.graphicContext = nil
try context.saveIfNeeded() try context.saveIfNeeded()
} }
previousStroke = nil previousStroke = nil
@@ -51,7 +52,7 @@ final class GraphicContext: @unchecked Sendable {
case .stroke(let stroke): case .stroke(let stroke):
strokes.append(stroke) strokes.append(stroke)
withPersistence(\.backgroundContext) { [weak self, stroke] context in withPersistence(\.backgroundContext) { [weak self, stroke] context in
stroke.object?.graphicContext = self?.object stroke.stroke(as: PenStroke.self)?.object?.graphicContext = self?.object
try context.saveIfNeeded() try context.saveIfNeeded()
} }
previousStroke = nil previousStroke = nil
@@ -92,7 +93,7 @@ extension GraphicContext {
func loadQuads(_ bounds: CGRect) { func loadQuads(_ bounds: CGRect) {
for stroke in self.strokes { for stroke in self.strokes {
guard stroke.isVisible(in: bounds), stroke.isEmpty else { continue } guard stroke.isVisible(in: bounds), stroke.isEmpty else { continue }
stroke.loadQuads() stroke.stroke(as: PenStroke.self)?.loadQuads()
} }
} }
} }
@@ -114,7 +115,7 @@ extension GraphicContext: Drawable {
} }
extension GraphicContext { extension GraphicContext {
func beginStroke(at point: CGPoint, pen: Pen) -> PenStroke { func beginStroke(at point: CGPoint, pen: Pen) -> any Stroke {
let stroke = PenStroke( let stroke = PenStroke(
bounds: [point.x - pen.thickness, point.y - pen.thickness, point.x + pen.thickness, point.y + pen.thickness], bounds: [point.x - pen.thickness, point.y - pen.thickness, point.x + pen.thickness, point.y + pen.thickness],
color: pen.rgba, color: pen.rgba,
@@ -156,7 +157,7 @@ extension GraphicContext {
currentStroke.saveQuads(to: currentStroke.quads.endIndex) currentStroke.saveQuads(to: currentStroke.quads.endIndex)
withPersistence(\.backgroundContext) { context in withPersistence(\.backgroundContext) { context in
try context.saveIfNeeded() try context.saveIfNeeded()
if let stroke = currentStroke.object { if let stroke = currentStroke.stroke(as: PenStroke.self)?.object {
context.refresh(stroke, mergeChanges: false) context.refresh(stroke, mergeChanges: false)
} }
} }
@@ -169,7 +170,7 @@ extension GraphicContext {
if !strokes.isEmpty { if !strokes.isEmpty {
let stroke = strokes.removeLast() let stroke = strokes.removeLast()
withPersistence(\.backgroundContext) { [graphicContext = object, _stroke = stroke] context in withPersistence(\.backgroundContext) { [graphicContext = object, _stroke = stroke] context in
if let stroke = _stroke.object { if let stroke = _stroke.stroke(as: PenStroke.self)?.object {
graphicContext?.strokes.remove(stroke) graphicContext?.strokes.remove(stroke)
context.delete(stroke) context.delete(stroke)
} }

View File

@@ -104,7 +104,7 @@ extension Canvas {
// MARK: - Graphic Context // MARK: - Graphic Context
extension Canvas { extension Canvas {
func beginTouch(at point: CGPoint, pen: Pen) -> PenStroke { func beginTouch(at point: CGPoint, pen: Pen) -> any Stroke {
graphicContext.beginStroke(at: point, pen: pen) graphicContext.beginStroke(at: point, pen: pen)
} }
@@ -124,7 +124,7 @@ extension Canvas {
graphicContext.renderType = renderType graphicContext.renderType = renderType
} }
func getNewlyAddedStroke() -> PenStroke? { func getNewlyAddedStroke() -> (any Stroke)? {
graphicContext.strokes.last graphicContext.strokes.last
} }
} }

View File

@@ -117,3 +117,9 @@ extension Stroke {
hasher.combine(id) hasher.combine(id)
} }
} }
extension Stroke {
func stroke<S: Stroke>(as type: S.Type) -> S? {
self as? S
}
}

View File

@@ -53,7 +53,7 @@ class History: ObservableObject {
switch event { switch event {
case .stroke(let _stroke): case .stroke(let _stroke):
withPersistence(\.backgroundContext) { context in withPersistence(\.backgroundContext) { context in
if let stroke = _stroke.object { if let stroke = _stroke.stroke(as: PenStroke.self)?.object {
context.delete(stroke) context.delete(stroke)
} }
try context.saveIfNeeded() try context.saveIfNeeded()

View File

@@ -8,5 +8,5 @@
import Foundation import Foundation
enum HistoryEvent { enum HistoryEvent {
case stroke(PenStroke) case stroke(any Stroke)
} }

View File

@@ -16,7 +16,7 @@ class EraserRenderPass: RenderPass {
var eraserPipelineState: MTLRenderPipelineState? var eraserPipelineState: MTLRenderPipelineState?
var quadPipelineState: MTLComputePipelineState? var quadPipelineState: MTLComputePipelineState?
var stroke: PenStroke? var stroke: (any Stroke)?
weak var graphicTexture: MTLTexture? weak var graphicTexture: MTLTexture?
init(renderer: Renderer) { init(renderer: Renderer) {

View File

@@ -18,7 +18,7 @@ class StrokeRenderPass: RenderPass {
var quadPipelineState: MTLComputePipelineState? var quadPipelineState: MTLComputePipelineState?
weak var graphicPipelineState: MTLRenderPipelineState? weak var graphicPipelineState: MTLRenderPipelineState?
var stroke: PenStroke? var stroke: (any Stroke)?
var strokeTexture: MTLTexture? var strokeTexture: MTLTexture?
init(renderer: Renderer) { init(renderer: Renderer) {