Using the TuioClient
This article will explain how you can create a Flash project using Tuio with callbacks.
This method isn't really going along nicely with Adobe's original eventmodel but was implemented this way to allow a better performance for additional Eventdispatching that also uses this method to receive its information from the TuioClient.
What you basically have to do is the implementation of the org.tuio.ITuioListener interface. You don't have to fully implement all functions but you will at least need to leave in the empty definitions or you will get a compiler error for not fully implementing the interface. More on interfaces, what they are and what they actually do or not do can be found in Adobe's as3 documentation and programming guide.
Intitializing Tuio and tracing receive notifications
This part will be rather easy if you already have some object oriented programming experience. What we will do is create a very simple implementation of the ITuioListener that just traces the received TuioObjects. We will also intialize a TuioClient and attach our ITuioListener as a listener in a way that is very similar to how some listeners where attached in as2.
So first we will create our implementation:
-
package {
-
import org.tuio.*;
-
import org.tuio.osc.*;
-
-
-
private var tuio:TuioClient;
-
-
public function TuioExampleTrace(){
-
-
/* Uncomment the connection type you want to use
-
* comment or remove the other one
-
* LocalConnection is the connection method used by default
-
*/
-
-
this.tuio = new TuioClient(new LCConnector());
-
this.tuio.addListener(this);
-
-
//this.tuio = new TuioClient(new TCPConnector());
-
//this.tuio.addListener(this);
-
-
}
-
-
public function addTuioObject(tuioObject:TuioObject):void {
-
trace("add: "+tuioObject.toString());
-
}
-
-
public function updateTuioObject(tuioObject:TuioObject):void {
-
trace("update: "+tuioObject.toString());
-
}
-
-
public function removeTuioObject(tuioObject:TuioObject):void {
-
trace("remove: "+tuioObject.toString());
-
}
-
-
-
public function addTuioCursor(tuioCursor:TuioCursor):void {
-
trace("add: "+tuioCursor.toString());
-
}
-
-
public function updateTuioCursor(tuioCursor:TuioCursor):void {
-
trace("update: "+tuioCursor.toString());
-
}
-
-
public function removeTuioCursor(tuioCursor:TuioCursor):void {
-
trace("remove: "+tuioCursor.toString());
-
}
-
-
-
public function addTuioBlob(tuioBlob:TuioBlob):void {
-
trace("add: "+tuioBlob.toString());
-
}
-
-
public function updateTuioBlob(tuioBlob:TuioBlob):void {
-
trace("update: "+tuioBlob.toString());
-
}
-
-
public function removeTuioBlob(tuioBlob:TuioBlob):void {
-
trace("remove: "+tuioBlob.toString());
-
}
-
-
}
-
}
As you can see I simply trace the String representation of the passed TuioCursor, TuioObject or TuioBlob. Every of these classes has an implementation of toString() that will give you all the content as defined in the Tuio specification in a single line String.
Also note the TuioClient intitialization in the constructor. As mentioned in the comment the LocalConnection baesd LCConnector is used as a default in this script. By uncommenting the succeeding two lines and removing/commenting the other two you can choose to use the TCP based IOSCConnector if your Tuio setup doesn't support LocalConnection yet.
The class can be simply used as a document class in a Flash project so no additional work is needed at this point.
Drawing and removing Sprites
Since we know now that the getting information into flash works just fine we can actually do something with it. This one will be kind of simple but should be fun to play around with and extend.
The code that we are going to add will draw circles on the coordinates sent by the tracker.
So first we will create a simple Sprite that draws circles.
-
package {
-
import flash.display.*;
-
-
-
this.graphics.beginFill(color, 0.5);
-
this.graphics.drawCircle(0,0,radius);
-
this.graphics.endFill();
-
this.x = x;
-
this.y = y;
-
this.name = name;
-
-
cont.addChild(this);
-
}
-
}
-
}
Next we will create a new implementation of the ITuioListener. This time only the Cursor specific callbacks will be implemented.
-
package {
-
-
import org.tuio.*;
-
import org.tuio.osc.*;
-
import flash.display.*;
-
import flash.ui.*;
-
import flash.events.*;
-
-
-
-
private var tuio:TuioClient;
-
-
public function TuioExampleDrawingCursor(){
-
-
this.circleSize = 10;
-
-
-
-
/* Uncomment the connection type you want to use
-
* comment or remove the other one
-
* LocalConnection is the connection method used by default
-
*/
-
-
this.tuio = new TuioClient(new LCConnector());
-
this.tuio.addListener(this);
-
-
//this.tuio = new TuioClient(new TCPConnector());
-
//this.tuio.addListener(this);
-
-
}
-
-
this.circleSize -= 2;
-
this.circleSize += 2;
-
}
-
}
-
-
public function addTuioCursor(tuioCursor:TuioCursor):void {
-
new Circle(tuioCursor.sessionID.toString(), stage, tuioCursor.x*stage.stageWidth, tuioCursor.y * stage.stageHeight, this.circleSize, 0xee3333);
-
}
-
-
public function updateTuioCursor(tuioCursor:TuioCursor):void {
-
var currentCircle:Circle = stage.getChildByName(tuioCursor.sessionID.toString()) as Circle;
-
currentCircle.x = tuioCursor.x*stage.stageWidth;
-
currentCircle.y = tuioCursor.y*stage.stageHeight;
-
}
-
-
public function removeTuioCursor(tuioCursor:TuioCursor):void {
-
var currentCircle:Circle = stage.getChildByName(tuioCursor.sessionID.toString()) as Circle;
-
stage.removeChild(currentCircle);
-
}
-
-
public function addTuioObject(tuioObject:TuioObject):void {}
-
public function updateTuioObject(tuioObject:TuioObject):void {}
-
public function removeTuioObject(tuioObject:TuioObject):void {}
-
public function addTuioBlob(tuioBlob:TuioBlob):void {}
-
public function updateTuioBlob(tuioBlob:TuioBlob):void {}
-
public function removeTuioBlob(tuioBlob:TuioBlob):void {}
-
}
-
}
On addTuioCursor we just simply create a new Circle and set its poisition to the values given in the TuioCursor. Since those position values are given in a range between 0 to 1 they have to be multiplied with the actual dimensions of the drawing canvas. in this case the Stage's stageWidth and stageHeight.
As name for the Circle we simply use Tuio's sessionID for the cursor since it is supposed unique at a time.
On updateTuioCursor we just simply use the given sessionID to retrieve the corresponding Circle and update its position.
On removeTuioCursor we just have to find the Circle again with the sessionID given and remove it from the stage.
Additionally you may have noticed the KeyboardEvent handler that enables you to adjust the Circle's radius by simply pressing the up and down arrows on you keyboard. This is quite handy if you want to test this on different Tuio setups with varying screen resolution.
Download Project
The project is included with the most recent release of the library in the "demo" folder.
It basically is the project explained in this article but draws differently colored circles for each one of the three different tuio container types (cursors, object and blob). It also has three connection indicators that will light up if tuio data is received.
October 29th, 2009 - 23:05
I found it here completely by accident, but got rapidly engaged to it. Downloading the library right now…I have a good feeling about this!
November 1st, 2009 - 13:33
hey oh…
Do you have any more documentation or tutorials… What do i need to get the demo to work? i’m using TUIO_simulator…?
November 1st, 2009 - 14:52
Everything you can find on this blog is what currently is available. A full documentation and additional guides will follow as soon as I get the new event model done and released.
To get everything to work you can use the latest version of the SimpleSimulator which should support both TCP and LocalConnection or the UDP-LC bridge by georg kaindl: http://gkaindl.com/software/udp-flashlc-bridge.
November 3rd, 2009 - 20:58
thank you… looking forward to getting stuck in… ill report back with my progress..!
November 27th, 2009 - 20:30
Hi,
interesting work, I’ve tried to get it to work but without success. The TuioExampleTrace returns “Connected” on the output panel but nothing else.
I am getting TUIO from a TCPConnector with host 127.0.0.1 and port 3000. The tuio messages are generated by Community Core Vision 1.2 on Windows (but I’ve tried it on a Mac too). CCV has “Send TUIO tcp for flash” enabled and if I connect from a linux machine with netcat I can see what’s coming from that port, eg:
This is the type of data the tuio-as3 library is expecting right?
Does anyone have any experience with CCV? Or could you suggest another system for generating the TUIO messages from camera input?
November 28th, 2009 - 14:35
I can’t see what you pasted but if it contains anything like:
<OSCPACKET>
<BUNDLE TIME=”02f43cb23450a3e4″>
<MESSAGE ADDRESS …
… it won’t work because the TCPConnecter expects the OSC Packet in its original binary form like it is sent via UDP and before each packet there has to be an integer stating the size of the following packet.
I’ll look further into it.
edit:
Okay I checked and as suspected the TCP mode sends xml formatted TUIO data. What you could do is use the LCConnector with georg kaindls udp-lc bridge.
November 30th, 2009 - 22:49
Hi gimmix,
thanks, I had tried that and now it works like a charm!
These comments could be useful for other people as the “send tcp for flash” button on CCV can be misleading in this case although it is a great function.
Thanks again for your wonderful work!
mjs2020
June 25th, 2010 - 15:47
Gimmix,
any way we could get a demo link up? It’s still not included in the download.
Thanks
June 25th, 2010 - 19:07
if you download the 0.6 release you can get the old demo which should still work. I am currently quite busy with preparing for some exams but I’ll get to it right afterwards. So I’ll maybe already have something in the first week of july.
July 5th, 2010 - 17:25
im trying to tun this code but i dont get any response
do i need to run the cammera and use markers
July 5th, 2010 - 18:49
what kind of tuio tracking setup do you have? tracker, tuio simulator,… ?
what connector do you use(LCConnector, TCPConnector)? If the LCConnector, do you have the udp-lc bridge up and running?
July 5th, 2010 - 20:00
hey. thanks for responding
sorry, but i dont know what exactly do you mean by tracking setup or tuio simulator, can you explain your self a little bit more or send me to some where that i can read about it
and sorry bat i am using the LCConnector and i dont have an udp-lc bridge