front_idill/extern/fajran-tuiojs/src/tuio.js
author bastiena
Fri, 06 Apr 2012 18:32:13 +0200
changeset 25 a7b0e40bcab0
child 26 858e90c7cbaa
permissions -rw-r--r--
Front IDILL : Basic JS TUIO lib loaded
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
     1
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
     2
(function() {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
     3
	var TUIO = function() {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
     4
		// Listener class
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
     5
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
     6
		this.Listener = function(impl) {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
     7
			if (impl != undefined) {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
     8
				// override original method implementation
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
     9
				for (var key in impl) {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    10
					this[key] = impl[key];
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    11
				}
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    12
			}
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    13
		}
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    14
		this.Listener.prototype = {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    15
			object_add:    function(data) { },
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    16
			object_update: function(data) { },
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    17
			object_remove: function(data) { },
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    18
			cursor_add:    function(data) { },
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    19
			cursor_update: function(data) { },
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    20
			cursor_remove: function(data) { }
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    21
		}
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    22
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    23
		// Instance variables
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    24
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    25
		this.objects = [];
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    26
		this.cursors = [];
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    27
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    28
		this._data = {};
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    29
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    30
		this._default_listener = new this.Listener();
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    31
		this._listeners = [this._default_listener];
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    32
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    33
		this._connector = undefined;
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    34
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    35
	};
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    36
	TUIO.prototype = {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    37
		start: function(name) {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    38
			var c = this._connector;
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    39
			if (c != undefined) {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    40
				if (c.start != undefined) {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    41
					c.start();
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    42
				}
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    43
			}
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    44
		},
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    45
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    46
		stop: function() {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    47
			var c = this._connector;
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    48
			if (c != undefined) {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    49
				if (c.stop != undefined) {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    50
					c.stop();
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    51
				}
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    52
			}
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    53
		},
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    54
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    55
		setConnector: function(connector) {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    56
			this._connector = connector;
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    57
		},
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    58
		
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    59
		addListener: function(listener) {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    60
			this._listeners.push(listener);
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    61
		},
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    62
		removeListener: function(listener) {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    63
			this._listeners.splice(this._listeners.indexOf(listener), 1);
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    64
		},
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    65
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    66
		_invoke: function(method, data) {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    67
			var i, len = this._listeners.length;
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    68
			for (i=0; i<len; i++) {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    69
				var listener = this._listeners[i];
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    70
				listener[method](data);
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    71
			}
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    72
		},
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    73
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    74
		callback: function(type, sid, fid, x, y, angle) {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    75
			var data;
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    76
			
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    77
			if ((type != 0) && (type != 3)) {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    78
				data = this._data[sid];
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    79
			}
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    80
			else {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    81
				data = {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    82
					sid: sid,
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    83
					fid: fid,
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    84
					path: []
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    85
				}
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    86
				this._data[sid] = data;
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    87
			}
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    88
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    89
			data.path.push([x, y]);
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    90
	
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    91
			data.x = x;
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    92
			data.y = y;
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    93
			
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    94
			if (type < 3) {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    95
				data.angle = angle;
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    96
			}
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    97
	
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    98
			switch (type) {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
    99
				case 0: 
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   100
					this.objects.push(data);
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   101
					this._invoke('object_add', data);
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   102
					break;
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   103
	
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   104
				case 1: 
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   105
					this._invoke('object_update', data);
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   106
					break;
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   107
	
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   108
				case 2: 
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   109
					this.objects.splice(this.objects.indexOf(data), 1);
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   110
					this._invoke('object_remove', data);
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   111
					break;
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   112
	
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   113
				case 3: 
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   114
					this.cursors.push(data);
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   115
					this._invoke('cursor_add', data);
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   116
					break;
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   117
	
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   118
				case 4: 
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   119
					this._invoke('cursor_update', data);
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   120
					break;
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   121
	
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   122
				case 5: 
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   123
					this.cursors.splice(this.cursors.indexOf(data), 1);
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   124
					this._invoke('cursor_remove', data);
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   125
					break;
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   126
	
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   127
				default:
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   128
					break;
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   129
			}
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   130
	
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   131
			if ((type == 2) || (type == 5)) {
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   132
				delete this._data[sid];
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   133
			}
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   134
		},
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   135
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   136
		// Convenient callbacks set
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   137
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   138
		object_add:    function(f) { this._default_listener.object_add = f;    },
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   139
		object_update: function(f) { this._default_listener.object_update = f; },
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   140
		object_remove: function(f) { this._default_listener.object_remove = f; },
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   141
		cursor_add:    function(f) { this._default_listener.cursor_add = f;    },
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   142
		cursor_update: function(f) { this._default_listener.cursor_update = f; },
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   143
		cursor_remove: function(f) { this._default_listener.cursor_remove = f; }
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   144
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   145
	};
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   146
	this.tuio = new TUIO(); 
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   147
})();
a7b0e40bcab0 Front IDILL :
bastiena
parents:
diff changeset
   148