From 45df11a7b7d5ebe463a7bcf70843c09713705c91 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Wed, 10 Aug 2022 13:51:12 +0000 Subject: [PATCH] WIP KeyShortcutWindow for NAT flixel --- .../src/kiss_flixel/KeyShortcutWindow.hx | 9 ++++++ .../src/kiss_flixel/KeyShortcutWindow.kiss | 29 +++++++++++++++++++ .../src/kiss_tools/KeyShortcutHandler.kiss | 5 +++- .../source/PlayState.hx | 1 + .../source/PlayState.kiss | 15 +++++----- 5 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 projects/kiss-flixel/src/kiss_flixel/KeyShortcutWindow.hx create mode 100644 projects/kiss-flixel/src/kiss_flixel/KeyShortcutWindow.kiss diff --git a/projects/kiss-flixel/src/kiss_flixel/KeyShortcutWindow.hx b/projects/kiss-flixel/src/kiss_flixel/KeyShortcutWindow.hx new file mode 100644 index 00000000..87fe55c8 --- /dev/null +++ b/projects/kiss-flixel/src/kiss_flixel/KeyShortcutWindow.hx @@ -0,0 +1,9 @@ +package kiss_flixel; + +import kiss.Prelude; +import kiss.List; +import kiss_tools.KeyShortcutHandler; +import flixel.util.FlxColor; + +@:build(kiss.Kiss.build()) +class KeyShortcutWindow extends SimpleWindow {} diff --git a/projects/kiss-flixel/src/kiss_flixel/KeyShortcutWindow.kiss b/projects/kiss-flixel/src/kiss_flixel/KeyShortcutWindow.kiss new file mode 100644 index 00000000..a59869fe --- /dev/null +++ b/projects/kiss-flixel/src/kiss_flixel/KeyShortcutWindow.kiss @@ -0,0 +1,29 @@ +// Wraps a given generic KeyShortcutHandler within +// the FlxKeyShortcutHandler that every SimpleWindow has, +// using the window to display available mappings +// from the currently active prefix map +(defNew [&prop :KeyShortcutHandler innerKeyShortcuts + &prop :T->String elementToStringWithoutBrackets + &opt :String title + :FlxColor bgColor + :FlxColor textColor + :Float percentWidth + :Float percentHeight] + (super title bgColor textColor percentWidth percentHeight) + (set innerKeyShortcuts.onSelectPrefixMap ->map { + (clearControls) + (doFor =>key keyBehavior map + (case keyBehavior + ((Final item) + (makeText "[${key}] - $(elementToString item)" null ->_ (innerKeyShortcuts.onSelectItem item))) + ((Prefix innerMap) + (makeText "[${key}] - $(count innerMap) more options" null ->_ (innerKeyShortcuts.onSelectPrefixMap innerMap))) + (null null)))})) + +(method &override :Void show [] + (super.show) + (innerKeyShortcuts.start)) + +(method &override :Void hide [] + (super.hide) + (innerKeyShortcuts.cancel)) \ No newline at end of file diff --git a/projects/kiss-tools/src/kiss_tools/KeyShortcutHandler.kiss b/projects/kiss-tools/src/kiss_tools/KeyShortcutHandler.kiss index bbbff5f6..b0afb8cf 100644 --- a/projects/kiss-tools/src/kiss_tools/KeyShortcutHandler.kiss +++ b/projects/kiss-tools/src/kiss_tools/KeyShortcutHandler.kiss @@ -30,11 +30,14 @@ (set currentMap null) (tryCall onFinishOrCancel)) +(method clear [] + (rootMap.clear)) + (method handleKey [:String key] (unless currentMap (tryCallOrThrow onBadKey "Tried to handle key $key without calling start() first" key null)) (if (= cancelKey key) (cancel) - (case (dictGet currentMap key) + (case ~(dictGet currentMap key) ((Final item) (tryCall onSelectItem item) (cancel)) diff --git a/projects/nat-flixel-desktop-playground/source/PlayState.hx b/projects/nat-flixel-desktop-playground/source/PlayState.hx index 5b311d95..36bcdf06 100644 --- a/projects/nat-flixel-desktop-playground/source/PlayState.hx +++ b/projects/nat-flixel-desktop-playground/source/PlayState.hx @@ -19,6 +19,7 @@ import flixel.addons.plugin.FlxMouseControl; import flixel.input.mouse.FlxMouseEventManager; using StringTools; using kiss_flixel.CameraTools; +import kiss_flixel.KeyShortcutWindow; import kiss_tools.KeyShortcutHandler; import kiss_tools.FlxKeyShortcutHandler; import nat.systems.PlaygroundSystem; diff --git a/projects/nat-flixel-desktop-playground/source/PlayState.kiss b/projects/nat-flixel-desktop-playground/source/PlayState.kiss index b7cec32a..5d102380 100644 --- a/projects/nat-flixel-desktop-playground/source/PlayState.kiss +++ b/projects/nat-flixel-desktop-playground/source/PlayState.kiss @@ -109,13 +109,10 @@ (clearUI)) (prop &mut :Bool confirmQuit false) -(defAlias &ident sh (cast shortcutHandler FlxKeyShortcutHandler)) +(prop &mut :KeyShortcutWindow shw) (method &override :Void update [:Float elapsed] (super.update elapsed) - (when sh.currentMap - (sh.update)) - (when (and FlxG.keys.justPressed.V FlxG.keys.pressed.CONTROL) (when (and textInput textInput.hasFocus) (whenLet [text (Clipboard.generalClipboard.getData ClipboardFormats.TEXT_FORMAT)] @@ -132,8 +129,8 @@ // This part is hacky... (set lastUI textInputLabel) (hideUI textInputLabel)) - (~sh.currentMap - (sh.cancel)) + ((and shw (shw.isShown)) + (shw.hide)) (confirmQuit (Sys.exit 0)) (true @@ -169,10 +166,12 @@ (FlxG.camera.updateScrollWheelZoom elapsed 1) // Don't check keys that can be used in shortcuts outside this block: - (unless (or sh.currentMap (and textInput textInput.hasFocus)) + (unless (or (and shw (shw.isShown)) (and textInput textInput.hasFocus)) (when FlxG.keys.justPressed.SEMICOLON (set confirmQuit false) - (sh.start) + (set shw (new KeyShortcutWindow shortcutHandler ->[:Entry e] (readComponentOr e Name "unnamed entry"))) + (set shw.cameras [uiCamera]) + (shw.show) (return)) // +/- keys to change an entry's z (doFor e (controller.getSelectedEntries)