Improvements to grid alignment impl.

This commit is contained in:
Andrey Antukh 2016-04-11 17:32:16 +03:00
parent 8916a9b7ac
commit 4f0a74b1a3
No known key found for this signature in database
GPG Key ID: 4DFEBCB8316A8B95
7 changed files with 58 additions and 50 deletions

15
src/uxbox/data/core.cljs Normal file
View File

@ -0,0 +1,15 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz>
(ns uxbox.data.core
"Worker related api and initialization events."
(:require [beicon.core :as rx]
[uxbox.rstore :as rs]
[uxbox.constants :as c]
[uxbox.util.workers :as uw]))
(defonce worker (uw/init "/js/worker.js"))

View File

@ -1,32 +0,0 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz>
(ns uxbox.data.worker
"Worker related api and initialization events."
(:require [beicon.core :as rx]
[uxbox.rstore :as rs]
[uxbox.constants :as c]
[uxbox.util.workers :as uw]))
(defonce worker (uw/init "/js/worker.js"))
;; --- Worker Initialization
(defrecord InitializeWorker [id]
rs/EffectEvent
(-apply-effect [_ state]
(let [page (get-in state [:pages-by-id id])
opts (:options page)
message {:cmd :grid/init
:width c/viewport-width
:height c/viewport-height
:x-axis (:grid/x-axis opts c/grid-x-axis)
:y-axis (:grid/y-axis opts c/grid-y-axis)}]
(uw/send! worker message))))
(defn initialize
[id]
(InitializeWorker. id))

View File

@ -8,18 +8,22 @@
(ns uxbox.data.workspace (ns uxbox.data.workspace
(:require [bouncer.validators :as v] (:require [bouncer.validators :as v]
[beicon.core :as rx] [beicon.core :as rx]
[uxbox.constants :as c]
[uxbox.shapes :as sh] [uxbox.shapes :as sh]
[uxbox.rstore :as rs] [uxbox.rstore :as rs]
[uxbox.state.shapes :as stsh] [uxbox.state.shapes :as stsh]
[uxbox.schema :as sc] [uxbox.schema :as sc]
[uxbox.data.core :refer (worker)]
[uxbox.data.pages :as udp] [uxbox.data.pages :as udp]
[uxbox.data.shapes :as uds] [uxbox.data.shapes :as uds]
[uxbox.data.worker :as wrk]
[uxbox.util.datetime :as dt] [uxbox.util.datetime :as dt]
[uxbox.util.math :as mth] [uxbox.util.math :as mth]
[uxbox.util.geom.point :as gpt])) [uxbox.util.geom.point :as gpt]
[uxbox.util.workers :as uw]))
;; --- Workspace Initialization ;; --- Initialize Workspace
(declare initialize-alignment-index)
(defrecord InitializeWorkspace [project page] (defrecord InitializeWorkspace [project page]
rs/UpdateEvent rs/UpdateEvent
@ -41,10 +45,10 @@
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state s] (-apply-watch [_ state s]
(if (get-in state [:pages-by-id page]) (if (get-in state [:pages-by-id page])
(rx/of (wrk/initialize page)) (rx/of (initialize-alignment-index page))
(->> (rx/filter udp/pages-fetched? s) (->> (rx/filter udp/pages-fetched? s)
(rx/take 1) (rx/take 1)
(rx/map #(wrk/initialize page)))))) (rx/map #(initialize-alignment-index page))))))
(defn initialize (defn initialize
"Initialize the workspace state." "Initialize the workspace state."
@ -146,3 +150,22 @@
[] []
(ResetZoom.)) (ResetZoom.))
;; --- Initialize Alignment Index
(defrecord InitializeAlignmentIndex [id]
rs/WatchEvent
(-apply-watch [_ state s]
(let [page (get-in state [:pages-by-id id])
opts (:options page)
message {:cmd :grid/init
:width c/viewport-width
:height c/viewport-height
:x-axis (:grid/x-axis opts c/grid-x-axis)
:y-axis (:grid/y-axis opts c/grid-y-axis)}]
(->> (uw/send! worker message)
(rx/map #(toggle-flag :alignment/indexed))))))
(defn initialize-alignment-index
[id]
(InitializeAlignmentIndex. id))

View File

@ -10,7 +10,7 @@
[lentes.core :as l] [lentes.core :as l]
[uxbox.state :as st] [uxbox.state :as st]
[uxbox.shapes :as sh] [uxbox.shapes :as sh]
[uxbox.data.worker :refer (worker)] [uxbox.data.core :refer (worker)]
[uxbox.ui.workspace.base :as wb] [uxbox.ui.workspace.base :as wb]
[uxbox.util.geom.point :as gpt] [uxbox.util.geom.point :as gpt]
[uxbox.util.workers :as uw])) [uxbox.util.workers :as uw]))

View File

@ -86,17 +86,20 @@
[] []
(let [shapes @selected-shapes-l (let [shapes @selected-shapes-l
options @page-options-l options @page-options-l
flags @wb/flags-l
indexed? (:alignment/indexed flags)
stoper (->> uuc/actions-s stoper (->> uuc/actions-s
(rx/map :type) (rx/map :type)
(rx/filter empty?) (rx/filter empty?)
(rx/take 1))] (rx/take 1))]
(as-> wb/mouse-delta-s $ (as-> wb/mouse-delta-s $
(rx/take-until stoper $) (rx/take-until stoper $)
(rx/map #(gpt/divide % @wb/zoom-l) $)
(rx/scan (fn [acc delta] (rx/scan (fn [acc delta]
(let [xf (map #(sh/move % delta))] (let [xf (map #(sh/move % delta))]
(into [] xf acc))) shapes $) (into [] xf acc))) shapes $)
(rx/mapcat (fn [items] (rx/mapcat (fn [items]
(if (:grid/align options) (if (and (:grid/align options) indexed?)
(->> (apply rx/of items) (->> (apply rx/of items)
(rx/mapcat align/translate) (rx/mapcat align/translate)
(rx/reduce conj [])) (rx/reduce conj []))

View File

@ -7,28 +7,28 @@
(ns uxbox.worker.align (ns uxbox.worker.align
"Workspace aligment indexes worker." "Workspace aligment indexes worker."
(:require [beicon.core :as rx] (:require [beicon.core :as rx]
[kdtree :as kd] [kdtree.core :as kd]
[uxbox.worker.core :as wrk] [uxbox.worker.core :as wrk]
[uxbox.util.geom.point :as gpt])) [uxbox.util.geom.point :as gpt]))
(defonce state (volatile! nil)) (defonce state (volatile! nil))
(defmethod wrk/handler :grid/init (defmethod wrk/handler :grid/init
[{:keys [width height x-axis y-axis] :as opts}] [{:keys [sender width height x-axis y-axis] :as opts}]
(println ":grid/init" opts) (time
(let [points (into-array (let [points (into-array
(for [x (range 0 width (or x-axis 10)) (for [x (range 0 width (or x-axis 10))
y (range 0 height (or y-axis 10))] y (range 0 height (or y-axis 10))]
#js [x y])) #js [x y]))
tree (kd/create2d points)] tree (kd/create2d points)]
(vreset! state tree))) (vreset! state tree)
(wrk/reply! sender nil))))
(defmethod wrk/handler :grid/align (defmethod wrk/handler :grid/align
[{:keys [sender point] :as message}] [{:keys [sender point] :as message}]
(println "request" point)
(let [point #js [(:x point) (:y point)] (let [point #js [(:x point) (:y point)]
results (js->clj (.nearest @state point 1)) results (js->clj (.nearest @state point 1))
[[x y] d] (first results) [[x y] d] (first results)
result (gpt/point x y)] result (gpt/point x y)]
(println "result:" result)
(wrk/reply! sender {:point (gpt/point x y)}))) (wrk/reply! sender {:point (gpt/point x y)})))

View File

@ -27,5 +27,4 @@
(defn reply! (defn reply!
[sender message] [sender message]
(let [message (assoc message :reply-to sender)] (let [message (assoc message :reply-to sender)]
(println "replying " message)
(.postMessage js/self (t/encode message)))) (.postMessage js/self (t/encode message))))