A shape can be glass: a lens, tiles, a wave, a ripple, a funhouse mirror
A warp is a remap in space. Like a remap it draws nothing of its own and lands on what is drawn before it, so it goes between things by draw order alone: what is under the glass is bent and what is drawn after it is not. It is the colour [:warp kind], offered after remap in the tool strip and the inspector's colour row. Each kind is a field in the shape's own space, so a lens magnifies about its own middle and moves, turns and scales with its node, inside a placed symbol too, where it still bends what that symbol is drawn over. Its amount, size and phase are channels a warp colour gives the shape, as a sound has a gain, so they key and tween like any other; amount 0 is untouched for every kind. The rasteriser collects what the shape covers, reads every pixel and then writes them, so the cost is what it covers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
d2245d7720
commit
4cee461ecd
13 changed files with 417 additions and 30 deletions
141
frontend/test/arthur/domain/warp_test.cljs
Normal file
141
frontend/test/arthur/domain/warp_test.cljs
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
(ns arthur.domain.warp-test
|
||||
(:require [cljs.test :refer [deftest is testing]]
|
||||
[arthur.domain.channel :as ch]
|
||||
[arthur.domain.clip :as clip]
|
||||
[arthur.domain.node :as node]
|
||||
[arthur.domain.paint :as paint]
|
||||
[arthur.domain.palette :as pal]
|
||||
[arthur.domain.pick :as pick]
|
||||
[arthur.domain.raster :as raster]))
|
||||
|
||||
(defn- sq [x0 y0 x1 y1] [x0 y0 x1 y0 x1 y1 x0 y1])
|
||||
|
||||
(defn- drawn
|
||||
"`document`'s root at frame 0, rasterised: the raster and its ops."
|
||||
([document] (drawn document (raster/make 320 200)))
|
||||
([document r]
|
||||
(let [ops (vec ((clip/resolver document :main nil (pal/compile document) nil) 0))]
|
||||
{:ops ops :ras (raster/draw-ops! (raster/clear! r 0) ops)})))
|
||||
|
||||
(defn- at [{:keys [ras]} x y] (aget (:buf ras) (+ x (* y (:w ras)))))
|
||||
|
||||
(defn- row
|
||||
"Slots along row `y` from `x0` up to `x1`."
|
||||
[d y x0 x1]
|
||||
(mapv #(at d % y) (range x0 x1)))
|
||||
|
||||
(defn- shapes
|
||||
"Shapes in the root, drawn in the order given."
|
||||
[& specs]
|
||||
(reduce (fn [doc [i [id pts c]]]
|
||||
(-> doc
|
||||
(paint/new-shape :main id 0 pts c)
|
||||
(assoc-in [:symbols :main :nodes id :z] (str "z" i))))
|
||||
(clip/blank) (map-indexed vector specs)))
|
||||
|
||||
;; A wall in two slots, a two-pixel dot on it, and over them a 20px square of
|
||||
;; glass about (20, 20). What is drawn after the glass is a mark on top.
|
||||
(def wall [:wall (sq 0 0 40 40) 2])
|
||||
(def dot [:dot (sq 20 20 22 22) 4])
|
||||
(def lens [:lens (sq 10 10 30 30) [:warp :lens]])
|
||||
(def mark [:mark (sq 25 25 28 28) 5])
|
||||
|
||||
(deftest a-lens-magnifies-what-is-under-it-and-nothing-over-it
|
||||
(let [d (drawn (shapes wall dot lens mark))]
|
||||
(is (= [2 2 4 4 4 4 2] (row d 21 18 25))
|
||||
"the dot under the lens is twice as wide, about the lens's middle")
|
||||
(is (= [2 4 4 2] (row (drawn (shapes wall dot mark)) 21 19 23))
|
||||
"where without it the dot is two pixels")
|
||||
(is (= [2 5 5 5 2] (row d 26 24 29))
|
||||
"the mark drawn after the lens is exactly the mark: above, untouched")
|
||||
(is (= 2 (at d 5 5)) "and the wall outside the glass is as it was")
|
||||
(is (= [:wall] (pick/hit (:ops d) [12 12])) "a click goes through the glass")
|
||||
(is (= [:lens] (pick/hit (:ops d) [12 12] [:lens]))
|
||||
"unless the glass is what is selected, which is how it is dragged")))
|
||||
|
||||
(deftest a-warp-at-amount-zero-is-not-there
|
||||
;; Amount 0 is untouched for every kind, which is what lets a tween from 0
|
||||
;; bring any of them in from nothing.
|
||||
(let [plain (vec (:buf (:ras (drawn (shapes wall dot mark)))))]
|
||||
(doseq [kind (keys node/warp-defaults)]
|
||||
(let [doc (-> (shapes wall dot [:glass (sq 4 4 36 36) [:warp kind]] mark)
|
||||
(assoc-in [:symbols :main :nodes :glass :channels [:warp :amount]]
|
||||
(ch/framed 0.0)))]
|
||||
(is (= plain (vec (:buf (:ras (drawn doc))))) (str kind " at 0"))))))
|
||||
|
||||
(deftest every-warp-moves-what-is-under-it
|
||||
;; A bar near the glass's edge as well as the dot in its middle: a funhouse
|
||||
;; mirror hardly moves the middle of itself.
|
||||
(let [bar [:bar (sq 30 4 32 36) 4]
|
||||
plain (vec (:buf (:ras (drawn (shapes wall dot bar mark)))))]
|
||||
(doseq [kind (keys node/warp-defaults)]
|
||||
(is (not= plain (vec (:buf (:ras (drawn (shapes wall dot bar [:glass (sq 4 4 36 36) [:warp kind]] mark))))))
|
||||
(str kind " at its defaults moves something")))))
|
||||
|
||||
(deftest a-warps-parameters-are-channels-its-colour-gives-it
|
||||
(let [doc (shapes wall lens)
|
||||
n (get-in doc [:symbols :main :nodes :lens])
|
||||
tile (assoc-in n [:channels [:style :color]] (ch/keyed {0 [:warp :lens] 3 [:warp :tiles]} :hold))]
|
||||
(is (= (ch/framed 1.0) (get (node/channels n) [:warp :amount]))
|
||||
"a lens has an amount, as a sound has a gain, without storing one")
|
||||
(is (nil? (get (node/channels n) [:warp :size])) "and nothing it does not read")
|
||||
(is (nil? (get (node/channels (get-in doc [:symbols :main :nodes :wall])) [:warp :amount]))
|
||||
"a shape in a slot has none")
|
||||
(is (contains? (node/channels tile) [:warp :size])
|
||||
"a colour keyed from one warp to another has what both read")
|
||||
(is (empty? (node/problems (assoc-in n [:channels [:warp :amount]] (ch/keyed {0 0.0 4 2.0} :linear))))
|
||||
"and keying one is a valid node")
|
||||
(is (= #{2} (pal/slots-used (vals (:symbols doc)))) "a warp names no slot")))
|
||||
|
||||
(deftest a-keyed-amount-tweens-the-warp
|
||||
(let [doc (-> (shapes wall dot lens)
|
||||
(assoc-in [:symbols :main :nodes :lens :channels [:warp :amount]]
|
||||
(ch/keyed {0 0.0 2 2.0} :linear)))
|
||||
frame (fn [f] (let [ops ((clip/resolver doc :main nil (pal/compile doc) nil) f)]
|
||||
(raster/draw-ops! (raster/clear! (raster/make 320 200) 0) ops)))
|
||||
wide (fn [f] (count (filter #(= 4 (aget (:buf (frame f)) (+ % (* 21 320)))) (range 10 30))))]
|
||||
(is (= [2 4 6] (mapv wide [0 1 2])) "1×, 2×, 3× through the tween")))
|
||||
|
||||
(deftest a-lens-in-a-symbol-magnifies-what-is-outside-the-symbol
|
||||
;; The point of a lens is to go BETWEEN things, so a lens a symbol holds sees
|
||||
;; everything drawn under that symbol, not only what the symbol draws. And it
|
||||
;; is a field in its shape's space: placed at (20, 20) twice the size, the
|
||||
;; symbol's 10px glass covers 20px about (20, 20).
|
||||
(let [base (shapes wall dot)
|
||||
doc (-> base
|
||||
(assoc-in [:symbols :glass]
|
||||
{:id :glass :frames 1
|
||||
:nodes {:lens {:id :lens :kind :poly :z "a"
|
||||
:channels {[:geom :pts] (ch/framed (sq -5 -5 5 5))
|
||||
[:style :color] (ch/framed [:warp :lens])}}}})
|
||||
(assoc-in [:symbols :main :nodes :held]
|
||||
{:id :held :kind :instance :z "z9" :source {:symbol :glass}
|
||||
:playback {:in 0 :speed 0 :end :hold}
|
||||
:channels {[:xform :pos] (ch/framed [20 20])
|
||||
[:xform :scale] (ch/framed [2 2])}}))
|
||||
d (drawn doc)]
|
||||
(is (= [2 2 4 4 4 4 2] (row d 21 18 25)) "the root's dot, magnified by the symbol's lens")))
|
||||
|
||||
(deftest a-warp-in-a-layer-moves-holes-with-marks
|
||||
;; A symbol that knocks out is drawn into a layer of its own, and what is
|
||||
;; under its lens is only what it drew — holes included.
|
||||
(let [doc (-> (shapes wall dot
|
||||
[:hole (sq 20 18 22 20) :clear]
|
||||
lens)
|
||||
(assoc-in [:symbols :main :nodes :wall :channels [:style :color]] (ch/framed 3)))
|
||||
d (drawn doc)]
|
||||
(is (= [3 3 0 0 0 0 3] (row d 17 18 25))
|
||||
"the knocked out pixels are magnified as holes onto the background")))
|
||||
|
||||
(deftest a-warp-in-part-of-the-work-area-is-the-same-warp
|
||||
;; The editor rasterises what is on screen, which need not start at the
|
||||
;; stage's corner. The same pixels come out wherever the raster starts.
|
||||
(let [doc (shapes wall dot lens mark)
|
||||
full (drawn doc)
|
||||
part (drawn doc (assoc (raster/make 30 30) :origin [8 8]))]
|
||||
(is (= (row full 21 10 30)
|
||||
(mapv #(aget (:buf (:ras part)) (+ (- % 8) (* (- 21 8) 30))) (range 10 30))))))
|
||||
|
||||
(deftest a-warp-of-no-kind-is-loud
|
||||
(is (= 255 (at (drawn (shapes wall [:glass (sq 4 4 36 36) [:warp :teleport]])) 20 20))
|
||||
"magenta, as a slot the palette lacks is"))
|
||||
Loading…
Add table
Add a link
Reference in a new issue