54

set COLS 3
Wish tag $this is stabilized
Wish $this is outlined green
Wish $this-display has a canvas with settle 0ms width 1024 height 1024
When display /disp/ has width /displayWidth/ height /displayHeight/ &\
     display /disp/ has intrinsics /displayIntrinsics/ &\
     the pose library is /poseLib/ &\
     the quad library is /quadLib/ &\
     the quad changer is /quadChange/ &\
     $this has resolved geometry /geom/ &\
     the animation toy's frame count is /N_FRAMES/ {

    fn quadChange

    set frameGeom [list width [* $geom(width) 0.5] \
                       height [* $geom(height) 0.5]]
    Claim $this-display has resolved geometry $frameGeom

    for {set i 1} {$i <= $N_FRAMES} {incr i} {
        Claim $this-frame-$i has resolved geometry $frameGeom
        When /someone/ wishes $this-frame-$i is outlined /color/ &\
             $this-frame-$i has quad /frameQuad/ {

            set dispQuad [quadChange $frameQuad "display $disp"]
            set dispPoints [lmap v [$quadLib vertices $dispQuad] {
                $poseLib project $displayIntrinsics \
                    $displayWidth $displayHeight $v
            }]
            lappend dispPoints [lindex $dispPoints 0]
            Wish to draw a line onto $disp with points $dispPoints \
                width 4 color $color
        }
    }
}

When the quad library is /quadLib/ & $this has quad /q/ {
    set displayQuad [$quadLib scale $q 50%]
    set displayQuad [$quadLib move $displayQuad up 60% right 160%]
    Claim -keep 10ms $this-display has quad $displayQuad
    Wish -keep 10ms $this-display is outlined white
}
When the quad library is /quadLib/ &\
    $this has quad /q/ &\
    $this-display has resolved geometry /geom/ &\
    the animation toy's fps is /FPS/ &\
    the animation toy's frame count is /N_FRAMES/ {

    Wish -keep 10ms $this is labelled "FPS: $FPS
Frame count: $N_FRAMES"
    for {set i 1} {$i <= $N_FRAMES} {incr i} {
        set col [expr {($i - 1) % $COLS}]
        set row [expr {($i - 1) / $COLS}]

        set frameQuad [$quadLib scale $q 30%]
        set frameQuad [$quadLib move $frameQuad \
                           right 240% down 300% \
                           right ${col}00% down ${row}00%]
        Claim $this-frame-$i has quad $frameQuad

        When -nonatomically $this-frame-$i has camera slice /slice/ {
            When -nonatomically the clock time is /t/ {
                Hold! -key anim -keep 25ms {
                    if {round($t * $FPS) % $N_FRAMES == ($i - 1)} {
                        Wish $this-frame-$i is outlined green
                        Wish $this-display displays image $slice \
                            with width $geom(width)
                    } else {
                        Wish $this-frame-$i is outlined red
                    }
                }
            }
        }
    }
}