Skip to content

Commit

Permalink
- Make tuio.Listener as an overridable class
Browse files Browse the repository at this point in the history
- Restructur the "tuio" object
  • Loading branch information
fajran committed Jun 29, 2009
1 parent 4ee03c0 commit 11d0f79
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 118 deletions.
2 changes: 1 addition & 1 deletion connector/npTuioClient/tuiojs.npTuioClient.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

tuio.connector.add('npTuioClient', {
tuio.setConnector({
_failmsg: "Unable to initialize npTuioClient plugin.",
_id: "__tuiojs_connector_npTuioClient",

Expand Down
3 changes: 1 addition & 2 deletions connector/stomp/tuio.stomp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

tuio.connector.add('stomp', {

tuio.setConnector({
_host: 'localhost',
_port: 61613,
_user: 'guest',
Expand Down
2 changes: 1 addition & 1 deletion examples/dump.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
});

function init() {
tuio.connector.start();
tuio.start();
}

</script>
Expand Down
2 changes: 1 addition & 1 deletion examples/paint.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
function init() {
canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
tuio.connector.start();
tuio.start();
updateCanvasSize();
}

Expand Down
2 changes: 1 addition & 1 deletion examples/tracker.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
ctx = canvas.getContext('2d');
updateCanvasSize();
timer = setInterval(update, 15);
tuio.connector.start();
tuio.start();
};

</script>
Expand Down
241 changes: 129 additions & 112 deletions src/tuio.js
Original file line number Diff line number Diff line change
@@ -1,128 +1,145 @@

var tuio = {
cursors: [],
objects: [],

_data: { },

_cb_object_add: function(data) { },
_cb_object_update: function(data) { },
_cb_object_remove: function(data) { },
_cb_cursor_add: function(data) { },
_cb_cursor_update: function(data) { },
_cb_cursor_remove: function(data) { },

_connectors: { },

// Callback from the main event handler

callback: function(type, sid, fid, x, y, angle) {
var data;

if ((type != 0) && (type != 3)) {
data = this._data[sid];
}
else {
data = {
sid: sid,
fid: fid
(function() {
var TUIO = function() {
// Listener class

this.Listener = function(impl) {
if (impl != undefined) {
// override original method implementation
for (var key in impl) {
this[key] = impl[key];
}
}
this._data[sid] = data;
}

data.x = x;
data.y = y;

if (type < 3) {
data.angle = angle;
}

switch (type) {
case 0:
this.objects.push(data);
this._cb_object_add(data);
break;

case 1:
this._cb_object_update(data);
break;

case 2:
this.objects.splice(this.objects.indexOf(data), 1);
this._cb_object_remove(data);
break;

case 3:
this.cursors.push(data);
this._cb_cursor_add(data);
break;

case 4:
this._cb_cursor_update(data);
break;

case 5:
this.cursors.splice(this.cursors.indexOf(data), 1);
this._cb_cursor_remove(data);
break;

default:
break;
this.Listener.prototype = {
object_add: function(data) { },
object_update: function(data) { },
object_remove: function(data) { },
cursor_add: function(data) { },
cursor_update: function(data) { },
cursor_remove: function(data) { }
}

if ((type == 2) || (type == 5)) {
delete this._data[sid];
}
},

// Callbacks for you, developers!

object_add: function(f) { this._cb_object_add = f; },
object_update: function(f) { this._cb_object_update = f; },
object_remove: function(f) { this._cb_object_remove = f; },
cursor_add: function(f) { this._cb_cursor_add = f; },
cursor_update: function(f) { this._cb_cursor_update = f; },
cursor_remove: function(f) { this._cb_cursor_remove = f; },

// Connectors. To be implemented separately.
// Instance variables

}
this.objects = [];
this.cursors = [];

tuio.connector = {
this._data = {};

// Register
this._default_listener = new this.Listener();
this._listeners = [this._default_listener];

add: function(name, impl) {
tuio._connectors[name] = impl;
},
this._connector = undefined;

// Start

start: function(name) {
this._call(name, 'start');
},

// Stop
};
TUIO.prototype = {
start: function(name) {
var c = this._connector;
if (c != undefined) {
if (c.start != undefined) {
c.start();
}
}
},

stop: function() {
var c = this._connector;
if (c != undefined) {
if (c.stop != undefined) {
c.stop();
}
}
},

stop: function(name) {
this._call(name, 'stop');
},
setConnector: function(connector) {
this._connector = connector;
},

addListener: function(listener) {
this._listeners.push(listener);
},
removeListener: function(listener) {
this._listeners.splice(this._listeners.indexOf(listener), 1);
},

_invoke: function(method, data) {
var i, len = this._listeners.length;
for (i=0; i<len; i++) {
var listener = this._listeners[i];
listener[method](data);
}
},

_call: function(name, method) {
if (name == undefined) {
for (key in tuio._connectors) {
name = key;
break;
callback: function(type, sid, fid, x, y, angle) {
var data;

if ((type != 0) && (type != 3)) {
data = this._data[sid];
}
}
else {
data = {
sid: sid,
fid: fid
}
this._data[sid] = data;
}

data.x = x;
data.y = y;

if (type < 3) {
data.angle = angle;
}

switch (type) {
case 0:
this.objects.push(data);
this._invoke('object_add', data);
break;

case 1:
this._invoke('object_update', data);
break;

case 2:
this.objects.splice(this.objects.indexOf(data), 1);
this._invoke('object_remove', data);
break;

case 3:
this.cursors.push(data);
this._invoke('cursor_add', data);
break;

case 4:
this._invoke('cursor_update', data);
break;

case 5:
this.cursors.splice(this.cursors.indexOf(data), 1);
this._invoke('cursor_remove', data);
break;

default:
break;
}

if ((type == 2) || (type == 5)) {
delete this._data[sid];
}
},

if (name != undefined) {
tuio._connectors[name][method]();
}
else {
// TODO: alert user
}
},
// Convenient callbacks set

object_add: function(f) { this._default_listener.object_add = f; },
object_update: function(f) { this._default_listener.object_update = f; },
object_remove: function(f) { this._default_listener.object_remove = f; },
cursor_add: function(f) { this._default_listener.cursor_add = f; },
cursor_update: function(f) { this._default_listener.cursor_update = f; },
cursor_remove: function(f) { this._default_listener.cursor_remove = f; }

}
};
this.tuio = new TUIO();
})();

0 comments on commit 11d0f79

Please sign in to comment.