-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.clj
56 lines (51 loc) · 2.08 KB
/
window.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
(ns strigui.window
(:require [clojure2d.core :as c2d])
(:import [javax.swing JFrame]))
(def context (atom {:canvas nil :window nil}))
(defn init-window
"Creates a window based on the canvas size. Alternatively, also accepts an already
existing window."
([^clojure2d.core.Window window]
(let [title (:window-name window)
window (assoc window :window-name "main-window")]
;; change the title of the underlying frame, so it doesn't mess with the
;; window name used for the clojure2d events
(.setTitle (:frame window) title)
(swap! context merge {:canvas (c2d/get-canvas window) :window window})))
([width height ^String title] (init-window width height title 10 :mid))
([width height ^String title fps quality]
(let [canvas (c2d/canvas width height quality)
window (c2d/show-window canvas "main-window" width height fps)
new-context {:canvas canvas
:window window}]
;; change the title of the underlying frame, so it doesn't mess with the
;; window name used for the clojure2d events
(.setTitle (:frame window) title)
(.setDefaultCloseOperation (:frame window) (. JFrame EXIT_ON_CLOSE))
(swap! context merge new-context)
new-context)))
(defn close-window
[^clojure2d.core.Window window]
(c2d/close-window window))
(defn display-info [context text]
(let [canvas (:canvas context)
height (c2d/height canvas)
width (c2d/width canvas)
[_ _ _ h] (c2d/with-canvas-> canvas
(c2d/text-bounding-box text))]
(c2d/with-canvas-> canvas
(c2d/set-color :white)
(c2d/rect 0 (- height h) width h)
(c2d/set-font-attributes 15)
(c2d/set-color :black)
(c2d/text (str text) 0 height))))
(defn within?
"Checks wheter the point (x y) is within the given coord
coord - vector [x-coord y-coord width height]
x - x-coord of point to check
y - y-coord of point to check"
[coord x y]
(and (>= x (first coord))
(>= y (nth coord 1))
(<= x (+ (first coord) (nth coord 2)))
(<= y (+ (nth coord 1) (nth coord 3)))))