001  (ns ^{:doc "Simple functions to allow a world to be visualised."
002        :author "Simon Brooke"}
003    mw-engine.display)
004  
005  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
006  ;;;;
007  ;;;; mw-engine: the state/transition engine of MicroWorld.
008  ;;;;
009  ;;;; This program is free software; you can redistribute it and/or
010  ;;;; modify it under the terms of the GNU General Public License
011  ;;;; as published by the Free Software Foundation; either version 2
012  ;;;; of the License, or (at your option) any later version.
013  ;;;;
014  ;;;; This program is distributed in the hope that it will be useful,
015  ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
016  ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017  ;;;; GNU General Public License for more details.
018  ;;;;
019  ;;;; You should have received a copy of the GNU General Public License
020  ;;;; along with this program; if not, write to the Free Software
021  ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022  ;;;; USA.
023  ;;;;
024  ;;;; Copyright (C) 2014 Simon Brooke
025  ;;;;
026  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
027  
028  (def ^:dynamic *image-base*
029    "Base url (i.e., url of directory) from which to load tile images."
030    "img/tiles")
031  
032  (defn format-css-class
033    "Format this `state`, assumed to be a keyword indicating a state in the
034     world, into a CSS class"
035    [state]
036    (subs (str state) 1))
037  
038  (defn format-image-path
039    "Render this `state`, assumed to be a keyword indicating a state in the
040     world, into a path which should recover the corresponding image file."
041    [state]
042    (format "%s/%s.png" *image-base* (format-css-class state)))
043  
044  (defn format-mouseover [cell]
045    (str cell))
046  
047  (defn render-cell
048    "Render this world cell as a Hiccup table cell."
049    [cell]
050    (let [state (:state cell)]
051      [:td {:class (format-css-class state) :title (format-mouseover cell)}
052       [:a {:href (format "inspect?x=%d&y=%d" (:x cell) (:y cell))}
053        [:img {:alt (:state cell) :width 32 :height 32 :src (format-image-path state)}]]]))
054  
055  (defn render-world-row
056    "Render this world `row` as a Hiccup table row."
057    [row]
058    (apply vector (cons :tr (map render-cell row))))
059  
060  (defn render-world-table
061    "Render this `world` as a Hiccup table."
062    [world]
063    (apply vector
064      (cons :table
065        (map render-world-row world))))