Add scale function, use bounding div for main display element, such that the display...
[guacamole-common-js.git] / src / main / resources / guacamole.js
index 55b71ba..43bd165 100644 (file)
@@ -67,6 +67,7 @@ Guacamole.Client = function(tunnel) {
 
     var displayWidth = 0;
     var displayHeight = 0;
+    var displayScale = 1;
 
     /**
      * Translation from Guacamole protocol line caps to Layer line caps.
@@ -86,12 +87,26 @@ Guacamole.Client = function(tunnel) {
         2: "round"
     };
 
+    // Create bounding div 
+    var bounds = document.createElement("div");
+    bounds.style.position = "relative";
+    bounds.style.width = (displayWidth*displayScale) + "px";
+    bounds.style.height = (displayHeight*displayScale) + "px";
+
     // Create display
     var display = document.createElement("div");
     display.style.position = "relative";
     display.style.width = displayWidth + "px";
     display.style.height = displayHeight + "px";
 
+    // Ensure transformations on display originate at 0,0
+    display.style.transformOrigin =
+    display.style.webkitTransformOrigin =
+    display.style.MozTransformOrigin =
+    display.style.OTransformOrigin =
+    display.style.msTransformOrigin =
+        "0 0";
+
     // Create default layer
     var default_layer_container = new Guacamole.Client.LayerContainer(displayWidth, displayHeight);
 
@@ -116,6 +131,9 @@ Guacamole.Client = function(tunnel) {
     display.appendChild(default_layer_container.getElement());
     display.appendChild(cursor.getElement());
 
+    // Add display to bounds
+    bounds.appendChild(display);
+
     // Initially, only default layer exists
     var layers =  [default_layer_container];
 
@@ -148,11 +166,8 @@ Guacamole.Client = function(tunnel) {
 
     function moveCursor(x, y) {
 
-        var element = cursor.getElement();
-
-        // Update rect
-        element.style.left = (x - cursorHotspotX) + "px";
-        element.style.top  = (y - cursorHotspotY) + "px";
+        // Move cursor layer
+        cursor.translate(x - cursorHotspotX, y - cursorHotspotY);
 
         // Update stored position
         cursorX = x;
@@ -161,7 +176,7 @@ Guacamole.Client = function(tunnel) {
     }
 
     guac_client.getDisplay = function() {
-        return display;
+        return bounds;
     };
 
     guac_client.sendKeyEvent = function(pressed, keysym) {
@@ -264,6 +279,21 @@ Guacamole.Client = function(tunnel) {
 
     }
 
+    /**
+     * Handlers for all defined layer properties.
+     */
+    var layerPropertyHandlers = {
+
+        "miter-limit": function(layer, value) {
+            layer.setMiterLimit(parseFloat(value));
+        }
+
+    };
+    
+    /**
+     * Handlers for all instruction opcodes receivable by a Guacamole protocol
+     * client.
+     */
     var instructionHandlers = {
 
         "arc": function(parameters) {
@@ -274,8 +304,9 @@ Guacamole.Client = function(tunnel) {
             var radius = parseInt(parameters[3]);
             var startAngle = parseFloat(parameters[4]);
             var endAngle = parseFloat(parameters[5]);
+            var negative = parseInt(parameters[6]);
 
-            layer.arc(x, y, radius, startAngle, endAngle);
+            layer.arc(x, y, radius, startAngle, endAngle, negative != 0);
 
         },
 
@@ -444,18 +475,7 @@ Guacamole.Client = function(tunnel) {
                 var layer_container = getLayerContainer(layer_index).getElement();
 
                 // Set layer transform 
-                layer_container.style.transform =
-                layer_container.style.WebkitTransform =
-                layer_container.style.MozTransform =
-                layer_container.style.OTransform =
-                layer_container.style.msTransform =
-
-                    /* a c e
-                     * b d f
-                     * 0 0 1
-                     */
-            
-                    "matrix(" + a + "," + b + "," + c + "," + d + "," + e + "," + f + ")";
+                layer_container.transform(a, b, c, d, e, f);
 
              }
 
@@ -520,17 +540,17 @@ Guacamole.Client = function(tunnel) {
             if (layer_index > 0 && parent_index >= 0) {
 
                 // Get container element
-                var layer_container = getLayerContainer(layer_index).getElement();
+                var layer_container = getLayerContainer(layer_index);
+                var layer_container_element = layer_container.getElement();
                 var parent = getLayerContainer(parent_index).getElement();
 
                 // Set parent if necessary
-                if (!(layer_container.parentNode === parent))
-                    parent.appendChild(layer_container);
+                if (!(layer_container_element.parentNode === parent))
+                    parent.appendChild(layer_container_element);
 
                 // Move layer
-                layer_container.style.left   = x + "px";
-                layer_container.style.top    = y + "px";
-                layer_container.style.zIndex = z;
+                layer_container.translate(x, y);
+                layer_container_element.style.zIndex = z;
 
             }
 
@@ -597,6 +617,19 @@ Guacamole.Client = function(tunnel) {
             layer.reset();
 
         },
+        
+        "set": function(parameters) {
+
+            var layer = getLayer(parseInt(parameters[0]));
+            var name = parameters[1];
+            var value = parameters[2];
+
+            // Call property handler if defined
+            var handler = layerPropertyHandlers[name];
+            if (handler)
+                handler(layer, value);
+
+        },
 
         "shade": function(parameters) {
             
@@ -636,6 +669,10 @@ Guacamole.Client = function(tunnel) {
                 display.style.width = displayWidth + "px";
                 display.style.height = displayHeight + "px";
 
+                // Update bounds size
+                bounds.style.width = (displayWidth*displayScale) + "px";
+                bounds.style.height = (displayHeight*displayScale) + "px";
+
             }
 
         },
@@ -786,6 +823,24 @@ Guacamole.Client = function(tunnel) {
         setState(STATE_WAITING);
     };
 
+    guac_client.scale = function(scale) {
+
+        display.style.transform =
+        display.style.WebkitTransform =
+        display.style.MozTransform =
+        display.style.OTransform =
+        display.style.msTransform =
+
+            "scale(" + scale + "," + scale + ")";
+
+        displayScale = scale;
+
+        // Update bounds size
+        bounds.style.width = (displayWidth*displayScale) + "px";
+        bounds.style.height = (displayHeight*displayScale) + "px";
+
+    };
+
 };
 
 
@@ -859,6 +914,75 @@ Guacamole.Client.LayerContainer = function(width, height) {
         return div;
     };
 
+    /**
+     * The translation component of this LayerContainer's transform.
+     */
+    var translate = "translate(0px, 0px)"; // (0, 0)
+
+    /**
+     * The arbitrary matrix component of this LayerContainer's transform.
+     */
+    var matrix = "matrix(1, 0, 0, 1, 0, 0)"; // Identity
+
+    /**
+     * Moves the upper-left corner of this LayerContainer to the given X and Y
+     * coordinate.
+     * 
+     * @param {Number} x The X coordinate to move to.
+     * @param {Number} y The Y coordinate to move to.
+     */
+    layer_container.translate = function(x, y) {
+
+        // Generate translation
+        translate = "translate("
+                        + x + "px,"
+                        + y + "px)";
+
+        // Set layer transform 
+        div.style.transform =
+        div.style.WebkitTransform =
+        div.style.MozTransform =
+        div.style.OTransform =
+        div.style.msTransform =
+
+            translate + " " + matrix;
+
+    };
+
+    /**
+     * Applies the given affine transform (defined with six values from the
+     * transform's matrix).
+     * 
+     * @param {Number} a The first value in the affine transform's matrix.
+     * @param {Number} b The second value in the affine transform's matrix.
+     * @param {Number} c The third value in the affine transform's matrix.
+     * @param {Number} d The fourth value in the affine transform's matrix.
+     * @param {Number} e The fifth value in the affine transform's matrix.
+     * @param {Number} f The sixth value in the affine transform's matrix.
+     */
+    layer_container.transform = function(a, b, c, d, e, f) {
+
+        // Generate matrix transformation
+        matrix =
+
+            /* a c e
+             * b d f
+             * 0 0 1
+             */
+    
+            "matrix(" + a + "," + b + "," + c + "," + d + "," + e + "," + f + ")";
+
+        // Set layer transform 
+        div.style.transform =
+        div.style.WebkitTransform =
+        div.style.MozTransform =
+        div.style.OTransform =
+        div.style.msTransform =
+
+            translate + " " + matrix;
+
+    };
+
 };
 
 /**