Skip to content

Commit

Permalink
2024/01/11 - List hotkey.combo
Browse files Browse the repository at this point in the history
  • Loading branch information
FadiShawki committed Jan 11, 2024
1 parent cc5e1d5 commit 8d79a18
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/@orbitmines/explorer/debug/DebugCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ const DebugInterface = ({ scale = 1.5 }: InterfaceOptions) => {
controls: Ray.vertex().o({
hotkeys: [
{
combo: "a", global: true, label: "", onKeyDown: () => {
combo: ["a", "arrowleft"], global: true, label: "", onKeyDown: () => {
Interface.any.selection = Interface.any.selection.move((self: Ray) => self.initial, memory, Interface);
}
},
{
combo: "d", global: true, label: "", onKeyDown: () => {
combo: ["d", "arrowright"], global: true, label: "", onKeyDown: () => {
Interface.any.selection = Interface.any.selection.move((self: Ray) => self.terminal, memory, Interface);
}
},
{
combo: "w", global: true, label: "", onKeyDown: () => {
combo: ["w", "arrowup"], global: true, label: "", onKeyDown: () => {
Interface.any.selection = Interface.any.selection.move((self: Ray) => self.self, memory, Interface);
}
},
Expand Down
26 changes: 19 additions & 7 deletions src/@orbitmines/js/react/hooks/useHotkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,44 @@ import IModule, {useModule} from "../IModule";
import {HotkeyConfig} from "@blueprintjs/core/src/hooks/hotkeys/hotkeyConfig";
import {useHotkeys as useBlueprintJSHotkeys} from '@blueprintjs/core';
import {useState} from "react";
import _ from "lodash";

export type HotkeyConfigMod = HotkeyConfig & { combo: string | string[] }

export const HOTKEYS_MODULE = 'hotkeys';

export type IHotkeysModule = IModule<HTMLElement, 'onKeyDown' | 'onKeyUp'> & {
reset: () => void,
add: (...hotkeys: HotkeyConfig[]) => void,
set: (...hotkeys: HotkeyConfig[]) => void,
all: () => HotkeyConfig[],
add: (...hotkeys: HotkeyConfigMod[]) => void,
set: (...hotkeys: HotkeyConfigMod[]) => void,
all: () => HotkeyConfigMod[],
}

export const useHotkeys = (): IHotkeysModule => useModule(HOTKEYS_MODULE) as IHotkeysModule;

export const useHotkeysModule = (...initialHotkeys: HotkeyConfig[]): IHotkeysModule => {
export const useHotkeysModule = (...initialHotkeys: HotkeyConfigMod[]): IHotkeysModule => {
const [hotkeys, setHotkeys] = useState<HotkeyConfig[]>(initialHotkeys ?? []);

// Hotkeys: https://blueprintjs.com/docs/#core/hooks/use-hotkeys
const { handleKeyDown: onKeyDown, handleKeyUp: onKeyUp } = useBlueprintJSHotkeys(hotkeys, {
showDialogKeyCombo: "?",
});

const setHotKeysMod = (hotkeys: HotkeyConfigMod[]): void => setHotkeys(
hotkeys.flatMap(hotkey =>
!_.isArray(hotkey.combo) ? hotkey : hotkey.combo.map(
combo => <HotkeyConfig>{...hotkey, combo}
)
)
)


const module: IHotkeysModule = {
identifier: HOTKEYS_MODULE,

reset: () => setHotkeys(initialHotkeys),
add: (...added: HotkeyConfig[]) => setHotkeys([...hotkeys, ...added]),
set: (...hotkeys: HotkeyConfig[]) => setHotkeys([...hotkeys]),
reset: () => setHotKeysMod(initialHotkeys),
add: (...added: HotkeyConfigMod[]) => setHotKeysMod([...hotkeys, ...added]),
set: (...hotkeys: HotkeyConfigMod[]) => setHotKeysMod([...hotkeys]),
all: () => hotkeys,

onKeyDown,
Expand Down

0 comments on commit 8d79a18

Please sign in to comment.