Skip to content

Commit

Permalink
Merge pull request #66 from clojerl/15-focused-function
Browse files Browse the repository at this point in the history
[#15] Implement focused function
  • Loading branch information
jfacorro authored Nov 8, 2021
2 parents 41da924 + 62ab977 commit d07258a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/doodler/core.clje
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,13 @@
vz u]
[vx vy vz]))

;; Window

(defn focused
"Returns `true` if the window has focus, `false` otherwise."
[]
(:focused *sketch*))

;; Keyboard

(def CODED-KEY ::coded-key)
Expand Down Expand Up @@ -1436,7 +1443,7 @@
(erlang/halt 0))

(defn sketch
"Create and start a new visualisation applet. Can be used to create
"Create and start a new visualisation window. Can be used to create
new sketches programmatically. See documentation for `defsketch` for
list of available options."
[& opts]
Expand Down
10 changes: 9 additions & 1 deletion src/doodler/sketch.clje
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@
;; Keys
:keys-pressed #{}

;; Window
:focused true

;; Camera (used for orbit control)
:camera (process-val! [0 0 (/ h 2 (math/tan (/ (math/pi) 6)))
0 0 0
Expand Down Expand Up @@ -203,11 +206,16 @@
[sketch event]
(let [event-key (:event event)]
(case* event-key
:focus-gained
(assoc sketch :focused true)

:focus-lost
;; Clean up the list of keys pressed when
;; the sketch looses focus, since the key release
;; won't be processed by the sketch.
(assoc sketch :keys-pressed #{})
(assoc sketch
:keys-pressed #{}
:focused false)

:mouse-pressed
(assoc sketch
Expand Down

0 comments on commit d07258a

Please sign in to comment.