Edit mode track updates

This commit is contained in:
Emmaline Autumn 2025-02-22 16:31:53 -07:00
parent 10d462edaf
commit 2176f67413
3 changed files with 22 additions and 15 deletions

View File

@ -58,7 +58,7 @@ export function getContext() {
return ctx;
}
export function getContextItem<K extends keyof ContextMap>(
prop: string,
prop: K,
): ContextMap[K];
export function getContextItem<T>(prop: string): T;
export function getContextItem<T>(prop: string): T {

View File

@ -51,7 +51,7 @@ export class EditTrackState extends State<States> {
p.set(mousePos);
p.add(relativePoint);
});
segment.recalculateTiePoints();
segment.update();
const ends = track.findEnds();
setContextItem("showEnds", true);
@ -130,7 +130,7 @@ export class EditTrackState extends State<States> {
break;
}
}
this.ghostSegment.recalculateTiePoints();
this.ghostSegment.update();
// } else if (closestEnd) {
// this.closestEnd = closestEnd;
@ -245,6 +245,7 @@ export class EditTrackState extends State<States> {
if (translation.x !== 0 || translation.y !== 0) {
track.translate(translation);
track.recalculateAll();
}
// TODO

View File

@ -35,7 +35,7 @@ export class TrackSystem {
recalculateAll() {
for (const segment of this._segments.values()) {
segment.recalculateRailPoints();
segment.update();
segment.length = segment.calculateApproxLength();
}
}
@ -290,9 +290,7 @@ export class TrackSegment extends PathSegment {
super(p);
this.doodler = getContextItem<Doodler>("doodler");
this.id = id ?? crypto.randomUUID();
this.recalculateRailPoints();
this.recalculateTiePoints();
this.updateAABB();
this.update();
}
updateAABB() {
@ -301,7 +299,7 @@ export class TrackSegment extends PathSegment {
let minY = Infinity;
let maxY = -Infinity;
this.points.forEach((p) => {
[...this.normalPoints, ...this.antiNormalPoints].forEach((p) => {
minX = Math.min(minX, p.x);
maxX = Math.max(maxX, p.x);
minY = Math.min(minY, p.y);
@ -349,6 +347,12 @@ export class TrackSegment extends PathSegment {
this.evenPoints = this.calculateEvenlySpacedPoints(this.length / spacing);
}
update() {
this.recalculateRailPoints();
this.recalculateTiePoints();
this.updateAABB();
}
setTrack(t: TrackSystem) {
this.track = t;
}
@ -431,13 +435,15 @@ export class TrackSegment extends PathSegment {
// color: "red",
// weight: 3,
// });
this.doodler.drawRect(this.aabb.pos, this.aabb.width, this.aabb.height, {
color: "lime",
});
this.doodler.drawCircle(this.aabb.center, 2, {
color: "cyan",
});
const debug = getContextItem("debug");
if (debug.track) {
this.doodler.drawRect(this.aabb.pos, this.aabb.width, this.aabb.height, {
color: "lime",
});
this.doodler.drawCircle(this.aabb.center, 2, {
color: "cyan",
});
}
}
serialize(): SerializedTrackSegment {