Permalink
Browse files

Making stuff happen...

P.S.: This is the worst commit message I ever written!
  • Loading branch information...
1 parent 75da907 commit b532e2ca0d9178a8ea8962e5c4b6623426679ca0 @budu committed Mar 31, 2012
Showing with 61 additions and 30 deletions.
  1. +10 −21 src/clj/board/core.clj
  2. +51 −9 src/cljs/board/core.cljs
View
@@ -1,15 +1,20 @@
(ns board.core
- (:use (lamina [core :only [enqueue]])
+ (:use (lamina [core :exclude [restart]])
aleph.http
(ring.middleware resource file-info)
(hiccup core page)))
+(def board-channel (named-channel :board))
+
+(defn board-handler [channel]
+ (siphon channel board-channel)
+ (siphon board-channel channel))
+
(defn page []
(html5
[:head]
[:body
- [:canvas#board {:width 600 :height 400}
- "Use a real brower please!"]
+ [:canvas#board {:width 1000 :height 800}]
(include-js "/js/app.js")]))
(defn sync-app [request]
@@ -24,23 +29,7 @@
(defn app [channel request]
(if (:websocket request)
- nil ;; TODO: fun stuff
+ (board-handler channel)
(enqueue channel (wrapped-sync-app request))))
-(defonce stopper (atom nil))
-
-(defn start []
- (when-not @stopper
- (reset! stopper
- (start-http-server app
- {:port 8080
- :websocket true}))))
-
-(defn stop []
- (when @stopper
- (@stopper)
- (reset! stopper nil)))
-
-(defn restart []
- (stop)
- (start))
+(start-http-server app {:port 8080 :websocket true})
View
@@ -1,13 +1,55 @@
(ns board.core
- (:require [clojure.browser.event :as event]
- [clojure.browser.dom :as dom]))
+ (:require [goog.events :as events]))
-(defn log [obj]
- (.log js/console obj))
+(def socket (new js/window.WebSocket "ws://localhost:8080"))
-(defn draw []
- (when-let [board (dom/get-element :board)]
- (doto (.getContext board "2d")
- (.fillRect 10 10 100 100))))
+(defn $ [selector]
+ (.querySelector js/document (name selector)))
-(draw)
+(defn listen [element event callback]
+ (events/listen element (name event) callback))
+
+(def board ($ :#board))
+
+(defn circ [ctx x y d]
+ (doto ctx
+ .beginPath
+ (.arc x y d 0 (* 2 Math/PI))
+ .closePath
+ .fill))
+
+(def buttons
+ {goog.events.BrowserEvent.MouseButton/LEFT :left
+ goog.events.BrowserEvent.MouseButton/MIDDLE :middle
+ goog.events.BrowserEvent.MouseButton/RIGHT :right})
+
+(def pressed-buttons (atom #{}))
+
+(defn pressed? [button]
+ (@pressed-buttons button))
+
+(defn draw-circ [[x y]]
+ (-> board (.getContext "2d") (circ x y 5)))
+
+(defn mouse-move-callback [event]
+ (let [x (.-clientX event)
+ y (.-clientY event)]
+ (when (pressed? :left)
+ (.send socket (str x "," y))
+ (draw-circ [x y]))))
+
+(defn button-event-callback [f event]
+ (->> (.-button event) buttons
+ (swap! pressed-buttons f)))
+
+(defn msg->coord [message]
+ (let [[x y] (.split (.-data message) ",")]
+ [(js/parseInt x) (js/parseInt y)]))
+
+(set! (.-onmessage socket) #(-> % msg->coord draw-circ))
+
+(listen board (name :mousemove) mouse-move-callback)
+
+(doto js/window
+ (listen :mousedown (partial button-event-callback conj))
+ (listen :mouseup (partial button-event-callback disj)))

0 comments on commit b532e2c

Please sign in to comment.