aboutsummaryrefslogtreecommitdiff
path: root/js/lib/drawing
diff options
context:
space:
mode:
authorhwc4872012-03-28 05:44:38 -0700
committerhwc4872012-03-28 05:44:38 -0700
commit0a0aa4662ffa687dd049cc5a24e701cedf87253a (patch)
treed2e81e2ad91bf3c05b2ead389d7e02b27e141f27 /js/lib/drawing
parent2acd77dac475a5197cb67b0257c758c357dfd443 (diff)
downloadninja-0a0aa4662ffa687dd049cc5a24e701cedf87253a.tar.gz
added a notification procedure to worlds.
Diffstat (limited to 'js/lib/drawing')
-rwxr-xr-xjs/lib/drawing/world.js71
1 files changed, 69 insertions, 2 deletions
diff --git a/js/lib/drawing/world.js b/js/lib/drawing/world.js
index ebeeeb20..f077a5e2 100755
--- a/js/lib/drawing/world.js
+++ b/js/lib/drawing/world.js
@@ -78,6 +78,9 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) {
78 // keep a counter for generating node names 78 // keep a counter for generating node names
79 this._nodeCounter = 0; 79 this._nodeCounter = 0;
80 80
81 // for sending notifications to listeners
82 this._notifier = new Notifier();
83
81 /////////////////////////////////////////////////////////////////////// 84 ///////////////////////////////////////////////////////////////////////
82 // Property accessors 85 // Property accessors
83 /////////////////////////////////////////////////////////////////////// 86 ///////////////////////////////////////////////////////////////////////
@@ -435,6 +438,9 @@ World.prototype.updateObject = function (obj) {
435 childTrNode.attachMeshNode(this.renderer.id + "_prim_" + this._nodeCounter++, prim); 438 childTrNode.attachMeshNode(this.renderer.id + "_prim_" + this._nodeCounter++, prim);
436 childTrNode.attachMaterial(materialNodes[i]); 439 childTrNode.attachMaterial(materialNodes[i]);
437 } 440 }
441
442 // send a notification that the tree has changed
443 this._notifier.sendNotification( this._notifier.OBJECT_CHANGE );
438}; 444};
439 445
440World.prototype.addObject = function( obj ) { 446World.prototype.addObject = function( obj ) {
@@ -463,6 +469,9 @@ World.prototype.addObject = function( obj ) {
463 obj.buildBuffers(); 469 obj.buildBuffers();
464 this.restartRenderLoop(); 470 this.restartRenderLoop();
465 } 471 }
472
473 // send a notification that the tree has changed
474 this._notifier.sendNotification( this._notifier.OBJECT_CHANGE );
466 } 475 }
467 476
468 catch(e) { 477 catch(e) {
@@ -520,13 +529,21 @@ World.prototype.addIfNewObject = function (obj) {
520 obj.buildBuffers(); 529 obj.buildBuffers();
521 this.restartRenderLoop(); 530 this.restartRenderLoop();
522 } 531 }
532
533 // send a notification that the tree has changed
534 this._notifier.sendNotification( this._notifier.OBJECT_CHANGE );
535
523 } catch (e) { 536 } catch (e) {
524 alert("Exception in GLWorld.addIfNewObject " + e); 537 alert("Exception in GLWorld.addIfNewObject " + e);
525 } 538 }
526}; 539};
527 540
528World.prototype.clearTree = function() { 541World.prototype.clearTree = function()
529 if (this._useWebGL) { 542{
543 this._notifier.sendNotification( this._notifier.OBJECT_DELETE );
544
545 if (this._useWebGL)
546 {
530 var root = this._rootNode; 547 var root = this._rootNode;
531 root.children = new Array(); 548 root.children = new Array();
532 g_Engine.unregisterCanvas( this._canvas.rdgeid ) 549 g_Engine.unregisterCanvas( this._canvas.rdgeid )
@@ -1085,6 +1102,56 @@ World.prototype.importSubObject = function( objStr, parentNode ) {
1085 return trNode; 1102 return trNode;
1086}; 1103};
1087 1104
1105function Notifier()
1106{
1107 // notification types supported
1108 this.OBJECT_DELETE = 1;
1109 this.OBJECT_REANIMATE = 2; // the object has come back after a deletion - as in undo
1110 this.OBJECT_CHANGE = 3;
1111
1112
1113 // the array of listener objects
1114 this._listenerArray = [];
1115
1116 this.sendNotification = function( type, callerData )
1117 {
1118 var n = this._listenerArray.length;
1119 for (var i=0; i<n; i++)
1120 {
1121 var obj = this._listenerArray[i];
1122 obj.callbackFunc( type, obj.callbackObj, obj.calleeData, callerData );
1123 }
1124 }
1125
1126 this.addListener = function( obj, callbackFunc, calleeData )
1127 {
1128 var obj = { 'callbackObj': obj, 'callbackFunc': callbackFunc, 'calleeData': calleeData };
1129 this._listenerArray.push( obj );
1130 }
1131
1132 this.removeListener = function( obj )
1133 {
1134 var arr = this._listenerArray;
1135 var n = arr.length;
1136 for (var i=0; i<n; i++)
1137 {
1138 var localObj = arr[i];
1139 if (obj === localObj)
1140 {
1141 var tmp = arr[n-1];
1142 arr[n-1] = arr[i];
1143 arr.pop();
1144 return;
1145 }
1146 }
1147
1148 console.log( "*** listener object not found in removeListener, " + obj );
1149 }
1150
1151}
1152
1153
1088if (typeof exports === "object") { 1154if (typeof exports === "object") {
1089 exports.World = World; 1155 exports.World = World;
1156 exports.Notifier = Notifier;
1090} \ No newline at end of file 1157} \ No newline at end of file