UNPKG

@criticalred/crlib

Version:

JS/TS wrapper for crLib exports

72 lines (71 loc) 2.14 kB
const keybinds = {}; class Keybind { name; description; defaultMapper; defaultKey; onPressed; onReleased; secondaryKey; secondaryMapper; disabled = false; isPressed = false; hash; constructor(data) { this.name = data.name; this.description = data.description; this.defaultMapper = data.defaultMapper ?? "keyboard"; this.defaultKey = data.defaultKey ?? ""; this.secondaryKey = data.secondaryKey; this.secondaryMapper = data.secondaryMapper; if (typeof data.disabled === "boolean") this.disabled = data.disabled; this.onPressed = data.onPressed; this.onReleased = data.onReleased; this.hash = GetHashKey("+" + this.name) | 0x80000000; } get currentKey() { return this.getCurrentKey(); } getCurrentKey() { const label = GetControlInstructionalButton(0, this.hash, true); return label.substring(2); } isControlPressed() { return this.isPressed; } disable(toggle) { this.disabled = toggle; } } export function addKeybind(data) { const kb = new Keybind(data); keybinds[kb.name] = kb; RegisterCommand("+" + kb.name, () => { if (kb.disabled || IsPauseMenuActive()) return; kb.isPressed = true; kb.onPressed?.call(kb); }, false); RegisterCommand("-" + kb.name, () => { if (kb.disabled || IsPauseMenuActive()) return; kb.isPressed = false; kb.onReleased?.call(kb); }, false); RegisterKeyMapping("+" + kb.name, kb.description, kb.defaultMapper ?? "keyboard", kb.defaultKey ?? ""); if (kb.secondaryKey) { RegisterKeyMapping("~!+" + kb.name, kb.description, kb.secondaryMapper ?? kb.defaultMapper ?? "keyboard", kb.secondaryKey); } setTimeout(() => { emit("chat:removeSuggestion", `/+${kb.name}`); emit("chat:removeSuggestion", `/-${kb.name}`); }, 500); return kb; } export function getKeybind(name) { return keybinds[name]; } export function getAllKeybinds() { return keybinds; }