From c45b2b42ef111639a0030f8d101a890197d1e7f8 Mon Sep 17 00:00:00 2001 From: av8ta Date: Sun, 2 Jun 2024 13:20:01 +0800 Subject: [PATCH 1/4] web: make my grug brain less confused https://grugbrain.dev/ --- .../web/src/components/tools/NewLine.svelte | 86 ++++++++++--------- 1 file changed, 44 insertions(+), 42 deletions(-) diff --git a/applications/web/src/components/tools/NewLine.svelte b/applications/web/src/components/tools/NewLine.svelte index eaad6628..f4d2f49f 100644 --- a/applications/web/src/components/tools/NewLine.svelte +++ b/applications/web/src/components/tools/NewLine.svelte @@ -8,46 +8,43 @@ export let pointsById: IDictionary, sketchIndex: string, active: boolean, projectToPlane: ProjectToPlane - // $: pointsById, log("[props]", pointsById, sketchIndex, active, projectToPlane) + let previousPoint: PointLikeById | null = null - let previousPoint: PointLikeById | null + let stack: PointLikeById[] = [] - $: if ($sketchTool !== "line") previousPoint = null + $: if ($sketchTool !== "line") clearStack() - function processPoint(point: PointLikeById | null) { - if (!previousPoint && point) { - // if there is no anchor point, set one - if (point.id) { - // nothing to do, the point exists! - log("[processPoint] nothing to do the point exists!") - } else { - log("[processPoint] oh cool, creating point!") - point.id = null - } - } else { - // there WAS an anchor point, so we should create a line - - // if the center point doesn't exist, then we should create a point - if (previousPoint?.id === null) previousPoint.id = addPointToSketch(sketchIndex, previousPoint?.twoD!, false) - - if (point?.id) { - // if the point exists, then we should create a line - if (previousPoint?.id && point.id) addLineToSketch(sketchIndex, +previousPoint.id, +point.id) - previousPoint = null - return - } else { - // if the point doesn't exist, then we should create a point and a line - point!.id = addPointToSketch(sketchIndex, point!.twoD!, false) - addLineToSketch(sketchIndex, +previousPoint!.id!, +point!.id!) - } + function pushToStack(point: PointLikeById) { + if (!point) return + if (!point.id) point.id = addPointToSketch(sketchIndex, point.twoD, false) + stack.push(point) + } + + function processPoint(point: PointLikeById) { + pushToStack(point) + + switch (stack.length) { + case 0: // nothing to do, the stack is empty + break + case 1: // can't create a line with only one point! + break + default: + const previousPoint = stack[stack.length - 2] + addLineToSketch(sketchIndex, +previousPoint.id!, +point!.id!) + popFromStack() + popFromStack() + // leave the last point on the stack in case we want to create another line from that point + pushToStack(point) + break } - // @ts-ignore todo rework points previousPoint = point } export function click(_event: Event, projected: Point) { - if ($snapPoints.length > 0) processPoint($snapPoints[0]) - else processPoint({twoD: projected.twoD, threeD: projected.threeD, id: null}) + if ($snapPoints.length > 0) { + log("[click] [snapPoints]", $snapPoints) + processPoint($snapPoints[0]) + } else processPoint({twoD: projected.twoD, threeD: projected.threeD, id: null}) } export function mouseMove(_event: Event, projected: {x: number; y: number}) { @@ -57,7 +54,6 @@ let snappedTo: PointLikeById | null = null for (const geom of $currentlyMousedOver) { - // log("[geom of $currentlyMousedOver]", geom) if (geom.type === "point3D") { if (geom.x && geom.y && geom.z) { const twoD = projectToPlane(new Vector3(geom.x, geom.y, geom.z)) @@ -66,19 +62,16 @@ threeD: {x: geom.x, y: geom.y, z: geom.z}, id: null, } satisfies PointLikeById - // log("[point:PointLikeById]", point) snappedTo = point } } if (geom.type === "point") { const point = pointsById[geom.id] - // log("[pointsById]", pointsById) snappedTo = { twoD: point.twoD, threeD: point.threeD, id: geom.id, } satisfies PointLikeById - // log("[snappedTo]", snappedTo) break // If there is a 2D point, prefer to use it rather than the 3D point } } @@ -92,11 +85,10 @@ if (snappedTo) end = snappedTo - // prettier-ignore const previewGeoms = [ - { type: "line", start: previousPoint, end: end, uuid: `line-${end.twoD!.x}-${end.twoD!.y}` }, - { type: "point", x: end.twoD!.x, y: end.twoD!.y, uuid: `point-${end.twoD!.x}-${end.twoD!.y}` } - ] satisfies PreviewGeometry[] + {type: "line", start: previousPoint, end: end, uuid: `line-${end.twoD!.x}-${end.twoD!.y}`}, + {type: "point", x: end.twoD!.x, y: end.twoD!.y, uuid: `point-${end.twoD!.x}-${end.twoD!.y}`}, + ] satisfies PreviewGeometry[] if (previousPoint.id === null) { const p = { @@ -115,11 +107,21 @@ export function onKeyDown(event: KeyboardEvent) { if (!active) return if (event.key === "Escape") { - previewGeometry.set([]) - previousPoint = null + clearStack() $sketchTool = "select" } } + + function clearStack() { + previousPoint = null + previewGeometry.set([]) + snapPoints.set([]) + stack = [] + } + + function popFromStack(): PointLikeById | undefined { + return stack.pop() + } From 7a80bb585805a58155cc2fdb666d312101d743ee Mon Sep 17 00:00:00 2001 From: av8ta Date: Mon, 3 Jun 2024 11:48:57 +0800 Subject: [PATCH 2/4] make the code more beautiful & then uglify it with an explanatory comment --- .../web/src/components/tools/NewLine.svelte | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/applications/web/src/components/tools/NewLine.svelte b/applications/web/src/components/tools/NewLine.svelte index f4d2f49f..5d51a428 100644 --- a/applications/web/src/components/tools/NewLine.svelte +++ b/applications/web/src/components/tools/NewLine.svelte @@ -15,8 +15,13 @@ $: if ($sketchTool !== "line") clearStack() function pushToStack(point: PointLikeById) { + // point should have the following properties: + // - twoD: an object with x and y properties representing the point in 2D space + // - threeD: an object with x, y, and z properties representing the point in 3D space + // - id: a string representing the id of the point in the sketch + // If the id is nullish we call addPointToSketch to create a new point in the sketch. if (!point) return - if (!point.id) point.id = addPointToSketch(sketchIndex, point.twoD, false) + point.id = point.id ?? addPointToSketch(sketchIndex, point.twoD, false) stack.push(point) } @@ -29,11 +34,11 @@ case 1: // can't create a line with only one point! break default: - const previousPoint = stack[stack.length - 2] - addLineToSketch(sketchIndex, +previousPoint.id!, +point!.id!) - popFromStack() - popFromStack() - // leave the last point on the stack in case we want to create another line from that point + const endPoint = popFromStack() + const startPoint = popFromStack() + addLineToSketch(sketchIndex, +startPoint.id, +endPoint.id) + + // leave the current point on the stack in case we want to create another line from here pushToStack(point) break } From a67b516593ce072ff010f70ceccaf568a1abaaa2 Mon Sep 17 00:00:00 2001 From: av8ta Date: Mon, 3 Jun 2024 16:15:05 +0800 Subject: [PATCH 3/4] web: reset line tool when closing polyline --- applications/web/src/components/tools/NewLine.svelte | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/applications/web/src/components/tools/NewLine.svelte b/applications/web/src/components/tools/NewLine.svelte index 5d51a428..37abceed 100644 --- a/applications/web/src/components/tools/NewLine.svelte +++ b/applications/web/src/components/tools/NewLine.svelte @@ -27,6 +27,7 @@ function processPoint(point: PointLikeById) { pushToStack(point) + previousPoint = point switch (stack.length) { case 0: // nothing to do, the stack is empty @@ -40,9 +41,11 @@ // leave the current point on the stack in case we want to create another line from here pushToStack(point) + // unless it's an earlier point, which means we're finished making lines, so we clear the stack + const isEarlierPoint = !!pointsById[point.id!] + if (isEarlierPoint) clearStack() break } - previousPoint = point } export function click(_event: Event, projected: Point) { From 76f4f0b549489e84ff44700c45e3d120f60b243e Mon Sep 17 00:00:00 2001 From: av8ta Date: Mon, 3 Jun 2024 16:26:54 +0800 Subject: [PATCH 4/4] remove unnecessary logging --- applications/web/src/components/tools/NewLine.svelte | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/applications/web/src/components/tools/NewLine.svelte b/applications/web/src/components/tools/NewLine.svelte index 37abceed..fd809ed2 100644 --- a/applications/web/src/components/tools/NewLine.svelte +++ b/applications/web/src/components/tools/NewLine.svelte @@ -49,10 +49,8 @@ } export function click(_event: Event, projected: Point) { - if ($snapPoints.length > 0) { - log("[click] [snapPoints]", $snapPoints) - processPoint($snapPoints[0]) - } else processPoint({twoD: projected.twoD, threeD: projected.threeD, id: null}) + if ($snapPoints.length > 0) processPoint($snapPoints[0]) + else processPoint({twoD: projected.twoD, threeD: projected.threeD, id: null}) } export function mouseMove(_event: Event, projected: {x: number; y: number}) {