Last active
April 14, 2021 11:43
-
-
Save saikyun/f13292f2560646d83ba3610d8ef123a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# two kinds of renders | |
(var mouse-stolen false) | |
(defn mouse-button-down? | |
[k] | |
(if ((dyn :context) :captured-mouse) | |
true | |
(and (not mouse-stolen) | |
(raylib/mouse-button-down? k)))) | |
# regular function renders | |
(fn [] | |
(when (mouse-button-down? 0) | |
(print "clicked")) | |
(draw-rectangle-rec [0 0 100 100] :blue)) | |
# function renders with context (env?) | |
(let [rec @[0 0 10 10]] | |
@{:context @{:capture-mouse rec} | |
:render (fn [] | |
(when (mouse-button-down? 0) | |
(print "hit me")) | |
(draw-rectangle-rec rec :green))}) | |
(defn render-all | |
[fs] | |
## reset mouse clicked | |
(put mouse-stolen false) | |
# loop through all things that might capture mouse | |
(loop [{:context context} :in (reverse (filter table? fs))] | |
(when-let [r (context :capture-mouse)] | |
(when (and (not mouse-stolen) | |
(in-rec? (get-mouse-position) r)) # check if mouse gets captured | |
(put context :captured-mouse true) | |
(put mouse-stolen true)))) | |
# render all the things | |
(loop [f :in fs] | |
(if (table? f) | |
(with-dyns [:context (f :context)] | |
((f :render))) | |
(f)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment