WIP KeyShortcutWindow for NAT flixel

This commit is contained in:
2022-08-10 13:51:12 +00:00
parent a32e5fd09b
commit 45df11a7b7
5 changed files with 50 additions and 9 deletions

View File

@@ -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<T> extends SimpleWindow {}

View File

@@ -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<T> 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))

View File

@@ -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))

View File

@@ -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;

View File

@@ -109,13 +109,10 @@
(clearUI))
(prop &mut :Bool confirmQuit false)
(defAlias &ident sh (cast shortcutHandler FlxKeyShortcutHandler<Dynamic>))
(prop &mut :KeyShortcutWindow<Entry> 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)