1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- function MouseInput() {
- var mouseListener = null;
- var mouseCanvas = null;
- this.attach = function(canvas, listener) {
- mouseListener = listener;
- mouseCanvas = canvas;
- mouseCanvas.addEventListener('mousemove', this.handleMouseMove);
- mouseCanvas.addEventListener('mousedown', this.handleMouseDown);
- mouseCanvas.addEventListener('mouseup', this.handleMouseUp);
- mouseCanvas.addEventListener('contextmenu', this.handleContextMenu);
- mouseCanvas.addEventListener('touchstart', this.handleTouchStart);
- mouseCanvas.addEventListener('touchmove', this.handleTouchMove);
- mouseCanvas.addEventListener('touchend', this.handleTouchEnd);
- };
- this.detatch = function() {
- mouseCanvas.removeEventListener('mousemove', this.handleMouseMove);
- mouseCanvas.removeEventListener('mousedown', this.handleMouseDown);
- mouseCanvas.removeEventListener('mouseup', this.handleMouseUp);
- mouseCanvas.removeEventListener('contextmenu', this.handleContextMenu);
- mouseCanvas.removeEventListener('touchstart', this.handleTouchStart);
- mouseCanvas.removeEventListener('touchmove', this.handleTouchMove);
- mouseCanvas.removeEventListener('touchend', this.handleTouchEnd);
- };
-
- this.handleMouseMove = function(event) {
- if(!mouseListener.hasOwnProperty("mouseMove")) { return; }
- mouseListener.mouseMove(mouseCanvas, event.offsetX, event.offsetY);
- };
- this.handleMouseDown = function(event) {
- if(!mouseListener.hasOwnProperty("mouseDown")) { return; }
- mouseListener.mouseDown(mouseCanvas, event.button, event.offsetX, event.offsetY);
- };
- this.handleMouseUp = function(event) {
- if(!mouseListener.hasOwnProperty("mouseUp")) { return; }
- mouseListener.mouseUp(mouseCanvas, event.button, event.offsetX, event.offsetY);
- };
- this.handleTouchStart = function(event) {
- if(!mouseListener.hasOwnProperty("touchStart")) { return; }
- mouseListener.touchStart(mouseCanvas, event.touches[0].clientX, event.touches[0].clientY);
- }
- this.handleTouchMove = function(event) {
- if(!mouseListener.hasOwnProperty("touchMove")) { return; }
- mouseListener.touchMove(mouseCanvas, event.touches[0].clientX, event.touches[0].clientY);
- }
- this.handleTouchEnd = function(event) {
- if(!mouseListener.hasOwnProperty("touchEnd")) { return; }
- mouseListener.touchEnd(mouseCanvas, event.offsetX, event.offsetY);
- }
- this.handleContextMenu = function(event) {
- event.preventDefault();
- if(!mouseListener.hasOwnProperty("contextMenu")) { return false; }
- mouseListener.touchEnd(mouseCanvas, event);
- return false;
- };
- };
|