Hi all. I am trying to move a widget via the mouse cursor using OnMouseMove in slate. From my understanding, absolute coordinates are from the top-left of the gameplay viewport/screen, and local coordinates are obviously from the top-left of the widget. However the absolute coordinates I am getting for either the mouse position or the widget position seems to be very different from the rendered position.
For clarity, this widget, SInventoryDisplay
, is a child of some Horizontal and Vertical boxes, which are children of an Overlay. If this changes how geometry works, please let me know as I had zero clue if so.
This is some code I wrote to display a border in the position that MyGeometry.GetAbsolutePosition()
returns:
void SInventoryDisplay::OnMouseEnter(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent)
{
const FVector2D Pos = MyGeometry.GetAbsolutePosition();
Brush = FSlateBrush();
SAssignNew(WTest, SConstraintCanvas).Visibility(EVisibility::HitTestInvisible)
+ SConstraintCanvas::Slot()
.AutoSize(true)
.Offset(FMargin(Pos.X, Pos.Y, 0, 0))
.Alignment(FVector2D(0, 0))
.Anchors(FAnchors(0, 0, 1, 1))
[
SNew(SBorder).ColorAndOpacity(FColor::Green).BorderImage(&Brush)
];
GEngine->GameViewport->AddViewportWidgetContent(WTest.ToSharedRef());
}
Which produces this result:
Try to ignore the debug text and icons. The widget in question,
SInventoryDisplay
, is from the top-left of the grid, including the gray outline. The white box is the border I create. For clarity, I want the white box to match the top left of the grid/inventory display.
Why does the border not line up? Am I missing scaling/DPI calculations? Do I have an incorrect understanding of absolute coordinates? Or am I somehow getting in correct geometry (which I doubt is the case)?
Thank you for your time.