Download

Moving Widget within SCanvas

Hi,

I am trying to move a widget which is in a SCanvas Slot. SCanvas allow me to place a widget wherever i want which is nice. Now i want to move that widget around, the mouse position values are changing but the widget is not moving.

here is a small part of my code :


SCanvas::FSlot s = Canvas->AddSlot()
	.Position(FVector2D(item->Position.X*slotSize, item->Position.Y * slotSize))
	.Size(FVector2D(item->Size.X * slotSize, item->Size.Y * slotSize))
	

		SNew(SDE_InventorySlotWidget)
		.OnDragDetectedDel(this, &SDE_InventoryUIWidget::OnDragItem, index)
		.OnMouseMoveDel(this, &SDE_InventoryUIWidget::OnMoveItem, index)
		.OwnerHUD(OwnerHUD)
		.Item(item)
		.Parent(s)
	];


void SDE_InventoryUIWidget::OnMoveItem(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent, SDE_InventorySlotWidget* widget, int32 index)
{
	
	positionList[index] = FVector2D(MouseEvent.GetScreenSpacePosition().X, MouseEvent.GetScreenSpacePosition().Y);
	widget->Parent.Position(positionList[index]);
	if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::Printf(TEXT("OnMoveItem at : %s"), *widget->Parent.PositionAttr.Get().ToString()));
	}
}

In another class i got widget moving but with an SOverlay and using “Parent.Padding(FMargin(posX, posY, 0, 0));” , i would prefer use SCanvas this time as it seems to be a better solution but i can’t get it to work, if someone has an idea let me know !

Thanks !

Got it working using :


.Position(TAttribute<FVector2D>::Create(TAttribute<FVector2D>::FGetter::CreateSP(this, &SDE_InventoryUIWidget::GetPositionItem, index)))

Problem is there is no way to control z-order with Canvas, so i had to rebuild the Canvas everytime the player want to drag something so i’m sure the widget is in the front. I don’t have too much Widget on 1 Canvas but it’s still feel not right to rebuild all.

Is there another way to put a widget in front within the canvas ?