001  (ns ^{:doc "Render the state of the world as an HTML table."
002        :author "Simon Brooke"}
003    mw-ui.render-world
004    (:require [mw-engine.core :as engine]
005              [mw-engine.heightmap :as heightmap]
006              [mw-parser.bulk :as compiler]
007              [noir.io :as io]
008              [noir.session :as session]))
009  
010  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
011  ;;;;
012  ;;;; mw-ui: a servlet user/visualisation interface for MicroWorld.
013  ;;;;
014  ;;;; This program is free software; you can redistribute it and/or
015  ;;;; modify it under the terms of the GNU General Public License
016  ;;;; as published by the Free Software Foundation; either version 2
017  ;;;; of the License, or (at your option) any later version.
018  ;;;;
019  ;;;; This program is distributed in the hope that it will be useful,
020  ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
021  ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
022  ;;;; GNU General Public License for more details.
023  ;;;;
024  ;;;; You should have received a copy of the GNU General Public License
025  ;;;; along with this program; if not, write to the Free Software
026  ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
027  ;;;; USA.
028  ;;;;
029  ;;;; Copyright (C) 2014 Simon Brooke
030  ;;;;
031  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
032  
033  
034  (defn format-css-class 
035    "Format this statekey, assumed to be a keyword indicating a state in the
036     world, into a CSS class"
037    [statekey]
038    (subs (str statekey) 1))
039  
040  
041  (defn format-image-path
042    "Render this statekey, assumed to be a keyword indicating a state in the
043     world, into a path which should recover the corresponding image file."
044    [statekey]
045    (format "img/tiles/%s.png" (format-css-class statekey)))
046  
047  
048  (defn format-mouseover [cell]
049    (str cell))
050  
051  
052  (defn render-cell
053    "Render this world cell as a Hiccup table cell."
054    [cell]
055    (let [state (:state cell)]
056      [:td {:class (format-css-class state) :title (format-mouseover cell)}
057       [:a {:href (format "inspect?x=%d&y=%d" (:x cell) (:y cell))}
058        [:img {:alt (:state cell) :src (format-image-path state)}]]]))
059  
060  
061  (defn render-world-row
062    "Render this world row as a Hiccup table row."
063    [row]
064    (apply vector (cons :tr (map render-cell row))))
065  
066  
067  (defn render-world-table
068    "Render the world implied by the current session as a complete HTML table in a DIV."
069    []
070    (let [world (or (session/get :world)
071                    (heightmap/apply-heightmap
072                        (io/get-resource "/img/heightmaps/small_hill.png")))
073          rules (or (session/get :rules)
074                    (do (session/put! :rules
075                                      (compiler/compile-file
076                                        (io/get-resource "/rulesets/basic.txt")))
077                      (session/get :rules)))
078          generation (inc (or (session/get :generation) 0))
079          w2 (engine/transform-world world rules)
080          ]
081      (session/put! :world w2)
082      (session/put! :generation generation)
083      [:div {:class "world"}
084        (apply vector
085                   (cons :table
086                         (map render-world-row w2)))
087        [:p
088         (str "Generation " generation)]]))
089  
090  
091  (defn render-inspector
092    "Render in Hiccup format the HTML content of an inspector on this cell."
093    ([cell _] (render-inspector cell))
094    ([cell]
095    [:table {:class "music-ruled"}
096     [:tr
097      [:td {:colspan 2 :style "text-align: center;"}
098       [:img {:src (str "img/tiles/" (name (:state cell)) ".png")
099              :width 64
100              :height 64}]]]
101     [:tr [:th "Key"][:th "Value"]]
102     (map #(vector :tr (vector :th %)(vector :td (cell %))) (keys cell))]))
103  
104