front_idill/extern/fajran-tuiojs/src/tuio.js
changeset 30 45c889eae324
parent 29 fcf435874395
child 31 2c7fc855eba8
equal deleted inserted replaced
29:fcf435874395 30:45c889eae324
     1 /*
       
     2     Modified by alexandre.bastien@iri.centrepompidou.fr to manage TUIO strings.
       
     3 */
       
     4 
       
     5 var test = 0;
       
     6 
       
     7 (function() {
       
     8     var TUIO = function() {
       
     9         // Listener class
       
    10 
       
    11         this.Listener = function(impl) {
       
    12             if (impl != undefined) {
       
    13                 // override original method implementation
       
    14                 for (var key in impl) {
       
    15                     this[key] = impl[key];
       
    16                 }
       
    17             }
       
    18         }
       
    19         this.Listener.prototype = {
       
    20             object_add:    function(data) { },
       
    21             object_update: function(data) { },
       
    22             object_remove: function(data) { },
       
    23             cursor_add:    function(data) { },
       
    24             cursor_update: function(data) { },
       
    25             cursor_remove: function(data) { },
       
    26             string_add:    function(data) { },
       
    27             string_update: function(data) { },
       
    28             string_remove: function(data) { }
       
    29         }
       
    30 
       
    31         // Instance variables
       
    32 
       
    33         this.objects = [];
       
    34         this.cursors = [];
       
    35         this.strings = [];
       
    36 
       
    37         this._data = {};
       
    38 
       
    39         this._default_listener = new this.Listener();
       
    40         this._listeners = [this._default_listener];
       
    41 
       
    42         this._connector = undefined;
       
    43 
       
    44     };
       
    45     TUIO.prototype = {
       
    46         start: function(name) {
       
    47             var c = this._connector;
       
    48             if (c != undefined) {
       
    49                 if (c.start != undefined) {
       
    50                     c.start();
       
    51                 }
       
    52             }
       
    53         },
       
    54 
       
    55         stop: function() {
       
    56             var c = this._connector;
       
    57             if (c != undefined) {
       
    58                 if (c.stop != undefined) {
       
    59                     c.stop();
       
    60                 }
       
    61             }
       
    62         },
       
    63 
       
    64         setConnector: function(connector) {
       
    65             this._connector = connector;
       
    66         },
       
    67         
       
    68         addListener: function(listener) {
       
    69             this._listeners.push(listener);
       
    70         },
       
    71         removeListener: function(listener) {
       
    72             this._listeners.splice(this._listeners.indexOf(listener), 1);
       
    73         },
       
    74 
       
    75         _invoke: function(method, data) {
       
    76             var i, len = this._listeners.length;
       
    77             for (i=0; i<len; i++) {
       
    78                 var listener = this._listeners[i];
       
    79                 listener[method](data);
       
    80             }
       
    81         },
       
    82 
       
    83         cursorCallback: function(type, sid, fid, x, y, z, angle) {
       
    84             if(type >= 6)
       
    85                 return;
       
    86             
       
    87             var data;
       
    88             
       
    89             if ((type != 0) && (type != 3)) {
       
    90                 data = this._data[sid];
       
    91             }
       
    92             else {
       
    93                 data = {
       
    94                     sid: sid,
       
    95                     fid: fid,
       
    96                     path: []
       
    97                 }
       
    98                 this._data[sid] = data;
       
    99             }
       
   100 
       
   101             data.path.push([x, y, z]);
       
   102     
       
   103             data.x = x;
       
   104             data.y = y;
       
   105             data.z = z;
       
   106             
       
   107             if (type < 3) {
       
   108                 data.angle = angle;
       
   109             }
       
   110     
       
   111             switch (type) {
       
   112                 case 0: 
       
   113                     this.objects.push(data);
       
   114                     this._invoke('object_add', data);
       
   115                     break;
       
   116     
       
   117                 case 1: 
       
   118                     this._invoke('object_update', data);
       
   119                     break;
       
   120     
       
   121                 case 2: 
       
   122                     this.objects.splice(this.objects.indexOf(data), 1);
       
   123                     this._invoke('object_remove', data);
       
   124                     break;
       
   125     
       
   126                 case 3: 
       
   127                     this.cursors.push(data);
       
   128                     this._invoke('cursor_add', data);
       
   129                     break;
       
   130     
       
   131                 case 4: 
       
   132                     this._invoke('cursor_update', data);
       
   133                     break;
       
   134     
       
   135                 case 5: 
       
   136                     this.cursors.splice(this.cursors.indexOf(data), 1);
       
   137                     this._invoke('cursor_remove', data);
       
   138                     break;
       
   139     
       
   140                 default:
       
   141                     break;
       
   142             }
       
   143     
       
   144             if ((type == 2) || (type == 5)) {
       
   145                 delete this._data[sid];
       
   146             }
       
   147         },
       
   148         
       
   149         stringCallback: function(type, sid, code) {
       
   150             if(type < 6)
       
   151                 return;
       
   152             
       
   153             var data;
       
   154             
       
   155             if ((type != 6)) {
       
   156                 data = this._data[sid];
       
   157             }
       
   158             else {
       
   159                 data = {
       
   160                     sid: sid,
       
   161                     code: code
       
   162                 }
       
   163                 this._data[sid] = data;
       
   164             }
       
   165             
       
   166             //data.code = code;
       
   167             
       
   168             switch (type) {
       
   169                 case 6: 
       
   170                     if(this.strings != null && this.strings.length <= 0)
       
   171                     {
       
   172                         this.strings.push(data);
       
   173                         this._invoke('string_add', data);
       
   174                         test++;
       
   175                         //alert(test);
       
   176                     }
       
   177                     break;
       
   178     
       
   179                 case 7: 
       
   180                     this._invoke('string_update', data);
       
   181                     break;
       
   182     
       
   183                 case 8: 
       
   184                     //var str = "";
       
   185                     //for(var j = 0 ; j < this.strings.length ; j++)
       
   186                         //str += "(" + this.strings[i].sid + ")" + this.strings[i].code + " ";
       
   187                     //alert(str);
       
   188                     //this.strings.splice(this.strings.indexOf(data), 1);
       
   189                     this.strings.length = 0;
       
   190                     this._invoke('string_remove', data);
       
   191                     test--;
       
   192                     alert(test);
       
   193                     break;
       
   194 
       
   195                 default:
       
   196                     break;
       
   197             }
       
   198     
       
   199             if ((type == 8)) {
       
   200                 delete this._data[sid];
       
   201             }
       
   202         },
       
   203 
       
   204         // Convenient callbacks set
       
   205 
       
   206         object_add:    function(f) { this._default_listener.object_add = f;    },
       
   207         object_update: function(f) { this._default_listener.object_update = f; },
       
   208         object_remove: function(f) { this._default_listener.object_remove = f; },
       
   209         cursor_add:    function(f) { this._default_listener.cursor_add = f;    },
       
   210         cursor_update: function(f) { this._default_listener.cursor_update = f; },
       
   211         cursor_remove: function(f) { this._default_listener.cursor_remove = f; },
       
   212         string_add:    function(f) { this._default_listener.string_add = f;    },
       
   213         string_update: function(f) { this._default_listener.string_update = f; },
       
   214         string_remove: function(f) { this._default_listener.string_remove = f; }
       
   215 
       
   216     };
       
   217     this.tuio = new TUIO(); 
       
   218 })();
       
   219