diff --git a/src/cljs/wish/db.cljs b/src/cljs/wish/db.cljs index 83240887..9adbae76 100644 --- a/src/cljs/wish/db.cljs +++ b/src/cljs/wish/db.cljs @@ -3,6 +3,7 @@ (def default-db {:page [:splash] :device-type :default + :touch? false ; true if there is a touch screen :updates {:latest nil :ignored :unknown} diff --git a/src/cljs/wish/events.cljs b/src/cljs/wish/events.cljs index 45ddd15c..8c3cd56a 100755 --- a/src/cljs/wish/events.cljs +++ b/src/cljs/wish/events.cljs @@ -41,6 +41,12 @@ (fn-traced [db [device-type]] (assoc db :device-type device-type))) +(reg-event-db + :set-touch + [trim-v] + (fn-traced [db [touch?]] + (assoc db :touch? touch?))) + (reg-event-fx ::update-keymap [trim-v (inject-cofx ::inject/sub [:meta/kind])] diff --git a/src/cljs/wish/sheets/dnd5e/overlays/hp.cljs b/src/cljs/wish/sheets/dnd5e/overlays/hp.cljs index 26eb2f02..0cc17846 100644 --- a/src/cljs/wish/sheets/dnd5e/overlays/hp.cljs +++ b/src/cljs/wish/sheets/dnd5e/overlays/hp.cljs @@ -12,7 +12,13 @@ [wish.views.widgets :as widgets :refer-macros [icon] :refer [expandable formatted-text]] - [wish.views.widgets.fast-numeric])) + [wish.views.widgets.fast-numeric] + [wish.views.widgets.spinning-modifier + :refer [spinning-modifier]])) + +(defn- apply-hp-delta! [delta max-hp] + (>evt [::events/update-hp delta max-hp]) + (>evt [:toggle-overlay nil])) (defn- condition-widget [[id level] _on-delete] @@ -118,8 +124,7 @@ {:on-submit (fn-click (let [{:keys [heal damage]} @state] (log "Update HP: heal +" heal " -" damage) - (>evt [::events/update-hp (- heal damage) max-hp]) - (>evt [:toggle-overlay nil])))} + (apply-hp-delta! (- heal damage) max-hp)))} [:div.sections [:div.quick-adjust @@ -191,6 +196,50 @@ :save! #(>evt [::events/temp-max-hp! %2])}]]]) +(defn- non-touchable-ui [{:keys [state hp max-hp max-mod new-hp]}] + [:<> + [:h4 "Hit Points"] + [hp-form + :hp hp + :max-hp max-hp + :max-mod max-mod] + + [:h5.centered.section-header "Quick Adjust"] + [quick-adjust-form state + :hp hp + :max-hp max-hp + :new-hp new-hp] + ]) + +(defn- touchable-ui [{:keys [state hp max-hp]}] + [:<> + [:h4 "Hit Points"] + + [:div.touchable + [spinning-modifier + state + :initial hp + :maximum max-hp + :delta->color (fn delta->color [delta] + (cond + (> delta 0) "#00cc00" + (< delta 0) "#cc0000")) + :per-rotation (condp > max-hp + 100 20 + 40) + :path [:heal]] + + (when (let [delta (:heal @state)] + (and delta (not= 0 delta))) + [:div.sections + [:input.apply {:type 'button + :value "Apply!" + :on-click (fn-click + (apply-hp-delta! (:heal @state) max-hp)) + }] ]) + ] + ]) + ; ======= public interface ================================ (defn overlay [] @@ -198,27 +247,27 @@ (let [[hp max-hp max-mod] ( delta js/Math.PI) (- two-pi delta) + (< delta (- js/Math.PI)) (+ two-pi delta) + :else delta)] + (-> normalized + + ; convert to degrees: + (* 180) + (/ js/Math.PI)))) + +(defn on-touch-move [state-ref rotation-ref, ^js e] + (let [touch (first (.-touches e)) + state @state-ref + this-touch [(.-clientX touch) (.-clientY touch)]] + (when-let [last-touch (:last-touch state)] + (swap! rotation-ref + (compute-rotation + (:element state) + last-touch + this-touch))) + (swap! state-ref assoc :last-touch this-touch))) + +(defn spinning-modifier [ratom & {:keys [initial maximum + delta->color + per-rotation path] + :or {delta->color (constantly nil)}}] + (letfn [(color delta)] + + (when-not (= ( delta 0) "+") + delta])] + ])))) diff --git a/test/cljs/wish/views/widgets/spinning_modifier_test.cljs b/test/cljs/wish/views/widgets/spinning_modifier_test.cljs new file mode 100644 index 00000000..2a1c51b3 --- /dev/null +++ b/test/cljs/wish/views/widgets/spinning_modifier_test.cljs @@ -0,0 +1,21 @@ +(ns wish.views.widgets.spinning-modifier-test + (:require [cljs.test :refer-macros [deftest testing is]] + [wish.views.widgets.spinning-modifier + :refer [compute-rotation]])) + +(deftest compute-rotation-test + (testing "Rotate from 3 o'clock to 6 o'clock" + (is (= 90 + (compute-rotation + [0 0 10 10] + + ; touches: + [10 5] [5 10])))) + + (testing "Rotate through 9 o'clock" + (is (< 0 + (compute-rotation + [0 0 10 10] + + [0 6] [0 4]))))) +