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.4k stars 67 forks source link

Revise input-handling interface. #156

Closed Kevinpgalligan closed 8 months ago

Kevinpgalligan commented 8 months ago

There's now a tree of 'on-mouse-' functions, the user can hook into them at any point. 'on-mouse-button' propagates to 'on-mouse-left' which propagates to 'on-mouse-left-up' and 'on-mouse-left-down'. Same for 'on-mouse-middle' and 'on-mouse-right'.

Keeps 'on-click', since that's a simple and common case.

Also changes the 'state' parameter of 'on-key' so that it can have values ':up' or ':down', to be consistent with the mouse interface.

Testing

Basically added print statements to all the methods and made sure they were called. (Except the middle-mouse ones, since I'm testing with a trackpad, ha).

(defsketch mouse () (circle 10 10 10) (stop-loop))
(defmethod on-mouse-button :before ((i mouse) button state x y)
  (format t "on-mouse-button: ~a ~a ~a ~a~%" button state x y))
(defmethod on-mouse-left :before ((i mouse) state x y)
  (format t "on-mouse-left: ~a ~a ~a~%" state x y))
(defmethod on-mouse-right :before ((i mouse) state x y)
  (format t "on-mouse-right: ~a ~a ~a~%" state x y))
(defmethod on-mouse-left-down :before ((i mouse) x y)
  (format t "on-mouse-left-down: ~a ~a~%" x y))
(defmethod on-mouse-left-up :before ((i mouse) x y)
  (format t "on-mouse-left-up: ~a ~a~%" x y))
(defmethod on-mouse-left-down :before ((i mouse) x y)
  (format t "on-mouse-left-down: ~a ~a~%" x y))
(defmethod on-mouse-right-down :before ((i mouse) x y)
  (format t "on-mouse-right-down: ~a ~a~%" x y))
(defmethod on-mouse-right-up :before ((i mouse) x y)
  (format t "on-mouse-right-up: ~a ~a~%" x y))
(defmethod on-click :before ((i mouse) x y)
  (format t "on-click: ~a ~a~%" x y))
(defmethod on-key :before ((i mouse) key state)
  (format t "on-key: ~a ~a~%" key state))

The output:

;; Pressed left mouse
on-mouse-button: LEFT DOWN 179 110
on-mouse-left: DOWN 179 110
on-mouse-left-down: 179 110
;; Released left mouse
on-mouse-button: LEFT UP 178 110
on-mouse-left: UP 178 110
on-mouse-left-up: 178 110
on-click: 178 110
;; Pressed right mouse
on-mouse-button: RIGHT DOWN 139 40
on-mouse-right: DOWN 139 40
on-mouse-right-down: 139 40
;; Released right mouse
on-mouse-button: RIGHT UP 139 39
on-mouse-right: UP 139 39
on-mouse-right-up: 139 39
;; And on-key
on-key: S DOWN
on-key: S UP
on-key: SPACE DOWN
on-key: SPACE UP