Can I use HaxeUI with HaxeFlixel?

1.1k views Asked by At

I tried to use both HaxeUI and HaxeFlixel, but what I obtain is HaxeUI's interface over a white background, covering everything underneath. Moreover, even if it was possible to somewhat make HaxeUI and HaxeFlixel work together, it's not clear how to change the UI of HaxeUI when the state change in HaxeFlixel. Here is the code I used:

private function setupGame():Void {

    Toolkit.theme = new GradientTheme();
    Toolkit.init();

    var stageWidth:Int = Lib.current.stage.stageWidth;
    var stageHeight:Int = Lib.current.stage.stageHeight;

    if (zoom == -1) {
        var ratioX:Float = stageWidth / gameWidth;
        var ratioY:Float = stageHeight / gameHeight;
        zoom = Math.min(ratioX, ratioY);
        gameWidth = Math.ceil(stageWidth / zoom);
        gameHeight = Math.ceil(stageHeight / zoom);
    }

    trace('stage: ${stageWidth}x${stageHeight}, game: ${gameWidth}x${gameHeight}, zoom=$zoom');
    addChild(new FlxGame(gameWidth, gameHeight, initialState, zoom, framerate, framerate, skipSplash, startFullscreen));

    Toolkit.openFullscreen(function(root:Root) {
        var view:IDisplayObject = Toolkit.processXmlResource("assets/xml/haxeui-resource.xml");
        root.addChild(view);
    });
}

I can guess that, probably, both HaxeUI and HaxeFlixel have their own main loop and that their event handling might not be compatible, but just in case, can someone have a more definitive answer?

Edit:

Actually, it's much better when using openPopup:

Toolkit.openPopup( { x:20, y:150, width:100, height:100 }, function(root:Root) {
            var view:IDisplayObject = Toolkit.processXmlResource("assets/xml/haxeui-naming.xml");
            root.addChild(view);
        });

It's possible to interact with the rest of the screen (managed with HaxeFlixel), but the mouse pointer present in the part of the screen managed with HaxeFlixel remains under the HaxeUI user interface elements.

3

There are 3 answers

1
JaytleBee On BEST ANSWER

When you open a fullscreen or popup with haxeui, the program flow will be blocked (your update() and draw() function won't be called). You should probably have a look at flixel-ui instead.

1
TiagoLr On

From my experience haxeflixel and haxeui work well together but they are totally independent projects, and as such, any coordination between flixel states and displayed UI must be added by the coder.

I don't recall having the white background problem you mention, it shouldn't happen unless haxeui root sprite has a solid background, in that case it should be addressed to haxeui project maintainer.

0
BHarms On

When using Flixel and HaxeUI together, its almost like running two applications at once. However, they both rely on OpenFL as a back-end and each attach themselves to its display tree.

One technique I'm experimenting with right now is to open a Flixel sub state, and within the sub state, call Toolkit.openFullscreen(). From inside of this, you can set the alpha of the root's background to 0, which allows you to see through it onto the underlying bitmap that Flixel uses to render.

Here is a minimal example of how you might "embed" an editor interface inside a Flixel sub state:

import haxe.ui.toolkit.core.Toolkit;
import haxe.ui.toolkit.core.RootManager;
import haxe.ui.toolkit.themes.DefaultTheme;

import flixel.FlxG;
import flixel.FlxSubState;

// This would typically be a Haxe UI XMLController
import app.MainEditor;

class HaxeUIState extends FlxSubState
{

    override public function create()
    {
        super.create();

        // Flixel uses a sprite-based cursor by default,
        // so you need to enable the system cursor to be
        // able to see what you're clicking.
        FlxG.mouse.useSystemCursor = true;

        Toolkit.theme = new DefaultTheme();
        Toolkit.init();
        Toolkit.openFullscreen(function (root) {
            var editor = new MainEditor();

            // Allows you to see what's going on in the sub state
            root.style.backgroundAlpha = 0;
            root.addChild(editor.view);
        });
    }

    override public function destroy()
    {
        super.destroy();

        // Switch back to Flixel's cursor
        FlxG.mouse.useSystemCursor = true;

        // Not sure if this is the "correct" way to close the UI,
        // but it works for my purposes. Alternatively you could
        // try opening the editor in advance, but hiding it
        // until the sub-state opens.
        RootManager.instance.destroyAllRoots();
    }

    // As far as I can tell, the update function continues to get
    // called even while Haxe UI is open.
    override public function update() {
        super.update();

        if (FlxG.keys.justPressed.ESCAPE) {
          // This will implicitly trigger destroy().
          close();
        }
    }
}

In this way, you can associate different Flixel states with different Haxe UI controllers. (NOTE: They don't strictly have to be sub-states, that's just what worked best in my case.)