Permalink
Browse files

Displaying hand position on canvas is working.

  • Loading branch information...
1 parent 0457cb7 commit bf83ef9506ec1ec7634dd4fee75c4254474db86d @ushadow committed Mar 12, 2012
@@ -4,8 +4,8 @@ class TestController
view.onDisconnect = => @disconnect()
@hand = {x: 0, y: 0}
- draw = => @view.drawRect @hand.x @hand.y
- setInterval draw 20
+ draw = => @view.drawRect @hand.x, @hand.y
+ setInterval draw, 100
connect: ->
@view.showInfo 'Connecting'
@@ -29,9 +29,11 @@ class TestController
@view.showInfo "WebSocket Error: #{errorMessage}"
onMessage: (data) ->
- @view.showInfo "Server: #{data}"
dataArray = data.split ','
if dataArray[0] is 'hand_created'
- @hand.x = parseInt(dataArray[1]) + 320
- @hand.y = parseInt(dataArray[2]) + 240
-
+ @hand.x = parseInt(dataArray[1])
+ @hand.y = parseInt(dataArray[2])
+ status = "x = #{@hand.x}, y = #{@hand.y}"
+ else
+ status = "Server: #{data}"
+ @view.showInfo status
@@ -7,6 +7,9 @@ class TestView
@$button = $('button')
@$button.click => @onButtonClick()
@canvas = document.getElementById 'canvas'
+ @canvasHeight = @canvas.offsetHeight
+ @canvasWidth = @canvas.offsetWidth
+ console.log "#{@canvasHeight}, #{@canvasWidth}"
showInfo: (message) ->
@$status.text message
@@ -16,8 +19,9 @@ class TestView
when 'Disconnected'
@$button.html 'Connect'
- drawRec: (x, y) ->
+ drawRect: (x, y) ->
context = @canvas.getContext '2d'
+ context.clearRect 0, 0, @canvasWidth, @canvasHeight
context.fillStyle = '#ff0000'
context.fillRect x, y, 10, 10
@@ -42,6 +42,7 @@ public void keyReleased(KeyEvent arg0) {}
public void keyPressed(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_ESCAPE) {
shouldRun = false;
+ System.exit(0);
}
}
});
@@ -1,7 +1,6 @@
package edu.mit.yingyin.websocket;
import java.io.IOException;
-import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
@@ -17,8 +16,6 @@
import org.eclipse.jetty.websocket.WebSocket.Connection;
import org.eclipse.jetty.websocket.WebSocketHandler;
-//import edu.mit.yingyin.websocket.HandTracker.HandEventListener;
-
/**
* A WebSocket server that sends input events to clients.
* @author yingyin
@@ -55,7 +52,6 @@ public void onOpen(Connection connection) {
private HandCreateObserver handCreateObserver = new HandCreateObserver();
private HandUpdateObserver handUpdateObserer = new HandUpdateObserver();
-
public InputListener(Connection connection) {
this.connection = connection;
try {
@@ -95,12 +91,16 @@ public void update(IObservable<GestureRecognizedEventArgs> observable,
public void update(IObservable<ActiveHandEventArgs> observable,
ActiveHandEventArgs args) {
Point3D pos = args.getPosition();
- String message = String.format("hand_created,%d,%d,%d", (int)pos.getX(),
- (int)pos.getY(), (int)pos.getZ());
try {
+ pos = tracker.convertRealWorldToProjective(pos);
+ String message = String.format("hand_created,%d,%d,%d",
+ (int)pos.getX(), (int)pos.getY(), (int)pos.getZ());
connection.sendMessage(message);
} catch (IOException ioe) {
ioe.printStackTrace();
+ } catch (StatusException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
}
}
}
@@ -113,18 +113,21 @@ public void update(IObservable<ActiveHandEventArgs> observable,
ActiveHandEventArgs args) {
Point3D pos = args.getPosition();
- String message = String.format("hand_created,%d,%d,%d", (int)pos.getX(),
- (int)pos.getY(), (int)pos.getZ());
try {
+ pos = tracker.convertRealWorldToProjective(pos);
+ String message = String.format("hand_created,%d,%d,%d",
+ (int)pos.getX(), (int)pos.getY(), (int)pos.getZ());
connection.sendMessage(message);
} catch (IOException ioe) {
ioe.printStackTrace();
+ } catch (StatusException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
}
}
}
}
-
/**
* This connector uses efficient NIO buffers with a non blocking threading
* model.
@@ -141,7 +144,6 @@ public InputServer(int port, HandTracker tracker) {
addConnector(connector);
wsHandler = new WebSocketHandler() {
-
@Override
public WebSocket doWebSocketConnect(HttpServletRequest request,
String protocol) {
@@ -9,6 +9,5 @@
}
canvas {
- width: 640px;
- height: 480px;
+ border: solid 1px;
}
@@ -15,6 +15,6 @@
<label>Status:</label>
<span id="status"></span>
</p>
- <canvas id="canvas"></canvas>
+ <canvas id="canvas" width="640" height="480"></canvas>
</body>
</html>

0 comments on commit bf83ef9

Please sign in to comment.