Dynamically set widget position on canvas panel?

I have been using SetRenderTransform and AddToViewport on my context menu until now, which pops up wherever you hold the right button. The problem is that there is nothing to stop the menu from extending beyond the viewport limits. I tried to clamp the render transform using the widget’s DesiredSize, but it’s always zero.

Then I figured it might be best to attach the menu to a “global” canvas panel filling out the whole screen and enable “Clip To Bounds” on it, so the whole thing is handled automatically. But no matter what I do, the menu always shows up on the very top-left corner.

How can I position it at my original render transform?

2 Likes

Dynamically set widget position on
canvas panel?

Get widget > As Canvas Slot - set Size / Position.

8 Likes

Now the position gets set, but the menu still extends beyond the edge of the viewport…

Can you show the hierarchy so we’re both sure what we’re dealing with. And what is the desired result, exactly? A full screen widget the size of the canvas?

Also, are you using anchors at all?

edit: I can swear I just saw your name on the forums

The hierarchy is: I have a kind of root-widget that all other UserWidget-derived widgets are either a part of, or they get dynamically attached to and removed from a panel of it. The root-panel of everything is a canvas in this root-widget. Like I described in the original question, I was hoping I could stop my context menus from being partly off-screen when clicking near the right or bottom edge by using the panel’s “Clipping” property. They are rather small, not full screen. I don’t know how to use anchors dynamically. The only function I found related to anchors was “Set Anchors In Viewport” and I’m not sure how to use it.

… and you probably saw my name here : Widget out of screen when opened with an event - Blueprint Visual Scripting - Unreal Engine Forums

My context menus open in a way that their upper left corner is at the mouse cursor (with a margin so that the cursor is on top of it). I only want to shift them when they would otherwise escape the screen, so using a bottom-right anchor for all wouldn’t be much help, I think. And then I would have the same problem in the upper left corner. But your forum reply should have the solution for me.

They are rather small, not full
screen. I don’t know how to use
anchors dynamically. The only function
I found related to anchors was “Set
Anchors In Viewport” and I’m not sure
how to use it.

Does not need to be dynamic. Let’s say I need this element to always be in the bottom right corner, no matter the size:

257138-untitled.png

Click the anchor normally, then shift-click it, and then control-click it. This will set all the pivots and alignment automatically so you do not need to punch it in manually.


If for some reason you need to use it dynamically, you can get access to the canvas slot (as in the original answer) and set the anchors and other slot related settings.

But setting the pivots should just work fine. Even for elements that resize dynamically. An expandable menu in the bottom right corner of a screen:

Image from Gyazo

Does it help?

Ah, I see. Then yes, you should be able to predict the the desired size with force prepass and offset it accordingly.


Alternatively, you can read the location of the cursor and compare it to viewport size. If the location of the cursor + widget size would exceed the dimensions of the screen, offset the widget position by the desired distance from the screen edge.

The above is a decent method if you want the icons to stick to the edge of the screen - like indicators for objects that have moved off-screen. Or for keeping a widget inside the screen space even if the object has left it.


Anyhow, glad you found a solution.

Good luck with the rest!

Yes, I just clamped the final position (top-left corner) to viewport size minus widget size. Works as expected. And thanks again!

Dropping this here in case anyone finds it through google, I didn’t find my solution with the above post.

This has everything you could need:

1 Like

Hi! I have 3 expandable areas on my project but when i expand the area of one of them it expands above the other expandable panel. I want to change the positions of the ones that are behind. I´m new with unreal, so can you give me your opinion or some help? Thanks!

Hi! I have 3 expandable areas on my project but when i expand the area of one of them it expands above the other expandable panel. I want to change the positions of the ones that are behind. I´m new with unreal, so can you give me your opinion or some help? Thanks

I know I’m kinda late to this post, but I’m looking to do this in C++ and I can’t seem to find the solution, any idea?

just manipulate Render Transform in details panel