(listen-for-mouse {:keys [context], :as game} *state)

Source

(defn listen-for-mouse [{:keys [context], :as game} *state] #?(:clj (GLFW/glfwSetCursorPosCallback context (proxy [GLFWCursorPosCallback] [] (invoke [window xpos ypos] (swap! *state (fn [{:keys [tx ty], :or {tx 0, ty 0}, :as state}] (let [*fb-width (MemoryUtil/memAllocInt 1) *fb-height (MemoryUtil/memAllocInt 1) *window-width (MemoryUtil/memAllocInt 1) *window-height (MemoryUtil/memAllocInt 1) _ (GLFW/glfwGetFramebufferSize context *fb-width *fb-height) _ (GLFW/glfwGetWindowSize context *window-width *window-height) fb-width (.get *fb-width) fb-height (.get *fb-height) window-width (.get *window-width) window-height (.get *window-height) width-ratio (/ fb-width window-width) height-ratio (/ fb-height window-height) x (- (* xpos width-ratio) tx) y (- (* ypos height-ratio) ty) rx (/ x fb-width) ry (/ y fb-height) r (Math/atan2 rx ry) cx (- x (/ fb-width 2)) cy (- (/ fb-height 2) y) cr (-> (/ cx fb-width) (* 360) m/deg->rad)] (MemoryUtil/memFree *fb-width) (MemoryUtil/memFree *fb-height) (MemoryUtil/memFree *window-width) (MemoryUtil/memFree *window-height) (assoc state :x x :y y :rx rx :ry ry :r r :cx cx :cy cy :cr cr))))))) :cljs (events/listen js/window "mousemove" (fn [event] (swap! *state (fn [{:keys [tx ty], :or {tx 0, ty 0}, :as state}] (let [bounds (.getBoundingClientRect context.canvas) x (- (.-clientX event) (.-left bounds) tx) y (- (.-clientY event) (.-top bounds) ty) rx (/ x (.-width bounds)) ry (/ y (.-height bounds)) r (js/Math.atan2 rx ry) cx (- x (/ (.-width bounds) 2)) cy (- (/ (.-height bounds) 2) y) cr (-> (/ cx (.-width bounds)) (* 360) m/deg->rad)] (assoc state :x x :y y :rx rx :ry ry :r r :cx cx :cy cy :cr cr))))))))