void UUserWidget::SetDesiredSizeInViewport(FVector2D DesiredSize) { ViewportOffsets.Right = DesiredSize.X; ViewportOffsets.Bottom = DesiredSize.Y; ViewportAnchors = FAnchors(0, 0); }
void UUserWidget::SetPositionInViewport(FVector2D Position) { float Scale = UWidgetLayoutLibrary::GetViewportScale(this); ViewportOffsets.Left = Position.X / Scale; ViewportOffsets.Top = Position.Y / Scale; ViewportAnchors = FAnchors(0, 0); }
UCanvasPanelSlot::UCanvasPanelSlot(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) , Slot(nullptr) { LayoutData.Offsets = FMargin(0, 0, 100, 30); LayoutData.Anchors = FAnchors(0.0f, 0.0f); LayoutData.Alignment = FVector2D(0.0f, 0.0f); bAutoSize = false; ZOrder = 0; }
UCanvasPanelSlot::UCanvasPanelSlot(const FPostConstructInitializeProperties& PCIP) : Super(PCIP) , Slot(NULL) { LayoutData.Offsets = FMargin(0, 0, 100, 30); LayoutData.Anchors = FAnchors(0.0f, 0.0f); LayoutData.Alignment = FVector2D(0.0f, 0.0f); bAutoSize = false; ZOrder = 0; }
virtual void Tick(const FGeometry& AllottedGeometry, const double InCurrentTime, const float InDeltaTime) override { TArray<UWidgetComponent*, TInlineAllocator<1>> DeadComponents; for ( TWeakObjectPtr<UWidgetComponent> Component : Components ) { if ( UWidgetComponent* WidgetComponent = Component.Get() ) { if ( ULocalPlayer* LocalPlayer = WidgetComponent->GetOwnerPlayer() ) { if ( APlayerController* PlayerController = LocalPlayer->PlayerController ) { FVector WorldLocation = WidgetComponent->GetComponentLocation(); FVector ScreenPosition; const bool bProjected = UWidgetLayoutLibrary::ProjectWorldLocationToWidgetPositionWithDistance(PlayerController, WorldLocation, ScreenPosition); if ( bProjected ) { WidgetComponent->GetUserWidgetObject()->SetVisibility(ESlateVisibility::SelfHitTestInvisible); if ( SConstraintCanvas::FSlot* CanvasSlot = ComponentToSlot.FindRef(WidgetComponent) ) { FVector2D DrawSize = WidgetComponent->GetDrawSize(); FVector2D Pivot = WidgetComponent->GetPivot(); CanvasSlot->AutoSize(DrawSize.IsZero()); CanvasSlot->Offset(FMargin(ScreenPosition.X, ScreenPosition.Y, DrawSize.X, DrawSize.Y)); CanvasSlot->Anchors(FAnchors(0, 0, 0, 0)); CanvasSlot->Alignment(Pivot); CanvasSlot->ZOrder(-ScreenPosition.Z); } } else { WidgetComponent->GetUserWidgetObject()->SetVisibility(ESlateVisibility::Hidden); } } } } else { DeadComponents.Add(WidgetComponent); } } // Normally components should be removed by someone calling remove component, but just in case it was // deleted in a way where they didn't happen, this is our backup solution to enure we remove stale widgets. for ( int32 Index = 0; Index < DeadComponents.Num(); Index++ ) { RemoveComponent(DeadComponents[Index]); } }
UUserWidget::UUserWidget(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) { ViewportAnchors = FAnchors(0, 0, 1, 1); Visiblity_DEPRECATED = Visibility = ESlateVisibility::SelfHitTestInvisible; bInitialized = false; bSupportsKeyboardFocus = true; ColorAndOpacity = FLinearColor::White; ForegroundColor = FSlateColor::UseForeground(); #if WITH_EDITORONLY_DATA bUseDesignTimeSize = false; DesignTimeSize = FVector2D(100, 100); PaletteCategory = LOCTEXT("UserCreated", "User Created"); #endif }
void UUserWidget::SetPositionInViewport(FVector2D Position, bool bRemoveDPIScale ) { if ( bRemoveDPIScale ) { float Scale = UWidgetLayoutLibrary::GetViewportScale(this); ViewportOffsets.Left = Position.X / Scale; ViewportOffsets.Top = Position.Y / Scale; } else { ViewportOffsets.Left = Position.X; ViewportOffsets.Top = Position.Y; } ViewportAnchors = FAnchors(0, 0); }
void UQuestLogPanelWidget::GenerateQuestSlots() { if (!m_OwningPlayer) return; ARPGDemoCharacter* thePlayer = Cast<ARPGDemoCharacter>(m_OwningPlayer); if (!thePlayer) return; UScrollBox* scrollPanel = (UScrollBox*)FindFirstWidget(UScrollBox::StaticClass()); if (!scrollPanel) return; scrollPanel->ClearChildren(); if (thePlayer->m_Quests.Num() == 0) return; for (int i = 0; i < thePlayer->m_Quests.Num(); i++) { UCanvasPanel* canvasPanel = NewObject<UCanvasPanel>(USizeBox::StaticClass()); canvasPanel->SetVisibility(ESlateVisibility::SelfHitTestInvisible); UCustomUserWidgetParent* slotWidget = CreateWidget<UCustomUserWidgetParent>(thePlayer->Controller->CastToPlayerController(), thePlayer->m_WidgetClasses.QuestLogSlotWidget); UQuestLogSlotWidget* questSlot = Cast<UQuestLogSlotWidget>(slotWidget); questSlot->m_ParentPanel = this; questSlot->m_QuestNameText = thePlayer->m_Quests[i].Name; if (thePlayer->m_Quests[i].Completed) questSlot->m_ImageTexture = UStaticFunctions::LoadTextureFromPath("/Game/Textures/UI/T_CheckMark"); UPanelSlot* panelSlot = canvasPanel->AddChild(slotWidget); UCanvasPanelSlot* canvasPanelSlot = Cast<UCanvasPanelSlot>(panelSlot); FAnchors theAnchors = FAnchors(); theAnchors.Maximum = FVector2D(0, 0); theAnchors.Minimum = FVector2D(0, 0); canvasPanelSlot->SetSize(m_QuestSlotSize); canvasPanelSlot->SetAnchors(theAnchors); UScrollBoxSlot* theSlot = Cast<UScrollBoxSlot>(scrollPanel->AddChild(canvasPanel)); theSlot->SetHorizontalAlignment(EHorizontalAlignment::HAlign_Fill); if (questSlot->m_QuestNameText == m_SelectedQuest) { questSlot->GenerateQuestInfo(); } } }
void UInventoryOptionsWidget::RemoveFirstButton() { TArray<UWidget*> buttonWidgetArray = FindAllWidgets(UCustomButtonWidget::StaticClass()); buttonWidgetArray[0]->RemoveFromParent(); buttonWidgetArray.RemoveAt(0); for (UWidget* aWidget : buttonWidgetArray) { UCustomButtonWidget* buttonWidget = Cast<UCustomButtonWidget>(aWidget); UPanelSlot* panelSlot = buttonWidget->Slot; UUniformGridSlot* gridSlot = Cast<UUniformGridSlot>(panelSlot); gridSlot->SetRow(FMath::Clamp(gridSlot->Row - 1, 0, 3)); } TArray<UWidget*> textWidgetArray = FindAllWidgets(UTextBlock::StaticClass()); textWidgetArray[0]->RemoveFromParent(); textWidgetArray.RemoveAt(0); for (UWidget* aWidget : textWidgetArray) { UTextBlock* textBlock = Cast<UTextBlock>(aWidget); UPanelSlot* panelSlot = textBlock->Slot; UUniformGridSlot* gridSlot = Cast<UUniformGridSlot>(panelSlot); gridSlot->SetRow(FMath::Clamp(gridSlot->Row - 1, 0, 3)); } UWidget* theWidget = FindFirstWidget(UUniformGridPanel::StaticClass()); UUniformGridPanel* thePanel = Cast<UUniformGridPanel>(theWidget); UCanvasPanelSlot* canvasSlot = Cast<UCanvasPanelSlot>(thePanel->Slot); SetDesiredSizeInViewport(FVector2D(canvasSlot->GetSize().X, canvasSlot->GetSize().Y * 0.5)); canvasSlot->SetSize(FVector2D(canvasSlot->GetSize().X, canvasSlot->GetSize().Y * 0.5)); FAnchors anchorData = FAnchors(); anchorData.Minimum = FVector2D(0.5, 1); anchorData.Maximum = FVector2D(0.5, 1); canvasSlot->SetAnchors(anchorData); }