Skip to content

Instantly share code, notes, and snippets.

@sabotai
Last active August 29, 2015 14:20
Show Gist options
  • Save sabotai/0e4ae2986b0c60033e86 to your computer and use it in GitHub Desktop.
Save sabotai/0e4ae2986b0c60033e86 to your computer and use it in GitHub Desktop.
udpRemote
/*
//unity c# code
using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Text;
using System.Collections;
public class OSCSend : MonoBehaviour {
private UdpClient clientP = new UdpClient();
private ASCIIEncoding byteEncoder = new ASCIIEncoding();
private string lastInput;
// Use this for initialization
void Start () {
//clientP.Connect("127.0.0.1", 6666);
clientP.Connect ("255.255.255.255", 6666);
lastInput = "";
}
// Update is called once per frame
void Update () {
float xPercent = 100 * (Input.mousePosition.x / Screen.width);
float yPercent = 100-(100 * (Input.mousePosition.y / Screen.height));
SendInt ("mouseX:", (int)xPercent);
SendInt ("mouseY:", (int)yPercent);
Debug.Log ("xpercent = " + xPercent + " ypercent = " + yPercent);
if (Input.inputString != ""){
if (Input.inputString != lastInput){
SendString ("key:" + Input.inputString);
Debug.Log ("sending " + Input.inputString);
}
}
if (Input.GetKey(KeyCode.UpArrow)){
SendString("dir:" + "UP");
}
if (Input.GetKey(KeyCode.DownArrow)){
SendString("dir:" + "DOWN");
}
if (Input.GetKey(KeyCode.LeftArrow)){
SendString("dir:" + "LEFT");
}
if (Input.GetKey(KeyCode.RightArrow)){
SendString("dir:" + "RIGHT");
}
if (Input.GetKey(KeyCode.Mouse0))
SendString("mouseClick:" + "mLEFT");
if (Input.GetKey(KeyCode.Mouse1))
SendString("mouseClick:" + "mRIGHT");
lastInput = Input.inputString;
}
public void SendInt(string routeString, int value){
string message = routeString + " " + value;
byte[] messageBytes = byteEncoder.GetBytes(message);
SendBytes(messageBytes);
}
public void SendString(string routeString){
string message = routeString;
byte[] messageBytes = byteEncoder.GetBytes(message);
SendBytes(messageBytes);
}
public void SendBytes(byte[] message){
try {
clientP.Send (message, message.Length);
} catch(SocketException e){
Debug.Log ("no udp response: " + e);
}
}
}
*/
import hypermedia.net.*;
import java.awt.*;
import java.awt.event.*;
Robot robo;
UDP udp;
float x, y;
int rectX, rectY;
char keyboard;
String keyInput;
void setup() {
size(1280,800);
try {
robo = new Robot();
} catch (AWTException e) {
}
udp = new UDP( this, 6666);//, "149.31.194.25" );
// udp.log( true ); // <-- printout the connection activity
udp.listen( true );
rectX = width/2;
rectY = height/2;
keyInput = " ";
}
//process events
void draw() {
background(100);
ellipse (x, y, 10,10);
rect(rectX, rectY, 100, 100);
text(keyInput, 0, 0);
influence();
//influence(110);
}
void influence(){ //mouse movement
if (x!=0 && y!=0)
//robo.mouseMove((int)x,(int)y);
println("influence mouseX = " + x + " mouseY = " + y);
}
void influence(int pressThis){
if (pressThis > 96 && pressThis < 123)
pressThis = pressThis - 32;
robo.keyPress(pressThis);
robo.keyRelease(pressThis);
println("influence = " + pressThis);
}
void influence(String pressThis){
/*
if (x!=0 && y!=0)
if (frameCount % 100 == 0)robo.mouseMove((int)x,(int)y);
*/
if (pressThis.equals("mLEFT")){
robo.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robo.mouseRelease(InputEvent.BUTTON1_MASK);
}
if (pressThis.equals("mRIGHT")){
robo.mousePress(InputEvent.BUTTON2_MASK);
robo.delay(100);
robo.mouseRelease(InputEvent.BUTTON2_MASK);
}
if (pressThis.equals("LEFT")){
robo.keyPress(LEFT);
robo.keyRelease(LEFT);
rectX--;
}
if (pressThis.equals("RIGHT")){
robo.keyPress(RIGHT);
robo.keyRelease(RIGHT);
rectX++;
}
if (pressThis.equals("UP")){
robo.keyPress(UP);
robo.keyRelease(UP);
rectY++;
}
if (pressThis.equals("DOWN")){
robo.keyPress(DOWN);
robo.keyRelease(DOWN);
rectY++;
}
println("influence = " + pressThis);
}
void receive( byte[] data, String ip, int port ) { // <-- extended handler
int sep = processRaw(data);
String type = new String(subset(data,0,sep));
//println("sep = " + sep + " length = " + data.length);
// data = );
//println("type = " + type);
String message = new String(subset(data, sep+1, data.length-sep-1));
//println( "receive type: " + type + " - message: " + message);
if (type.equals("mouseX")){
float dataF = float(message) / 100;
x = dataF * width;
} else if (type.equals("mouseY")){
float dataF = float(message) / 100;
y = dataF * height;
} else if (type.equals("dir")){
influence(message);
} else if (type.equals("mouseClick")){
influence(message);
} else if(type.equals("key")){
influence((int)data[data.length-1]);
}
//println("received from ip : " + ip);
}
int processRaw(byte[] dataA){
for (int i = 0; i <= dataA.length; i++){
if (dataA[i] == ':'){
return i;
}
}
return 0;
}
void keyPressed(){
keyInput+= key;
println("key input = " + keyInput);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment