2015-12-16 18:08:28 +02:00
|
|
|
(ns uxbox.data.workspace
|
2015-12-30 01:25:26 +02:00
|
|
|
(:require [bouncer.validators :as v]
|
2016-01-10 23:13:29 +02:00
|
|
|
[beicon.core :as rx]
|
2015-12-30 01:25:26 +02:00
|
|
|
[uxbox.rstore :as rs]
|
2015-12-16 18:08:28 +02:00
|
|
|
[uxbox.router :as r]
|
|
|
|
[uxbox.state :as st]
|
|
|
|
[uxbox.schema :as sc]
|
|
|
|
[uxbox.time :as time]
|
2015-12-30 01:25:26 +02:00
|
|
|
[uxbox.shapes :as shapes]))
|
2015-12-16 18:08:28 +02:00
|
|
|
|
2015-12-28 16:33:14 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Schemas
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(def ^:static +shape-props-schema+
|
|
|
|
{:x [v/required v/integer]
|
|
|
|
:y [v/required v/integer]
|
|
|
|
:width [v/required v/integer]
|
|
|
|
:height [v/required v/integer]})
|
|
|
|
|
2015-12-29 23:38:09 +02:00
|
|
|
(def ^:static +shape-schema+
|
|
|
|
{:x [v/integer]
|
|
|
|
:y [v/integer]
|
|
|
|
:width [v/integer]
|
|
|
|
:height [v/integer]
|
|
|
|
:type [v/required sc/shape-type]})
|
|
|
|
|
2016-01-06 21:03:59 +02:00
|
|
|
(def ^:static +shape-update-size-schema+
|
|
|
|
{:width [v/integer]
|
|
|
|
:height [v/integer]
|
|
|
|
:lock [v/boolean]})
|
|
|
|
|
2016-01-07 01:36:36 +02:00
|
|
|
(def ^:static +shape-update-fill-schema+
|
|
|
|
{:fill [sc/color]
|
|
|
|
:opacity [v/number]})
|
|
|
|
|
2016-01-07 02:19:33 +02:00
|
|
|
(def ^:static +shape-update-position-schema+
|
|
|
|
{:x [v/integer]
|
|
|
|
:y [v/integer]})
|
|
|
|
|
2015-12-16 18:08:28 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Events
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2015-12-17 18:34:15 +02:00
|
|
|
(defn toggle-tool
|
2015-12-28 16:33:14 +02:00
|
|
|
"Toggle the enabled flag of the specified tool."
|
2015-12-17 18:34:15 +02:00
|
|
|
[toolname]
|
2015-12-16 18:08:28 +02:00
|
|
|
(reify
|
|
|
|
rs/UpdateEvent
|
|
|
|
(-apply-update [_ state]
|
2015-12-17 18:34:15 +02:00
|
|
|
(let [key (keyword (str (name toolname) "-enabled"))]
|
2016-01-09 12:22:14 +02:00
|
|
|
(update-in state [:workspace key] (fnil not false))))))
|
2015-12-18 17:50:34 +02:00
|
|
|
|
|
|
|
(defn toggle-toolbox
|
2015-12-28 16:33:14 +02:00
|
|
|
"Toggle the visibility flag of the specified toolbox."
|
2015-12-18 17:50:34 +02:00
|
|
|
[toolname]
|
|
|
|
(reify
|
|
|
|
rs/UpdateEvent
|
|
|
|
(-apply-update [_ state]
|
2016-01-07 17:33:40 +02:00
|
|
|
(let [toolboxes (get-in state [:workspace :toolboxes])]
|
|
|
|
(assoc-in state [:workspace :toolboxes]
|
|
|
|
(if (contains? toolboxes toolname)
|
|
|
|
(disj toolboxes toolname)
|
|
|
|
(conj toolboxes toolname)))))))
|
2015-12-28 16:33:14 +02:00
|
|
|
|
|
|
|
(defn select-for-drawing
|
|
|
|
"Mark a shape selected for drawing in the canvas."
|
|
|
|
[shape]
|
|
|
|
(reify
|
|
|
|
rs/UpdateEvent
|
|
|
|
(-apply-update [_ state]
|
|
|
|
(if shape
|
|
|
|
(assoc-in state [:workspace :drawing] shape)
|
2016-01-09 12:22:14 +02:00
|
|
|
(update-in state [:workspace] dissoc :drawing)))))
|
2015-12-28 16:33:14 +02:00
|
|
|
|
2015-12-28 20:39:24 +02:00
|
|
|
(defn select-shape
|
|
|
|
"Mark a shape selected for drawing in the canvas."
|
|
|
|
[id]
|
|
|
|
(reify
|
|
|
|
rs/UpdateEvent
|
|
|
|
(-apply-update [_ state]
|
|
|
|
(let [selected (get-in state [:workspace :selected])]
|
|
|
|
(if (contains? selected id)
|
|
|
|
(update-in state [:workspace :selected] disj id)
|
|
|
|
(update-in state [:workspace :selected] conj id))))))
|
|
|
|
|
2016-01-02 14:36:19 +02:00
|
|
|
(defn contained-in-selrect?
|
|
|
|
[shape selrect]
|
|
|
|
(let [sx1 (:x selrect)
|
|
|
|
sx2 (+ sx1 (:width selrect))
|
|
|
|
sy1 (:y selrect)
|
|
|
|
sy2 (+ sy1 (:height selrect))
|
|
|
|
rx1 (:x shape)
|
|
|
|
rx2 (+ rx1 (:width shape))
|
|
|
|
ry1 (:y shape)
|
|
|
|
ry2 (+ ry1 (:height shape))]
|
|
|
|
(and (neg? (- (:y selrect) (:y shape)))
|
|
|
|
(neg? (- (:x selrect) (:x shape)))
|
|
|
|
(pos? (- (+ (:y selrect)
|
|
|
|
(:height selrect))
|
|
|
|
(+ (:y shape)
|
|
|
|
(:height shape))))
|
|
|
|
(pos? (- (+ (:x selrect)
|
|
|
|
(:width selrect))
|
|
|
|
(+ (:x shape)
|
|
|
|
(:width shape)))))))
|
|
|
|
|
|
|
|
(defn select-shapes
|
|
|
|
"Select shapes that matches the select rect."
|
|
|
|
[selrect]
|
|
|
|
(reify
|
|
|
|
rs/UpdateEvent
|
|
|
|
(-apply-update [_ state]
|
|
|
|
(let [pid (get-in state [:workspace :page])
|
|
|
|
shapes (->> (vals (:shapes-by-id state))
|
|
|
|
(filter #(= (:page %) pid))
|
|
|
|
(filter #(contained-in-selrect? % selrect))
|
|
|
|
(map :id))]
|
|
|
|
(assoc-in state [:workspace :selected] (into #{} shapes))))))
|
|
|
|
|
2015-12-28 21:05:52 +02:00
|
|
|
(defn deselect-all
|
|
|
|
"Mark a shape selected for drawing in the canvas."
|
|
|
|
[]
|
|
|
|
(reify
|
|
|
|
rs/UpdateEvent
|
|
|
|
(-apply-update [_ state]
|
|
|
|
(assoc-in state [:workspace :selected] #{}))))
|
2015-12-28 20:39:24 +02:00
|
|
|
|
2015-12-28 16:33:14 +02:00
|
|
|
(defn add-shape
|
|
|
|
"Mark a shape selected for drawing in the canvas."
|
|
|
|
[shape props]
|
2015-12-29 23:40:01 +02:00
|
|
|
(sc/validate! +shape-schema+ shape)
|
2015-12-28 16:33:14 +02:00
|
|
|
(sc/validate! +shape-props-schema+ props)
|
|
|
|
(reify
|
|
|
|
rs/UpdateEvent
|
|
|
|
(-apply-update [_ state]
|
2015-12-29 15:51:47 +02:00
|
|
|
(let [sid (random-uuid)
|
|
|
|
pid (get-in state [:workspace :page])
|
|
|
|
shape (merge shape props {:id sid :page pid})]
|
2015-12-28 20:06:59 +02:00
|
|
|
(as-> state $
|
2015-12-29 15:51:47 +02:00
|
|
|
(update-in $ [:pages-by-id pid :shapes] conj sid)
|
2016-01-09 12:22:14 +02:00
|
|
|
(assoc-in $ [:shapes-by-id sid] shape))))))
|
2015-12-18 17:50:34 +02:00
|
|
|
|
2016-01-10 23:13:29 +02:00
|
|
|
(defn delete-shape
|
|
|
|
"Remove the shape using its id."
|
|
|
|
[sid]
|
|
|
|
(reify
|
|
|
|
rs/UpdateEvent
|
|
|
|
(-apply-update [_ state]
|
|
|
|
(let [pageid (get-in state [:shapes-by-id sid :page])
|
|
|
|
shapes (as-> state $
|
|
|
|
(get-in $ [:pages-by-id pageid :shapes])
|
|
|
|
(remove #(= % sid) $)
|
|
|
|
(into [] $))]
|
|
|
|
(as-> state $
|
|
|
|
(assoc-in $ [:pages-by-id pageid :shapes] shapes)
|
|
|
|
(update-in $ [:shapes-by-id] dissoc sid))))))
|
|
|
|
|
|
|
|
(defn remove-selected
|
|
|
|
"Deselect all and remove all selected shapes."
|
|
|
|
[]
|
|
|
|
(reify
|
|
|
|
rs/WatchEvent
|
|
|
|
(-apply-watch [_ state]
|
|
|
|
(let [selected (get-in state [:workspace :selected])]
|
|
|
|
(rx/from-coll
|
2016-01-10 23:18:37 +02:00
|
|
|
(into [(deselect-all)] (map #(delete-shape %) selected)))))))
|
2016-01-10 23:13:29 +02:00
|
|
|
|
2015-12-20 20:19:07 +02:00
|
|
|
(defn initialize
|
2015-12-28 16:33:14 +02:00
|
|
|
"Initialize the workspace state."
|
2015-12-20 20:19:07 +02:00
|
|
|
[projectid pageid]
|
|
|
|
(reify
|
|
|
|
rs/UpdateEvent
|
|
|
|
(-apply-update [_ state]
|
|
|
|
(let [s {:project projectid
|
|
|
|
:toolboxes #{}
|
2015-12-28 14:31:33 +02:00
|
|
|
:drawing nil
|
2015-12-28 13:31:56 +02:00
|
|
|
:selected #{}
|
2015-12-20 20:19:07 +02:00
|
|
|
:page pageid}]
|
2016-01-09 12:22:14 +02:00
|
|
|
(assoc state :workspace s)))))
|
2015-12-28 16:33:14 +02:00
|
|
|
|
2015-12-30 01:25:26 +02:00
|
|
|
(defn move-shape
|
2015-12-28 20:06:59 +02:00
|
|
|
"Mark a shape selected for drawing in the canvas."
|
2015-12-29 15:51:47 +02:00
|
|
|
[sid [dx dy :as delta]]
|
2015-12-28 20:06:59 +02:00
|
|
|
(reify
|
|
|
|
rs/UpdateEvent
|
|
|
|
(-apply-update [_ state]
|
2015-12-29 15:51:47 +02:00
|
|
|
(let [shape (get-in state [:shapes-by-id sid])]
|
2015-12-30 01:25:26 +02:00
|
|
|
(update-in state [:shapes-by-id sid] shapes/-move {:dx dx :dy dy})))))
|
2016-01-06 21:03:59 +02:00
|
|
|
|
2016-01-07 00:37:30 +02:00
|
|
|
(defn update-shape-rotation
|
|
|
|
[sid rotation]
|
|
|
|
{:pre [(number? rotation)
|
|
|
|
(>= rotation 0)
|
|
|
|
(>= 360 rotation)]}
|
|
|
|
(reify
|
|
|
|
rs/UpdateEvent
|
|
|
|
(-apply-update [_ state]
|
|
|
|
(update-in state [:shapes-by-id sid]
|
|
|
|
shapes/-rotate rotation))))
|
|
|
|
|
2016-01-06 21:03:59 +02:00
|
|
|
;; TODO: implement locked resize
|
|
|
|
|
|
|
|
(defn update-shape-size
|
|
|
|
[sid {:keys [width height lock] :as opts}]
|
|
|
|
(sc/validate! +shape-update-size-schema+ opts)
|
|
|
|
(reify
|
|
|
|
rs/UpdateEvent
|
|
|
|
(-apply-update [_ state]
|
|
|
|
(let [shape (get-in state [:shapes-by-id sid])
|
|
|
|
size (select-keys shape [:width :height])
|
|
|
|
size (merge size
|
|
|
|
(when width {:width width})
|
|
|
|
(when height {:height height}))]
|
|
|
|
(update-in state [:shapes-by-id sid]
|
|
|
|
shapes/-resize size)))))
|
2016-01-07 01:24:11 +02:00
|
|
|
|
2016-01-07 02:19:33 +02:00
|
|
|
(defn update-shape-position
|
|
|
|
[sid {:keys [x y] :as opts}]
|
|
|
|
(sc/validate! +shape-update-position-schema+ opts)
|
|
|
|
(reify
|
|
|
|
rs/UpdateEvent
|
|
|
|
(-apply-update [_ state]
|
|
|
|
(update-in state [:shapes-by-id sid]
|
|
|
|
merge
|
|
|
|
(when x {:x x})
|
|
|
|
(when y {:y y})))))
|
|
|
|
|
2016-01-07 01:36:36 +02:00
|
|
|
(defn update-shape-fill
|
|
|
|
[sid {:keys [fill opacity] :as opts}]
|
|
|
|
(sc/validate! +shape-update-fill-schema+ opts)
|
2016-01-07 01:24:11 +02:00
|
|
|
(reify
|
|
|
|
rs/UpdateEvent
|
|
|
|
(-apply-update [_ state]
|
2016-01-07 01:36:36 +02:00
|
|
|
(update-in state [:shapes-by-id sid]
|
|
|
|
merge
|
|
|
|
(when fill {:fill fill})
|
|
|
|
(when opacity {:opacity opacity})))))
|