/* Interactive elements * Copyright 2016 Harmen de Weerd * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ function addMouseTracker(elem) { elem.mouse = {x : 0, y : 0, buttons : 0, targetElement : null, hasFocus: false}; elem.mouse.interactiveElements = []; elem.getInteractiveElement = findElement; elem.addEventListener("mousedown", elem.mouse.downListener = function(event) { this.mouse.buttons = event.buttons; this.mouse.x = event.offsetX; this.mouse.y = event.offsetY; this.mouse.targetElement = this.getInteractiveElement(event.offsetX, event.offsetY); if (this.mouse.targetElement != null) { this.mouse.targetElement.startAction(event); } }); elem.addEventListener("mouseup", elem.mouse.upListener = function(event) { this.mouse.buttons = event.buttons; if (this.mouse.targetElement != null) { this.mouse.targetElement.endAction(event); this.mouse.targetElement = null; } }); elem.addEventListener("mousemove", elem.mouse.moveListener = function(event) { this.mouse.hasFocus = true; this.mouse.x = event.offsetX; this.mouse.y = event.offsetY; if (this.mouse.targetElement != null) { this.mouse.targetElement.continueAction(event); } else { var interactiveElement = this.getInteractiveElement(event.offsetX, event.offsetY); if (interactiveElement != null) { this.style.cursor = interactiveElement.getActionLabel(event.offsetX - interactiveElement.x, event.offsetY - interactiveElement.y); } else { this.style.cursor = "initial"; } } }); elem.addEventListener("mouseout", elem.mouse.outListener = function(event) { this.mouse.hasFocus = false; }); elem.addInteractiveElement = function(obj) { if (!obj.mouseHit) obj.mouseHit = mouseHit this.mouse.interactiveElements.push(obj); elem.removeMouseTracker = function() { elem.removeEventListener("mousedown", this.mouse.downListener) elem.removeEventListener("mouseup", this.mouse.upListener) elem.removeEventListener("mousemove", this.mouse.moveListener) elem.removeEventListener("mouseout", this.mouse.outListener) delete elem.mouse } }; } function mouseHit(x, y) { return (x < this.x + this.getWidth() && x > this.x && y < this.y + this.getHeight() && y > this.y) } function findElement(x, y) { return this.mouse.interactiveElements.find(elem => elem.mouseHit(x, y)) }