Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/tauri
Browse files Browse the repository at this point in the history
  • Loading branch information
jbcpollak committed Jun 5, 2024
2 parents dd819f7 + 87a1fab commit da18ce3
Showing 1 changed file with 49 additions and 41 deletions.
90 changes: 49 additions & 41 deletions applications/web/src/components/tools/NewLine.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,44 @@
export let pointsById: IDictionary<SketchPoint>, 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!)
}
}
// @ts-ignore todo rework points
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
point.id = point.id ?? addPointToSketch(sketchIndex, point.twoD, false)
stack.push(point)
}
function processPoint(point: PointLikeById) {
pushToStack(point)
previousPoint = 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 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)
// 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
}
}
export function click(_event: Event, projected: Point) {
Expand All @@ -57,7 +60,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))
Expand All @@ -66,19 +68,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
}
}
Expand All @@ -92,11 +91,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 = {
Expand All @@ -115,11 +113,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()
}
</script>

<svelte:window on:keydown={onKeyDown} />

0 comments on commit da18ce3

Please sign in to comment.