Skip to content
Browse files

add wasd navigation. fix padding in ctrl packages

  • Loading branch information...
1 parent c7f3d54 commit 62c77083fe0cea7c1ee50069888ef3f3d96cd695 @jonasb jonasb committed Feb 16, 2012
View
35 src/se/forskningsavd/Communicator.java
@@ -69,14 +69,16 @@ public void abort() {
*/
private static final int TIMEOUT_TRUST = 16;
private final DatagramSocket mSocket;
+ private final Navigator mNavigator;
private boolean mRunning = true;
private byte mTrustServer;
private byte mTrustClient;
private final ArrayList<byte[]> mTrustMessages = new ArrayList<byte[]>();
private int mTrustTimeout;
- public SenderThread(DatagramSocket socket) {
+ public SenderThread(DatagramSocket socket, Navigator navigator) {
mSocket = socket;
+ mNavigator = navigator;
}
@Override
@@ -94,22 +96,31 @@ public void run() {
return;
}
-
while (mRunning) {
+ byte kb = 0;
+ if (mNavigator.left)
+ kb |= 1;
+ if (mNavigator.right)
+ kb |= 1 << 1;
+ if (mNavigator.up)
+ kb |= 1 << 2;
+ if (mNavigator.down)
+ kb |= 1 << 3;
//TODO reuse buffer/modify the data
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.put("CTRL".getBytes());
buffer.put(mTrustServer);
buffer.put(mTrustClient);
+ buffer.put((byte) 0); //padding
+ buffer.put((byte) 0); //padding
buffer.putInt(0); //mx
buffer.putInt(0); //my
buffer.putInt(0); //dx
buffer.putInt(0); //dy
- buffer.put((byte) 0); //kb
-
- //padding bytes
- buffer.put((byte) 0);
- buffer.putInt(0);
+ buffer.put(kb);
+ buffer.put((byte) 0); //padding
+ buffer.put((byte) 0); //padding
+ buffer.put((byte) 0); //padding
synchronized (mTrustMessages) {
if (mTrustTimeout == 0) {
@@ -133,12 +144,13 @@ public void run() {
}
Log.d("XXX", "sending " + debug.toString());
} catch (IOException e) {
- e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ e.printStackTrace();
}
try {
sleep(20);
} catch (InterruptedException e) {
+ //ignore
}
}
}
@@ -175,6 +187,11 @@ public void addTrustedMessage(byte[] ident, byte[] message) {
private ReceiverThread mReceiverThread;
private SenderThread mSenderThread;
+ private final Navigator mNavigator;
+
+ public Communicator(Navigator nav) {
+ mNavigator = nav;
+ }
public void connect() {
disconnect();
@@ -186,7 +203,7 @@ public void connect() {
e.printStackTrace();
return;
}
- mSenderThread = new SenderThread(socket);
+ mSenderThread = new SenderThread(socket, mNavigator);
mSenderThread.start();
mReceiverThread = new ReceiverThread(socket, mSenderThread);
View
11 src/se/forskningsavd/MainActivity.java
@@ -5,6 +5,9 @@
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
+import android.widget.LinearLayout.LayoutParams;
+
+import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
public class MainActivity extends Activity {
private Communicator mCommunicator;
@@ -13,9 +16,11 @@
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- mCommunicator = new Communicator();
+ Navigator nav = new Navigator();
+ mCommunicator = new Communicator(nav);
LinearLayout layout = new LinearLayout(this);
+ layout.setOrientation(LinearLayout.VERTICAL);
Button button = new Button(this);
button.setText("Connect");
@@ -35,6 +40,10 @@ public void onClick(View view) {
});
layout.addView(helloWorld);
+ NavigationView navigationView = new NavigationView(this, nav);
+ LayoutParams p = new LinearLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT);
+ layout.addView(navigationView, p);
+
setContentView(layout);
}
View
55 src/se/forskningsavd/NavigationView.java
@@ -0,0 +1,55 @@
+package se.forskningsavd;
+
+import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.Paint;
+import android.view.MotionEvent;
+import android.view.View;
+
+import static android.view.MotionEvent.*;
+
+class NavigationView extends View {
+ private final Paint mPaint = new Paint();
+ private final Navigator mNavigator;
+
+ public NavigationView(Context context, Navigator navigator) {
+ super(context);
+ mNavigator = navigator;
+ mPaint.setColor(0xffffffff);
+ mPaint.setStyle(Paint.Style.STROKE);
+ }
+
+ @Override
+ protected void onDraw(Canvas canvas) {
+ final int width = getWidth();
+ final int height = getHeight();
+ canvas.drawLine(0, height / 3, width, height / 3, mPaint);
+ canvas.drawLine(0, height * 2 / 3, width, height * 2 / 3, mPaint);
+ canvas.drawLine(width / 3, 0, width / 3, height, mPaint);
+ canvas.drawLine(width * 2 / 3, 0, width * 2 / 3, height, mPaint);
+ }
+
+ @Override
+ public boolean onTouchEvent(MotionEvent event) {
+ switch (event.getAction()) {
+ case ACTION_MOVE:
+ case ACTION_DOWN:
+ final int width = getWidth();
+ final int height = getHeight();
+
+ final float x = event.getX();
+ final float y = event.getY();
+ mNavigator.left = (x < width / 3);
+ mNavigator.right = (x > width * 2 / 3);
+ mNavigator.up = (y < height / 3);
+ mNavigator.down = (y > height * 2 / 3);
+ return true;
+ case ACTION_CANCEL:
+ case ACTION_UP:
+ mNavigator.up = mNavigator.down = mNavigator.left = mNavigator.right = false;
+ return true;
+ default:
+ return false;
+ }
+ }
+}
View
8 src/se/forskningsavd/Navigator.java
@@ -0,0 +1,8 @@
+package se.forskningsavd;
+
+class Navigator {
+ public boolean left = false;
+ public boolean right = false;
+ public boolean up = false;
+ public boolean down = false;
+}

0 comments on commit 62c7708

Please sign in to comment.
Something went wrong with that request. Please try again.