Skip to content

Commit

Permalink
Fixed popup not closing on clicking the buttons (Fix for HaxeFlixel#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
UncertainProd committed Nov 27, 2023
1 parent 7819729 commit 0523212
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion UserInterface/RPGInterface/source/State_DefaultTest.hx
@@ -1,3 +1,7 @@
import flixel.addons.ui.FlxUI;
import flixel.addons.ui.interfaces.IFlxUIState;
import flixel.addons.ui.FlxUITypedButton;
import flixel.addons.ui.interfaces.IFlxUIWidget;
import flixel.addons.ui.FlxUIPopup;
import flixel.FlxG;
import flixel.addons.ui.FlxUIState;
Expand Down Expand Up @@ -29,7 +33,7 @@ class State_DefaultTest extends FlxUIState
{
case "back": FlxG.switchState(new State_Title());
case "popup":
var popup:FlxUIPopup = new FlxUIPopup(); // create the popup
var popup:FlxUIPopup = new PopupFix(); // create the popup
popup.quickSetup // set it up
(Main.tongue.get("$POPUP_DEMO_2_TITLE", "ui"), // title text
Main.tongue.get("$POPUP_DEMO_2_BODY", "ui"), // body text
Expand All @@ -55,3 +59,44 @@ class State_DefaultTest extends FlxUIState
}
}
}

/**
* An extension to `FlxUIPopup` that fixes a bug in `getEvent` where the event was not being broadcast correctly to the parent widget
*/
class PopupFix extends FlxUIPopup
{
override function getEvent(id:String, sender:IFlxUIWidget, data:Dynamic, ?eventParams:Array<Dynamic>)
{
if (eventParams == null)
{
if (params != null)
{
eventParams = [];
}
}
if (params != null)
{
eventParams = eventParams.concat(params);
}

switch (id)
{
case FlxUITypedButton.CLICK_EVENT:
if (eventParams != null)
{
var buttonAmount:Int = Std.int(eventParams[0]); // 0 is Yes, 1 is No and 2 is Cancel
if ((_parentState is IFlxUIState))
{
// This fixes a bug where the event was being sent to this popup rather than the state that created it
castParent().getEvent(FlxUIPopup.CLICK_EVENT, this, buttonAmount, eventParams);
}
else
{
// This is a generic fallback in case something goes wrong
FlxUI.event(FlxUIPopup.CLICK_EVENT, this, buttonAmount, eventParams);
}
close();
}
}
}
}

0 comments on commit 0523212

Please sign in to comment.