* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-
- function GuacamoleMouse(element) {
-
- /*****************************************/
- /*** Mouse Handler ***/
- /*****************************************/
-
-
- var mouseIndex = 0;
-
- var mouseLeftButton = 0;
- var mouseMiddleButton = 0;
- var mouseRightButton = 0;
-
- var mouseX = 0;
- var mouseY = 0;
-
- var absoluteMouseX = 0;
- var absoluteMouseY = 0;
-
-
- function getMouseState(up, down) {
- var mouseState = new MouseEvent(mouseX, mouseY,
- mouseLeftButton, mouseMiddleButton, mouseRightButton, up, down);
-
- return mouseState;
- }
-
+ // Guacamole namespace
+ var Guacamole = Guacamole || {};
+
+ /**
+ * Provides cross-browser mouse events for a given element. The events of
+ * the given element are automatically populated with handlers that translate
+ * mouse events into a non-browser-specific event provided by the
+ * Guacamole.Mouse instance.
+ *
+ * Touch event support is planned, but currently only in testing (translate
+ * touch events into mouse events).
+ *
+ * @constructor
+ * @param {Element} element The Element to use to provide mouse events.
+ */
+ Guacamole.Mouse = function(element) {
+
+ /**
+ * Reference to this Guacamole.Mouse.
+ * @private
+ */
+ var guac_mouse = this;
+
+ /**
+ * The current mouse state. The properties of this state are updated when
+ * mouse events fire. This state object is also passed in as a parameter to
+ * the handler of any mouse events.
+ *
+ * @type Guacamole.Mouse.State
+ */
+ this.currentState = new Guacamole.Mouse.State(
+ 0, 0,
+ false, false, false, false, false
+ );
+
+ /**
+ * Fired whenever the user presses a mouse button down over the element
+ * associated with this Guacamole.Mouse.
+ *
+ * @event
+ * @param {Guacamole.Mouse.State} state The current mouse state.
+ */
+ this.onmousedown = null;
+
+ /**
+ * Fired whenever the user releases a mouse button down over the element
+ * associated with this Guacamole.Mouse.
+ *
+ * @event
+ * @param {Guacamole.Mouse.State} state The current mouse state.
+ */
+ this.onmouseup = null;
+
+ /**
+ * Fired whenever the user moves the mouse over the element associated with
+ * this Guacamole.Mouse.
+ *
+ * @event
+ * @param {Guacamole.Mouse.State} state The current mouse state.
+ */
+ this.onmousemove = null;
- // Block context menu so right-click gets sent properly
- element.oncontextmenu = function(e) {
- return false;
- };
+ function moveMouse(pageX, pageY) {
- absoluteMouseX = pageX;
- absoluteMouseY = pageY;
-
- mouseX = absoluteMouseX - element.offsetLeft;
- mouseY = absoluteMouseY - element.offsetTop;
- element.onmousemove = function(e) {
-
- e.stopPropagation();
-
- var absoluteMouseX = e.pageX;
- var absoluteMouseY = e.pageY;
-
- guac_mouse.currentState.x = absoluteMouseX - element.offsetLeft;
- guac_mouse.currentState.y = absoluteMouseY - element.offsetTop;
++ guac_mouse.currentState.x = pageX - element.offsetLeft;
++ guac_mouse.currentState.y = pageY - element.offsetTop;
// This is all JUST so we can get the mouse position within the element
var parent = element.offsetParent;
parent = parent.offsetParent;
}
- movementHandler(getMouseState(0, 0));
+ if (guac_mouse.onmousemove)
+ guac_mouse.onmousemove(guac_mouse.currentState);
+ }
+
+
+ // Block context menu so right-click gets sent properly
- element.oncontextmenu = function(e) {return false;};
++ element.oncontextmenu = function(e) {
++ return false;
++ };
+
+ element.onmousemove = function(e) {
+
+ e.stopPropagation();
+
+ moveMouse(e.pageX, e.pageY);
+
+ };
+
+ element.ontouchend = function(e) {
+
+ e.stopPropagation();
+ e.preventDefault();
+
+ // Release all buttons (FIXME: for now...)
+ if (mouseLeftButton || mouseMiddleButton || mouseRightButton) {
+ mouseLeftButton = 0;
+ mouseMiddleButton = 0;
+ mouseRightButton = 0;
+
+ buttonReleasedHandler(getMouseState(0, 0));
+ }
+
+ }
+
+ element.ontouchstart = function(e) {
+
+ e.stopPropagation();
+ e.preventDefault();
+
+ if (e.touches.length == 1)
+ element.ontouchmove(e);
+
+ else {
+
+ var button = e.touches[0];
+ var pointer = e.touches[1];
+
+ if (pointer.pageX < button.pageX)
+ mouseLeftButton = 1;
+ else
+ mouseRightButton = 1;
+
+ buttonPressedHandler(getMouseState(0, 0));
+ }
+
+ };
+
+ element.ontouchmove = function(e) {
+
+ e.stopPropagation();
+ e.preventDefault();
+
+ var touch = e.touches[0];
+ moveMouse(touch.pageX, touch.pageY);
+
};