# Trying to force widget to stay on screen

**URL:** <https://forums.unrealengine.com/t/trying-to-force-widget-to-stay-on-screen/1184721>\
**Category:** UI\
**Tags:** unreal-engine, question\
**Created:** [May 31, 2023, 4:25pm UTC](https://forums.unrealengine.com/t/trying-to-force-widget-to-stay-on-screen/1184721 "2023-05-31T16:25:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krozjin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Krozjin](https://forums.unrealengine.com/u/Krozjin)\
**Post date:** [May 31, 2023, 4:25pm UTC](https://forums.unrealengine.com/t/trying-to-force-widget-to-stay-on-screen/1184721/1 "2023-05-31T16:25:37Z")

</div>

Hey, I have a bit of a head scratcher here. What I’m trying to accomplish is for my right click menu to just not go off screen. So what I’m doing is seeing what the viewport size is and if it’s more than 94% of that size, then don’t go further even if my mouse does. This way there’s a max it can go:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/1/f/c1fe3fa86316780a5d405276ef3b5028454c0f6f.png)

Now this works to some degree, but I have noticed that when I go full screen the position of that widget is not the same as the lower resolution counterpart. Everything scales up with higher resolution, so I figured if I use a screen percent then it should stay consistent. Unfortunately the offset gets smaller / bigger in a different speed that the actual resolution change between the widgets.

Small viewport:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/f/7/8f7c9d3efab3681725bc1cef826d10639ef9ed93.png)

Large Viewport:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/b/c/bbc433d00092f538e89d463e8cec772ee0680f4c.jpeg)

I just want that when it’s positioned, it doesn’t go off screen, so if there’s much better ways of doing that I’m open to any suggestions. Thanks! 🙂

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [May 31, 2023, 6:18pm UTC](https://forums.unrealengine.com/t/trying-to-force-widget-to-stay-on-screen/1184721/2 "2023-05-31T18:18:54Z")

</div>

> [@Krozjin](#):
>
> so if there’s much better ways of doing that I’m open to any suggestions.

The [Menu Anchor](https://docs.unrealengine.com/5.0/en-US/using-menu-anchors-in-umg-for-unreal-engine/) is the UE’s native way of handling context menus. And here’s how you can try it the hard way:

> [@UMG Widget: Stay on screen - But how?](https://forums.unrealengine.com/t/umg-widget-stay-on-screen-but-how/123044/5):
>
> while there’s half a dozen ways to implement this, the gist is to subtract half the widget size from the viewport size and use it as a clamp: vid: [https://i.gyazo.com/7e60baa21a0fbe5e5796d81524b27671.mp4](https://i.gyazo.com/7e60baa21a0fbe5e5796d81524b27671.mp4)

---

<div class="post-metadata">

**Author:** ![Krozjin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Krozjin](https://forums.unrealengine.com/u/Krozjin)\
**Post date:** [May 31, 2023, 7:00pm UTC](https://forums.unrealengine.com/t/trying-to-force-widget-to-stay-on-screen/1184721/3 "2023-05-31T19:00:17Z")

</div>

The Menu Anchor is interesting, but in the demo it’s always in the same place, while mine generates based on mouse position, so it doesn’t seem like it’ll work. But I’m only now learning about it so maybe there’s more I need to figure out first.

As for the hard method, I’ve tried to get the Desired Size, but it just returns 0 for me, despite the User Widget being set to Desired in the top right. I’ve even tried to get the desired size from the vertical box inside the widget, still returns 0.

---

<div class="post-metadata">

**Author:** ![Krozjin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Krozjin](https://forums.unrealengine.com/u/Krozjin)\
**Post date:** [May 31, 2023, 8:19pm UTC](https://forums.unrealengine.com/t/trying-to-force-widget-to-stay-on-screen/1184721/4 "2023-05-31T20:19:24Z")

</div>

After playing around a bit with the Menu Anchor, it’s doing what I want, but there’s an issue. When I currently create the widget I have a lot of exposed variables that I push through the one that created it. With it being a menu anchor, I can’t seem to find any way to set the variables. Is it possible?

---

<div class="post-metadata">

**Author:** ![SamiTheGreat](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SamiTheGreat](https://forums.unrealengine.com/u/SamiTheGreat)\
**Post date:** [May 31, 2023, 8:28pm UTC](https://forums.unrealengine.com/t/trying-to-force-widget-to-stay-on-screen/1184721/5 "2023-05-31T20:28:23Z")

</div>

Desired Size returns correct size only after the underlying slate widgets have been created. I have very similar thing in my project where I am showing interaction options in a widget when certain objects are interacted with, so the player can choose an option.

The interaction point is first projected to screen space and the widget is positioned to the projected screen location but is also clamped inside screen.

I am using C++ and UMG-View Models to do this but maybe you can get some help from it.

```auto
// Header
UFUNCTION(BlueprintPure, Category = "Widget Utils")
static FVector2D ClampWidgetPositionInsideViewport(APlayerController* PlayerController, const FVector2D& ScreenLocation, const FMargin& Margin);

// Cpp
FVector2D UWidgetUtils::ClampWidgetPositionInsideViewport(APlayerController* PlayerController, const FVector2D& ScreenLocation, const FMargin& Margin)
{
	if (!IsValid(PlayerController))
	{
		return FVector2D::ZeroVector;
	}

	int32 VPSizeX, VPSizeY;
	PlayerController->GetViewportSize(VPSizeX, VPSizeY);
	if (VPSizeX == 0 || VPSizeY == 0)
	{
		return FVector2D::ZeroVector;
	}

	const float MinX = Margin.Left;
	const float MaxX = ((float)VPSizeX) - Margin.Right;
	const float MinY = Margin.Top;
	const float MaxY = ((float)VPSizeY) - Margin.Bottom;

	if (MinX >= MaxX || MinY >= MaxY)
	{
		return FVector2D(VPSizeX / 2.0f, VPSizeY / 2.0f);
	}

	FVector2D Result = ScreenLocation;
	Result.X = FMath::Clamp(ScreenLocation.X, MinX, MaxX);
	Result.Y = FMath::Clamp(ScreenLocation.Y, MinY, MaxY);
	return Result;
}

```

This very simple function clamps the given screen location inside viewport with additional margin from each screen edge.

Then in view model I am calculating the screen transform for the widget using the following code:

```auto
    // Widget screen location is the same as interactable screen location
	FVector2D NewWidgetLocation = InteractableScreenLocation;

	// Center alignment
	NewWidgetLocation = NewWidgetLocation - FVector2D(WidgetSize.X / 2.0f, WidgetSize.Y / 2.0f);

	// Clamp inside screen
	NewWidgetLocation = UWidgetUtils::ClampWidgetPositionInsideViewport(PlayerController, NewWidgetLocation, FMargin(WidgetSize.X, WidgetSize.Y, WidgetSize.X, WidgetSize.Y));
	SetOptionWidgetScreenLocation(NewWidgetLocation);

```

The code expects that the widget pivot is at the center. WidgetSize is set to the view model by using Desired Size of the widget. SetOptionWidgetScreenLocation just sets a property which is Vector2D with field notify, so the widget can bind into it.

Hope that you get it solved.

---

<div class="post-metadata">

**Author:** ![Krozjin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Krozjin](https://forums.unrealengine.com/u/Krozjin)\
**Post date:** [May 31, 2023, 8:29pm UTC](https://forums.unrealengine.com/t/trying-to-force-widget-to-stay-on-screen/1184721/6 "2023-05-31T20:29:10Z")

</div>

One rabit hole later I found an old post of yours @Everynone from 2017 that helped me out with getting those variables through:

> [@Getting reference to a widget created by the Menu Anchor.](https://forums.unrealengine.com/t/getting-reference-to-a-widget-created-by-the-menu-anchor/409990/3):
>
> Hello Is there a way to access a widget created by the menu anchor? For example, I want to be able to pass some additional data to it after creation. I can access the class, but not the actual created widget. Getting child / content / parent doesn’t work. I know I could use a construct event in the menu blueprint to get the data from a player controller for example, but I want to have multiple menus open at once so I have no way of telling them which one is which. Thanks.

So thanks for point me in the right direction. A bit of fiddling left to do but the main problem has now been fixed! 😃

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [May 31, 2023, 8:53pm UTC](https://forums.unrealengine.com/t/trying-to-force-widget-to-stay-on-screen/1184721/7 "2023-05-31T20:53:41Z")

</div>

When it comes to Desired Size returning 0, try calling Force Layout Prepass first and then get widget size. This may be needed if you construct and want to use the widget immediately in the very same frame.

Good luck.

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [May 31, 2023, 9:33pm UTC](https://forums.unrealengine.com/t/trying-to-force-widget-to-stay-on-screen/1184721/8 "2023-05-31T21:33:10Z")

</div>

[WidgetPos.zip](https://forums.unrealengine.com/uploads/short-url/ohJ9Daigjs9hHNeIHfKH2Rni0lu.zip) (102.4 KB)

Here’s my take. Full BP, respects screen scale and dpi scaling.

Right click to show context menu (can hook update to tick in main menu to have it follow cursor all the time)

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [June 30, 2023, 9:34pm UTC](https://forums.unrealengine.com/t/trying-to-force-widget-to-stay-on-screen/1184721/9 "2023-06-30T21:34:12Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
