While there is no real modal panel, here's a dirty hack. [HACK]

Got fed up enough to come up with a hack to enable a sort of modal panel.

It’s pretty simple really.

It’s a fullscreen fully transparent panel (starts at 0, 0 and use

screen.getApplication().getContext().getSettings().getWidth()
and
screen.getApplication().getContext().getSettings().getHeight()

since those are readily available from the ElementManager.

Note that you have to pass the element you want as modal in the constructor.

It’s slightly convoluted, but it works. Using that way anything beneath that ModalPanel isn’t interactive, thus can’t be clicked or interacted with, as it should when modal. This is definitively not the right way to do it, but for now, it works. That’s more than the actual way. :stuck_out_tongue:

[java]
/**

  • A {@link Panel} faking MODALITY.

  • @author MadJack
    */
    public class ModalPanel extends Element implements MouseButtonListener {

    private Element interactiveElement;
    // private GuiManager manager;

    public ModalPanel(ElementManager screen, String UID, /*GuiManager manager, */Element boundaryElement) {
    super(screen, UID, new Vector2f(0, 0), new Vector2f(screen.getApplication().getContext().getSettings().getWidth(), screen.getApplication().getContext().getSettings().getHeight()), new Vector4f(0, 0, 0, 0), null);
    setIsMovable(false);
    setIsResizable(false);
    setZOrder(999f);
    this.interactiveElement = boundaryElement;
    // this.manager = manager;
    addToPanel(interactiveElement);
    }

    private void addToPanel(Element element) {
    interactiveElement = element;
    if (interactiveElement != null) {
    addChild(interactiveElement);
    }
    }

    public void setInteractiveElement(Element element) {
    addToPanel(element);
    }

    @Override
    public void onMouseLeftPressed(MouseButtonEvent evt) {
    }

    /**

    • This is used to compensate the lack of interactivity with Panels.
    • For now this is only used when the “modal” windows are visible, we intercept
    • mouse interaction of the “out of interactive area” events and trigger a
    • “negative” sound to indicate failure to process. Like a real modal element.
    • @param evt The mouse event that was triggered.
      */
      @Override
      public void onMouseLeftReleased(MouseButtonEvent evt) {
      if (getIsVisible() && interactiveElement != null) {
      float x = interactiveElement.getX();
      float y = interactiveElement.getY();
      float xW = x + interactiveElement.getWidth();
      float yW = y + interactiveElement.getHeight();
      if (evt.getX() < x || evt.getY() < y || evt.getX() > xW || evt.getY() > yW) {
      // off the interactive area, play DENIED sound.
      // manager.playClickDenied();
      evt.setConsumed();
      }
      }
      }

    @Override
    public void onMouseRightPressed(MouseButtonEvent evt) {
    }

    @Override
    public void onMouseRightReleased(MouseButtonEvent evt) {
    }

}
[/java]

Hopefully that’ll be useful for someone.

2 Likes

This is really cool @madjack . I thought I had something like this in the library already, but apparently, I don’t. Is this something you would like added to the screen? If so, let me know and what method name you would like associated with it.

@t0neg0d said: This is really cool @madjack . I thought I had something like this in the library already, but apparently, I don't. Is this something you would like added to the screen? If so, let me know and what method name you would like associated with it.

This should be how modal should be actually. So, if you want to “fix” it, be my guest and use that.