From 0f227a5a5d716adaafac0732dcd19703cf2d5a8e Mon Sep 17 00:00:00 2001 From: Yoshman29 <81191621+YoshiCrafter29@users.noreply.github.com> Date: Fri, 24 Mar 2023 13:13:06 +0100 Subject: [PATCH] new keybinds system for topmenus --- source/funkin/editors/charter/Charter.hx | 94 ++++++++--------------- source/funkin/editors/ui/UIContextMenu.hx | 8 +- source/funkin/editors/ui/UIUtil.hx | 62 +++++++++++++++ 3 files changed, 100 insertions(+), 64 deletions(-) diff --git a/source/funkin/editors/charter/Charter.hx b/source/funkin/editors/charter/Charter.hx index 1cd35a2c..3e625660 100644 --- a/source/funkin/editors/charter/Charter.hx +++ b/source/funkin/editors/charter/Charter.hx @@ -95,31 +95,31 @@ class Charter extends UIState { childs: [ { label: "Undo", - keybind: "Ctrl+Z", + keybind: [CONTROL, Z], onSelect: _edit_undo }, { label: "Redo", - keybind: "Ctrl+Y", + keybind: [CONTROL, Y], onSelect: _edit_redo }, null, { label: "Cut", - keybind: "Ctrl+X" + keybind: [CONTROL, X] }, { label: "Copy", - keybind: "Ctrl+C" + keybind: [CONTROL, C] }, { label: "Paste", - keybind: "Ctrl+V" + keybind: [CONTROL, V] }, null, { label: "Delete", - keybind: "Del", + keybind: [DELETE], onSelect: _edit_delete } ] @@ -129,35 +129,35 @@ class Charter extends UIState { childs: [ { label: "Playtest", - keybind: "Enter" + keybind: [ENTER] }, { label: "Playtest here", - keybind: "Shift+Enter" + keybind: [SHIFT, ENTER] }, null, { label: "Playtest as opponent", - keybind: "Ctrl+Enter" + keybind: [CONTROL, ENTER] }, { label: "Playtest as opponent here", - keybind: "Ctrl+Shift+Enter" + keybind: [CONTROL, SHIFT, ENTER] }, null, { label: "Zoom in", - keybind: "Ctrl+[+]", + keybind: [CONTROL, NUMPADPLUS], onSelect: _chart_zoomin }, { label: "Zoom out", - keybind: "Ctrl+[-]", + keybind: [CONTROL, NUMPADMINUS], onSelect: _chart_zoomout }, { label: "Reset zoom", - keybind: "Ctrl+[0]", + keybind: [CONTROL, NUMPADZERO], onSelect: _chart_zoomreset }, null, @@ -171,29 +171,29 @@ class Charter extends UIState { childs: [ { label: "Play/Pause", - keybind: "Space", + keybind: [SPACE], onSelect: _playback_play }, null, { label: "Go back a section", - keybind: "A", + keybind: [A], onSelect: _playback_back }, { label: "Go forward a section", - keybind: "D", + keybind: [D], onSelect: _playback_forward }, null, { label: "Go back to the start", - keybind: "Home", + keybind: [HOME], onSelect: _playback_start }, { label: "Go to the end", - keybind: "End", + keybind: [END], onSelect: _playback_end }, null, @@ -503,7 +503,20 @@ class Charter extends UIState { // TODO: canTypeText in case an ui input element is focused if (true) { - handleShortcuts(); + __crochet = ((60 / Conductor.bpm) * 1000); + + UIUtil.processShortcuts(topMenu); + + if (FlxG.keys.pressed.CONTROL) { + if (FlxG.mouse.wheel != 0) { + zoom += 0.25 * FlxG.mouse.wheel; + __camZoom = Math.pow(2, zoom); + } + } else { + if (!FlxG.sound.music.playing) { + Conductor.songPosition -= __crochet * FlxG.mouse.wheel; + } + } } Conductor.songPosition = FlxMath.bound(Conductor.songPosition, 0, FlxG.sound.music.length); @@ -519,49 +532,6 @@ class Charter extends UIState { } } - public function handleShortcuts() { - if (FlxG.keys.justPressed.DELETE) // DELETE - _edit_delete(null); - - __crochet = ((60 / Conductor.bpm) * 1000); - - if (FlxG.keys.pressed.CONTROL) { - if (FlxG.keys.justPressed.Y || (FlxG.keys.pressed.SHIFT && FlxG.keys.justPressed.Z)) // CTRL+Y or CTRL+SHIFT+Z - _edit_redo(null); - else if (FlxG.keys.justPressed.Z) // CTRL+Z - _edit_undo(null); - else if (FlxG.keys.justPressed.NUMPADPLUS) // CTRL+[+] - _chart_zoomin(null); - else if (FlxG.keys.justPressed.NUMPADMINUS) // CTRL+[-] - _chart_zoomout(null); - else if (FlxG.keys.justPressed.NUMPADZERO) // CTRL+[0] - _chart_zoomreset(null); - - - if (FlxG.mouse.wheel != 0) { - zoom += 0.25 * FlxG.mouse.wheel; - __camZoom = Math.pow(2, zoom); - } - } else { - if (!FlxG.sound.music.playing) { - Conductor.songPosition -= __crochet * FlxG.mouse.wheel; - } - } - - if (FlxG.keys.justPressed.SPACE) // SPACE - _playback_play(null); - - - if (FlxG.keys.justPressed.D) - _playback_forward(null); - if (FlxG.keys.justPressed.A) - _playback_back(null); - if (FlxG.keys.justPressed.HOME) - _playback_start(null); - if (FlxG.keys.justPressed.END) - _playback_end(null); - } - var zoom:Float = 0; var __camZoom:Float = 1; diff --git a/source/funkin/editors/ui/UIContextMenu.hx b/source/funkin/editors/ui/UIContextMenu.hx index 1f571364..7fbd53e7 100644 --- a/source/funkin/editors/ui/UIContextMenu.hx +++ b/source/funkin/editors/ui/UIContextMenu.hx @@ -1,5 +1,7 @@ package funkin.editors.ui; +import flixel.input.keyboard.FlxKey; + class UIContextMenu extends MusicBeatSubstate { public var options:Array; var x:Float; @@ -111,7 +113,7 @@ class UIContextMenu extends MusicBeatSubstate { typedef UIContextMenuCallback = UIContextMenu->Int->UIContextMenuOption->Void; typedef UIContextMenuOption = { var label:String; - var ?keybind:String; + var ?keybind:Array; var ?icon:Int; var ?onSelect:UIContextMenuOption->Void; var ?childs:Array; @@ -137,7 +139,9 @@ class UIContextMenuOptionSpr extends UISliceSprite { } if (option.keybind != null) { - labelKeybind = new UIText(label.x + label.frameWidth + 10, 2, 0, option.keybind); + var text = ""; + text += [for(o in option.keybind) o.toUIString()].join("+"); + labelKeybind = new UIText(label.x + label.frameWidth + 10, 2, 0, text); labelKeybind.alpha = 0.75; } diff --git a/source/funkin/editors/ui/UIUtil.hx b/source/funkin/editors/ui/UIUtil.hx index 766c2264..23b0cd41 100644 --- a/source/funkin/editors/ui/UIUtil.hx +++ b/source/funkin/editors/ui/UIUtil.hx @@ -1,5 +1,6 @@ package funkin.editors.ui; +import flixel.input.keyboard.FlxKey; import funkin.editors.ui.UIContextMenu.UIContextMenuOption; class UIUtil { @@ -12,4 +13,65 @@ class UIUtil { public static function contextMenuOpened(contextMenu:UIContextMenu) { return contextMenu != null && UIState.state.curContextMenu == contextMenu; } + + /** + * Process all options with shortcuts present in a `Array`. Also checks childrens. + * @param topMenuOptions + */ + public static function processShortcuts(topMenuOptions:Array) { + for(o in topMenuOptions) { + if (o == null) continue; + + if (o.keybind != null) { + var pressed = true; + var justPressed = false; + for(keybind in o.keybind) { + if (FlxG.keys.checkStatus(keybind, JUST_PRESSED)) { + justPressed = true; + } else if (!FlxG.keys.checkStatus(keybind, PRESSED)) { + pressed = false; + break; + } + } + if (!pressed || !justPressed) continue; + + if (o.onSelect != null) + o.onSelect(o); + + return true; + } + + if (o.childs != null && processShortcuts(o.childs)) + return true; + } + return false; + } + + public static function toUIString(key:FlxKey):String { + return switch(key) { + case CONTROL: #if mac "Cmd" #else "Ctrl" #end; + case HOME: "Home"; + case ENTER: "Enter"; + case DELETE: "Del"; + case SHIFT: "Shift"; + case SPACE: "Space"; + case NUMPADZERO: "[0]"; + case NUMPADONE: "[1]"; + case NUMPADTWO: "[2]"; + case NUMPADTHREE: "[3]"; + case NUMPADFOUR: "[4]"; + case NUMPADFIVE: "[5]"; + case NUMPADSIX: "[6]"; + case NUMPADSEVEN: "[7]"; + case NUMPADEIGHT: "[8]"; + case NUMPADNINE: "[9]"; + case NUMPADPLUS: "[+]"; + case NUMPADMINUS: "[-]"; + default: prettify(key.toString()); + } + } + + public static function prettify(str:String) { + return [for(s in str.split(" ")) [for(k=>l in s.split("")) k == 0 ? l.toUpperCase() : l.toLowerCase()].join("")].join(" "); + } } \ No newline at end of file