context menu

This commit is contained in:
Yoshman29
2023-03-19 13:49:15 +01:00
parent 36ab52b160
commit 98df016360
5 changed files with 54 additions and 11 deletions
Binary file not shown.

Before

Width:  |  Height:  |  Size: 906 B

After

Width:  |  Height:  |  Size: 983 B

+1 -1
View File
@@ -1 +1 @@
2609
2611
+4 -4
View File
@@ -74,22 +74,22 @@ class UIDebugState extends UIState {
{
label: "Test 1",
onSelect: function() {
trace("Test 1 clicked");
}
},
{
label: "Test 2",
onSelect: function() {
trace("Test 2 clicked");
}
},
{
label: "Test 3",
childs: [
{
label: "Test 2",
label: "Test 4",
onSelect: function() {
trace("Test 4 clicked");
}
}
]
+46 -4
View File
@@ -7,12 +7,16 @@ class UIContextMenu extends MusicBeatSubstate {
var contextCam:FlxCamera;
var bg:UISpliceSprite;
var callback:UIContextMenuCallback;
public function new(options:Array<UIContextMenuOption>, x:Float, y:Float) {
public var contextMenuOptions:Array<UIContextMenuOptionSpr> = [];
public function new(options:Array<UIContextMenuOption>, callback:UIContextMenuCallback, x:Float, y:Float) {
super();
this.options = options.getDefault([]);
this.x = x;
this.y = y;
this.callback = callback;
}
public override function create() {
@@ -24,6 +28,34 @@ class UIContextMenu extends MusicBeatSubstate {
bg = new UISpliceSprite(x, y, 100, 100, 'editors/ui/context-bg');
add(bg);
var lastY:Float = bg.y + 4;
for(o in options) {
var spr = new UIContextMenuOptionSpr(bg.x + 4, lastY, o, this);
lastY = spr.y + spr.bHeight;
contextMenuOptions.push(spr);
add(spr);
}
var maxW = bg.bWidth - 8;
for(o in contextMenuOptions)
if (o.bWidth > maxW)
maxW = o.bWidth;
for(o in contextMenuOptions)
o.bWidth = maxW;
bg.bWidth = maxW + 8;
bg.bHeight = Std.int(lastY - bg.y + 4);
}
public function select(option:UIContextMenuOption) {
var index = options.indexOf(option);
if (option.onSelect != null)
option.onSelect();
if (callback != null)
callback(this, index, option);
close();
}
public override function update(elapsed:Float) {
@@ -41,6 +73,7 @@ class UIContextMenu extends MusicBeatSubstate {
}
}
typedef UIContextMenuCallback = UIContextMenu->Int->UIContextMenuOption->Void;
typedef UIContextMenuOption = {
var label:String;
var ?icon:Int;
@@ -52,17 +85,26 @@ class UIContextMenuOptionSpr extends UISpliceSprite {
public var label:UIText;
public var option:UIContextMenuOption;
public function new(x:Float, y:Float, option:UIContextMenuOption) {
var parent:UIContextMenu;
public function new(x:Float, y:Float, option:UIContextMenuOption, parent:UIContextMenu) {
label = new UIText(20, 2, 0, option.label);
this.option = option;
this.parent = parent;
super(x, y, label.frameWidth + 22, label.frameHeight + 4, 'editors/ui/button');
super(x, y, label.frameWidth + 22, label.frameHeight, 'editors/ui/button');
members.push(label);
}
public override function draw() {
alpha = hovered ? 1 : 0;
label.follow(this, 20, 2);
label.follow(this, 20, 0);
super.draw();
}
public override function onHovered() {
super.onHovered();
if (FlxG.mouse.justReleased)
parent.select(option);
}
}
+3 -2
View File
@@ -1,5 +1,6 @@
package funkin.editors.ui;
import funkin.editors.ui.UIContextMenu.UIContextMenuCallback;
import openfl.ui.Mouse;
import funkin.editors.ui.UIContextMenu.UIContextMenuOption;
import flixel.math.FlxPoint;
@@ -73,7 +74,7 @@ class UIState extends MusicBeatState {
__mousePos.put();
}
public function openContextMenu(options:Array<UIContextMenuOption>, ?x:Float, ?y:Float) {
public function openContextMenu(options:Array<UIContextMenuOption>, ?callback:UIContextMenuCallback, ?x:Float, ?y:Float) {
var state = FlxG.state;
while(state.subState != null)
state = state.subState;
@@ -81,6 +82,6 @@ class UIState extends MusicBeatState {
state.persistentDraw = true;
state.persistentUpdate = true;
openSubState(new UIContextMenu(options, x.getDefault(__mousePos.x), y.getDefault(__mousePos.y)));
openSubState(new UIContextMenu(options, callback, x.getDefault(__mousePos.x), y.getDefault(__mousePos.y)));
}
}