UNPKG

malwoden

Version:

![alt text](./coverage/badge-lines.svg) ![alt text](./coverage/badge-statements.svg) ![alt text](./coverage/badge-functions.svg) ![alt text](./coverage/badge-branches.svg)

167 lines 5.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MouseContext = exports.MouseHandler = void 0; /** * Represents a global mouse. Will likely only create one per app. * * You can bind/switch MouseContexts to the MouseHandler * to change input 'modes'. */ var MouseHandler = /** @class */ (function () { /** Creates a new Mouse Handler */ function MouseHandler() { this.x = 0; this.y = 0; this._isDown = new Set(); document.addEventListener("mousemove", this.onMouseUpdateEvent.bind(this), false); document.addEventListener("mouseenter", this.onMouseUpdateEvent.bind(this), false); document.addEventListener("mousedown", this.onMouseDownEvent.bind(this)); document.addEventListener("mouseup", this.onMouseUpEvent.bind(this)); } MouseHandler.prototype.onMouseDownEvent = function (e) { this._isDown.add(e.button); if (this.context) { var x = e.clientX; var y = e.clientY; this.context.callOnMouseDown({ x: x, y: y, button: e.button, type: "mousedown", }); } }; MouseHandler.prototype.onMouseUpEvent = function (e) { this._isDown.delete(e.button); if (this.context) { var x = e.clientX; var y = e.clientY; this.context.callOnMouseUp({ x: x, y: y, button: e.button, type: "mouseup" }); } }; MouseHandler.prototype.onMouseUpdateEvent = function (e) { this.x = e.clientX; this.y = e.clientY; }; /** * Returns true if the given mouse button is down. * @param mouseButton number - Default 0 for left click. */ MouseHandler.prototype.isMouseDown = function (mouseButton) { if (mouseButton === void 0) { mouseButton = 0; } return this._isDown.has(mouseButton); }; /** * Gets the current position of the mouse * @returns Vector2 */ MouseHandler.prototype.getPos = function () { return { x: this.x, y: this.y, }; }; /** * Sets the active mouse context * @param context MouseContext * @returns this */ MouseHandler.prototype.setContext = function (context) { this.context = context; return this; }; /** * Clears the active mouse context * @returns this */ MouseHandler.prototype.clearContext = function () { this.context = undefined; return this; }; return MouseHandler; }()); exports.MouseHandler = MouseHandler; /** * MouseContext represents a single 'mode' of the game's mouse controls. * For instance, you might have one context to use for the overworld, another for * menus, another for inventory, etc. * * You can set the active context through a MouseHandler's setContext(ctx) * and clearContext() methods. * * * You can register multiple callbacks for onUp/onDown. */ var MouseContext = /** @class */ (function () { function MouseContext() { this._onDown = []; this._onUp = []; } /** * Registers a callback for a mousedown event. * @param callback - The function to call on mousedown * @return this */ MouseContext.prototype.onMouseDown = function (callback) { this._onDown.push(callback); return this; }; /** * Registers a callback for a mouseup event. * @param callback - The function to call on mousedown * @return this */ MouseContext.prototype.onMouseUp = function (callback) { this._onUp.push(callback); return this; }; /** * Clears a registered function for mousedown. If no callback is provided will clear all. * @param callback - The callback to clear. */ MouseContext.prototype.clearMouseDown = function (callback) { if (callback) { this._onDown = this._onDown.filter(function (x) { return x !== callback; }); } else { this._onDown = []; } return this; }; /** * Clears a registered function for mouseup. If no callback is provided will clear all. * @param callback - The callback to clear. */ MouseContext.prototype.clearMouseUp = function (callback) { if (callback) { this._onUp = this._onUp.filter(function (x) { return x !== callback; }); } else { this._onUp = []; } return this; }; /** * Invokes a registered callback for mousedown * @param e MouseHandlerEvent */ MouseContext.prototype.callOnMouseDown = function (e) { for (var _i = 0, _a = this._onDown; _i < _a.length; _i++) { var f = _a[_i]; f(e); } }; /** * Invokes a registered callback for mouseup * @param e MouseHandlerEvent */ MouseContext.prototype.callOnMouseUp = function (e) { for (var _i = 0, _a = this._onUp; _i < _a.length; _i++) { var f = _a[_i]; f(e); } }; return MouseContext; }()); exports.MouseContext = MouseContext; //# sourceMappingURL=mouse.js.map