From 8f9fa081570b1403972bf8ce28d987e2746c000c Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Tue, 1 Jul 2014 20:21:38 +0100 Subject: [PATCH] My monster, she lives! --- resources/public/css/states.css | 27 +++++++++++++++++ src/mw_ui/render_world.clj | 54 +++++++++++++++++++++++++++++++++ src/mw_ui/routes/home.clj | 6 ++-- 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 resources/public/css/states.css create mode 100644 src/mw_ui/render_world.clj diff --git a/resources/public/css/states.css b/resources/public/css/states.css new file mode 100644 index 0000000..02dfd23 --- /dev/null +++ b/resources/public/css/states.css @@ -0,0 +1,27 @@ +td.waste { + background-color: gray; +} + +td.pasture { + background-color: lime; +} + +td.scrub { + background-color: chartreuse; +} + +td.scrub2 { + background-color: lime; +} + +td.forest { + background-color: forestgreen; +} + +td.climax { + background-color: olive; +} + +td.fire { + background-color: orange; +} diff --git a/src/mw_ui/render_world.clj b/src/mw_ui/render_world.clj new file mode 100644 index 0000000..9c5328f --- /dev/null +++ b/src/mw_ui/render_world.clj @@ -0,0 +1,54 @@ +(ns mw-ui.render-world + (:require [mw-engine.core :as engine] + [mw-engine.world :as world] + [mw-engine.natural-rules :as rules] + [hiccup.core :refer [html]] + [noir.session :as session])) + + +(defn format-css-class [statekey] + "Format this statekey, assumed to be a keyword indicating a state in the + world, into a CSS class" + (subs (str statekey) 1)) + +(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 "img/tiles/%s.png" (format-css-class statekey))) + +(defn render-cell + "Render this world cell as a Hiccup table cell." + [cell] + (let [state (:state cell)] + [:td {:class (format-css-class state)} + [:img {:alt (world/format-cell cell) :img (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 + "Render this world as a complete HTML page." + [] + (let [world (or (session/get :world) (world/make-world 20 20)) + rules (or (session/get :rules) rules/natural-rules) + w2 (engine/transform-world world rules)] + (session/put! :world w2) + (html + [:html + [:head + [:title "MicroWorld demo"] + [:link {:media "only screen and (max-device-width: 480px)" :href "css/phone.css" :type "text/css" :rel "stylesheet"}] + [:link {:media "only screen and (min-device-width: 481px) and (max-device-width: 1024px)" :href "css/tablet.css" :type "text/css" :rel "stylesheet"}] + [:link {:media "screen and (min-device-width: 1025px)" :href "css/standard.css" :type "text/css" :rel "stylesheet"}] + [:link {:media "print" :href "css/print.css" :type "text/css" :rel "stylesheet"}] + [:link {:href "css/states.css" :type "text/css" :rel "stylesheet"}] + [:meta {:http-equiv "refresh" :content "5"}]] + [:body + [:h1 "MicroWorld"] + (apply vector + (cons :table + (map render-world-row w2)))]]))) + \ No newline at end of file diff --git a/src/mw_ui/routes/home.clj b/src/mw_ui/routes/home.clj index e910032..93288c5 100644 --- a/src/mw_ui/routes/home.clj +++ b/src/mw_ui/routes/home.clj @@ -1,7 +1,8 @@ (ns mw-ui.routes.home (:use compojure.core) (:require [mw-ui.layout :as layout] - [mw-ui.util :as util])) + [mw-ui.util :as util] + [mw-ui.render-world :as world])) (defn home-page [] (layout/render @@ -12,4 +13,5 @@ (defroutes home-routes (GET "/" [] (home-page)) - (GET "/about" [] (about-page))) + (GET "/about" [] (about-page)) + (GET "/world" [] (world/render-world)))