diff --git a/src/client/ui/client.js b/src/client/ui/client.js index 559c89e13..5307842ca 100644 --- a/src/client/ui/client.js +++ b/src/client/ui/client.js @@ -49,19 +49,111 @@ network = realTimeConnection; ghostPointerElements = {}; + + var clearEl = clearScreenButton.toDomElement() + + debugEl = HtmlElement.appendHtmlToBody( + "

DEBUG

" + ).toDomElement(); + + + onTouchClick(clearScreenButton, function() { + debug("CLEAR ELEMENT, TOUCH-CLICKED"); + }); + + documentBody.onMouseMove(function(coordinate) { + debug("BODY, MOUSE MOVE: " + coordinate); + debug(clearScreenButton.isMyCoordinate(coordinate) ? "OVER CLEAR BUTTON" : "NOT OVER CLEAR BUTTON"); + }); + documentBody.onSingleTouchMove(function(coordinate) { + debug("BODY, TOUCH MOVE: " + coordinate); + }); + documentBody.onTouchEnd(function(coordinate) { + debug("BODY, TOUCH END: " + coordinate); + }); + documentBody.onMouseDown(function(coordinate) { + debug("BODY, MOUSE DOWN: " + coordinate); + }); + documentBody.onMouseUp(function(coordinate) { + debug("BODY, MOUSE UP: " + coordinate); + }); + documentBody.onMouseClick(function(coordinate) { + debug("BODY, MOUSE CLICK: " + coordinate); + }); + + // clearScreenButton.onMouseMove(function(coordinate) { + // debug("CLEAR BUTTON, MOUSE MOVE: " + coordinate); + // }); + clearScreenButton.onSingleTouchStart(function(coordinate) { + debug("CLEAR BUTTON, TOUCH START: " + coordinate); + }); + clearScreenButton.onSingleTouchMove(function(coordinate) { + debug("CLEAR BUTTON, TOUCH MOVE: " + coordinate); + }); + clearScreenButton.onTouchEnd(function(coordinate) { + debug("CLEAR BUTTON, TOUCH END: " + coordinate); + }); + clearScreenButton.onMouseDown(function(coordinate) { + debug("CLEAR BUTTON, MOUSE DOWN: " + coordinate); + }); + clearScreenButton.onMouseUp(function(coordinate) { + debug("CLEAR BUTTON, MOUSE UP: " + coordinate); + }); + clearScreenButton.onMouseClick(function(coordinate) { + debug("CLEAR BUTTON, MOUSE CLICK: " + coordinate); + }); + + + + network.connect(window.location.port); handlePointerMovement(); handleClearScreenAction(); handleDrawing(); - debugEl = HtmlElement.appendHtmlToBody( - "

DEBUG

" - ).toDomElement(); - return svgCanvas; }; + function onTouchClick(element, doSomething) { + var clickInProgress; + var lastLocation; + var elementDom = element.toDomElement(); + elementDom.addEventListener("touchstart", function(event) { + if (event.touches.length !== 1) return; + debug("TOUCH-CLICK STARTING"); + + event.preventDefault(); + + clickInProgress = true; + lastLocation = HtmlCoordinate.fromPageOffset(event.touches[0].pageX, event.touches[0].pageY); + + }); + elementDom.addEventListener("touchmove", function(event) { + if (!clickInProgress) return; + if (event.touches.length !== 1) return; + + lastLocation = HtmlCoordinate.fromPageOffset(event.touches[0].pageX, event.touches[0].pageY); + }); + elementDom.addEventListener("touchend", function(event) { + if (!clickInProgress) return; + debug("TOUCH-CLICK ENDING"); + + clickInProgress = false; + + if (element.isMyCoordinate(lastLocation)) { + debug("TOUCH-CLICK SUCCESS"); + doSomething(); + } + else { + debug("TOUCH-CLICK CANCELLED"); + } + }); + elementDom.addEventListener("touchcancel", function(event) { + clickInProgress = false; + }); + } + exports.drawingAreaHasBeenRemovedFromDom = function() { svgCanvas = null; }; @@ -80,29 +172,11 @@ drawingArea.onTouchEnd(sendRemovePointerEvent); network.onEvent(ServerPointerEvent, displayNetworkPointer); network.onEvent(ServerRemovePointerEvent, removeNetworkPointer); - - - drawingArea.onSingleTouchMove(function(coordinate) { - debug("ON SINGLE TOUCH MOVE: " + coordinate); - }); - drawingArea.onTouchEnd(function(coordinate) { - debug("ON SINGLE TOUCH END: " + coordinate); - }); - documentBody.onMouseMove(function(coordinate) { - debug("ON MOUSE MOVE: " + coordinate); - }); - clearScreenButton.onTouchEnd(function(coordinate) { - debug("CLEAR BUTTON, TOUCH END: " + coordinate); - }); - - } function sendPointerEvent(coordinate) { - debug("SEND POINTER EVENT: " + coordinate); - var relativeOffset = coordinate.toRelativeOffset(drawingArea); network.sendEvent(new ClientPointerEvent(relativeOffset.x, relativeOffset.y)); } @@ -133,6 +207,7 @@ function handleClearScreenAction() { clearScreenButton.onMouseClick(clearDrawingAreaAndSendEvent); + onTouchClick(clearScreenButton, clearDrawingAreaAndSendEvent); network.onEvent(ServerClearScreenEvent, clearDrawingArea); } diff --git a/src/client/ui/html_coordinate.js b/src/client/ui/html_coordinate.js index 8265ab1fc..0c2f42dd6 100644 --- a/src/client/ui/html_coordinate.js +++ b/src/client/ui/html_coordinate.js @@ -20,6 +20,16 @@ }; }; + HtmlCoordinate.prototype.toViewportOffset = function(htmlElement) { + failFastIfStylingPresent(htmlElement); + + var scroll = scrollOffset(htmlElement); + return { + x: this._pageX - scroll.x, + y: this._pageY - scroll.y + }; + }; + HtmlCoordinate.fromRelativeOffset = function(htmlElement, relativeX, relativeY) { failFastIfStylingPresent(htmlElement); diff --git a/src/client/ui/html_element.js b/src/client/ui/html_element.js index dfd6aacaa..48dd4aca0 100644 --- a/src/client/ui/html_element.js +++ b/src/client/ui/html_element.js @@ -310,6 +310,12 @@ /* Position and Dimensions */ + HtmlElement.prototype.isMyCoordinate = function(coordinate) { + var offset = coordinate.toViewportOffset(this); + var element = document.elementFromPoint(offset.x, offset.y); + return element === this.toDomElement(); + } + HtmlElement.prototype.getPosition = function() { return HtmlCoordinate.fromRelativeOffset(this, 0, 0); };