Align items in grid layout

This commit is contained in:
alonso.torres 2023-05-11 15:41:58 +02:00
parent 47e927d571
commit c31dc94496
5 changed files with 113 additions and 5 deletions

View file

@ -104,8 +104,54 @@
(defn child-modifiers
[parent parent-bounds child child-bounds layout-data cell-data]
(let [fill-modifiers (fill-modifiers parent parent-bounds child child-bounds layout-data cell-data)
position-delta (gpt/subtract (:start-p cell-data) (gpo/origin child-bounds))]
(let [cell-bounds (cell-bounds layout-data cell-data)
fill-modifiers (fill-modifiers parent parent-bounds child child-bounds layout-data cell-data)
align (:layout-align-items parent)
justify (:layout-justify-items parent)
align-self (:align-self cell-data)
justify-self (:justify-self cell-data)
align-self (when (and align-self (not= align-self :auto)) align-self)
justify-self (when (and justify-self (not= justify-self :auto)) justify-self)
align (or align-self align)
justify (or justify-self justify)
position-delta (gpt/point)
;; Adjust alignment/justify
[from-h to-h]
(case justify
:end
[(gpo/project-point cell-bounds :h (nth child-bounds 1))
(nth cell-bounds 1)]
:center
[(gpo/project-point cell-bounds :h (gpo/center child-bounds))
(gpo/project-point cell-bounds :h (gpo/center cell-bounds))]
[(gpo/project-point cell-bounds :h (first child-bounds))
(first cell-bounds)])
[from-v to-v]
(case align
:end
[(gpo/project-point cell-bounds :v (nth child-bounds 3))
(nth cell-bounds 3)]
:center
[(gpo/project-point cell-bounds :v (gpo/center child-bounds))
(gpo/project-point cell-bounds :v (gpo/center cell-bounds))]
[(gpo/project-point cell-bounds :v (first child-bounds))
(first cell-bounds)])
position-delta
(-> position-delta
(gpt/add (gpt/to-vec from-h to-h))
(gpt/add (gpt/to-vec from-v to-v)))]
(-> (ctm/empty)
(ctm/add-modifiers fill-modifiers)
(ctm/move position-delta))))

View file

@ -80,7 +80,7 @@
"Given a point and a line returns the parametric t the cross point with the line going through the other axis projected"
[point [start end] other-axis-vec]
(let [line-vec (gpt/to-vec start end)
(let [line-vec (gpt/to-vec start end)
pr-point (gsi/line-line-intersect point (gpt/add point other-axis-vec) start end)]
(cond
(not (mth/almost-zero? (:x line-vec)))
@ -93,6 +93,15 @@
:else
0)))
(defn project-point
"Project the point into the given axis: `:h` or `:v` means horizontal or vertical axis"
[[p0 p1 _ p3 :as bounds] axis point]
(let [[other-vec start end]
(if (= axis :h)
[(gpt/to-vec p0 p3) p0 p1]
[(gpt/to-vec p0 p1) p0 p3])]
(gsi/line-line-intersect point (gpt/add point other-vec) start end)))
(defn parent-coords-bounds
[child-bounds [p1 p2 _ p4 :as parent-bounds]]
@ -154,3 +163,13 @@
[bounds vector]
(->> bounds
(map #(gpt/add % vector))))
(defn center
[bounds]
(let [width (width-points bounds)
height (height-points bounds)
half-h (start-hv bounds (/ width 2))
half-v (start-vv bounds (/ height 2))]
(-> (origin bounds)
(gpt/add half-h)
(gpt/add half-v))))