comparison org/world.org @ 26:bbffa41a12a9

moved apply-map to util.org from world.org, fixed some grammar problems, made examples more concise
author Robert McIntyre <rlm@mit.edu>
date Mon, 24 Oct 2011 05:41:50 -0700
parents 775d97247dd0
children 6372c108c5c6
comparison
equal deleted inserted replaced
25:775d97247dd0 26:bbffa41a12a9
191 : ([class]) 191 : ([class])
192 : Takes a class and creates a map of the static constant integer 192 : Takes a class and creates a map of the static constant integer
193 : fields with their names. This helps with C wrappers where they have 193 : fields with their names. This helps with C wrappers where they have
194 : just defined a bunch of integer constants instead of enums 194 : just defined a bunch of integer constants instead of enums
195 195
196 #+begin_src clojure :exports both :results verbatim
197 (take 5 (vals (pokemon.lpsolve/constant-map KeyInput)))
198 #+end_src
199
200 #+results:
201 : ("KEY_ESCAPE" "KEY_1" "KEY_2" "KEY_3" "KEY_4")
202
196 =(all-keys)= converts the constant names like =KEY_J= to the more 203 =(all-keys)= converts the constant names like =KEY_J= to the more
197 clojure-like =key-j=, and returns a map from these keys to 204 clojure-like =key-j=, and returns a map from these keys to
198 jMonkeyEngine =KeyTrigger= objects, which jMonkeyEngine3 uses as it's 205 jMonkeyEngine =KeyTrigger= objects, which jMonkeyEngine3 uses as it's
199 abstraction over the physical keys. =all-keys= also adds the three 206 abstraction over the physical keys. =all-keys= also adds the three
200 mouse button controls to the map. 207 mouse button controls to the map.
201 208
202
203 #+begin_src clojure :exports both :results output 209 #+begin_src clojure :exports both :results output
204 (require 'clojure.contrib.pprint) 210 (require 'clojure.contrib.pprint)
205 (clojure.contrib.pprint/pprint 211 (clojure.contrib.pprint/pprint
206 (take 10 (keys (cortex.world/all-keys)))) 212 (take 6 (cortex.world/all-keys)))
207 #+end_src 213 #+end_src
208 214
209 #+results: 215 #+results:
210 #+begin_example 216 : (["key-n" #<KeyTrigger com.jme3.input.controls.KeyTrigger@9f9fec0>]
211 ("key-n" 217 : ["key-apps" #<KeyTrigger com.jme3.input.controls.KeyTrigger@28edbe7f>]
212 "key-apps" 218 : ["key-pgup" #<KeyTrigger com.jme3.input.controls.KeyTrigger@647fd33a>]
213 "key-pgup" 219 : ["key-f8" #<KeyTrigger com.jme3.input.controls.KeyTrigger@24f97188>]
214 "key-f8" 220 : ["key-o" #<KeyTrigger com.jme3.input.controls.KeyTrigger@685c53ff>]
215 "key-o" 221 : ["key-at" #<KeyTrigger com.jme3.input.controls.KeyTrigger@4c3e2e5f>])
216 "key-at"
217 "key-f9"
218 "key-0"
219 "key-p"
220 "key-subtract")
221 #+end_example
222
223 #+begin_src clojure :exports both :results output
224 (clojure.contrib.pprint/pprint
225 (take 10 (vals (cortex.world/all-keys))))
226 #+end_src
227
228 #+results:
229 #+begin_example
230 (#<KeyTrigger com.jme3.input.controls.KeyTrigger@6ec21e52>
231 #<KeyTrigger com.jme3.input.controls.KeyTrigger@a54d24d>
232 #<KeyTrigger com.jme3.input.controls.KeyTrigger@1ba5e91b>
233 #<KeyTrigger com.jme3.input.controls.KeyTrigger@296af9cb>
234 #<KeyTrigger com.jme3.input.controls.KeyTrigger@2e3593ab>
235 #<KeyTrigger com.jme3.input.controls.KeyTrigger@3f71d740>
236 #<KeyTrigger com.jme3.input.controls.KeyTrigger@4aeacb4a>
237 #<KeyTrigger com.jme3.input.controls.KeyTrigger@7cc88db2>
238 #<KeyTrigger com.jme3.input.controls.KeyTrigger@52cee11e>
239 #<KeyTrigger com.jme3.input.controls.KeyTrigger@c1da30b>)
240 #+end_example
241
242 222
243 223
244 ** World Creation 224 ** World Creation
245 #+srcname: world 225 #+srcname: world
246 #+begin_src clojure :results silent 226 #+begin_src clojure :results silent
349 (.setShowSettings false) 329 (.setShowSettings false)
350 ;; continue running simulation even if the window has lost 330 ;; continue running simulation even if the window has lost
351 ;; focus. 331 ;; focus.
352 (.setPauseOnLostFocus false) 332 (.setPauseOnLostFocus false)
353 (.setSettings *app-settings*)))) 333 (.setSettings *app-settings*))))
354
355 (defn apply-map
356 "Like apply, but works for maps and functions that expect an
357 implicit map and nothing else as in (fn [& {}]).
358 ------- Example -------
359 (defn demo [& {:keys [www] :or {www \"oh yeah\"} :as env}]
360 (println www))
361 (apply-map demo {:www \"hello!\"})
362 -->\"hello\""
363 [fn m]
364 (apply fn (reduce #(into %1 %2) [] m)))
365
366 #+end_src 334 #+end_src
367 335
368 336
369 =(world)= is the most important function here. It presents a more 337 =(world)= is the most important function here. It presents a more
370 functional interface to the Application life-cycle, and all it's 338 functional interface to the Application life-cycle, and all its
371 objects except =root-node= are plain clojure data structures. It's now 339 arguments except =root-node= are plain immutable clojure data
372 possible to extend functionally by composing multiple functions 340 structures. This makes it easier to extend functionally by composing
373 together, and to add more keyboard-driven actions by combining clojure 341 multiple functions together, and to add more keyboard-driven actions
374 maps. 342 by combining clojure maps.
375 343
376 344
377 345
378 * COMMENT code generation 346 * COMMENT code generation
379 #+begin_src clojure :tangle ../src/cortex/world.clj 347 #+begin_src clojure :tangle ../src/cortex/world.clj