jMonkeyEngine-Contributions / Lemur

Lemur is a jMonkeyEngine-based UI toolkit.
http://jmonkeyengine-contributions.github.io/Lemur/
BSD 3-Clause "New" or "Revised" License
116 stars 32 forks source link

Proposal: A handy method in GuiGlobals to position Panel #79

Open Ali-RS opened 5 years ago

Ali-RS commented 5 years ago

I have a GuiManager with a static method to position a Panel on gui (ex: Center, Left, Right, Up, Bottom)

GuiManager.setPosition(panel, GuiManager.Position.CENTER, getApplication().getCamera());

public static void setPosition(Panel panel, Position position, Camera cam) {
        // Apply standard scale
        float scale = getStandardScale(cam);
        panel.setLocalScale(1.2f * scale);

        int width = cam.getWidth();
        int height = cam.getHeight();

        Vector3f prefSize = new Vector3f(panel.getPreferredSize());
        prefSize.multLocal(1.2f * scale);
        // Position window to selected gui pos
        switch (position) {
            case CENTER:
                panel.setLocalTranslation((width - prefSize.x) / 2, (height + prefSize.y) / 2, 0);
                break;
            case RIGHT:
                panel.setLocalTranslation((width - prefSize.x), (height + prefSize.y) / 2, 0);
                break;
            case LEFT:
                panel.setLocalTranslation(0, (height + prefSize.y) / 2, 0);
                break;
            case TOP:
                panel.setLocalTranslation((width - prefSize.x) / 2, height, 0);
                break;
            case BOTTOM:
                panel.setLocalTranslation((width - prefSize.x) / 2, prefSize.y, 0);
                break;
        }
    }

Would be something similar useful to add in GuiGlobals ?

pspeed42 commented 5 years ago

I don't think it belongs on GuiGlobals as it isn't really what it's for and also "which camera" becomes a critial question centrally. On something lilke PopupState it probably makes sense because that both is already tied to one camera/root and has a concept of a scaling that is different than Camera by itself. (Useful for resolution independent UIs.)

Wherever the method goes, it doesn't need its own position enum, though. It can just combine HAlignment and VAlignment... which would make that method a bit simpler anyway.

mrlem commented 4 years ago

I feel the right way to achieve this would be put the target panel in a fullscreen container and let the layout do the positioning. However, this currently isn't a panacea: from what I gather in Lemur's code, it would prevent you from animating translations (due to a clash in layout vs animation mechanisms both intervening on the panel's local translation).

But I may be wrong, having only a fairly recent interest in Lemur 😛.