fix background! bugs; delete a bunch of p5 adapter code, now moved to JS

This commit is contained in:
Scott Richmond 2024-07-30 18:35:55 -04:00
parent 657ff3dedb
commit af125ffbbb

View File

@ -1106,90 +1106,14 @@ let turtle_init = #{
box turtle_commands = []
box turtle_state = turtle_init
& fn reset_turtle! {
& "Resets the turtle to its original state."
& () -> store! (turtle_states, [turtle_init])
& }
fn add_command! (command) -> {
update! (turtle_commands, append! (_, command))
let prev = unbox (turtle_state)
let curr = apply_command (prev, command)
store! (turtle_state, curr)
& let call = state/call ()
& if call then { add_call! (call); :ok } else :ok
:ok
}
& fn make_line ((x1, y1), (x2, y2)) -> (:line, x1, y1, x2, y2)
& let turtle_radius = 20
& let turtle_angle = 0.385
& let turtle_color = (255, 255, 255, 150)
& fn render_turtle! () -> {
& let state = do turtle_states > unbox > last
& if state :visible?
& then {
& let (r, g, b, a) = turtle_color
& add_call! ((:fill, r, g, b, a))
& let #{heading
& :pencolor (pen_r, pen_g, pen_b, pen_a)
& :position (x, y)
& pendown?
& ...} = state
& let origin = mult ((0, 1), turtle_radius)
& let (x1, y1) = origin
& let (x2, y2) = rotate (origin, turtle_angle)
& let (x3, y3) = rotate (origin, neg (turtle_angle))
& add_call! ((:push))
& add_call! ((:translate, x, y))
& add_call! ((:rotate, turn/rad (heading)))
& add_call! ((:noStroke))
& add_call! ((:beginShape))
& add_call! ((:vertex, x1, y1))
& add_call! ((:vertex, x2, y2))
& add_call! ((:vertex, x3, y3))
& add_call! ((:endShape))
& & there's a happy bug here: the stroke will be the same width as the pen width. Keep this for now. Consider also showing the pen colour here?
& add_call! ((:stroke, pen_r, pen_g, pen_b, pen_a))
& if pendown? then add_call! ((:line, 0, 0, x1, y1)) else nil
& add_call! ((:pop))
& :ok
& }
& else :ok
& }
& fn state/call () -> {
& let cmd = do turtle_commands > unbox > last > first
& let states = unbox (turtle_states)
& let curr = last (states)
& let prev = at (states, sub (count (states), 2))
& match cmd with {
& :forward -> if curr :pendown?
& then make_line (prev :position, curr :position)
& else nil
& :back -> if curr :pendown?
& then make_line (prev :position, curr :position)
& else nil
& :home -> if curr :pendown?
& then make_line (prev :position, curr :position)
& else nil
& :goto -> if curr :pendown?
& then make_line (prev :position, curr :position)
& else nil
& :penwidth -> (:strokeWeight, curr :penwidth)
& :pencolor -> {
& let (r, g, b, a) = curr :pencolor
& (:stroke, r, g, b, a)
& }
& :clear -> (:background, 0, 0, 0, 255)
& _ -> nil
& }
& }
fn forward! {
"Moves the turtle forward by a number of steps. Alias: fd!"
(steps as :number) -> add_command! ((:forward, steps))
@ -1251,7 +1175,7 @@ let pw! = penwidth!
fn background! {
"Sets the background color behind the turtle and path. Alias: bg!"
(color as :keyword) -> add_command! ((:background, :color))
(color as :keyword) -> add_command! ((:background, color))
(gray as :number) -> add_command! ((:background, gray, gray, gray, 255))
((r as :number, g as :number, b as :number)) -> add_command! ((:background, r, g, b, 255))
((r as :number, g as :number, b as :number, a as :number)) -> add_command! ((:background, r, g, b, a))
@ -1338,6 +1262,7 @@ fn apply_command {
(:loadstate, x, y, heading, visible?, pendown?, penwidth, r, g, b, a) -> #{:position (x, y), heading, visible?, pendown?, penwidth, :pencolor (r, g, b, a)}
(:show) -> assoc (state, :visible?, true)
(:hide) -> assoc (state, :visible?, false)
(:background, _) -> state
}
}