changeset 4:0771180bf7c2

Abandoned approach at event dispatching.
author Mikhail Kryshen <mikhail@kryshen.net>
date Thu, 10 Jun 2010 02:54:35 +0400
parents 6bc931b1b755
children 74f1f265c3d9
files project.clj src/indyvon/component.clj src/indyvon/core.clj
diffstat 3 files changed, 101 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/project.clj	Tue Jun 08 15:24:45 2010 +0400
+++ b/project.clj	Thu Jun 10 02:54:35 2010 +0400
@@ -2,4 +2,5 @@
   :description "FIXME: write"
   :dependencies [[org.clojure/clojure "1.2.0-master-SNAPSHOT"]
                  [org.clojure/clojure-contrib "1.2.0-SNAPSHOT"]]
-  :dev-dependencies [[leiningen/lein-swank "1.2.0-SNAPSHOT"]])
+  :dev-dependencies [[leiningen/lein-swank "1.2.0-SNAPSHOT"]]
+  :namespaces [indyvon.core])
--- a/src/indyvon/component.clj	Tue Jun 08 15:24:45 2010 +0400
+++ b/src/indyvon/component.clj	Thu Jun 10 02:54:35 2010 +0400
@@ -1,17 +1,54 @@
 ;; (ns indyvon.component
-;;   (:use indyvon.layer)
-;;   (:import (java.awt Graphics Graphics2D))
+;;   (:use indyvon.core)
+;;   (:import (java.awt Graphics Graphics2D Dimension AWTEvent))
 ;;   (:gen-class
-;;    :name indyvon.Component
+;;    :name indyvon.IdvComponent
 ;;    :extends java.awt.Component
-;;    :constructor {[Layer] []}
-;;    :state layer
-;;    :init init-state))
+;;    :constructors {[indyvon.core.Layer] [],
+;;                   [indyvon.core.Layer indyvon.core.EventDispatcher] []}
+;;    :exposes-methods {enableEvents enableEventsSuper}
+;;    :state state
+;;    :init init
+;;    :post-init post-init))
+
+;; (defrecord State [layer event])
+
+;; (defn- -init
+;;   ([layer]
+;;      [[] (State. layer (make-event-dispatcher))])
+;;   ([layer dispatcher]
+;;      [[] (State. layer dispatcher)]))
+
+;; (defn -enableEvents [this mask]
+;;   (.enableEventsSuper this mask))
 
-;; (defn- init-state [layer]
-;;   [[] layer])
+;; (defn- -post-init [this & args]
+;;   (.enableEvents
+;;    this
+;;    ^long (reduce bit-or
+;;                  [AWTEvent/MOUSE_EVENT_MASK
+;;                   AWTEvent/MOUSE_MOTION_EVENT_MASK
+;;                   AWTEvent/MOUSE_WHEEL_EVENT_MASK
+;;                   AWTEvent/KEY_EVENT_MASK])))
+
+;; (defn- make-update-fn [component]
+;;   (fn [] (.repaint component)))
 
-;; (defn -paint [this graphics])
+;; (defn -paint [this g]
+;;   (let [layer (-> this .state :layer)
+;;         size (.getSize this)
+;;         width (.width size)
+;;         height (.height size)]
+;;     (binding [*graphics* g
+;;               *update-fn* (make-update-fn this)]
+;;       (render-layer! layer 0 0 width height false))))
 
-;; (defn -update [this graphics]
-;;      (-paint this graphics))
+;; (defn -update [this g]
+;;      (-paint this g))
+
+;; (defn -processEvent [this event]
+;;   (dispatch (-> this .state :dispatcher) event))
+
+;; (defn -getPreferredSize [this]
+;;   (let [s (-> this .state :layer size)]
+;;     (Dimension. (s 0) (s 1))))
--- a/src/indyvon/core.clj	Tue Jun 08 15:24:45 2010 +0400
+++ b/src/indyvon/core.clj	Thu Jun 10 02:54:35 2010 +0400
@@ -1,5 +1,7 @@
 (ns indyvon.core
-  (:import (java.awt Dimension Point Component Graphics2D AWTEvent)))
+  (:import (java.awt Dimension Point Component Graphics2D AWTEvent)
+           (java.awt.event MouseAdapter MouseMotionAdapter
+                           MouseListener MouseMotionListener)))
 
 (def ^{:private true} *rx* 0)
 (def ^{:private true} *ry* 0)
@@ -64,33 +66,51 @@
   (fn [layer event]
     [layer (.getID event)]))
 
-(defmethod handle-layer-event :default [layer event])
+(defmethod handle-layer-event :default [layer event]
+           false)
 
 (defprotocol EventDispatcher
   (register [this layer])
   (commit [this])
   (dispatch [this event]))
 
+;;(defrecord LayerContextState [hovered contexts])
+
 (defrecord LayerContext [layer rx ry width height update-fn])
 
+;; LayerContext сам реализует EventDispatcher.
+;; Дерево диспетчеров-контекстов.
+;; Передача события от корня.
+
 (defn make-event-dispatcher []
-  (let [contexts-r (ref [])
-        contexts-d (ref [])]
+  (let [contexts-r (ref []) ; register
+        contexts (ref [])   ; dispatch
+        hovered (ref [])
+        picked (ref [])]
     (reify
      EventDispatcher
      (register [this layer]
-      (dosync
-       (alter contexts-r conj
-              (LayerContext. layer *rx* *ry*
-                             *width* *height*
-                             *update-fn*))))
+               (dosync
+                (alter contexts-r conj
+                       (LayerContext. layer *rx* *ry*
+                                      *width* *height*
+                                      *update-fn*))))
      (commit [this]
-      (dosync (ref-set contexts-d @contexts-r)
-              (ref-set contexts-r [])))
+             (dosync (ref-set contexts @contexts-r)
+                     (ref-set contexts-r [])))
      (dispatch [this event]
                (println "dispatch" this event)
                ;; TODO
-               ))))
+               )
+     MouseListener
+     (mouseClicked [this event])
+     (mouseEntered [this event])
+     (mouseExited [this event])
+     (mousePressed [this event])
+     (mouseReleased [this event])
+     MouseMotionListener
+     (mouseDragged [this event])
+     (mouseMoved [this event]))))
 
 ;;
 ;; Connection to AWT.
@@ -121,20 +141,25 @@
   ([layer]
      (make-component layer (make-event-dispatcher)))
   ([layer event-dispatcher]
-     (proxy [Component] []
-       (update [g] (.paint this g))
-       (paint [g]
-              (let [size (.getSize this)
-                    width (.width size)
-                    height (.height size)]
-                (binding [*graphics* g
-                          *update-fn* (make-update-fn this)]
-                  (render-layer! layer 0 0 width height false))))
-       (getPreferredSize []
-                         (let [s (size layer)]
-                           (Dimension. (s 0) (s 1))))
-       (processEvent [event]
-                     (dispatch event-dispatcher event)))))
+     (doto
+         (proxy [Component] []
+           (update [g] (.paint this g))
+           (paint [g]
+                  (let [size (.getSize this)
+                        width (.width size)
+                        height (.height size)]
+                    (binding [*graphics* g
+                              *update-fn* (make-update-fn this)]
+                      (render-layer! layer 0 0 width height false))))
+           (getPreferredSize []
+                             (let [s (size layer)]
+                               (Dimension. (s 0) (s 1))))
+           (processEvent [event]
+                         (dispatch event-dispatcher event)))
+       ;; No way to call protected final evenbleEvents even in gen-class,
+       ;; have to use the following hack:
+       (.addMouseListener (proxy [MouseAdapter] []))
+       (.addMouseMotionListener (proxy [MouseMotionAdapter] [])))))
 
 (comment
   (do