Update framework
This commit is contained in:
10
framework/input/inputs/base-input.d.ts
vendored
Normal file
10
framework/input/inputs/base-input.d.ts
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
export declare class BaseInput {
|
||||
protected element: HTMLElement;
|
||||
protected active: boolean;
|
||||
constructor(element: HTMLElement);
|
||||
getState(): any;
|
||||
capture(preventDefault: boolean): void;
|
||||
release(): void;
|
||||
}
|
||||
export interface IBaseInput {
|
||||
}
|
12
framework/input/inputs/base-input.js
Normal file
12
framework/input/inputs/base-input.js
Normal file
@@ -0,0 +1,12 @@
|
||||
export class BaseInput {
|
||||
constructor(element) {
|
||||
this.element = element;
|
||||
}
|
||||
getState() { }
|
||||
capture(preventDefault) {
|
||||
return;
|
||||
}
|
||||
release() {
|
||||
return;
|
||||
}
|
||||
}
|
6
framework/input/inputs/joystick.d.ts
vendored
Normal file
6
framework/input/inputs/joystick.d.ts
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
import { BaseInput } from './base-input';
|
||||
export declare class Joystick extends BaseInput {
|
||||
constructor(element: HTMLElement);
|
||||
}
|
||||
export interface IJoystick {
|
||||
}
|
6
framework/input/inputs/joystick.js
Normal file
6
framework/input/inputs/joystick.js
Normal file
@@ -0,0 +1,6 @@
|
||||
import { BaseInput } from './base-input';
|
||||
export class Joystick extends BaseInput {
|
||||
constructor(element) {
|
||||
super(element);
|
||||
}
|
||||
}
|
18
framework/input/inputs/keyboard.d.ts
vendored
Normal file
18
framework/input/inputs/keyboard.d.ts
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
import { BaseInput } from './base-input';
|
||||
export declare class Keyboard extends BaseInput {
|
||||
private keysDown;
|
||||
private keysJustPressed;
|
||||
private keysJustReleased;
|
||||
private preventDefault;
|
||||
constructor(element: HTMLElement);
|
||||
capture(preventDefault: boolean): void;
|
||||
release(): void;
|
||||
getState(): IKeyboard;
|
||||
private handleKeyDown;
|
||||
private handleKeyUp;
|
||||
}
|
||||
export interface IKeyboard {
|
||||
keysDown: Map<number, boolean>;
|
||||
keysJustPressed: Map<number, boolean>;
|
||||
keysJustReleased: Map<number, boolean>;
|
||||
}
|
50
framework/input/inputs/keyboard.js
Normal file
50
framework/input/inputs/keyboard.js
Normal file
@@ -0,0 +1,50 @@
|
||||
import { BaseInput } from './base-input';
|
||||
export class Keyboard extends BaseInput {
|
||||
constructor(element) {
|
||||
super(element);
|
||||
this.keysDown = new Map();
|
||||
this.keysJustPressed = new Map();
|
||||
this.keysJustReleased = new Map();
|
||||
this.handleKeyDown = this.handleKeyDown.bind(this);
|
||||
this.handleKeyUp = this.handleKeyUp.bind(this);
|
||||
}
|
||||
capture(preventDefault) {
|
||||
this.active = true;
|
||||
this.preventDefault = preventDefault;
|
||||
this.element.addEventListener('keydown', this.handleKeyDown);
|
||||
this.element.addEventListener('keyup', this.handleKeyUp);
|
||||
}
|
||||
release() {
|
||||
this.active = false;
|
||||
this.element.removeEventListener('keydown', this.handleKeyDown);
|
||||
this.element.removeEventListener('keyup', this.handleKeyUp);
|
||||
}
|
||||
getState() {
|
||||
const state = {
|
||||
keysDown: new Map(this.keysDown),
|
||||
keysJustPressed: new Map(this.keysJustPressed),
|
||||
keysJustReleased: new Map(this.keysJustReleased)
|
||||
};
|
||||
this.keysJustPressed.clear();
|
||||
this.keysJustReleased.clear();
|
||||
return state;
|
||||
}
|
||||
handleKeyDown(event) {
|
||||
if (this.active && this.preventDefault)
|
||||
event.preventDefault();
|
||||
if (this.keysDown.get(event.keyCode))
|
||||
return;
|
||||
this.keysDown.set(event.keyCode, true);
|
||||
this.keysJustPressed.set(event.keyCode, true);
|
||||
this.keysJustReleased.set(event.keyCode, false);
|
||||
}
|
||||
handleKeyUp(event) {
|
||||
if (this.active && this.preventDefault)
|
||||
event.preventDefault();
|
||||
if (!this.keysDown.get(event.keyCode))
|
||||
return;
|
||||
this.keysDown.set(event.keyCode, false);
|
||||
this.keysJustPressed.set(event.keyCode, false);
|
||||
this.keysJustReleased.set(event.keyCode, true);
|
||||
}
|
||||
}
|
34
framework/input/inputs/mouse.d.ts
vendored
Normal file
34
framework/input/inputs/mouse.d.ts
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
import { BaseInput } from './base-input';
|
||||
export declare class Mouse extends BaseInput {
|
||||
private mousePosition;
|
||||
private mouseDelta;
|
||||
private mouseWheel;
|
||||
private mouseButtons;
|
||||
constructor(element: HTMLElement);
|
||||
capture(): void;
|
||||
release(): void;
|
||||
getState(): IMouse;
|
||||
private handleMouseDown;
|
||||
private handleMouseMove;
|
||||
private handleMouseUp;
|
||||
private handlePointerChange;
|
||||
}
|
||||
export declare class Position {
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
export declare class MouseButtons {
|
||||
keysDown: Map<number, boolean>;
|
||||
keysJustPressed: Map<number, boolean>;
|
||||
keysJustReleased: Map<number, boolean>;
|
||||
}
|
||||
export declare class Delta {
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
export interface IMouse {
|
||||
mouseButtons: MouseButtons;
|
||||
mousePosition: Position;
|
||||
mouseWheel: Delta;
|
||||
mouseDelta: Delta;
|
||||
}
|
87
framework/input/inputs/mouse.js
Normal file
87
framework/input/inputs/mouse.js
Normal file
@@ -0,0 +1,87 @@
|
||||
import { BaseInput } from './base-input';
|
||||
export class Mouse extends BaseInput {
|
||||
constructor(element) {
|
||||
super(element);
|
||||
this.mousePosition = new Position();
|
||||
this.mouseDelta = new Delta();
|
||||
this.mouseWheel = new Delta();
|
||||
this.mouseButtons = new MouseButtons();
|
||||
}
|
||||
capture() {
|
||||
this.handleMouseDown = this.handleMouseDown.bind(this);
|
||||
this.handleMouseMove = this.handleMouseMove.bind(this);
|
||||
this.handleMouseUp = this.handleMouseUp.bind(this);
|
||||
this.handlePointerChange = this.handlePointerChange.bind(this);
|
||||
this.active = true;
|
||||
this.element.addEventListener('mousedown', this.handleMouseDown);
|
||||
this.element.addEventListener('mousemove', this.handleMouseMove);
|
||||
this.element.addEventListener('mouseup', this.handleMouseUp);
|
||||
document.addEventListener('pointerlockchange', this.handlePointerChange);
|
||||
}
|
||||
release() {
|
||||
this.active = false;
|
||||
this.element.removeEventListener('mousedown', this.handleMouseDown);
|
||||
this.element.removeEventListener('mousemove', this.handleMouseMove);
|
||||
this.element.removeEventListener('mouseup', this.handleMouseUp);
|
||||
}
|
||||
getState() {
|
||||
const { mouseButtons, mouseDelta, mousePosition, mouseWheel } = this;
|
||||
const state = {
|
||||
mouseButtons: {
|
||||
keysDown: new Map(this.mouseButtons.keysDown),
|
||||
keysJustPressed: new Map(this.mouseButtons.keysJustPressed),
|
||||
keysJustReleased: new Map(this.mouseButtons.keysJustReleased)
|
||||
},
|
||||
mouseDelta,
|
||||
mousePosition,
|
||||
mouseWheel
|
||||
};
|
||||
this.mouseButtons.keysJustPressed.clear();
|
||||
this.mouseButtons.keysJustReleased.clear();
|
||||
this.mouseDelta.x = 0;
|
||||
this.mouseDelta.y = 0;
|
||||
return state;
|
||||
}
|
||||
handleMouseDown(event) {
|
||||
if (this.active)
|
||||
event.preventDefault();
|
||||
this.mouseButtons.keysDown.set(event.button, true);
|
||||
this.mouseButtons.keysJustPressed.set(event.button, true);
|
||||
this.mouseButtons.keysJustReleased.set(event.button, false);
|
||||
}
|
||||
handleMouseMove(event) {
|
||||
if (this.active)
|
||||
event.preventDefault();
|
||||
this.mousePosition.x = event.clientX;
|
||||
this.mousePosition.y = event.clientY;
|
||||
this.mouseDelta.x = event.movementX;
|
||||
this.mouseDelta.y = event.movementY;
|
||||
}
|
||||
handleMouseUp(event) {
|
||||
if (this.active)
|
||||
event.preventDefault();
|
||||
this.mouseButtons.keysJustReleased.set(event.button, true);
|
||||
this.mouseButtons.keysDown.set(event.button, false);
|
||||
this.mouseButtons.keysJustPressed.set(event.button, false);
|
||||
}
|
||||
handlePointerChange() {
|
||||
if (document.pointerLockElement !== this.element) {
|
||||
this.element.addEventListener('click', () => {
|
||||
this.element.requestPointerLock();
|
||||
}, {
|
||||
once: true
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
export class Position {
|
||||
}
|
||||
export class MouseButtons {
|
||||
constructor() {
|
||||
this.keysDown = new Map();
|
||||
this.keysJustPressed = new Map();
|
||||
this.keysJustReleased = new Map();
|
||||
}
|
||||
}
|
||||
export class Delta {
|
||||
}
|
Reference in New Issue
Block a user