(ns mw-engine.render "Render a world as HTML. Adapted (simplified) from mw-ui.render-world; this is for visualisation, not interaction." ;; TODO: but possibly it would be better if there is to be a newer version of ;; mw-ui, to base it on this. (:require [hiccup2.core :refer [html]]) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; ;;;; This program is free software; you can redistribute it and/or ;;;; modify it under the terms of the GNU General Public License ;;;; as published by the Free Software Foundation; either version 2 ;;;; of the License, or (at your option) any later version. ;;;; ;;;; This program is distributed in the hope that it will be useful, ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;;; GNU General Public License for more details. ;;;; ;;;; You should have received a copy of the GNU General Public License ;;;; along with this program; if not, write to the Free Software ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, ;;;; USA. ;;;; ;;;; Copyright (C) 2024 Simon Brooke ;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (def ^:dynamic *state-images-relative-path* "img/tiles/") (defn format-css-class "Format this statekey, assumed to be a keyword indicating a state in the world, into a CSS class" [statekey] (name statekey)) (defn format-image-path "Render this statekey, assumed to be a keyword indicating a state in the world, into a path which should recover the corresponding image file." [statekey] (format "%s%s.png" *state-images-relative-path* (format-css-class statekey))) (defn format-mouseover [cell] (str cell)) (defn render-cell "Render this world cell as a Hiccup table cell." [cell] (let [state (:state cell)] [:td {:class (format-css-class state) :title (format-mouseover cell)} [:img {:alt (:state cell) :src (format-image-path state)}]])) (defn render-world-row "Render this world row as a Hiccup table row." [row] (apply vector (cons :tr (map render-cell row)))) (defn render-world-table "Render this `world` as a complete HTML table in a DIV. If `state-images-relative-path` is passed, use that to override the default path." ([world] [:div {:class "world"} (apply vector (cons :table (map render-world-row world))) [:p (str "Generation " (:generation (first (flatten world))))]]) ([world state-images-relative-path] (binding [*state-images-relative-path* state-images-relative-path] (render-world-table world)))) (defn render-world-page ([world] [:html [:head [:title "Rendered world"] [:style "div.world table, div.world table tr td { padding: 0; margin: 0; border-collapse: collapse; border: none;}"]] [:body (render-world-table world)]]) ([world state-images-relative-path] (binding [*state-images-relative-path* state-images-relative-path] (render-world-page world)))) (defn world->html-file ([world output-path] (spit output-path (str (html (render-world-page world))))) ([world output-path state-images-relative-path] (binding [*state-images-relative-path* state-images-relative-path] (world->html-file world output-path))))