PageRenderTime 59ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/monome_serial/event_handlers.clj

http://github.com/samaaron/monome-serial
Clojure | 58 lines | 43 code | 15 blank | 0 comment | 4 complexity | d34b6d053298481ca10a471005e8ce0b MD5 | raw file
  1. (ns monome-serial.event-handlers
  2. (:use [clojure.core.incubator :only [dissoc-in]]))
  3. (defn on-action
  4. "Add an event handler function f to monome m within the specified group.
  5. The supplied function should have an arity of 3: action x y
  6. Where action is either :press or :release and x and y are the coords of the button that generated the event
  7. The specified handler group is created if it doesn't previously exist"
  8. ([m f group] (on-action m f group f))
  9. ([m f group name]
  10. (dosync (alter (:handlers m) assoc-in [group name] f))
  11. m))
  12. (defn on-press
  13. "Add an event handler function f to monome m within the specified group. This handler then only handles key press events.
  14. The supplied function should have an arity of 2: x y
  15. Where x and y are the coords of the button that was pressed
  16. The specified handler group is created if it doesn't previously exist"
  17. ([m f group] (on-press m f group f))
  18. ([m f group name]
  19. (on-action m (fn [op x y] (if (= op :press) (apply f [x y]))) group name)
  20. m))
  21. (defn on-release
  22. "Add an event handler function f to monome m within the specified group. This handler only handles key release events.
  23. The supplied function should have an arity of 2: x y
  24. Where x and y are the coords of the button that was released
  25. The specified handler group is created if it doesn't previously exist."
  26. ([m f group] (on-release m f group f))
  27. ([m f group name]
  28. (on-action m (fn [op x y] (if (= op :release) (apply f [x y]))) group name)
  29. m))
  30. (defn remove-handler
  31. "Remove the given handler function belonging to the specified group and having the specified name from monome m."
  32. [m group name]
  33. (dosync (alter (:handlers m) dissoc-in [group name]))
  34. m)
  35. (defn remove-group-handlers
  36. "Remove all handlers from the specified group from monome m."
  37. [m group]
  38. (dosync (alter (:handlers m) dissoc group)))
  39. (defn remove-all-handlers
  40. "Removes all of the given monome's handlers."
  41. [m]
  42. (dosync (ref-set (:handlers m) {}))
  43. m)