-
Notifications
You must be signed in to change notification settings - Fork 1
/
layout.clj
111 lines (96 loc) · 3.13 KB
/
layout.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
(ns pp-grid.layout
(:require [pp-grid.core :as c]))
(defn valign
"Constructs a grid containing given grids aligned vertically.
For example, (valign [(text \"A\") (text \"B\") (text \"C\")]) is
A
B
C
"
([grids]
(valign grids 0))
([grids y-padding]
(valign grids y-padding false))
([grids y-padding center?]
(reduce
(fn [top bottom]
(cond
(empty? top) bottom
(empty? bottom) top
:else (let [top-width (:width top)
bottom-width (:width bottom)
top-center-x (+ (:min-x top) (/ top-width 2))
bottom-center-x (+ (:min-x bottom) (/ bottom-width 2))
x-diff (- top-center-x bottom-center-x)
dx (if center? x-diff 0)
dy (+ y-padding (:height top))]
(c/add top (c/transform bottom (c/tf-translate dx dy))))))
(first grids)
(rest grids))))
(defn halign
"Constructs a grid containing given grids aligned horizontally.
For example, (halign [(text \"A\") (text \"B\") (text \"C\")]) is ABC."
([grids]
(halign grids 0))
([grids x-padding]
(halign grids x-padding false))
([grids x-padding center?]
(reduce
(fn [left right]
(cond
(empty? left) right
(empty? right) left
:else (let [dx (+ x-padding (:width left))
left-height (:height left)
right-height (:height right)
left-center-y (+ (:min-y left) (/ left-height 2))
right-center-y (+ (:min-y right) (/ right-height 2))
y-diff (- left-center-y right-center-y)
dy (if center? y-diff 0)]
(c/add left (c/transform right (c/tf-translate dx dy))))))
(first grids)
(rest grids))))
(defn hspacer
"Constructs a horizontal space of given length.
For example, (hspacer 5) is \" \"."
([n]
(if (<= n 0) (c/empty-grid)
(-> (c/empty-grid)
(assoc [0 0] \space)
(assoc [(dec n) 0] \space)))))
(defn vspacer
"Constructs a vertical space of given length.
For example, (vspacer 5) is
(space)
(space)
(space)
(space)
(space)
"
([n]
(if (<= n 0) (c/empty-grid)
(-> (c/empty-grid)
(assoc [0 0] \space)
(assoc [0 (dec n)] \space)))))
(defn spacer
"Constructs a rectangular space of given width and height."
([width height]
(c/add (hspacer width) (vspacer height))))
(defn ===
"Horizontally aligns given grids with some defaults.
Just a convenience wrapper for halign to accept grids as args and
use some default values for padding and centering."
[x-padding & grids]
(halign grids x-padding true))
(defn ||
"Vertically aligns given grids with some defaults.
Just a convenience wrapper for valign to accept grids as args and
use some default values for padding and centering."
[y-padding & grids]
(valign grids y-padding true))
(defn pull
"Pulls a grid by given amounts horizontally and vertically.
Just a convenience function for transforming with tf-translate. Useful
for tweaking alignments."
[g dx dy]
(c/transform g (c/tf-translate dx dy)))