changeset 75:ddfde9cce39a

EventDispatcher can report hovered and picked states for handles.
author Mikhail Kryshen <mikhail@kryshen.net>
date Mon, 30 Aug 2010 20:44:23 +0400
parents a823dd0c2736
children dafd4ff9d313
files src/net/kryshen/indyvon/core.clj src/net/kryshen/indyvon/demo.clj
diffstat 2 files changed, 60 insertions(+), 29 deletions(-) [+]
line diff
     1.1 --- a/src/net/kryshen/indyvon/core.clj	Mon Aug 30 20:04:21 2010 +0400
     1.2 +++ b/src/net/kryshen/indyvon/core.clj	Mon Aug 30 20:44:23 2010 +0400
     1.3 @@ -90,7 +90,13 @@
     1.4        handlers (an event-id -> handler-fn map). Handle is used to
     1.5        match the contexts between commits.")
     1.6    (commit [this]
     1.7 -     "Apply the registered handlers for event processing."))
     1.8 +     "Apply the registered handlers for event processing.")
     1.9 +  (handle-picked? [this handle]
    1.10 +     "Returns true if the specified handle received the :mouse-pressed
    1.11 +      event and have not yet received :moused-released.")
    1.12 +  (handle-hovered? [this handle]
    1.13 +     "Returns true if the specified handle received the :mouse-entered
    1.14 +      event and have not yet received :mouse-exited."))
    1.15  
    1.16  (defprotocol Anchored
    1.17    "Provide anchor point for Layers. Used by viewport."
    1.18 @@ -245,26 +251,6 @@
    1.19    [x y w h & body]
    1.20    `(with-bounds* ~x ~y ~w ~h (fn [] ~@body)))
    1.21  
    1.22 -(defn with-handlers*
    1.23 -  [handle handlers f & args]
    1.24 -  (binding
    1.25 -      [*event-dispatcher* (create-dispatcher
    1.26 -                           *event-dispatcher* handle handlers)]
    1.27 -    (apply f args)))
    1.28 -
    1.29 -(defmacro with-handlers
    1.30 -  "specs => (:event-id name & handler-body)*
    1.31 -
    1.32 -  Execute form with the specified event handlers."
    1.33 -  [handle form & specs]
    1.34 -  `(with-handlers* ~handle
    1.35 -     ~(reduce (fn [m spec]
    1.36 -                (assoc m (first spec)
    1.37 -                       `(fn [~(second spec)]
    1.38 -                          ~@(nnext spec)))) {}
    1.39 -                          specs)
    1.40 -     (fn [] ~form)))
    1.41 -
    1.42  (defmacro with-theme
    1.43    [theme & body]
    1.44    `(binding [*theme* (merge *theme* ~theme)]
    1.45 @@ -374,6 +360,37 @@
    1.46         (layer-size layer))))
    1.47  
    1.48  ;;
    1.49 +;; Event handling.
    1.50 +;;
    1.51 +
    1.52 +(defn with-handlers*
    1.53 +  [handle handlers f & args]
    1.54 +  (binding
    1.55 +      [*event-dispatcher* (create-dispatcher
    1.56 +                           *event-dispatcher* handle handlers)]
    1.57 +    (apply f args)))
    1.58 +
    1.59 +(defmacro with-handlers
    1.60 +  "specs => (:event-id name & handler-body)*
    1.61 +
    1.62 +  Execute form with the specified event handlers."
    1.63 +  [handle form & specs]
    1.64 +  `(with-handlers* ~handle
    1.65 +     ~(reduce (fn [m spec]
    1.66 +                (assoc m (first spec)
    1.67 +                       `(fn [~(second spec)]
    1.68 +                          ~@(nnext spec)))) {}
    1.69 +                          specs)
    1.70 +     (fn [] ~form)))
    1.71 +
    1.72 +(defn picked? [handle]
    1.73 +  (handle-picked? *event-dispatcher* handle))
    1.74 +
    1.75 +(defn hovered? [handle]
    1.76 +  (handle-hovered? *event-dispatcher* handle))
    1.77 +
    1.78 +
    1.79 +;;
    1.80  ;; EventDispatcher implementation
    1.81  ;;
    1.82  
    1.83 @@ -391,7 +408,9 @@
    1.84        EventDispatcher
    1.85        (listen! [this component])
    1.86        (create-dispatcher [this handle handlers] this)
    1.87 -      (commit [this])))
    1.88 +      (commit [this])
    1.89 +      (handle-picked? [this handle])
    1.90 +      (handle-hovered? [this handle])))
    1.91  
    1.92  (defrecord DispatcherNode [handle handlers parent
    1.93                             ^Shape clip ^AffineTransform transform
    1.94 @@ -402,7 +421,11 @@
    1.95    (create-dispatcher [this handle handlers]
    1.96       (create-dispatcher parent handle handlers))
    1.97    (commit [this]
    1.98 -     (commit parent)))
    1.99 +     (commit parent))
   1.100 +  (handle-picked? [this handle]
   1.101 +     (handle-picked? parent handle))
   1.102 +  (handle-hovered? [this handle]
   1.103 +     (handle-hovered? parent handle)))
   1.104  
   1.105  (defn- make-node [handle handlers]
   1.106    (DispatcherNode. handle handlers *event-dispatcher* *clip*
   1.107 @@ -492,6 +515,10 @@
   1.108       (commit [this]
   1.109          (dosync (ref-set tree @tree-r)
   1.110                  (ref-set tree-r {})))
   1.111 +     (handle-picked? [this handle]
   1.112 +        (some #(= handle (:handle %)) @picked))
   1.113 +     (handle-hovered? [this handle]
   1.114 +        (some #(= handle (:handle %)) @hovered))
   1.115       MouseListener
   1.116       (mouseEntered [this event]
   1.117          (dispatch-mouse-motion hovered @tree this event))
     2.1 --- a/src/net/kryshen/indyvon/demo.clj	Mon Aug 30 20:04:21 2010 +0400
     2.2 +++ b/src/net/kryshen/indyvon/demo.clj	Mon Aug 30 20:44:23 2010 +0400
     2.3 @@ -31,12 +31,16 @@
     2.4     Layer
     2.5     (render! [layer]
     2.6        (with-handlers layer
     2.7 -        (doto *graphics*
     2.8 -          (.setColor (rand-nth [Color/RED Color/ORANGE]))
     2.9 -          (.fillRect 0 0 *width* *height*))
    2.10 -        (:mouse-entered e (println e))
    2.11 -        (:mouse-exited e (println e))
    2.12 -        (:mouse-moved e (println e))))
    2.13 +        (with-color (if (hovered? layer) Color/ORANGE Color/RED)
    2.14 +          (.fillRect *graphics* 0 0 *width* *height*))
    2.15 +        (:mouse-entered e
    2.16 +         (repaint)
    2.17 +         (println e))
    2.18 +        (:mouse-exited e
    2.19 +         (repaint)
    2.20 +         (println e))
    2.21 +        (:mouse-moved e
    2.22 +         (println e))))
    2.23     (layer-size [layer]
    2.24        (Size. 30 20))))
    2.25