improcket/js/input.mjs
2018-03-03 13:29:14 +00:00

51 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {canvas} from './graphics/index.mjs';
export const mouse = { pressed: {}, held: {}, x: 0, y: 0, scroll: 0 };
export const keyCode = { pressed: {}, held: {} };
export const key = { pressed: {}, held: {} };
export const action = {};
const mapping = {};
export function tick() {
mouse.pressed = {};
keyCode.pressed = {};
key.pressed = {};
mouse.scroll = 0;
}
export function init() {
window.addEventListener('keydown', event => {
keyCode.pressed[event.code] = !keyCode.held[event.code];
keyCode.held[event.code] = true;
key.pressed[event.key] = !keyCode.held[event.key];
key.held[event.key] = true;
});
window.addEventListener('keyup', event => {
keyCode.pressed[event.code] = false;
keyCode.held[event.code] = false;
key.pressed[event.key] = false;
key.held[event.key] = false;
});
// Ṕ͕͖ẖ̨̖̺͓̪̹n̼͇͔̯̝̖g̙̩̭͕ͅͅl̻̰͘u͎̥͍̗ͅi̼̞̪̩͚̜͖ ̫̝̻͚͟m͎̳̙̭̩̩̕g̟̤̬̮l̫̕w̶͚͈͚̟͔͖n͏̝͖̞̺ͅa͏̹͓̬̺f̗̬̬̬̖̫͜h͙̘̝̱̬̗͜ ̼͎͖C̱͔̱͖ṭ̬̱͖h̵̰̼̘̩ùlh̙́u̪̫ ̪̺̹̙̯R̞͓̹̞͍͎͉͎̦͙ͅl͇̠̮y̙̪̰̪͙̖e̢̩͉͙̼h̗͔̹̳ ̶w̨̼͍̝̭̣̣ͅg̶̳̦̳a̴͉̹͙̭̟ͅh͈͎̞̜͉̼̜̠́͞n̲a̯g̮͚͓̝l̠ ̹̹̱͙̝f̧̝͖̱h̪̟̻͖̖t͎͘aͅg̤̘͜n̶͈̻̻̝̳
window.addEventListener('mousedown', event => {
mouse.pressed[event.button] = !mouse.held[event.button];
mouse.held[event.button] = true;
});
window.addEventListener('mouseup', event => {
mouse.pressed[event.button] = false;
mouse.held[event.button] = false;
});
window.addEventListener('mousemove', event => {
let rect = canvas.getBoundingClientRect();
mouse.x = event.clientX - rect.left;
mouse.y = event.clientY - rect.top;
});
window.addEventListener('wheel', event => {
mouse.scroll = event.deltaY;
});
}