vydd / sketch

A Common Lisp framework for the creation of electronic art, visual design, game prototyping, game making, computer graphics, exploration of human-computer interaction, and more.
MIT License
1.39k stars 66 forks source link

Extend `on-leave` and `on-enter` to sketch windows #135

Open swapneils opened 5 months ago

swapneils commented 5 months ago

I was toying around with show-cursor and hide-cursor from cl-sdl2, and found that running 2 different sketches with different settings for this led to them competing for control of cursor visibility.

Presumably SDL2's choice to make these kinds of things global variables means that many other effects have the same issue. Extending on-leave and on-enter to sketch windows could allow tracking of which sketch has mouse-focus, to better filter when to call these methods.

vydd commented 5 months ago

Hey, @swapneils, thank you for the PR! Could you please provide an example sketch describing the demonstrated behavior before, and expected behavior after?

swapneils commented 5 months ago

Here's one example. In the code as written we shouldn't be triggering the on-enter and on-leave methods, since on-hover only has advice for entity instances, and sketch is a direct child of standard-object instead. In this PR the methods activate appropriately.

(defsketch tutorial ((mouse-on nil) (x 100) (y 100))
          (circle 300 100 50)
          (ellipse 200 200 100 50)
          (when mouse-on
              (circle x y 20)))
(defmethod on-enter ((i tutorial))
          (with-slots (mouse-on) i (setf mouse-on t) (sdl2:hide-cursor)) (print "enter"))
(defmethod on-leave ((i tutorial))
          (with-slots (mouse-on) i (setf mouse-on nil) (sdl2:show-cursor)) (print "leave"))