Is it possible to create context menu in UMG???

Context menus are pretty standard part of any game or application, but i’m having a big problem to get it them to work. In theory it sounds easy, right click on some widget, and spawn a context menu widget on a mouse position. Well that part works perfectly well.

The problem begins when you want to cancel the context menu. The way it should work is that if you click anywhere outside the context menu it should cancel the context menu. How would i do that so that it works even if i click on some other widget… for example button??? It has to disappear regardless of me clicking on an empty space or some other widget/button

This is not a definitive answer regarding how it *should *be done, but rather an idea that popped for me:

  • Make the context menu sit in an otherwise blank, full screen canvas.
  • When creating the context menu, give it high enough Z, so it takes priority over any other viewport elements.
  • Override the onMouseClick - remove from the viewport.
  • Clicking on the canvas removes the whole thing, clicking on the context menu does context menu stuff.

This approach was suggested to me numerous times, but there is a flaw. When i’d click on canvas it would consume the mouse event and it wouldn’t get propagated to some other widget that in theory could be behind the canvas at a cursor location. This is not how context menus work, is it?

The context menus work like if you click for example on some other widget that is outside the context menu it would both close the menu and the mouse event of the other widget would be handled as well.

Perhaps there is a way for canvas to handle the event and then pass it down?

franktech thanks for the workaround suggestion but that’s not what i want. I would like a context menu as described in the original post

“Might be something there. If not, then can you manually route the close?
Make underlying widgets accept all clicks & close any menus that exist…”

Yes that’s the only solution i could think of. But it looks terrible having to have all other widgets implement some logic to close the popup menu.

I’m still open to possible solutions to this problem…

I have a suggestion (though without, I’m afraid, the full solution because I’m not somewhere I can test/figure it out right now).

If you can figure out a way to fire a mouse click manually at a particular point in screen-space, then you could use @Everynone’s suggestion of having an invisible canvas that consumes the off-menu mouse-click, and does two things:

(1) Closes the context menu (and canvas etc.)
(2) Takes the screen-space location of the mouse and immediately fires another mouse click in the exact same location, so that if there is a button/widget beneath it, it will be clicked upon.

I don’t know if it’s possible to ‘simulate’ a mouse click in a specific location like this, but I don’t see why not.

Something I’ve done is to, in the player controller, whenever a mouse click is made an event is called. This event tells the context menu to close. Though I had to set a boolean in the context menu that it was recently opened, which with a delay of 0 (next frame) is reset.

This way, if the context menu is open and a non-button location is clicked, it will be closed.
The problem remains that buttons consume the mouse click events in the player controller. Can that be disabled?

But this all feels very hacky and it feels like there’s a decent risk of changes as the engine updates.

@**localstarlight **your idea sound reasonable, but i haven’t been able to google a way to fire a mouse event from code.

I think we’ve established there is no a straightforward unhacky way to do this (if any at all). Is there a way i could bring this problem to Epic in order to have them implement some solution to this problem?

@**mihovil **Have a look at MenuAnchorwidget. It’s rather great at handling context menus. It also seems to do what you need:

1 Like

Seems exactly what i needed :DDDDD
Thanks!

@Everynone

i know this question is coming kinda late, but how did you actually implement the closing part in that animated gif you posted?
is it that every button has some logic that closes the menu anchor, or using some other methods?

I did not and that’s the best part. If I remember correctly, that’s one thing that the anchor does automagically. It kills the roll-out as soon as it loses focus or fails to handle the input.