forked from cljfx/cljfx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe13_re_frame_like_state.clj
203 lines (182 loc) · 7.02 KB
/
e13_re_frame_like_state.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
(ns e13-re-frame-like-state
(:require [cljfx.api :as fx]
[clojure.core.cache :as cache]))
(def *state
(atom
(fx/create-context
{:id->potion {0 {:id 0
:name "Antidote"
:ingredient-ids [0 1]}
1 {:id 1
:name "Explosive cocktail"
:ingredient-ids [2 3 4]}
2 {:id 2
:name "Petrificoffee"
:ingredient-ids [1 5]}
3 {:id 3
:name "Viscositea"
:ingredient-ids [0 4]}}
:id->ingredient {0 {:id 0
:name "Applebloom"}
1 {:id 1
:name "Dragonwort"}
2 {:id 2
:name "Ruby"}
3 {:id 3
:name "Fireberries"}
4 {:id 4
:name "Sulfur"}
5 {:id 5
:name "Web"}}}
cache/lru-cache-factory)))
(defn sub-potion-ids [context]
(sort (keys (fx/sub-val context :id->potion))))
(defn sub-ingredient-ids [context]
(sort (keys (fx/sub-val context :id->ingredient))))
(defmulti event-handler :event/type)
(defmethod event-handler ::remove-ingredient [{:keys [potion-id ingredient-id]}]
(swap! *state
fx/swap-context
update-in
[:id->potion potion-id :ingredient-ids]
#(vec (remove #{ingredient-id} %))))
(defn close-icon [{:keys [on-remove]}]
{:fx/type :stack-pane
:on-mouse-clicked on-remove
:children
[{:fx/type :svg-path
:fill :gray
:scale-x 0.75
:scale-y 0.75
:content (str "M15.898,4.045c-0.271-0.272-0.713-0.272-0.986,0l-4.71,4.711L5.493,"
"4.045c-0.272-0.272-0.714-0.272-0.986,0s-0.272,0.714,0,0.986l4.709,"
"4.711l-4.71,4.711c-0.272,0.271-0.272,0.713,0,0.986c0.136,0.136,0.314,"
"0.203,0.492,0.203c0.179,0,0.357-0.067,0.493-0.203l4.711-4.711l4.71,"
"4.711c0.137,0.136,0.314,0.203,0.494,0.203c0.178,0,0.355-0.067,"
"0.492-0.203c0.273-0.273,0.273-0.715,0-0.986l-4.711-4.711l4.711-4."
"711C16.172,4.759,16.172,4.317,15.898,4.045z")}]})
(defn sub-ingredient-id->potion-ids-map [context]
(->> (fx/sub-val context :id->potion)
vals
(mapcat (fn [potion]
(map (fn [ingredient-id]
[(:id potion) ingredient-id])
(:ingredient-ids potion))))
(group-by second)
(map (juxt key
#(->> % val (map first) sort)))
(into {})))
(defn sub-ingredient-id->potion-ids [context id]
(get (fx/sub-ctx context sub-ingredient-id->potion-ids-map) id))
(defn section-title [{:keys [text]}]
{:fx/type :label
:style {:-fx-font [:bold 20 :sans-serif]}
:text text})
(defn item-title [{:keys [text]}]
{:fx/type :label
:style {:-fx-font [15 :sans-serif]}
:text text})
(defn small-label [{:keys [text]}]
{:fx/type :label
:style {:-fx-font [12 :sans-serif]
:-fx-text-fill :grey}
:text text})
(defn badge [{:keys [text on-remove]}]
{:fx/type :h-box
:alignment :center
:spacing 2
:style {:-fx-font [12 :sans-serif]
:-fx-text-fill :grey
:-fx-border-width 1
:-fx-border-style :solid
:-fx-border-color :lightgray
:-fx-border-radius 4
:-fx-padding [0 2 0 2]}
:children [{:fx/type :label :text text}
{:fx/type close-icon
:on-remove on-remove}]})
(defn ingredient-badge [{:keys [fx/context id potion-id]}]
{:fx/type badge
:text (fx/sub-val context get-in [:id->ingredient id :name])
:on-remove {:event/type ::remove-ingredient
:potion-id potion-id
:ingredient-id id}})
(defn potion-view [{:keys [fx/context id]}]
(let [potion (fx/sub-val context get-in [:id->potion id])]
{:fx/type :v-box
:children [{:fx/type item-title
:text (:name potion)}
{:fx/type :h-box
:spacing 2
:children (concat
[{:fx/type small-label
:text "needs"}]
(for [ingredient-id (:ingredient-ids potion)]
{:fx/type ingredient-badge
:potion-id id
:id ingredient-id}))}]}))
(defn potion-list [{:keys [fx/context]}]
{:fx/type :v-box
:padding 10
:spacing 10
:children [{:fx/type section-title
:text "Potions"}
{:fx/type :v-box
:spacing 5
:children (for [id (fx/sub-ctx context sub-potion-ids)]
{:fx/type potion-view
:id id})}]})
(defn potion-badge [{:keys [fx/context id ingredient-id]}]
{:fx/type badge
:text (fx/sub-val context get-in [:id->potion id :name])
:on-remove {:event/type ::remove-ingredient
:potion-id id
:ingredient-id ingredient-id}})
(defn ingredient-view [{:keys [fx/context id]}]
(let [potion-ids (fx/sub-ctx context sub-ingredient-id->potion-ids id)]
{:fx/type :v-box
:children [{:fx/type item-title
:text (fx/sub-val context get-in [:id->ingredient id :name])}
{:fx/type :h-box
:spacing 2
:children (if (empty? potion-ids)
[{:fx/type small-label
:text "unused"}]
(concat
[{:fx/type small-label
:text "used in"}]
(for [potion-id potion-ids]
{:fx/type potion-badge
:ingredient-id id
:id potion-id})))}]}))
(defn ingredient-list [{:keys [fx/context]}]
{:fx/type :v-box
:padding 10
:spacing 10
:children [{:fx/type section-title
:text "Ingredients"}
{:fx/type :v-box
:spacing 5
:children (for [id (fx/sub-ctx context sub-ingredient-ids)]
{:fx/type ingredient-view
:id id})}]})
(defn root-view [_]
{:fx/type :stage
:title "Book of potions"
:showing true
:width 600
:height 400
:scene {:fx/type :scene
:root {:fx/type :split-pane
:divider-positions [0.5]
:items [{:fx/type potion-list}
{:fx/type ingredient-list}]}}})
(def renderer
(fx/create-renderer
:middleware (comp
fx/wrap-context-desc
(fx/wrap-map-desc (fn [_] {:fx/type root-view})))
:opts {:fx.opt/type->lifecycle #(or (fx/keyword->lifecycle %)
(fx/fn->lifecycle-with-context %))
:fx.opt/map-event-handler event-handler}))
(fx/mount-renderer *state renderer)