123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- /// <reference path="../Controls.ts" />
- /// <reference path="../Classes/StoredMemory.ts" />
- /// <reference path="../../Elements/Elements.ts" />
- interface KeyCode {
- evCode : string;
- representation : string;
- name : string;
- }
- module Controls.KeyHandler {
- let promiseStack : Array<(value?: KeyCode | PromiseLike<KeyCode>) => void> = [];
- let promiseOriginalStack : Array<Promise<KeyCode>> = [];
- export function rejectPromise (promise : (value?: KeyCode | PromiseLike<KeyCode>) => void) {
- promiseStack.splice(promiseStack.indexOf(promise), 1);
- promiseOriginalStack.splice(promiseStack.indexOf(promise), 1);
- }
- export function createKeyCode (ev : KeyboardEvent) {
- let name = ev.key.toUpperCase();
- let representation = name;
- let evCode = ev.code;
- if (ev.shiftKey) {
- name = "Shift + " + name;
- evCode = "Sh" + evCode;
- representation = "S" + representation;
- }
- if (ev.altKey) {
- name = "Alt + " + name;
- evCode = "Al" + evCode;
- representation = "A" + representation;
- }
- if (ev.ctrlKey) {
- name = "Control + " + name;
- evCode = "Ct" + evCode;
- representation = "C" + representation;
- }
- let keyCode : KeyCode = {
- evCode : evCode,
- name : name,
- representation : representation
- };
- return keyCode;
- }
- window.addEventListener("mousedown", function (event) {
- let keyCode : KeyCode = {
- representation : "Click",
- evCode : "Click",
- name : "Click"
- };
- let promise = promiseStack.shift();
- if (promise != undefined) {
- promise(keyCode);
- promiseOriginalStack.shift();
- return;
- }
- });
- window.addEventListener("keydown", function (event) {
- //event.preventDefault(); This fucks up browser control. Let's just assume that this is always going to be the last thing to catch a key.
- let keyCode = createKeyCode(event);
- let promise = promiseStack.shift();
- if (promise != undefined) {
- promise(keyCode);
- promiseOriginalStack.shift();
- return;
- }
- if (keyCode.evCode == scrollKeyCode.getValue().evCode) {
- Elements.CurrentTurnHandler.scrollSpace();
- return;
- } else if (keyCode.evCode == imageKeyCode.getValue().evCode) {
- if (SayImage.imageViewer.style.display == "block") {
- SayImage.imageViewer.style.display = "none";
- } else {
- let images = Elements.CurrentTurnHandler.currentTurnTab.getElementsByClassName("contentImage");
- if (images.length > 0) {
- (<HTMLElement> images[images.length - 1]).click();
- }
- }
- return;
- }
- let ele;
- Elements.getLinkElements().some((element : HTMLElement) => {
- let keyables = element.getElementsByClassName("keyable");
- for (let i = 0; i < keyables.length; i++) {
- if ((<HTMLElement> keyables[i]).dataset['shortcutcode'] == keyCode.evCode) {
- ele = keyables[i];
- return true;
- }
- }
- return false;
- });
- if (ele != undefined) {
- ele.click();
- }
- });
- export function createKeyCodes (codes : Array<string>, keys : Array<string>, mods? : Array<boolean>) {
- let arr = [];
- for (let mod = 0; mod < 4; mod++) {
- if (mod == 0 || mods[mod] == true) {
- for (let i = 0; i < codes.length; i++) {
- let name = keys[i];
- let representation = keys[i];
- let evCode = codes[i];
- if (mod == 1) {
- name = "Shift + " + name;
- evCode = "Sh" + evCode;
- representation = "S" + representation;
- }
- if (mod == 2) {
- name = "Alt + " + name;
- evCode = "Al" + evCode;
- representation = "A" + representation;
- }
- if (mod == 3) {
- name = "Control + " + name;
- evCode = "Ct" + evCode;
- representation = "C" + representation;
- }
- let keyCode: KeyCode = {
- evCode: evCode,
- representation: representation,
- name : name
- };
- arr.push(keyCode);
- }
- }
- }
- return arr;
- }
- export var scrollKeyCode = new StoredMemory("scrollKeyCode", <KeyCode> {
- evCode : "Space",
- representation : "Space"
- });
- export var imageKeyCode = new StoredMemory("imageKeyCode", <KeyCode> {
- evCode : "KeyI",
- representation : "I"
- });
- export var keyCodes1 = new StoredMemory(
- "keyCodesRoom",
- createKeyCodes( ["Digit1", "Digit2", "Digit3", "Digit4", "Digit5"],
- ["1", "2", "3", "4", "5"],
- [true, true, true, false])
- );
- export var keyCodes2 = new StoredMemory(
- "keyCodesActions",
- createKeyCodes( ["KeyQ", "KeyW", "KeyE", "KeyR", "KeyT"],
- ["Q", "W", "E", "R", "T"],
- [true, true, true, false])
- );
- export var keyCodes3 = new StoredMemory(
- "keyCodesGlobal",
- createKeyCodes( ["KeyF", "KeyV", "KeyG", "KeyB"],
- ["F", "V", "G", "B"],
- [true, true, true, false])
- );
- export var keyCodeNorth = new StoredMemory(
- "keyCodeNorth",
- <KeyCode> {
- evCode : "KeyS",
- representation : "S"
- }
- );
- export var keyCodeEast = new StoredMemory(
- "keyCodeEast",
- <KeyCode> {
- evCode : "KeyC",
- representation : "C"
- }
- );
- export var keyCodeSouth = new StoredMemory(
- "keyCodeSouth",
- <KeyCode> {
- evCode : "KeyX",
- representation : "X"
- }
- );
- export var keyCodeWest = new StoredMemory(
- "keyCodeWest",
- <KeyCode> {
- evCode : "KeyZ",
- representation : "Z"
- }
- );
- export var keyCodeUp = new StoredMemory(
- "keyCodeUp",
- <KeyCode> {
- evCode : "KeyA",
- representation : "A"
- }
- );
- export var keyCodeDown = new StoredMemory(
- "keyCodeDown",
- <KeyCode> {
- evCode : "KeyD",
- representation : "D"
- }
- );
- // TODO: Make this safer by creating it using Room.DIRECTIONS.
- var directionCodeByIndex = [keyCodeNorth, undefined, keyCodeEast, undefined,
- keyCodeSouth, undefined, keyCodeWest, undefined, keyCodeUp, keyCodeDown];
- export function getDirectionCode (direction : Direction) {
- return getDirectionCodeByIndex(Room.DIRECTIONS.indexOf(direction));
- }
- export function getDirectionCodeByIndex (index : number) {
- return directionCodeByIndex[index].getValue();
- }
- export var keyRows = 3;
- export var available1 : Array<KeyCode> = [];
- export var available2 : Array<KeyCode> = [];
- export var available3 : Array<KeyCode> = [];
- export function reset () {
- available1 = keyCodes1.getValue().slice();
- available2 = keyCodes2.getValue().slice();
- available3 = keyCodes3.getValue().slice();
- }
- let showKeys = new StoredMemory("ShowKeys", !Elements.isMobile);
- export function applyCode (element : HTMLElement, code : KeyCode) {
- if (code == undefined) {
- delete(element.dataset['shortcut']);
- delete(element.dataset['shortcutcode']);
- element.classList.remove("keyable");
- } else {
- element.dataset['shortcutcode'] = code.evCode;
- if (showKeys.getValue()) {
- element.dataset['shortcut'] = code.representation;
- }
- element.classList.add("keyable");
- }
- }
- export function getFirstKeyCode () {
- let chosen = available1.length > 0 ? available1 :
- available2.length > 0 ? available2 :
- available3.length > 0 ? available3 : undefined;
- if (chosen != undefined) {
- return chosen.shift();
- }
- return undefined;
- }
- export function getSecondKeyCode () {
- let chosen = available2.length > 0 ? available2 :
- available3.length > 0 ? available3 : undefined;
- if (chosen != undefined) {
- return chosen.splice(0, 1)[0];
- }
- return undefined;
- }
- export function getThirdKeyCode () {
- let chosen = available3.length > 0 ? available3 : undefined;
- if (chosen != undefined) {
- return chosen.splice(0, 1)[0];
- }
- return undefined;
- }
- /**
- * Returns a promise of a key press.
- * If after timeout seconds a key was not pressed, the promise fails.
- * @param {number} timeout
- */
- export function getNextKey (timeout? : number) : Promise<KeyCode> {
- let timeoutNumber;
- let promise = <Promise<KeyCode>> (new Promise((resolve, reject) => {
- promiseStack.push(resolve);
- if (timeout != undefined) {
- timeoutNumber = setTimeout(() => {
- KeyHandler.rejectPromise(resolve);
- reject();
- }, timeout * 1000);
- }
- }));
- promise.then(() => {
- clearTimeout(timeoutNumber);
- });
- return promise;
- }
- export function stopGivingNextKey (originalPromise : Promise<KeyCode>) {
- let index = promiseOriginalStack.indexOf(originalPromise);
- if (index != -1) {
- rejectPromise(promiseStack[index]);
- }
- }
- export function addKey (row : number, ev : KeyboardEvent) {
- let keyName = ev.key.toUpperCase();
- if (["ALT", "CONTROL", "SHIFT", " ", "TAB"].includes(keyName)) {
- return;
- }
- if (keyName == "BACKSPACE") {
- clearKeys(row);
- return;
- }
- if (row >= 1 && row <= Controls.KeyHandler.keyRows) {
- let rowArray : StoredMemory<Array<KeyCode>> = Controls.KeyHandler["keyCodes" + row];
- let keyArray = rowArray.getValue().splice(0);
- keyArray.push(...
- createKeyCodes( [ev.code],
- [keyName],
- [true, true, true, false])
- );
- let modNumber = (name : string) => {
- if (name.indexOf("Shift") != -1) {
- return 1;
- } else if (name.indexOf("Control") != -1) {
- return 2;
- } else if (name.indexOf("Alt") != -1) {
- return 3;
- }
- return 0;
- };
- keyArray.sort((a, b) => {
- let ma = modNumber(a.name);
- let mb = modNumber(b.name);
- if (ma != mb) {
- return ma - mb;
- }
- return 0;
- });
- rowArray.storeValue(keyArray);
- }
- }
- export function clearKeys (row : number) {
- if (row >= 1 && row <= Controls.KeyHandler.keyRows) {
- let rowArray: StoredMemory<Array<KeyCode>> = Controls.KeyHandler["keyCodes" + row];
- rowArray.storeValue([]);
- }
- }
- }
|