new keybinds system for topmenus

This commit is contained in:
Yoshman29
2023-03-24 13:13:06 +01:00
parent e592144f93
commit 0f227a5a5d
3 changed files with 100 additions and 64 deletions
+32 -62
View File
@@ -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;
+6 -2
View File
@@ -1,5 +1,7 @@
package funkin.editors.ui;
import flixel.input.keyboard.FlxKey;
class UIContextMenu extends MusicBeatSubstate {
public var options:Array<UIContextMenuOption>;
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<FlxKey>;
var ?icon:Int;
var ?onSelect:UIContextMenuOption->Void;
var ?childs:Array<UIContextMenuOption>;
@@ -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;
}
+62
View File
@@ -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<UIContextMenuOption>`. Also checks childrens.
* @param topMenuOptions
*/
public static function processShortcuts(topMenuOptions:Array<UIContextMenuOption>) {
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(" ");
}
}