forked from CADmium-Co/CADmium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the history features to their own folder
Signed-off-by: Dimitris Zervas <[email protected]>
- Loading branch information
Showing
10 changed files
with
227 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
applications/web/src/routes/(CADmium)/NewArcTool.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
<script lang="ts"> | ||
import { snapPoints, sketchTool, previewGeometry, currentlyMousedOver } from "shared/stores" | ||
import { addCircleBetweenPoints, addPointToSketch } from "shared/projectUtils" | ||
import { Vector3, type Vector2Like, type Vector3Like } from "three" | ||
import type { PointLikeById, Point2D, PointsLikeById, ProjectToPlane } from "shared/types" | ||
// prettier-ignore | ||
const log = (function () { const context = "[NewArcTool.svelte]"; const color="gray"; return Function.prototype.bind.call(console.log, console, `%c${context}`, `font-weight:bold;color:${color};`)})() | ||
export let pointsById: PointsLikeById | ||
export let sketchIndex: string | ||
export let active: boolean | ||
export let projectToPlane: ProjectToPlane | ||
// log("[props]", "pointsById:", pointsById, "sketchIndex:", sketchIndex, "active:", active) | ||
let centerPoint: PointLikeById | null | ||
$: if ($sketchTool !== "arc") centerPoint = null | ||
// $: centerPoint, log("[centerPoint]", centerPoint) | ||
function processPoint(point: PointLikeById) { | ||
if (!centerPoint) { | ||
// if there is no center point, set one | ||
if (point.id) { | ||
// nothing to do, the point exists! | ||
// log('nothing to do the point exists!') | ||
} else { | ||
// again, don't actually DO anything yet to the sketch | ||
point.id = null | ||
} | ||
centerPoint = point | ||
} else { | ||
// there WAS an center point, so we should create a circle! | ||
// if the center point doesn't exist, then we should create a point | ||
if (centerPoint.id === null) centerPoint.id = addPointToSketch(sketchIndex, centerPoint.twoD!, false) | ||
if (point.id && centerPoint.id) { | ||
// if the point exists, then we should create a circle between the two existing points | ||
addCircleBetweenPoints(sketchIndex, centerPoint.id, point.id) | ||
} else { | ||
// if the point doesn't exist, then we should create a point and a circle | ||
point.id = addPointToSketch(sketchIndex, point.twoD!, true) | ||
} | ||
if (point.id && centerPoint.id) addCircleBetweenPoints(sketchIndex, centerPoint.id, point.id) | ||
centerPoint = null | ||
} | ||
} | ||
export function click(_event: Event, projected: { twoD: Vector2Like; threeD: Vector3Like }) { | ||
if ($snapPoints.length > 0) processPoint($snapPoints[0]) | ||
else { | ||
let pt: PointLikeById = { twoD: projected.twoD, threeD: projected.threeD, id: null } | ||
processPoint(pt) | ||
} | ||
} | ||
// $: $snapPoints, log("[$snapPoints]", $snapPoints) | ||
export function mouseMove(_event: Event, projected: { x: number; y: number }) { | ||
// search through the existing points to see if we're close to one | ||
// if we are, then we should snap to it | ||
// TODO: in the future, we should also snap to the midpoints of lines | ||
// and to the perimeters of circles and so on | ||
// so these snap points do not necessarily correspond to actual points in the sketch | ||
let snappedTo = null | ||
for (const geom of $currentlyMousedOver) { | ||
// log("[currentlyMousedOver geom]", geom) | ||
if (geom.type === "point3D") { | ||
const twoD = projectToPlane(new Vector3(geom.x, geom.y, geom.z)) | ||
// log("[projectToPlane twoD]", twoD) | ||
const point = { | ||
twoD: { x: twoD.x, y: twoD.y }, | ||
threeD: { x: geom.x, y: geom.y, z: geom.z }, | ||
id: null | ||
} | ||
snappedTo = point | ||
} | ||
if (geom.type === "point") { | ||
// log("[currentlyMousedOver geom is type point]", geom) | ||
const point = pointsById[geom.id] | ||
// oops! point.twoD etc does not exist here, we have: | ||
// const example = { | ||
// type: "point", | ||
// id: "1" | ||
// } | ||
function querySnapPoint(id: string | null) { | ||
const points = $snapPoints.filter((point) => id && point.id === id) | ||
return points.length > 0 ? points[0] : false | ||
} | ||
// see if we can retrieve it? unlikely | ||
// log("[querySnapPoint found point:]", querySnapPoint(point?.id!)) | ||
// have not seen a successful query yet! sort it out with an if: | ||
if (point.twoD && point.threeD && geom.id) snappedTo = { twoD: point.twoD, threeD: point.threeD, id: geom.id } | ||
break // If there is a 2D point, prefer to use it rather than the 3D point | ||
} | ||
} | ||
// if (snappedTo) log("[snappedTo]", snappedTo) | ||
// only reset $snapPoints if something has changed | ||
if (snappedTo) { | ||
// @ts-ignore todo rework snapping | ||
$snapPoints = [snappedTo] // todo all these different point representations need work! | ||
} else { | ||
if ($snapPoints.length > 0) { | ||
$snapPoints = [] | ||
} | ||
} | ||
if (centerPoint) { | ||
function calcDeltas(a: Vector2Like | Point2D | { x: number; y: number }, b: Vector2Like | undefined) { | ||
const dx = a.x - b?.x! | ||
const dy = a.y - b?.y! | ||
return Math.hypot(dx, dy) | ||
} | ||
const radius = snappedTo ? calcDeltas(snappedTo.twoD, centerPoint.twoD) : calcDeltas(projected, centerPoint.twoD) | ||
previewGeometry.set([ | ||
{ | ||
type: "circle", | ||
center: centerPoint, | ||
radius, | ||
uuid: `circle-${centerPoint.twoD?.x}-${centerPoint.twoD?.y}-${radius}` | ||
}, | ||
{ | ||
type: "point", | ||
x: centerPoint.twoD?.x, | ||
y: centerPoint.twoD?.y, | ||
uuid: `point-${centerPoint.twoD?.x}-${centerPoint.twoD?.y}` | ||
} | ||
]) | ||
} else { | ||
previewGeometry.set([]) | ||
} | ||
} | ||
export function onKeyDown(event: KeyboardEvent) { | ||
if (!active) return | ||
if (event.key === "Escape") { | ||
previewGeometry.set([]) | ||
centerPoint = null | ||
$sketchTool = "select" | ||
} | ||
} | ||
</script> | ||
|
||
<svelte:window on:keydown={onKeyDown} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
applications/web/src/routes/(CADmium)/features/Feature.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<script lang="ts"> | ||
</script> | ||
|
||
<div class="feature"> | ||
<slot></slot> | ||
</div> |
20 changes: 20 additions & 0 deletions
20
applications/web/src/routes/(CADmium)/features/FeatureInstance.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<script lang="ts"> | ||
import * as AllFeatures from "./"; | ||
import type { HistoryStep } from "shared/types" | ||
// prettier-ignore | ||
const log = (function () { const context = "[FeatureInstance.svelte]"; const color="yellow"; return Function.prototype.bind.call(console.log, console, `%c${context}`, `font-weight:bold;color:${color};`)})() | ||
export let feature: HistoryStep, featureIdx: number; | ||
const FeatureComponent: ConstructorOfATypedSvelteComponent = AllFeatures[feature.data.type]; | ||
log("FeatureComponent:", FeatureComponent, "feature:", feature, "featureIdx:", featureIdx, "args:"); | ||
</script> | ||
|
||
{#if FeatureComponent} | ||
<!-- We're expanding the whole `feature` which will result in `<feature> was created with unknown prop '<x>'` --> | ||
<svelte:component this={FeatureComponent} {...feature} {featureIdx} /> | ||
{:else} | ||
<div>TODO: {feature.name} ({feature.data.type})</div> | ||
{/if} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.