changeset 12:cd8a378414d1

Support mouse button events.
author Mikhail Kryshen <mikhail@kryshen.net>
date Mon, 14 Jun 2010 05:04:03 +0400
parents ea6fc44f19c8
children c6009a144727
files src/indyvon/component.clj src/indyvon/event.clj
diffstat 2 files changed, 27 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/indyvon/component.clj	Mon Jun 14 04:38:00 2010 +0400
+++ b/src/indyvon/component.clj	Mon Jun 14 05:04:03 2010 +0400
@@ -88,5 +88,8 @@
     (defmethod handle-layer-event [layer2 MouseEvent/MOUSE_MOVED]
       [layer context event]
       (println "2 MOVED"))
+    (defmethod handle-layer-event [layer2 MouseEvent/MOUSE_DRAGGED]
+      [layer context event]
+      (println "2 DRAGGED"))
     )
   )
--- a/src/indyvon/event.clj	Mon Jun 14 04:38:00 2010 +0400
+++ b/src/indyvon/event.clj	Mon Jun 14 05:04:03 2010 +0400
@@ -62,7 +62,7 @@
   (filter #(not (some (partial pred %) coll2)) coll1))
 
 (defn- translate-mouse-event
-  [#^MouseEvent event x y id]
+  [event x y id]
   (proxy [MouseEvent] [(.getComponent event)
                        id
                        (.getWhen event)
@@ -89,7 +89,7 @@
 (defn- dispatch-mouse-motion*
   "Dispatches mouse motion events. Returns a new set of contexts which
   currently are under cursor."
-  [hovered context-tree #^MouseEvent event]
+  [hovered context-tree event]
   (let [x (.getX event)
         y (.getY event)
         hovered2 (under-cursor context-tree x y)
@@ -103,10 +103,25 @@
     hovered2))
 
 (defn- dispatch-mouse-motion
-  [hovered-ref context-tree #^MouseEvent event]
+  [hovered-ref context-tree event]
   (dosync
    (alter hovered-ref dispatch-mouse-motion* context-tree event)))
 
+(defn- dispatch-mouse-button*
+  "Dispatches mouse button events. Returns a new set of contexts which
+  currently are picked with a pressed button."
+  [picked hovered context-tree event]
+  (translate-and-dispatch hovered event)
+  (if (= (.getID event) MouseEvent/MOUSE_PRESSED)
+    hovered
+    nil))
+  
+(defn- dispatch-mouse-button
+  [picked-ref hovered-ref context-tree event]
+  (dosync
+   (alter picked-ref dispatch-mouse-button*
+          @hovered-ref context-tree event)))
+
 (defn make-event-dispatcher []
   (let [context-tree-r (ref {}) ; register
         context-tree (ref {})   ; dispatch
@@ -126,13 +141,16 @@
      (picked? [this layer] false)
      (hovered? [this layer] false)
      MouseListener
-     (mouseClicked [this event])
      (mouseEntered [this event]
                    (dispatch-mouse-motion hovered context-tree event))
      (mouseExited [this event]
                   (dispatch-mouse-motion hovered context-tree event))
-     (mousePressed [this event])
-     (mouseReleased [this event])
+     (mouseClicked [this event]
+                   (dispatch-mouse-button picked hovered context-tree event))
+     (mousePressed [this event]
+                   (dispatch-mouse-button picked hovered context-tree event))
+     (mouseReleased [this event]
+                    (dispatch-mouse-button picked hovered context-tree event))
      MouseMotionListener
      (mouseDragged [this event]
                    (translate-and-dispatch @picked event))