Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

property plugins ? #2

Open
clojj opened this issue Dec 23, 2014 · 1 comment
Open

property plugins ? #2

clojj opened this issue Dec 23, 2014 · 1 comment

Comments

@clojj
Copy link

clojj commented Dec 23, 2014

commit fa67cd7
sounds like a new and interesting feature

Could you provide any how-to/example for such a property-plugin ?

@aaronc
Copy link
Owner

aaronc commented Dec 23, 2014

I'm using it for a custom command system like this:

(definterface ICommand
  (getCommand [])
  (getCommandData []))

(defonce ^:private cmd-evt-type (EventType. "command"))

(defonce ^:private cmd-evt-types (atom {}))

(defn- get-event-type [cmd-name]
  (or (get @cmd-evt-types cmd-name)
      (let [evt-type (EventType. cmd-evt-type
                                 (name cmd-name))]
        (swap! cmd-evt-types assoc cmd-name evt-type)
        evt-type)))

(defn dispatch-command
  ([cmd-name scene-or-target]
    (dispatch-command cmd-name scene-or-target nil))
  ([cmd-name scene-or-target data]
    (let [target (if (instance? Scene scene-or-target)
                   (.getFocusOwner scene-or-target)
                   scene-or-target)
          evt-type (get-event-type cmd-name)
          evt
          (proxy [Event ICommand] [scene-or-target target evt-type]
            (getCommand [] cmd-name)
            (getCommandData [] data))]
      (Event/fireEvent target evt))))

(fx-clj.core.extensibility/register-property-plugin!
 "cmd"
 (fn [node cmd-name handler]
   (.addEventHandler node (get-event-type (keyword cmd-name))
                     (util/event-handler [e]
                                         (handler {:target (.getTarget e)
                                                   :node node
                                                   :cmd (.getCommand e)
                                                   :data (.getCommandData e)})))))

Then, I can write something like:

(build [:some-control {:cmd/copy (fn [e] ...}])

and then bind an accelerator (Ctrl-C) on the scene that calls (dispatch-cmd :copy scene)

Make sure you are using 0.2.0-SNAPSHOT or git master until 0.2.0 release (which hopefully I'll have time for soon - all I really need to do is update docs).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants