From 9d4e114ef537212f634e1549ef46f8ee127d49c6 Mon Sep 17 00:00:00 2001 From: leomcelroy Date: Fri, 20 Oct 2023 12:30:05 -0400 Subject: [PATCH] added jump, save cue --- astro/src/components/Toolbar.tsx | 4 ++-- astro/src/lib/drawingToolkit/turtle.ts | 29 ++++++++++++++++++-------- astro/src/lib/init.js | 6 ++++++ astro/src/lib/saveFile.ts | 4 ++-- astro/src/lib/state.ts | 6 ++++-- docs/TOOLKIT.md | 23 +++++++++++++++++++- 6 files changed, 56 insertions(+), 16 deletions(-) diff --git a/astro/src/components/Toolbar.tsx b/astro/src/components/Toolbar.tsx index 90910fa28..5a1156ca9 100644 --- a/astro/src/components/Toolbar.tsx +++ b/astro/src/components/Toolbar.tsx @@ -22,7 +22,7 @@ import GitHubIcon from '../ui/GitHubIcon.tsx' import { saveFile } from "../lib/saveFile.ts"; export default function Toolbar() { - const { connected } = getStore() + const { connected, needsSaving } = getStore() const [hidden, setHidden] = useState(true) @@ -39,7 +39,7 @@ export default function Toolbar() {
saveFile(getCode())}> - save (ctrl/cmd+s) + { needsSaving ? "save* (ctrl/cmd+s)" : "save (ctrl/cmd+s)" }
diff --git a/astro/src/lib/drawingToolkit/turtle.ts b/astro/src/lib/drawingToolkit/turtle.ts index b6751c0be..cd933651c 100644 --- a/astro/src/lib/drawingToolkit/turtle.ts +++ b/astro/src/lib/drawingToolkit/turtle.ts @@ -58,6 +58,13 @@ export class Turtle { return this } + jump(pt: Point) { + this.up(); + this.goTo(pt); + this.down(); + return this; + } + forward(distance: number) { const last = this.position const a = (this.angle / 180) * Math.PI @@ -217,9 +224,9 @@ export class Turtle { return getAngle(this.path, t) } - getNormal(t: number) { - return getNormal(this.path, t) - } + // getNormal(t: number) { + // return getNormal(this.path, t) + // } trim(t0: number, t1: number) { trimPolylines(this.path, t0, t1) @@ -466,15 +473,19 @@ function extrema(pts: Point[]) { function tValuesForPoints(polylines: Polyline[]) { let totalLength = 0 let lengths = [] - let tValues = [0] + let tValues = [] + let segmentLength = 0; for (let i = 0; i < polylines.length; i++) { let polyline = polylines[i] - for (let j = 1; j < polyline.length; j++) { - let dx = polyline[j][0] - polyline[j - 1][0] - let dy = polyline[j][1] - polyline[j - 1][1] - let segmentLength = Math.sqrt(dx * dx + dy * dy) - totalLength += segmentLength + for (let j = 0; j < polyline.length; j++) { + if (j > 0) { + let dx = polyline[j][0] - polyline[j - 1][0] + let dy = polyline[j][1] - polyline[j - 1][1] + segmentLength = Math.sqrt(dx * dx + dy * dy) + totalLength += segmentLength + } + lengths.push(segmentLength) } } diff --git a/astro/src/lib/init.js b/astro/src/lib/init.js index e5e0bee65..752457571 100644 --- a/astro/src/lib/init.js +++ b/astro/src/lib/init.js @@ -6,6 +6,8 @@ import { addMachineControl } from './events/addMachineControl.js' import { addLoadBackup } from './events/addLoadBackup.js' import { addSrcURLParam } from './events/addSrcURLParam.js' import { saveFile } from "./saveFile.ts"; +import { useOnEditorChange } from "./events.ts"; + export function init() { console.log('init') @@ -39,6 +41,10 @@ export function init() { } }) + useOnEditorChange(() => { + patchStore({ needsSaving: true }); + }) + // get settings from localStorage const theme = localStorage.getItem('colorTheme') ?? 'light' diff --git a/astro/src/lib/saveFile.ts b/astro/src/lib/saveFile.ts index 473a04e14..fc8a02158 100644 --- a/astro/src/lib/saveFile.ts +++ b/astro/src/lib/saveFile.ts @@ -22,8 +22,6 @@ export async function saveFile(content, { filename, fileHandle } = {} ) { suggestedName: filename ?? "anon", }); - patchStore({ fileHandle }); - // Create a FileSystemWritableFileStream to write to. const writableStream = await fileHandle.createWritable(); @@ -35,6 +33,8 @@ export async function saveFile(content, { filename, fileHandle } = {} ) { // Here, you can only print the file name, not the full path console.log(`File saved: ${fileHandle.name}`); + + patchStore({ fileHandle, needsSaving: false }); } catch (err) { console.error("File save failed", err); } diff --git a/astro/src/lib/state.ts b/astro/src/lib/state.ts index 099c4c0fa..992437f86 100644 --- a/astro/src/lib/state.ts +++ b/astro/src/lib/state.ts @@ -38,7 +38,8 @@ export type GlobalState = { view: EditorView | null theme: 'light' | 'dark' vimMode: boolean, - fileHandle: any + fileHandle: any, + needsSaving: boolean } // setting/initializing state @@ -58,7 +59,8 @@ const newState: Omit = { view: null, theme: 'light', vimMode: false, - fileHandle: null + fileHandle: null, + needsSaving: false } export const makeNewState = (): GlobalState => { diff --git a/docs/TOOLKIT.md b/docs/TOOLKIT.md index aeb08509a..d86f037f3 100644 --- a/docs/TOOLKIT.md +++ b/docs/TOOLKIT.md @@ -18,6 +18,9 @@ t.goTo([ x: number, y: number ]) t.forward(distance: number) t.arc(angle: number, radius: number) +// to move to pt without drawing and end up in drawing position +t.jump([ x: number, y: number ]) + // change angle t.setAngle(theta: number) t.right(theta: number) @@ -66,14 +69,32 @@ t.height t.join(anotherTurtle) // to apply a function to all pts in a turtle -// this will replace the old point values with the new ones +// fn takes (pt, tValue) => { ... } +// return [ x, y ] to replace the old point value with the new one +// return "BREAK" to split path at that point +// return "REMOVE" to filter out that point t.iteratePath(fn) t.resample(resolution) +// takes value 0 - 1 and returns point that far along paths t.interpolate(tValue) + +// takes value 0 - 1 and returns angle that far along paths +t.getAngle(tValue) ``` + + To render a turtle use `drawTurtles`, it takes a list of turtles to draw. ```js