Refactor rect, rename copyRect -> copy, add several necessary operations, remove...
[guacamole-common-js.git] / src / main / resources / guacamole.js
index a9fcbed..0cbf024 100644 (file)
@@ -20,6 +20,7 @@
  * the Initial Developer. All Rights Reserved.
  *
  * Contributor(s):
+ * Matt Hortman
  *
  * Alternatively, the contents of this file may be used under the terms of
  * either the GNU General Public License Version 2 or later (the "GPL"), or
@@ -123,6 +124,9 @@ Guacamole.Client = function(tunnel) {
     var cursorHotspotX = 0;
     var cursorHotspotY = 0;
 
+    var cursorX = 0;
+    var cursorY = 0;
+
     function moveCursor(x, y) {
 
         var element = cursor.getElement();
@@ -131,6 +135,10 @@ Guacamole.Client = function(tunnel) {
         element.style.left = (x - cursorHotspotX) + "px";
         element.style.top  = (y - cursorHotspotY) + "px";
 
+        // Update stored position
+        cursorX = x;
+        cursorY = y;
+
     }
 
     guac_client.getDisplay = function() {
@@ -308,6 +316,32 @@ Guacamole.Client = function(tunnel) {
 
         },
 
+        "dispose": function(parameters) {
+            
+            var layer_index = parseInt(parameters[0]);
+
+            // If visible layer, remove from parent
+            if (layer_index > 0) {
+
+                // Get container element
+                var layer_container = getLayerContainer(layer_index).getElement();
+
+                // Remove from parent
+                layer_container.parentNode.removeChild(layer_container);
+
+                // Delete reference
+                delete layers[layer_index];
+
+            }
+
+            // If buffer, just delete reference
+            else if (layer_index < 0)
+                delete buffers[-1 - layer_index];
+
+            // Attempting to dispose the root layer currently has no effect.
+
+        },
+
         "png": function(parameters) {
 
             var channelMask = parseInt(parameters[0]);
@@ -344,7 +378,7 @@ Guacamole.Client = function(tunnel) {
 
             dstL.setChannelMask(channelMask);
 
-            dstL.copyRect(
+            dstL.copy(
                 srcL,
                 srcX,
                 srcY,
@@ -356,29 +390,32 @@ Guacamole.Client = function(tunnel) {
 
         },
 
-        "rect": function(parameters) {
-
-            var channelMask = parseInt(parameters[0]);
-            var layer = getLayer(parseInt(parameters[1]));
-            var x = parseInt(parameters[2]);
-            var y = parseInt(parameters[3]);
-            var w = parseInt(parameters[4]);
-            var h = parseInt(parameters[5]);
-            var r = parseInt(parameters[6]);
-            var g = parseInt(parameters[7]);
-            var b = parseInt(parameters[8]);
-            var a = parseInt(parameters[9]);
+        "transfer": function(parameters) {
 
-            layer.setChannelMask(channelMask);
+            var srcL = getLayer(parseInt(parameters[0]));
+            var srcX = parseInt(parameters[1]);
+            var srcY = parseInt(parameters[2]);
+            var srcWidth = parseInt(parameters[3]);
+            var srcHeight = parseInt(parameters[4]);
+            var transferFunction = Guacamole.Client.DefaultTransferFunction[parameters[5]];
+            var dstL = getLayer(parseInt(parameters[6]));
+            var dstX = parseInt(parameters[7]);
+            var dstY = parseInt(parameters[8]);
 
-            layer.drawRect(
-                x, y, w, h,
-                r, g, b, a
+            dstL.transfer(
+                srcL,
+                srcX,
+                srcY,
+                srcWidth, 
+                srcHeight, 
+                dstX,
+                dstY,
+                transferFunction
             );
 
         },
 
-        "clip": function(parameters) {
+        "rect": function(parameters) {
 
             var layer = getLayer(parseInt(parameters[0]));
             var x = parseInt(parameters[1]);
@@ -386,7 +423,15 @@ Guacamole.Client = function(tunnel) {
             var w = parseInt(parameters[3]);
             var h = parseInt(parameters[4]);
 
-            layer.clipRect(x, y, w, h);
+            layer.rect(x, y, w, h);
+
+        },
+
+        "clip": function(parameters) {
+
+            var layer = getLayer(parseInt(parameters[0]));
+
+            layer.clip();
 
         },
 
@@ -414,7 +459,8 @@ Guacamole.Client = function(tunnel) {
                 0 
             );
 
-            // FIXME: Update cursor position when hotspot changes
+            // Update cursor position (hotspot may have changed)
+            moveCursor(cursorX, cursorY);
 
         },
 
@@ -588,4 +634,120 @@ Guacamole.Client.LayerContainer = function(width, height) {
         return div;
     };
 
-};
\ No newline at end of file
+};
+
+/**
+ * Map of all Guacamole binary raster operations to transfer functions.
+ * @private
+ */
+Guacamole.Client.DefaultTransferFunction = {
+
+    /* BLACK */
+    0x0: function (src, dst) {
+        dst.red = dst.green = dst.blue = 0x00;
+    },
+
+    /* WHITE */
+    0xF: function (src, dst) {
+        dst.red = dst.green = dst.blue = 0xFF;
+    },
+
+    /* SRC */
+    0x3: function (src, dst) {
+        dst.red   = src.red;
+        dst.green = src.green;
+        dst.blue  = src.blue;
+        dst.alpha = src.alpha;
+    },
+
+    /* DEST (no-op) */
+    0x5: function (src, dst) {
+        // Do nothing
+    },
+
+    /* Invert SRC */
+    0xC: function (src, dst) {
+        dst.red   = 0xFF & ~src.red;
+        dst.green = 0xFF & ~src.green;
+        dst.blue  = 0xFF & ~src.blue;
+        dst.alpha =  src.alpha;
+    },
+    
+    /* Invert DEST */
+    0xA: function (src, dst) {
+        dst.red   = 0xFF & ~dst.red;
+        dst.green = 0xFF & ~dst.green;
+        dst.blue  = 0xFF & ~dst.blue;
+    },
+
+    /* AND */
+    0x1: function (src, dst) {
+        dst.red   =  ( src.red   &  dst.red);
+        dst.green =  ( src.green &  dst.green);
+        dst.blue  =  ( src.blue  &  dst.blue);
+    },
+
+    /* NAND */
+    0xE: function (src, dst) {
+        dst.red   = 0xFF & ~( src.red   &  dst.red);
+        dst.green = 0xFF & ~( src.green &  dst.green);
+        dst.blue  = 0xFF & ~( src.blue  &  dst.blue);
+    },
+
+    /* OR */
+    0x7: function (src, dst) {
+        dst.red   =  ( src.red   |  dst.red);
+        dst.green =  ( src.green |  dst.green);
+        dst.blue  =  ( src.blue  |  dst.blue);
+    },
+
+    /* NOR */
+    0x8: function (src, dst) {
+        dst.red   = 0xFF & ~( src.red   |  dst.red);
+        dst.green = 0xFF & ~( src.green |  dst.green);
+        dst.blue  = 0xFF & ~( src.blue  |  dst.blue);
+    },
+
+    /* XOR */
+    0x6: function (src, dst) {
+        dst.red   =  ( src.red   ^  dst.red);
+        dst.green =  ( src.green ^  dst.green);
+        dst.blue  =  ( src.blue  ^  dst.blue);
+    },
+
+    /* XNOR */
+    0x9: function (src, dst) {
+        dst.red   = 0xFF & ~( src.red   ^  dst.red);
+        dst.green = 0xFF & ~( src.green ^  dst.green);
+        dst.blue  = 0xFF & ~( src.blue  ^  dst.blue);
+    },
+
+    /* AND inverted source */
+    0x4: function (src, dst) {
+        dst.red   =  0xFF & (~src.red   &  dst.red);
+        dst.green =  0xFF & (~src.green &  dst.green);
+        dst.blue  =  0xFF & (~src.blue  &  dst.blue);
+    },
+
+    /* OR inverted source */
+    0xD: function (src, dst) {
+        dst.red   =  0xFF & (~src.red   |  dst.red);
+        dst.green =  0xFF & (~src.green |  dst.green);
+        dst.blue  =  0xFF & (~src.blue  |  dst.blue);
+    },
+
+    /* AND inverted destination */
+    0x2: function (src, dst) {
+        dst.red   =  0xFF & ( src.red   & ~dst.red);
+        dst.green =  0xFF & ( src.green & ~dst.green);
+        dst.blue  =  0xFF & ( src.blue  & ~dst.blue);
+    },
+
+    /* OR inverted destination */
+    0xB: function (src, dst) {
+        dst.red   =  0xFF & ( src.red   | ~dst.red);
+        dst.green =  0xFF & ( src.green | ~dst.green);
+        dst.blue  =  0xFF & ( src.blue  | ~dst.blue);
+    }
+
+};