Polymorphically isolate quil.

This commit is contained in:
Robert C. Martin 2022-01-21 15:58:46 -06:00
parent 25b645849d
commit 0ce8dae44e
4 changed files with 91 additions and 51 deletions

View File

@ -1,13 +1,13 @@
(ns more-speech.ui.application (ns more-speech.ui.application
(:require [quil.core :as q] (:require [more-speech.ui.text :as text]
[more-speech.ui.text :as text]
[more-speech.ui.widget :refer [widget [more-speech.ui.widget :refer [widget
draw-widget draw-widget
draw-child-widgets]] draw-child-widgets]]
[more-speech.ui.article-window :refer [map->article-window [more-speech.ui.article-window :refer [map->article-window
draw-article-window]] draw-article-window]]
[more-speech.ui.author-window :refer [map->author-window [more-speech.ui.author-window :refer [map->author-window
draw-author-window]])) draw-author-window]]
[more-speech.ui.graphics :as g]))
(defrecord application [widgets] (defrecord application [widgets]
widget widget
@ -15,8 +15,6 @@
(update-widget [widget state]) (update-widget [widget state])
(draw-widget [application state] (draw-widget [application state]
;(draw-widget (:article-window application) state)
;(draw-widget (:author-window application) state)
(draw-child-widgets application state) (draw-child-widgets application state)
) )
@ -25,19 +23,21 @@
) )
(defn make-application [bold regular] (defn make-application [bold regular]
(let [g (g/->quil-graphics)]
(map->application (map->application
{:articles [] {:graphics g
:articles []
:nicknames {} :nicknames {}
:article-window (map->article-window :article-window (map->article-window
{:x 50 :y 10 :w (text/pos-width 105) :h (- (q/screen-height) 100) {:x 50 :y 10 :w (text/pos-width 105) :h (- (g/screen-height g) 100)
:fonts {:bold bold :regular regular} :fonts {:bold bold :regular regular}
}) })
:author-window (map->author-window :author-window (map->author-window
{:x (+ 50 (text/pos-width 110)) :y 10 {:x (+ 50 (text/pos-width 110)) :y 10
:w (text/pos-width 30) :h (- (q/screen-height) 100) :w (text/pos-width 30) :h (- (g/screen-height g) 100)
:fonts {:bold bold :regular regular}}) :fonts {:bold bold :regular regular}})
:widgets [:article-window :author-window] :widgets [:article-window :author-window]
} }
)) )))

View File

@ -1,9 +1,9 @@
(ns more-speech.ui.article-window (ns more-speech.ui.article-window
(:require (:require
[quil.core :as q]
[more-speech.ui.text :as text] [more-speech.ui.text :as text]
[more-speech.article :as a] [more-speech.article :as a]
[more-speech.ui.widget :refer [widget]])) [more-speech.ui.widget :refer [widget]]
[more-speech.ui.graphics :as g]))
(declare draw-article-window) (declare draw-article-window)
@ -17,26 +17,28 @@
(mouse-down [widget state position]) (mouse-down [widget state position])
) )
(defn draw-article [window cursor article] (defn draw-article [g window cursor article]
(q/text-align :left) (g/text-align g [:left])
(q/fill 0 0 0) (g/fill g [0 0 0])
(text/render cursor window (a/markup-article article)) (text/render cursor window (a/markup-article article))
) )
(defn draw-articles [application {:keys [fonts] :as window}] (defn draw-articles [g application {:keys [fonts] :as window}]
(loop [cursor (text/->cursor 0 (text/line-height) 5) (loop [cursor (text/->cursor 0 (text/line-height) 5)
articles (take 20 (:articles application))] articles (take 20 (:articles application))]
(if (empty? articles) (if (empty? articles)
cursor cursor
(recur (draw-article window cursor (first articles)) (recur (draw-article g window cursor (first articles))
(rest articles))))) (rest articles)))))
(defn draw-article-window [application window] (defn draw-article-window [application window]
(q/with-translation (let [g (:graphics application)]
[(:x window) (:y window)] (g/with-translation
(q/stroke 0 0 0) g [(:x window) (:y window)]
(q/stroke-weight 2) (fn [g]
(q/fill 255 255 255) (g/stroke g [0 0 0])
(q/rect 0 0 (:w window) (:h window)) (g/stroke-weight g 2)
(draw-articles application window) (g/fill g [255 255 255])
)) (g/rect g [0 0 (:w window) (:h window)])
(draw-articles g application window))
)))

View File

@ -1,10 +1,10 @@
(ns more-speech.ui.author-window (ns more-speech.ui.author-window
(:require (:require
[quil.core :as q]
[clojure.string :as string] [clojure.string :as string]
[more-speech.ui.widget :refer [widget]] [more-speech.ui.widget :refer [widget]]
[more-speech.ui.text :as text] [more-speech.ui.text :as text]
[more-speech.article :as a])) [more-speech.article :as a]
[more-speech.ui.graphics :as g]))
(declare draw-author-window) (declare draw-author-window)
@ -18,28 +18,28 @@
(mouse-down [widget state position]) (mouse-down [widget state position])
) )
(defn draw-author [window cursor author] (defn draw-author [g window cursor author]
(q/text-align :left) (g/text-align g [:left])
(q/fill 0 0 0) (g/text-color g [0 0 0])
(text/render cursor window (a/markup-author author))) (text/render cursor window (a/markup-author author)))
(defn draw-authors [application window] (defn draw-authors [g application window]
(q/text-align :left) (g/text-align g [:left])
(q/fill 0 0 0) (g/text-color g [0 0 0])
(loop [cursor (text/->cursor 0 (text/line-height) 5) (loop [cursor (text/->cursor 0 (text/line-height) 5)
authors (take-last 60 (sort-by #(string/lower-case (text/nil->blank (second %))) (:nicknames application)))] authors (take-last 60 (sort-by #(string/lower-case (text/nil->blank (second %))) (:nicknames application)))]
(if (empty? authors) (if (empty? authors)
cursor cursor
(recur (draw-author window cursor (first authors)) (recur (draw-author g window cursor (first authors))
(rest authors)))) (rest authors))))
) )
(defn draw-author-window [application window] (defn draw-author-window [application window]
(q/with-translation (let [g (:graphics application)]
[(:x window) (:y window)] (g/with-translation
(q/stroke 0 0 0) g [(:x window) (:y window)]
(q/stroke-weight 2) (fn [g] (g/stroke g [0 0 0])
(q/fill 255 255 255) (g/stroke-weight g 2)
(q/rect 0 0 (:w window) (:h window)) (g/fill g [255 255 255])
(draw-authors application window) (g/rect g [0 0 (:w window) (:h window)])
)) (draw-authors g application window)))))

View File

@ -0,0 +1,38 @@
(ns more-speech.ui.graphics
(:require
[quil.core :as q]
))
(defprotocol graphics
(screen-height [graphics])
(screen-width [graphics])
(text-align [graphics alignment])
(text-color [graphics color])
(stroke [graphics color])
(stroke-weight [graphics weight])
(fill [graphics color])
(rect [graphics rect])
(with-translation [graphics translation f])
)
(defrecord quil-graphics []
graphics
(screen-height [graphics]
(q/screen-height))
(screen-width [graphics]
(q/screen-width))
(text-align [graphics alignment]
(apply q/text-align alignment))
(text-color [graphics color]
(apply q/fill color))
(stroke [graphics color]
(apply q/stroke color))
(stroke-weight [graphics weight]
(q/stroke-weight weight))
(fill [graphics color]
(apply q/fill color))
(rect [graphics rect]
(apply q/rect rect))
(with-translation [graphics translation f]
(q/with-translation translation (f graphics)))
)