arthur/frontend/src/arthur/ui/convert.cljs

94 lines
4.4 KiB
Clojure

(ns arthur.ui.convert
"The question a dropped video asks: which of its frames become a symbol, and
what is that symbol called.
The video plays in the dialog so the frames can be chosen by looking at them.
The two handles under it are the range, `[start end)` in source frames; moving
either seeks the video to the frame it is on, so the edge being chosen is the
picture being shown. Detection then runs on those frames only — see
`events/footage/analyse!` — and the result lands where the video was dropped."
(:require [arthur.events.footage :as footage]
[arthur.subs.playback :as playback]
[arthur.subs.ui :as sub]
[re-frame.core :as rf]
[reagent.core :as r]))
(defn- frame-at [^js track x frames]
(let [box (.getBoundingClientRect track)]
(-> (/ (* (- x (.-left box)) frames) (.-width box))
js/Math.round (max 0) (min frames))))
(defn- range-bar
"Two handles over the footage's length. `player` holds the element to seek."
[{:keys [frames fps range]} player]
(r/with-let [held (atom nil)
track (atom nil)]
(let [[start end] range
pct #(str (* 100 (/ % (max 1 frames))) "%")
move! (fn [^js e]
(when-let [which @held]
(let [f (frame-at @track (.-clientX e) frames)
[s' e'] (if (= :start which)
[(min f (dec end)) end]
[start (max f (inc start))])]
(rf/dispatch [::footage/convert-set :range [s' e']])
(when-let [^js v @player]
(set! (.-currentTime v)
(/ (if (= :start which) s' (dec e')) fps))))))
grab (fn [which]
(fn [^js e]
(.preventDefault e)
(reset! held which)
(try (.setPointerCapture (.-currentTarget e) (.-pointerId e))
(catch :default _ nil))))]
[:div.convert-range {:ref #(reset! track %)}
[:div.convert-kept {:style {:left (pct start) :width (pct (- end start))}}]
(doall
(for [[which f] [[:start start] [:end end]]]
^{:key which}
[:div {:class (str "convert-handle " (name which))
:style {:left (pct f)}
:title (str (name which) " · frame " f)
:on-pointer-down (grab which)
:on-pointer-move move!
:on-pointer-up #(reset! held nil)
:on-pointer-cancel #(reset! held nil)}]))])))
(defn view []
(r/with-let [player (atom nil)]
(when-let [{:keys [label frames fps video range name] :as request}
@(rf/subscribe [::sub/convert])]
(let [{:keys [loading? status]} @(rf/subscribe [::playback/footage])
[start end] range
n (- end start)]
[:div.convert-scrim
[:div.convert
[:div.pane-head (str "make a symbol from " label)]
[:div.convert-body
[:video.convert-video
{:ref #(reset! player %)
:src video :controls true :muted false :preload "auto"
:plays-inline true}]
[range-bar request player]
[:div.row.dim
(str "frames " start " … " end " · " n " frames · "
(.toFixed (/ n fps) 1) "s of " frames)]
[:label.convert-name
[:span.dim "name"]
[:input {:type "text" :value name :disabled loading?
:auto-focus true
:on-change #(rf/dispatch [::footage/convert-set :name
(.. % -target -value)])
:on-key-down #(when (= "Enter" (.-key %))
(rf/dispatch [::footage/convert]))}]]
(when loading? [:div.dim status])]
[:div.convert-actions
[:button {:disabled loading?
:on-click #(rf/dispatch [::footage/convert-cancel])} "cancel"]
[:button {:disabled (or loading? (< n 1) (empty? name))
:title "draw over these frames · nothing is detected, and it is never exported"
:on-click #(rf/dispatch [::footage/convert-tracing])}
"tracing layer"]
[:button.on {:disabled (or loading? (< n 1) (empty? name))
:on-click #(rf/dispatch [::footage/convert])}
(if loading? "working…" "make symbol")]]]]))))