From 98df01636038e1fdacdb207bdbf0eb6f1c05743d Mon Sep 17 00:00:00 2001 From: Yoshman29 <81191621+YoshiCrafter29@users.noreply.github.com> Date: Sun, 19 Mar 2023 13:49:15 +0100 Subject: [PATCH] context menu --- assets/images/editors/ui/context-bg.png | Bin 906 -> 983 bytes buildnumber.txt | 2 +- source/funkin/editors/UIDebugState.hx | 8 ++-- source/funkin/editors/ui/UIContextMenu.hx | 50 ++++++++++++++++++++-- source/funkin/editors/ui/UIState.hx | 5 ++- 5 files changed, 54 insertions(+), 11 deletions(-) diff --git a/assets/images/editors/ui/context-bg.png b/assets/images/editors/ui/context-bg.png index 3f070653fc9b5868e516c566947552c8a8b8c49a..757ccfc18be29494385753495331cd7f801302ca 100644 GIT binary patch delta 940 zcmV;d15^Bp2iFIXIe&>sL_t(|+U?s(OjAJ^2k?1?LR-4f@(KuPDT3jE1QaZbA{q)L z1`h7ggE7IQ(P*$xLZpPf2ZA0vuv|SD@j#3b1%vD&pwSpP04XgZODLral$Q4SS}h_b z#Du^IGykNgN!suII`ca7nvb9;3T(_JQ4#N;JT#xl^T}fin1AtH(vJtWh|}Tp0}$iG z^s#R}&a^G*t;05xt97(f*Zjm#W8OGUYy`L|ud0xx1*?{3tqkkJDljivfpdiddpQ9? z{(vvy01oK~*a7~ov7Wi|M?=NxzW{EZuC3(VJsDpi=#pg?)SR-f&44Af;Sj)Y&TQJC!BGd9jL2z7jb<+A{?lt9yI8 z|7-X(f;52ygO9ah;;y2uNFedA9vhs2UDvKAGEod1i-d|OKKHQ8YF$k?OqpQUw5th= z4g_poKsTYf{J=0fz9`)9YkzYB#Qq>WCTNM!6f{tu+kf=1>w%KE90|Yxnt~?kL)#?m zTA<`eP6P(P02lxRAP>+$4ZYIAt_4bxvZa8izSoHM48Q<1zzCvC;HY;AAP?~VHJVG{ zSXKlEzyQbtRMG%L?P%TvlN3@6fB}#PC@*ZL4?|kmwZH+$mSF%7z#yUk@}B^W)H_7? z0m<%t0DlIc0R|8~4W?vE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DF(~Fc=2F_!ghix(Rr|o%-U> zUsp~}OiNXjHp$rkcI|k2F*~5h-d}P!lF4$Ab2z)DLJya9=ks$k&*yFaF`u`yvz=;f o?PxTa%yn-X-q-#{?W>;u1Dr(w@(CjkfB*mh07*qoM6N<$g4UybTmS$7 diff --git a/buildnumber.txt b/buildnumber.txt index b8ffba82..43e249ec 100644 --- a/buildnumber.txt +++ b/buildnumber.txt @@ -1 +1 @@ -2609 \ No newline at end of file +2611 \ No newline at end of file diff --git a/source/funkin/editors/UIDebugState.hx b/source/funkin/editors/UIDebugState.hx index ed6c2a44..8efc97ed 100644 --- a/source/funkin/editors/UIDebugState.hx +++ b/source/funkin/editors/UIDebugState.hx @@ -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"); } } ] diff --git a/source/funkin/editors/ui/UIContextMenu.hx b/source/funkin/editors/ui/UIContextMenu.hx index 9190fcd9..9ae9eeab 100644 --- a/source/funkin/editors/ui/UIContextMenu.hx +++ b/source/funkin/editors/ui/UIContextMenu.hx @@ -7,12 +7,16 @@ class UIContextMenu extends MusicBeatSubstate { var contextCam:FlxCamera; var bg:UISpliceSprite; + var callback:UIContextMenuCallback; - public function new(options:Array, x:Float, y:Float) { + public var contextMenuOptions:Array = []; + + public function new(options:Array, 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); + } } \ No newline at end of file diff --git a/source/funkin/editors/ui/UIState.hx b/source/funkin/editors/ui/UIState.hx index 28f2c79f..1843c28d 100644 --- a/source/funkin/editors/ui/UIState.hx +++ b/source/funkin/editors/ui/UIState.hx @@ -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, ?x:Float, ?y:Float) { + public function openContextMenu(options:Array, ?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))); } } \ No newline at end of file