diff --git a/frontend/src/arthur/domain/clip.cljs b/frontend/src/arthur/domain/clip.cljs index 96eaa1f..a92106d 100644 --- a/frontend/src/arthur/domain/clip.cljs +++ b/frontend/src/arthur/domain/clip.cljs @@ -303,7 +303,10 @@ (+ (* (aget m 1) x) (* (aget m 3) y) (aget m 5))]) scale (node/mean-scale m) n (:node op) - op (assoc op :node (if (vector? n) (into path n) (conj path n)))] + op (cond-> (assoc op :node (if (vector? n) (into path n) (conj path n))) + ;; A warp's field is in its shape's space, which is now one + ;; matrix further down. + (:warp op) (assoc :m (node/mul! (node/mat) m (:m op))))] (case (:kind op) :poly (let [out (js/Float64Array. (.-length (:pts op)))] (dotimes [i (:n op)] diff --git a/frontend/src/arthur/domain/node.cljs b/frontend/src/arthur/domain/node.cljs index cc7b305..24838da 100644 --- a/frontend/src/arthur/domain/node.cljs +++ b/frontend/src/arthur/domain/node.cljs @@ -76,13 +76,13 @@ ;; A sound is not in the picture: no transform, no visibility. After ;; Effects' audio-only layer has no Transform group for the same reason. :audio #{[:audio :gain] [:audio :pan] [:audio :rate]} - :poly (into base [[:geom :pts] [:style :color]]) + :poly (into base [[:geom :pts] [:style :color] [:warp :amount] [:warp :size] [:warp :phase]]) ;; A disc's radius is framed in practice — iris size is a knob, not a ;; performance — but it is a channel like any other so it can be keyed. - :disc (into base [[:geom :radius] [:style :color]]) + :disc (into base [[:geom :radius] [:style :color] [:warp :amount] [:warp :size] [:warp :phase]]) ;; :size, not a radius: the pupil is a SQUARE, an exactly size x size ;; block. See raster/fill-rect!. - :rect (into base [[:geom :size] [:style :color]])})) + :rect (into base [[:geom :size] [:style :color] [:warp :amount] [:warp :size] [:warp :phase]])})) (def defaults "The identity transform, as channels. A node's channel map is merged over this, @@ -109,10 +109,21 @@ [:audio :pan] (ch/framed 0.0) [:audio :rate] (ch/framed 1.0)}) +(def warp-defaults + "The channels a shape coloured `[:warp kind]` has, by kind, at values that show + the warp plainly. A lens has only an amount; a wave also has a wavelength and + where along it the wave is. See `warp/field` for what each one means." + {:lens {[:warp :amount] (ch/framed 1.0)} + :tiles {[:warp :amount] (ch/framed 2.0) [:warp :size] (ch/framed 8.0)} + :wave {[:warp :amount] (ch/framed 2.0) [:warp :size] (ch/framed 16.0) [:warp :phase] (ch/framed 0.0)} + :ripple {[:warp :amount] (ch/framed 2.0) [:warp :size] (ch/framed 8.0) [:warp :phase] (ch/framed 0.0)} + :mirror {[:warp :amount] (ch/framed 0.3) [:warp :size] (ch/framed 64.0) [:warp :phase] (ch/framed 0.0)}}) + (def ^:private channel-names {[:xform :pos] "pos" [:xform :rot] "rot°" [:xform :scale] "scale" [:xform :skew] "skew" [:xform :pivot] "pivot" [:geom :pts] "shape" - [:style :color] "color" [:vis] "vis" [:palette] "palette"}) + [:style :color] "color" [:vis] "vis" [:palette] "palette" + [:warp :amount] "warp" [:warp :size] "size" [:warp :phase] "phase"}) (defn channel-name "What to call channel `path` on screen: the same short word in the inspector @@ -120,8 +131,21 @@ [path] (or (channel-names path) (str/join " " (map name path)))) -(defn defaults-of [n] - (if (= :audio (:kind n)) audio-defaults defaults)) +(defn defaults-of + "The channels node `n` has before it stores any: its kind's, and a warp's for + each warp its colour names on any key, as a sound has a gain. Read every + frame for every node, so a colour that names no vector is answered without + walking its keys." + [n] + (let [c (get-in n [:channels [:style :color]])] + (cond + (= :audio (:kind n)) audio-defaults + (not (or (:keys c) (vector? (:value c)))) defaults + :else (reduce (fn [d v] + (if (and (vector? v) (= :warp (first v))) + (merge d (warp-defaults (second v))) + d)) + defaults (cons (:value c) (vals (:keys c))))))) (defn channels "The node's channels with its kind's defaults filled in." diff --git a/frontend/src/arthur/domain/palette.cljs b/frontend/src/arthur/domain/palette.cljs index d213375..6ae16f7 100644 --- a/frontend/src/arthur/domain/palette.cljs +++ b/frontend/src/arthur/domain/palette.cljs @@ -195,14 +195,14 @@ (defn- colour-slots "The slots a colour channel names at any key: a slot, a legacy tone, or both - sides of a remap's map. A knockout names none." + sides of a remap's map. A knockout and a warp name none." [ch] (let [values (if (:keys ch) (vals (:keys ch)) [(:value ch)])] (into #{} (comp (mapcat (fn [v] (cond (and (vector? v) (= :remap (first v))) (mapcat identity (second v)) - (= :clear v) [] + (or (= :clear v) (and (vector? v) (= :warp (first v)))) [] :else [v]))) (map slot) (remove #{255})) diff --git a/frontend/src/arthur/domain/pick.cljs b/frontend/src/arthur/domain/pick.cljs index 9e35423..347b50d 100644 --- a/frontend/src/arthur/domain/pick.cljs +++ b/frontend/src/arthur/domain/pick.cljs @@ -104,7 +104,8 @@ nil. A knockout is never hit: it is a hole, and a click in it is a click on whatever shows through — so it hides the ops beneath it in its own symbol, of the colour it clears. A remap is the same kind of thing the other way up: it is - light on what is under it, so a click goes through it to what it lights. + light on what is under it, so a click goes through it to what it lights, and + a warp is glass a click goes through in the same way. EXCEPT WHAT IS ALREADY SELECTED, which `selected` is the row path of. Both of those rules are about reaching PAST a shape, and neither has anything to say @@ -123,7 +124,7 @@ (cond (not (on? op x y)) holes (and selected (prefix? selected (path-of op))) (reduced {:op op}) - (:lut op) holes + (or (:lut op) (:warp op)) holes (:knock op) (conj holes [(pop (path-of op)) (:knock op)]) (some (fn [[in k]] (and (prefix? in (path-of op)) (or (neg? k) (== k (:color op))))) diff --git a/frontend/src/arthur/domain/raster.cljs b/frontend/src/arthur/domain/raster.cljs index 38cd5ba..288e249 100644 --- a/frontend/src/arthur/domain/raster.cljs +++ b/frontend/src/arthur/domain/raster.cljs @@ -19,7 +19,8 @@ A raster may have an `:origin`, the stage point its first pixel is, and ops stay in stage pixels either way: the editor rasterises whatever part of the - work area is on screen, which need not be the stage.") + work area is on screen, which need not be the stage." + (:require [arthur.domain.warp :as warp])) (defn make [w h] {:w w :h h :buf (js/Uint8Array. (* w h))}) @@ -39,7 +40,7 @@ ;; only where it is covered. The main raster has no `:cov` and is always ;; covered. ;; -;; THE INK is how a shape marks the pixels it covers, and there are three, as +;; THE INK is how a shape marks the pixels it covers, and there are four, as ;; Deluxe Paint and Animator Pro had inks: ;; ;; nil an ordinary fill: write the shape's index @@ -47,6 +48,10 @@ ;; that index only ;; a Uint8Array a REMAP, index -> index: what is already there is drawn ;; in another slot. A flashlight is a circle of this. +;; an Int32Array a WARP: what is already there is drawn from somewhere +;; else. The fill only collects the pixels it covers here, +;; counted in its first entry; `warp!` draws them, once it +;; has read every one. (defn- plot! [^js buf cov o index ink] (cond @@ -54,6 +59,9 @@ (when cov (aset cov o 1))) (number? ink) (when (and cov (or (neg? ink) (== (aget buf o) ink))) (aset cov o 0)) + (instance? js/Int32Array ink) (let [n (inc (aget ink 0))] + (aset ink n o) + (aset ink 0 n)) :else (aset buf o (aget ink (aget buf o))))) (defn- shows? @@ -304,6 +312,52 @@ (swap! layers assoc depth l) (assoc l :origin origin))))) +;; The pixels a warp covers, and what each will show. Reused from op to op, as +;; the layers are, and grown only for a bigger raster: a warp costs what it +;; covers and allocates nothing. +(defonce ^:private scratch (volatile! {:covered (js/Int32Array. 1) :shown (js/Int32Array. 0)})) +(defonce ^:private at (js/Float64Array. 2)) + +(defn- covered-for + "The scratch a warp over raster `r` records its pixels into, emptied." + [{:keys [w h]}] + (when (< (.-length (:shown @scratch)) (* w h)) + (vreset! scratch {:covered (js/Int32Array. (inc (* w h))) :shown (js/Int32Array. (* w h))})) + (doto (:covered @scratch) (aset 0 0))) + +(defn- warp! + "Draw each pixel the fill before this covered as what `field` says it shows, + read from where it shows it. + + EVERY PIXEL IS READ BEFORE ANY IS WRITTEN. A pixel a lens moves is one some + other pixel of the lens reads, so writing as it goes would magnify what it + had already magnified. Nothing outside what is covered is written, so a read + outside it sees the buffer as the warp found it. + + A point off the raster reads its nearest edge. In a layer what is shown + includes whether anything is there: a warp moves a hole as it moves a mark." + [{:keys [w h ^js buf ^js cov origin] :as r} field] + (let [[ox oy] (or origin [0 0]) + {:keys [^js covered ^js shown]} @scratch + n (aget covered 0) + layer? (some? cov)] + ;; `js-mod` and a division rather than `rem` and `quot`, which are calls: + ;; this is once per covered pixel. + (dotimes [i n] + (let [o (aget covered (inc i)) + ix (js-mod o w)] + (field at (+ ox ix 0.5) (+ oy (/ (- o ix) w) 0.5)) + (let [x (max 0 (min (dec w) (js/Math.floor (- (aget at 0) ox)))) + y (max 0 (min (dec h) (js/Math.floor (- (aget at 1) oy)))) + s (+ (* y w) x)] + (aset shown i (if layer? (+ (aget buf s) (* 256 (aget cov s))) (aget buf s)))))) + (dotimes [i n] + (let [o (aget covered (inc i)) + v (aget shown i)] + (aset buf o (bit-and v 255)) + (when layer? (aset cov o (bit-shift-right v 8))))) + r)) + (defn draw-ops! "Paint a list of draw ops, in the order given, into the raster. Stage 7. @@ -314,12 +368,16 @@ here indistinguishable from each other, which is the point. `:begin` and `:end` bracket the ops of a symbol drawn into a layer of its own; - `:knock` on an op makes it a knockout and `:lut` a remap. See `plot!`." + `:knock` on an op makes it a knockout, `:lut` a remap and `:warp` a warp. See + `plot!`. A warp with no field — a kind there is none of — is drawn MAGENTA, + as a colour the palette lacks is: loud, and not fatal." [r ops] (reduce (fn [stack {:keys [kind pts n color stencil cx cy size] :as op}] - (let [top (peek stack) - ink (or (:knock op) (:lut op))] + (let [top (peek stack) + field (when (:warp op) (warp/field op)) + color (if (and (:warp op) (nil? field)) 255 color) + ink (if field (covered-for top) (or (:knock op) (:lut op)))] (case kind :begin (let [l (layer-at (count stack) r)] (.fill (:cov l) 0) @@ -327,10 +385,13 @@ :end (if (< 1 (count stack)) (let [s (pop stack)] (composite! (peek s) top) s) stack) - :poly (do (fill-poly-buf! top pts n color ink) stack) - :disc (do (fill-disc! top cx cy (:r op) color stencil ink) stack) - :rect (do (fill-rect! top cx cy size color stencil ink) stack) - :mask (do (fill-mask! top (:mask op) color ink) stack) - (throw (ex-info "draw op kind is not rasterisable" {:op (dissoc op :pts)}))))) + (do (case kind + :poly (fill-poly-buf! top pts n color ink) + :disc (fill-disc! top cx cy (:r op) color stencil ink) + :rect (fill-rect! top cx cy size color stencil ink) + :mask (fill-mask! top (:mask op) color ink) + (throw (ex-info "draw op kind is not rasterisable" {:op (dissoc op :pts)}))) + (when field (warp! top field)) + stack)))) [r] ops) r) diff --git a/frontend/src/arthur/domain/symbol.cljs b/frontend/src/arthur/domain/symbol.cljs index 17e75c9..5570f36 100644 --- a/frontend/src/arthur/domain/symbol.cljs +++ b/frontend/src/arthur/domain/symbol.cljs @@ -294,6 +294,13 @@ [c] (and (vector? c) (= :remap (first c)))) +(defn warp? + "Is colour `c` a WARP: `[:warp kind]`? Like a remap it draws nothing of its + own; what is already under it is drawn from somewhere else — a lens, a glass + brick, a ripple. See `warp/field`." + [c] + (and (vector? c) (= :warp (first c)))) + (defn- knock-index [palette c] (if (= :clear c) -1 (colour-index palette (second c)))) @@ -448,6 +455,12 @@ (cond (knockout? c) (assoc op :knock (knock-index palette c) :color 0) (remap? c) (assoc op :lut (lut palette c) :color 0) + ;; The field is in the shape's own space, so the op + ;; carries the matrix that space is drawn through. + (warp? c) (assoc op :warp (second c) :m m :color 0 + :amount (rd [:warp :amount]) + :size (rd [:warp :size]) + :phase (rd [:warp :phase])) :else (assoc op :color (colour-index palette c)))))] (case (:kind n) :group nil diff --git a/frontend/src/arthur/domain/warp.cljs b/frontend/src/arthur/domain/warp.cljs new file mode 100644 index 0000000..1761559 --- /dev/null +++ b/frontend/src/arthur/domain/warp.cljs @@ -0,0 +1,73 @@ +(ns arthur.domain.warp + "What a warp shape shows at each pixel it covers: what is under it, from + somewhere else. + + A WARP IS A REMAP IN SPACE. A remap redraws what is under a shape in another + slot and leaves it where it is; a warp redraws it from another place and + leaves its slot alone. So it lands where a remap does — on what was drawn + before it, in the buffer it is drawn into — and what is drawn after it is + untouched. A lens is between things for the same reason a flashlight is: draw + order. Nothing here knows about depth. + + Each kind is a FIELD, a map from a point to the point it shows, in the shape's + OWN space: the origin is the middle of what it draws (`paint/centred`), and a + field that moves, turns and scales with its node is a field written about that + origin and carried by its matrix. A lens magnifies about the lens. + + AMOUNT 0 IS UNTOUCHED, for every kind, so tweening amount from 0 brings any + warp in from nothing. Size is a length in the shape's space — a wavelength or + a tile — and phase is in turns, so keying it 0 then 1 runs a wave through once. + + Sampling is nearest-neighbour, by construction: a warp moves indices and + never invents one, so it keeps the hard indexed edge the rasteriser exists + for." + (:require [arthur.domain.node :as node])) + +(def ^:private tau (* 2 js/Math.PI)) + +(defn- local-field + "Kind `kind`'s field about the origin, as (fn [out x y]) writing the point + `(x, y)` shows into `out`. nil for a kind there is none of." + [kind a s ph] + (case kind + ;; Everything (1 + a) times as big. A lens cannot shrink past nothing. + :lens (let [k (/ 1 (max 0.01 (+ 1 a)))] + (fn [^js out x y] (aset out 0 (* x k)) (aset out 1 (* y k)))) + ;; Each tile of glass shows what is behind its own middle: 1 is one flat + ;; colour per tile, 2 the tile's view turned over, as a glass brick does. + :tiles (fn [^js out x y] + (let [cx (* s (+ 0.5 (js/Math.floor (/ x s)))) + cy (* s (+ 0.5 (js/Math.floor (/ y s))))] + (aset out 0 (+ cx (* (- x cx) (- 1 a)))) + (aset out 1 (+ cy (* (- y cy) (- 1 a)))))) + ;; Rows slid side to side: water, heat over a road. + :wave (fn [^js out x y] + (aset out 0 (+ x (* a (js/Math.sin (* tau (+ (/ y s) ph)))))) + (aset out 1 y)) + ;; Rings running out from the middle: a stone in a pond. + :ripple (fn [^js out x y] + (let [r (js/Math.hypot x y) + d (if (pos? r) (/ (* a (js/Math.sin (* tau (- (/ r s) ph)))) r) 0)] + (aset out 0 (+ x (* x d))) + (aset out 1 (+ y (* y d))))) + ;; Widths swelling and pinching down the height: the fairground mirror. + :mirror (fn [^js out x y] + (aset out 0 (* x (- 1 (* a (js/Math.sin (* tau (+ (/ y s) ph))))))) + (aset out 1 y)) + nil)) + +(defn field + "Warp op `op`'s field in stage space: (fn [out x y]) writing into `out` the + stage point that stage point `(x, y)` shows. nil when it has none — a kind + there is no field for, or a shape scaled to nothing, which covers nothing." + [{:keys [warp m amount size phase]}] + (when-let [^js inv (node/invert m)] + (when-let [f (local-field warp (or amount 0) (max 1 (or size 1)) (or phase 0))] + (fn [^js out x y] + (f out + (+ (* (aget inv 0) x) (* (aget inv 2) y) (aget inv 4)) + (+ (* (aget inv 1) x) (* (aget inv 3) y) (aget inv 5))) + (let [u (aget out 0) v (aget out 1)] + (aset out 0 (+ (* (aget m 0) u) (* (aget m 2) v) (aget m 4))) + (aset out 1 (+ (* (aget m 1) u) (* (aget m 3) v) (aget m 5)))) + out)))) diff --git a/frontend/src/arthur/ui/icon.cljs b/frontend/src/arthur/ui/icon.cljs index a80a026..2a16b0b 100644 --- a/frontend/src/arthur/ui/icon.cljs +++ b/frontend/src/arthur/ui/icon.cljs @@ -74,6 +74,15 @@ "M4.4 7.8 8.5 2" "M7.6 7.8 3.5 2"]} :paste {:stroke ["M3.8 2.5H2.5v8h7v-8H8.2" "M4.5 1.5h3v2h-3z"]} :duplicate {:stroke ["M4.5 4.5h5v5h-5z" "M2.5 7.5v-5h5" "M7 5.8v2.4" "M5.8 7h2.4"]} + ;; The warps, each as what it does to what is behind it: a magnifying + ;; glass, a square of glass bricks, water, rings on a pond, and the + ;; fairground mirror's waist. + :lens {:stroke ["M2 5a3 3 0 1 0 6 0a3 3 0 1 0 -6 0" "M7.2 7.2 10.5 10.5"]} + :tiles {:stroke ["M1.5 1.5h9v9h-9z" "M6 1.5v9" "M1.5 6h9"]} + :wave {:stroke ["M1.5 4.5c1.5-2 3-2 4.5 0s3 2 4.5 0" "M1.5 8.5c1.5-2 3-2 4.5 0s3 2 4.5 0"]} + :ripple {:fill ["M5 6a1 1 0 1 0 2 0a1 1 0 1 0 -2 0z"] + :stroke ["M3 6a3 3 0 1 0 6 0a3 3 0 1 0 -6 0" "M1 6a5 5 0 1 0 10 0a5 5 0 1 0 -10 0"]} + :mirror {:stroke ["M3 1.5c2.2 2.5 2.2 6.5 0 9" "M9 1.5c-2.2 2.5-2.2 6.5 0 9"]} ;; Whether others may follow you: an eye, open to being watched. :eye {:stroke ["M1.5 6C2.8 3.8 4.3 2.8 6 2.8S9.2 3.8 10.5 6C9.2 8.2 7.7 9.2 6 9.2S2.8 8.2 1.5 6z"] :fill ["M4.8 6a1.2 1.2 0 1 0 2.4 0a1.2 1.2 0 1 0 -2.4 0z"]}}) diff --git a/frontend/src/arthur/ui/palette.cljs b/frontend/src/arthur/ui/palette.cljs index 247d5bf..a0a3538 100644 --- a/frontend/src/arthur/ui/palette.cljs +++ b/frontend/src/arthur/ui/palette.cljs @@ -21,6 +21,7 @@ [arthur.footage.store :as store] [arthur.domain.palette :as pal] [arthur.domain.symbol :as symbol] + [arthur.ui.icon :as icon] [arthur.subs.playback :as playback] [arthur.events.project :as project] [arthur.events.ui :as ui] @@ -84,6 +85,20 @@ (rf/dispatch [::ui/set-tone tone])) ;; --------------------------------------------------------------------------- +;; warps: a shape that shows what is under it from somewhere else +;; +;; As a remap does, a warp draws nothing of its own and lands on what is drawn +;; before it; how much is its `[:warp …]` channels, in the inspector and the +;; timeline like any other. See `domain/warp`. + +(def warps + "Each warp a shape can be: its kind, its name and what it does." + [[:lens "lens" "lens — magnifies what is under it about its middle. How much is its warp channel."] + [:tiles "glass tiles" "glass tiles — each tile shows what is behind it, turned over"] + [:wave "wave" "wave — slides what is under it side to side, as water does. Key its phase to run it."] + [:ripple "ripple" "ripple — rings out from its middle, as a stone in a pond. Key its phase to run it."] + [:mirror "funhouse mirror" "funhouse mirror — widths swell and pinch down its height"]]) + ;; remap: a shape that lights what is under it ;; ;; A shape coloured `[:remap {from to …}]` draws nothing of its own: whatever is @@ -196,12 +211,16 @@ ", the palette the stage is in") :on-click #(rf/dispatch [::select followed followed])} "↩ active"]) - [:div {:class (str "chip" (cond (= :clear tone) " clear" (symbol/remap? tone) " map")) + [:div {:class (str "chip" (cond (= :clear tone) " clear" (symbol/remap? tone) " map" + (symbol/warp? tone) " warp")) :style (when hex {:background hex}) :title (cond (= :clear tone) "clear: what is drawn knocks out what is under it in its symbol" (symbol/remap? tone) "remap: what is drawn lights what is under it" + (symbol/warp? tone) (some (fn [[k _ t]] (when (= k (second tone)) t)) warps) :else (str "slot " tone " · " hex))} - [:span (cond (= :clear tone) "×" (symbol/remap? tone) "⇄" :else tone)]] + (if (symbol/warp? tone) + [icon/view (second tone)] + [:span (cond (= :clear tone) "×" (symbol/remap? tone) "⇄" :else tone)])] [:div.cells (let [rm (remap) hex-of #(:hex (get (:slots palette) %))] (doall (map-indexed #(swatch pid %1 %2 tone rm hex-of (contains? used %1)) @@ -214,7 +233,15 @@ [:button {:class (str "cell map" (when (symbol/remap? tone) " on")) :title "remap — draws light: what is under it is drawn in other slots of the palette. Set which in the inspector." :on-click #(pick! [:remap {}])} - "⇄"]]] + "⇄"]] + (doall + (for [[kind label title] warps] + ^{:key kind} + [:div.slot + [:button {:class (str "cell warp" (when (= [:warp kind] tone) " on")) + :aria-label label :title title + :on-click #(pick! [:warp kind])} + [icon/view kind]]]))] [:div.grid-tools [:button {:class (when solo? "on") :disabled (not (number? tone)) diff --git a/frontend/src/arthur/ui/params.cljs b/frontend/src/arthur/ui/params.cljs index 52da6b0..2e65f0b 100644 --- a/frontend/src/arthur/ui/params.cljs +++ b/frontend/src/arthur/ui/params.cljs @@ -126,7 +126,7 @@ (def ^:private order (zipmap [[:xform :pos] [:xform :rot] [:xform :scale] [:xform :skew] [:xform :pivot] - paint/geometry [:style :color] [:vis]] + paint/geometry [:style :color] [:warp :amount] [:warp :size] [:warp :phase] [:vis]] (range))) (defn- gap-at @@ -215,7 +215,7 @@ (doall (map-indexed (fn [i x] (field i x #(put (assoc (vec v) i %)))) v))))) (defn- swatches - "A shape's colour, to change: every slot, then clear and remap, as the strip + "A shape's colour, to change: every slot, then clear, remap and the warps, as the strip beside the tools offers them for the next shape. This is where something already drawn is recoloured — the strip only says what is drawn next." [v off? palette pick!] @@ -245,7 +245,17 @@ :disabled off? :aria-label "remap" :aria-pressed (boolean (symbol/remap? v)) :title "remap — lights what is under it in other slots; set which below" - :on-click #(when-not (symbol/remap? v) (pick! [:remap {}]))}]])) + :on-click #(when-not (symbol/remap? v) (pick! [:remap {}]))}] + (doall + (for [[kind label title] palette/warps] + ^{:key kind} + [:button.swatch.warp + {:class (when (= [:warp kind] v) "on") + :disabled off? + :aria-label label :aria-pressed (= [:warp kind] v) + :title title + :on-click #(pick! [:warp kind])} + [icon/view kind]]))])) (defn- channel-value "The editor for channel `path` of node `n`: boxes, a switch, swatches — or, diff --git a/frontend/src/arthur/ui/player.cljs b/frontend/src/arthur/ui/player.cljs index fe7cd4d..f622fce 100644 --- a/frontend/src/arthur/ui/player.cljs +++ b/frontend/src/arthur/ui/player.cljs @@ -22,7 +22,9 @@ [arthur.domain.clip :as clip] [arthur.domain.cut :as cut] [arthur.domain.outline :as outline] + [arthur.domain.node :as node] [arthur.domain.onion :as onion] + [arthur.domain.paint :as paint] [arthur.domain.palette :as pal] [arthur.domain.pick :as pick] [arthur.domain.raster :as raster] @@ -344,10 +346,22 @@ (defn- inked "Op `op` drawn in tone `tone`: a slot's colour, or — for a remap — light on - what is under it, through the same table the saved shape gets." + what is under it, through the same table the saved shape gets. A warp is + about the middle of what is drawn, where the saved shape's origin will be, + at its kind's defaults." [op tone] - (if (symbol/remap? tone) + (cond + (symbol/remap? tone) (assoc op :lut (symbol/lut pal/slot tone) :color 0) + + (symbol/warp? tone) + (let [[cx cy] (paint/middle (array-seq (:pts op)))] + (into (assoc op :warp (second tone) :color 0 + :m (js/Float64Array. #js [1 0 0 1 cx cy])) + (map (fn [[[_ k] c]] [k (:value c)])) + (node/warp-defaults (second tone)))) + + :else (assoc op :color (pal/slot tone)))) (defn- with-previews @@ -362,7 +376,7 @@ {:keys [slot knock paths] :as s} @stroke rings (when (:mask s) (traced s fit))] (cond-> ops - (and (<= 6 (count pts)) (or (number? tone) (symbol/remap? tone))) + (and (<= 6 (count pts)) (or (number? tone) (symbol/remap? tone) (symbol/warp? tone))) (clip/atop target (inked (poly pts {}) tone)) paths diff --git a/frontend/test/arthur/domain/warp_test.cljs b/frontend/test/arthur/domain/warp_test.cljs new file mode 100644 index 0000000..a8bc614 --- /dev/null +++ b/frontend/test/arthur/domain/warp_test.cljs @@ -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")) diff --git a/static/arthur/app.css b/static/arthur/app.css index 2a4d237..b8fd3e6 100644 --- a/static/arthur/app.css +++ b/static/arthur/app.css @@ -1866,6 +1866,17 @@ button.tl-gap:hover { background: rgba(47, 111, 192, .28); } background: linear-gradient(135deg, #3a3f52 0 50%, #e8d9a8 50% 100%); color: #fff; font-size: 11px; line-height: 1; text-shadow: 0 1px 1px rgba(0, 0, 0, .6); } +/* A warp: glass, which shows what is behind it from somewhere else. */ +.palette-grid .cell.warp, +.palette-grid .chip.warp, +.swatch.warp { + display: grid; place-items: center; + background: linear-gradient(160deg, #e9f3f7 0 45%, #b9d3de 100%); + color: #22303a; +} +.palette-grid .cell.warp svg { width: 11px; height: 11px; } +.palette-grid .chip.warp svg { width: 18px; height: 18px; } +.swatch.warp svg { width: 10px; height: 10px; } .palette-grid .cell { position: relative; } .palette-grid .cell .badge { position: absolute; right: -2px; bottom: -2px;