Breathing boxes: Unterschied zwischen den Versionen
Aus hyperdramatik
(Die Seite wurde neu angelegt: „Datei:Boxes.png Datei:Cp5.PNG“) |
|||
Zeile 1: | Zeile 1: | ||
[[Datei:Boxes.png]] | [[Datei:Boxes.png]] | ||
[[Datei:Cp5.PNG]] | [[Datei:Cp5.PNG]] | ||
+ | |||
+ | <source lang="java" line start="2" highlight="4-6"> | ||
+ | import mqtt.*; | ||
+ | import controlP5.*; | ||
+ | import processing.serial.*; | ||
+ | |||
+ | Serial myPort; | ||
+ | |||
+ | MQTTClient client; | ||
+ | ControlP5 cp5; | ||
+ | |||
+ | String inputPlayerName = "noplayerinput"; | ||
+ | String otherplayername; | ||
+ | ArrayList<PlayerBox> playerBoxes = new ArrayList<PlayerBox>(); | ||
+ | int offSetX = 60; | ||
+ | int offSetY = 100; | ||
+ | boolean nameCheck = true; | ||
+ | float myBreath = 0; | ||
+ | float boxSize = 30; | ||
+ | |||
+ | void setup() { | ||
+ | size(700, 700, P3D); | ||
+ | //MQTT_Stuff///////////// | ||
+ | client = new MQTTClient(this); | ||
+ | client.connect("mqtt://anton_self86:58bd19b910910593@broker.shiftr.io", "Anton"); | ||
+ | |||
+ | //GUI_Stuff/////////// | ||
+ | cp5 = new ControlP5(this); | ||
+ | cp5.addTextfield("playername").setPosition(10, 10).setSize(100, 20).setAutoClear(false); | ||
+ | cp5.addBang("Send_Name").setPosition(110, 10).setSize(80, 20); | ||
+ | |||
+ | ///Arduino //// | ||
+ | String portName = Serial.list()[0]; | ||
+ | myPort = new Serial(this,portName, 9600); | ||
+ | myPort.bufferUntil('\n'); | ||
+ | } | ||
+ | |||
+ | void draw() { | ||
+ | background(0); | ||
+ | boxSize = map(myBreath, 0, 1023, 0, 60); | ||
+ | client.publish(inputPlayerName, str(int(boxSize))); | ||
+ | |||
+ | |||
+ | for (int i = 0; i < playerBoxes.size(); i++) { | ||
+ | PlayerBox pB = playerBoxes.get(i); | ||
+ | if (pB.playername.equals(inputPlayerName)){ | ||
+ | pB.setSize(int(boxSize)); | ||
+ | } | ||
+ | pB.update(); | ||
+ | } | ||
+ | delay(100); | ||
+ | } | ||
+ | |||
+ | |||
+ | /////this is happens of you press the Button /////// | ||
+ | void Send_Name() { | ||
+ | nameCheck = true; | ||
+ | inputPlayerName = cp5.get(Textfield.class, "playername").getText(); | ||
+ | client.publish("/control/", "init"); | ||
+ | initPlayer(inputPlayerName); | ||
+ | } | ||
+ | |||
+ | /////MQTT Connect and subscribe//////// | ||
+ | |||
+ | void clientConnected() { | ||
+ | println("client connected"); | ||
+ | |||
+ | client.subscribe("/player/"); | ||
+ | client.subscribe("/control/"); | ||
+ | } | ||
+ | |||
+ | void messageReceived(String topic, byte[] payload) { | ||
+ | println("new message: " + topic + " - " + new String(payload)); | ||
+ | String pl = new String(payload); | ||
+ | |||
+ | ///when topic is /control do something///// | ||
+ | if (topic.equals("/control")) { | ||
+ | if (pl.equals("init")) { | ||
+ | for (int i = 0; i < playerBoxes.size(); i++) { | ||
+ | PlayerBox pB = playerBoxes.get(i); | ||
+ | client.publish("/player/", pB.playername); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | ///when topic is /player do something///// | ||
+ | else if (topic.equals("/player")) { | ||
+ | println("init non local"); | ||
+ | nameCheck = true; | ||
+ | initPlayer(new String(payload)); | ||
+ | }else{ | ||
+ | |||
+ | for (int i = 0; i < playerBoxes.size(); i++) { | ||
+ | PlayerBox pB = playerBoxes.get(i); | ||
+ | if ((topic.equals("/" + pB.playername) == true) && topic.equals(pB.playername)== false){ | ||
+ | int bs = int(pl); | ||
+ | pB.setSize(bs); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void connectionLost() { | ||
+ | println("connection lost"); | ||
+ | } | ||
+ | |||
+ | void serialEvent (Serial myPort) { | ||
+ | // get the ASCII string: | ||
+ | String inString = myPort.readStringUntil('\n'); | ||
+ | |||
+ | if (inString != null) { | ||
+ | // trim off any whitespace: | ||
+ | inString = trim(inString); | ||
+ | //println("inString (raw): " + inString); | ||
+ | myBreath = float(inString); | ||
+ | //println("myBreath (raw): " + myBreath); | ||
+ | |||
+ | } | ||
+ | } | ||
+ | |||
+ | void initPlayer(String name) { | ||
+ | |||
+ | for (int i = 0; i < playerBoxes.size(); i++) { | ||
+ | PlayerBox pB = playerBoxes.get(i); | ||
+ | if (name.equals(pB.playername)) { | ||
+ | nameCheck = false; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if (nameCheck) { | ||
+ | client.subscribe("/" + name); | ||
+ | playerBoxes.add(new PlayerBox(20)); | ||
+ | PlayerBox pb = playerBoxes.get(playerBoxes.size() - 1); ///should be last element in List | ||
+ | pb.setPlayerName(name); | ||
+ | pb.setPosition(offSetX, offSetY); | ||
+ | offSetX = offSetX + 120; | ||
+ | if (offSetX > width - 110) { | ||
+ | offSetX = 60; | ||
+ | offSetY = offSetY + 150; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /////class for box player objects ////// | ||
+ | |||
+ | class PlayerBox { | ||
+ | int rc, gc, bc, size; | ||
+ | float spin = 0; | ||
+ | int xscreen, yscreen; | ||
+ | String playername; | ||
+ | PlayerBox(int s) { | ||
+ | rc = int(random(255)); | ||
+ | gc = int(random(255)); | ||
+ | bc = int(random(255)); | ||
+ | size = s; | ||
+ | } | ||
+ | |||
+ | void setPlayerName(String playerN) { | ||
+ | playername = playerN; | ||
+ | } | ||
+ | |||
+ | void setPosition(int x, int y) { | ||
+ | xscreen = x; | ||
+ | yscreen = y; | ||
+ | } | ||
+ | |||
+ | void setSize(int newSize){ | ||
+ | size = newSize; | ||
+ | } | ||
+ | |||
+ | void update() { | ||
+ | push(); | ||
+ | translate(xscreen, yscreen); | ||
+ | textSize(30); | ||
+ | textAlign(CENTER); | ||
+ | fill(255, 255, 255); | ||
+ | text(playername, 0, 80, 0); | ||
+ | rotateX(0.3); | ||
+ | rotateY(spin); | ||
+ | fill(rc, gc, bc); | ||
+ | box(size); | ||
+ | pop(); | ||
+ | spin = spin - 0.05; | ||
+ | } | ||
+ | } | ||
+ | </source> |
Version vom 11. Mai 2020, 14:03 Uhr
import mqtt.*; import controlP5.*; import processing.serial.*; Serial myPort; MQTTClient client; ControlP5 cp5; String inputPlayerName = "noplayerinput"; String otherplayername; ArrayList<PlayerBox> playerBoxes = new ArrayList<PlayerBox>(); int offSetX = 60; int offSetY = 100; boolean nameCheck = true; float myBreath = 0; float boxSize = 30; void setup() { size(700, 700, P3D); //MQTT_Stuff///////////// client = new MQTTClient(this); client.connect("mqtt://anton_self86:58bd19b910910593@broker.shiftr.io", "Anton"); //GUI_Stuff/////////// cp5 = new ControlP5(this); cp5.addTextfield("playername").setPosition(10, 10).setSize(100, 20).setAutoClear(false); cp5.addBang("Send_Name").setPosition(110, 10).setSize(80, 20); ///Arduino //// String portName = Serial.list()[0]; myPort = new Serial(this,portName, 9600); myPort.bufferUntil('\n'); } void draw() { background(0); boxSize = map(myBreath, 0, 1023, 0, 60); client.publish(inputPlayerName, str(int(boxSize))); for (int i = 0; i < playerBoxes.size(); i++) { PlayerBox pB = playerBoxes.get(i); if (pB.playername.equals(inputPlayerName)){ pB.setSize(int(boxSize)); } pB.update(); } delay(100); } /////this is happens of you press the Button /////// void Send_Name() { nameCheck = true; inputPlayerName = cp5.get(Textfield.class, "playername").getText(); client.publish("/control/", "init"); initPlayer(inputPlayerName); } /////MQTT Connect and subscribe//////// void clientConnected() { println("client connected"); client.subscribe("/player/"); client.subscribe("/control/"); } void messageReceived(String topic, byte[] payload) { println("new message: " + topic + " - " + new String(payload)); String pl = new String(payload); ///when topic is /control do something///// if (topic.equals("/control")) { if (pl.equals("init")) { for (int i = 0; i < playerBoxes.size(); i++) { PlayerBox pB = playerBoxes.get(i); client.publish("/player/", pB.playername); } } } ///when topic is /player do something///// else if (topic.equals("/player")) { println("init non local"); nameCheck = true; initPlayer(new String(payload)); }else{ for (int i = 0; i < playerBoxes.size(); i++) { PlayerBox pB = playerBoxes.get(i); if ((topic.equals("/" + pB.playername) == true) && topic.equals(pB.playername)== false){ int bs = int(pl); pB.setSize(bs); } } } } void connectionLost() { println("connection lost"); } void serialEvent (Serial myPort) { // get the ASCII string: String inString = myPort.readStringUntil('\n'); if (inString != null) { // trim off any whitespace: inString = trim(inString); //println("inString (raw): " + inString); myBreath = float(inString); //println("myBreath (raw): " + myBreath); } } void initPlayer(String name) { for (int i = 0; i < playerBoxes.size(); i++) { PlayerBox pB = playerBoxes.get(i); if (name.equals(pB.playername)) { nameCheck = false; } } if (nameCheck) { client.subscribe("/" + name); playerBoxes.add(new PlayerBox(20)); PlayerBox pb = playerBoxes.get(playerBoxes.size() - 1); ///should be last element in List pb.setPlayerName(name); pb.setPosition(offSetX, offSetY); offSetX = offSetX + 120; if (offSetX > width - 110) { offSetX = 60; offSetY = offSetY + 150; } } } /////class for box player objects ////// class PlayerBox { int rc, gc, bc, size; float spin = 0; int xscreen, yscreen; String playername; PlayerBox(int s) { rc = int(random(255)); gc = int(random(255)); bc = int(random(255)); size = s; } void setPlayerName(String playerN) { playername = playerN; } void setPosition(int x, int y) { xscreen = x; yscreen = y; } void setSize(int newSize){ size = newSize; } void update() { push(); translate(xscreen, yscreen); textSize(30); textAlign(CENTER); fill(255, 255, 255); text(playername, 0, 80, 0); rotateX(0.3); rotateY(spin); fill(rc, gc, bc); box(size); pop(); spin = spin - 0.05; } }