Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
[Engine]: Documenting the Mouse input (JS Doc) #65
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva committed May 16, 2022
1 parent 445e56b commit 08647ce
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/Inputs/Inputs.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import KeyBoard from "./Keyboard.js";
import Mouse from "./Mouse.js";
import MouseManager from "./Mouse.js";

/**
* @class Inputs
* @description Inputs class
* @memberof Impacto
* @instance
*/
export default class Inputs {
constructor() {
this.KeyBoard = new KeyBoard();
this.Mouse = new Mouse();
this.Mouse = new MouseManager();
}
}

Expand Down
118 changes: 115 additions & 3 deletions src/Inputs/Mouse.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { CanvasStateInstance } from "../State/CanvasState.js";
export default class Mouse {

/**
* @classdesc
* The Rectangle Shape is a 2D Game Object that can be added to a Scene.
* Its possible to set a rounder corner radius.
*
* @example
* const { left } = Impacto.Inputs.KeyBoard.keys;
* if (Impacto.Inputs.KeyBoard.isKeyPressed(left)) { console.log("Left"); }
*
* @class KeyboardManager
* @memberof Impacto.Inputs
* @constructors
*/
export default class MouseManager {
constructor() {
this.x = 0;
this.y = 0;
Expand All @@ -20,9 +34,38 @@ export default class Mouse {
window.addEventListener("contextmenu", e => e.preventDefault()); // Right click show options
}

/**
* Returns the mouse position relative to the canvas.
*
* @example
* const { x, y } = Impacto.Inputs.Mouse.getPosition();
*
* @returns {Object} - The current mouse position {x, y}
* @memberof Impacto.Inputs.MouseManager
*/
getPosition() { return { x: this.x, y: this.y, }; }

/**
* Returns the mouse position relative to the window.
*
* @example
* const { x, y } = Impacto.Inputs.Mouse.getWindowPosition();
*
* @returns {Object} - The current mouse position {x, y}
* @memberof Impacto.Inputs.MouseManager
*/
getWindowPosition() { return { x: this.windowX, y: this.windowY, }; }

/**
* Returns the name of the button by the button code.
*
* @example
* console.log(Impacto.Inputs.Mouse.getNameByButtonCode(1)); // "middle"
*
* @param {number} buttonCode - The button code
* @returns {string} - The name of the button
* @memberof Impacto.Inputs.MouseManager
*/
getNameByButtonCode(buttonCode) {
switch (buttonCode) {
case this.buttons.left: return "left";
Expand All @@ -32,6 +75,16 @@ export default class Mouse {
}
}

/**
* Returns the code of the button by the button name.
*
* @example
* console.log(Impacto.Inputs.Mouse.getButtonCodeByName("left")); // 0
*
* @param {string} buttonName - The name of the button
* @returns {number} - The button code
* @memberof Impacto.Inputs.MouseManager
*/
getButtonKeyByName(name) {
switch (name) {
case "left": return this.buttons.left;
Expand All @@ -41,20 +94,58 @@ export default class Mouse {
}
}

/**
* Returns if the button is pressed.
*
* @example
* Impacto.Inputs.Mouse.isButtonPressed("left") // True
*
* @param {string|number} button - The button name or code
* @returns {boolean} - True if the button is pressed
* @memberof Impacto.Inputs.MouseManager
*/
isButtonDown(button) {
if (typeof button === "string") return this.isButtonDownByName(button);
else if (typeof button === "number") return this.isButtonDownByButtonCode(button);
}

/**
* Returns if the button is pressed by the button name.
*
* @example
* Impacto.Inputs.Mouse.isButtonDownByName("left") // True
*
* @param {string|number} button - The button name
* @returns {boolean} - True if the button is pressed
* @memberof Impacto.Inputs.MouseManager
*/
isButtonDownByName(name) {
return !!this.isDown[name];
}

/**
* Returns if the button is pressed by the button code.
*
* @example
* Impacto.Inputs.Mouse.isButtonDownByName(2) // True
*
* @param {string|number} button - The button code
* @returns {boolean} - True if the button is pressed
* @memberof Impacto.Inputs.MouseManager
*/
isButtonDownByButtonCode(buttonCode) {
return !!this.isDown[this.getNameByButtonCode(buttonCode)];
}

// ----- Private methods -----

// ------
/**
* @description
* Private (Core) function to handle the mouse position.
*
* @private
* @memberof Impacto.Inputs.KeyBoard
*/
_updateMousePosition(e) {
this.windowX = e.clientX;
this.windowY = e.clientY;
Expand All @@ -63,19 +154,40 @@ export default class Mouse {
this.y = this.windowY - CanvasStateInstance.canvas.offsetTop;
}

/**
* @description
* Private (Core) function to handle the mouse position.
*
* @private
* @memberof Impacto.Inputs.KeyBoard
*/
_mousemove(e) {
this._updateMousePosition(e);
}

/**
* @description
* Private (Core) function to handle the mouse position.
*
* @private
* @memberof Impacto.Inputs.KeyBoard
*/
_mousedown(e) {
this._updateMousePosition(e);
this.isDown[this.getNameByButtonCode(e.button)] = true;
}

/**
* @description
* Private (Core) function to handle the mouse position.
*
* @private
* @memberof Impacto.Inputs.KeyBoard
*/
_mouseup(e) {
this._updateMousePosition(e);
this.isDown[this.getNameByButtonCode(e.button)] = false;
}
}

export const MouseInstance = new Mouse();
export const MouseInstance = new MouseManager();

0 comments on commit 08647ce

Please sign in to comment.