mirror of
https://github.com/dscyrescotti/Memola.git
synced 2026-07-02 02:51:34 +02:00
feat: save quads by batch
This commit is contained in:
@@ -160,6 +160,7 @@ extension GraphicContext {
|
|||||||
guard currentPoint != nil, let currentStroke = currentStroke?.stroke(as: PenStroke.self) else { return }
|
guard currentPoint != nil, let currentStroke = currentStroke?.stroke(as: PenStroke.self) else { return }
|
||||||
currentStroke.finish(at: point)
|
currentStroke.finish(at: point)
|
||||||
tree.insert(currentStroke.anyStroke, in: currentStroke.strokeBox)
|
tree.insert(currentStroke.anyStroke, in: currentStroke.strokeBox)
|
||||||
|
currentStroke.saveQuads()
|
||||||
withPersistence(\.backgroundContext) { [currentStroke] context in
|
withPersistence(\.backgroundContext) { [currentStroke] context in
|
||||||
guard let stroke = currentStroke.stroke(as: PenStroke.self) else { return }
|
guard let stroke = currentStroke.stroke(as: PenStroke.self) else { return }
|
||||||
stroke.object?.bounds = stroke.bounds
|
stroke.object?.bounds = stroke.bounds
|
||||||
|
|||||||
@@ -28,6 +28,9 @@ final class PenStroke: Stroke, @unchecked Sendable {
|
|||||||
|
|
||||||
var object: StrokeObject?
|
var object: StrokeObject?
|
||||||
|
|
||||||
|
let batchSize: Int = 50
|
||||||
|
var batchIndex: Int = 0
|
||||||
|
|
||||||
init(
|
init(
|
||||||
bounds: [CGFloat],
|
bounds: [CGFloat],
|
||||||
color: [CGFloat],
|
color: [CGFloat],
|
||||||
@@ -84,16 +87,27 @@ final class PenStroke: Stroke, @unchecked Sendable {
|
|||||||
max(quad.originX.cgFloat, bounds[2]),
|
max(quad.originX.cgFloat, bounds[2]),
|
||||||
max(quad.originY.cgFloat, bounds[3])
|
max(quad.originY.cgFloat, bounds[3])
|
||||||
]
|
]
|
||||||
withPersistence(\.backgroundContext) { [object, _quad = quad] context in
|
if quads.endIndex >= batchIndex + batchSize {
|
||||||
let quad = QuadObject(\.backgroundContext)
|
saveQuads(to: batchIndex + batchSize)
|
||||||
quad.originX = _quad.originX.cgFloat
|
}
|
||||||
quad.originY = _quad.originY.cgFloat
|
}
|
||||||
quad.size = _quad.size.cgFloat
|
|
||||||
quad.rotation = _quad.rotation.cgFloat
|
func saveQuads(to endIndex: Int? = nil) {
|
||||||
quad.shape = _quad.shape
|
let endIndex = endIndex ?? quads.endIndex
|
||||||
quad.color = _quad.getColor()
|
let batch = quads[batchIndex..<endIndex]
|
||||||
quad.stroke = object
|
batchIndex = endIndex
|
||||||
object?.quads.add(quad)
|
withPersistence(\.backgroundContext) { [object, quads = batch] context in
|
||||||
|
for _quad in quads {
|
||||||
|
let quad = QuadObject(\.backgroundContext)
|
||||||
|
quad.originX = _quad.originX.cgFloat
|
||||||
|
quad.originY = _quad.originY.cgFloat
|
||||||
|
quad.size = _quad.size.cgFloat
|
||||||
|
quad.rotation = _quad.rotation.cgFloat
|
||||||
|
quad.shape = _quad.shape
|
||||||
|
quad.color = _quad.getColor()
|
||||||
|
quad.stroke = object
|
||||||
|
object?.quads.add(quad)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user