FVector2D AHUD::GetCoordinateOffset() const { FVector2D Offset(0.f, 0.f); ULocalPlayer* LocalPlayer = Cast<ULocalPlayer>(GetOwningPlayerController()->Player); if (LocalPlayer) { // Create a view family for the game viewport FSceneViewFamilyContext ViewFamily(FSceneViewFamily::ConstructionValues( LocalPlayer->ViewportClient->Viewport, GetWorld()->Scene, LocalPlayer->ViewportClient->EngineShowFlags) .SetRealtimeUpdate(true)); // Calculate a view where the player is to update the streaming from the players start location FVector ViewLocation; FRotator ViewRotation; FSceneView* SceneView = LocalPlayer->CalcSceneView(&ViewFamily, /*out*/ ViewLocation, /*out*/ ViewRotation, LocalPlayer->ViewportClient->Viewport); if (SceneView) { Offset.X = (SceneView->ViewRect.Min.X - SceneView->UnscaledViewRect.Min.X) // This accounts for the borders when the aspect ratio is locked - SceneView->UnscaledViewRect.Min.X; // And this will deal with the viewport offset if its a split screen Offset.Y = (SceneView->ViewRect.Min.Y - SceneView->UnscaledViewRect.Min.Y) - SceneView->UnscaledViewRect.Min.Y; } } return Offset; }
void ABaseHUD::BeginPlay() { Super::BeginPlay(); // Create MainWidget from MainWidgetClass MainWidget = CreateWidget<UUserWidget>(GetOwningPlayerController(), MainWidgetClass); if (MainWidget) { MainWidget->AddToViewport(); } }
void APlayerSelectHUD::BeginPlay() { Super::BeginPlay(); PlayerSelectWidget = CreateWidget<UPlayerSelectUserWidget>(GetOwningPlayerController(), PlayerSelectWidgetClass); PlayerSelectWidget->AddToViewport(0); APlayerController* Controller = GetWorld()->GetFirstPlayerController(); if (Controller) { Controller->bShowMouseCursor = true; Controller->bEnableClickEvents = true; } }
void APacmanMainMenuHUD::DisplayMenu(TSharedPtr<SCompoundWidget> MenuToDisplay) { // If there is visible menu, hide it if (DisplayedMenuStack.Num() > 0) { GEngine->GameViewport->RemoveViewportWidgetContent(DisplayedMenuStack.Top().ToSharedRef()); } // Show new menu GEngine->GameViewport->AddViewportWidgetContent(MenuToDisplay.ToSharedRef()); DisplayedMenuStack.Push(MenuToDisplay.ToSharedRef()); // Only GUI elements can handle input FInputModeUIOnly InputMode; InputMode.SetLockMouseToViewport(true); InputMode.SetWidgetToFocus(MenuToDisplay); GetOwningPlayerController()->SetInputMode(InputMode); }
void APacmanMainMenuHUD::HideMenu() { // There must be at least 2 menus, main menu and one sub-menu that we want to hide if (DisplayedMenuStack.Num() < 2) { return; } GEngine->GameViewport->RemoveViewportWidgetContent(DisplayedMenuStack.Pop().ToSharedRef()); auto &MenuToDisplay = DisplayedMenuStack.Top(); // Show previous menu GEngine->GameViewport->AddViewportWidgetContent(MenuToDisplay.ToSharedRef()); // Only GUI elements can handle input FInputModeUIOnly InputMode; InputMode.SetLockMouseToViewport(true); InputMode.SetWidgetToFocus(MenuToDisplay); GetOwningPlayerController()->SetInputMode(InputMode); }
APlayerController* AKUIInterface::GetPlayerController() const { return GetOwningPlayerController(); }
//DrawHUD gets called every tick void AMyHUD::DrawHUD(){ //Have PC for Mouse Cursor? if (!ThePC) { //Attempt to Reacquire PC ThePC = GetOwningPlayerController(); //Could Not Obtain PC if (!ThePC) return; } //Display the Cursor ThePC->bShowMouseCursor = true; //Health Bar is always drawn on screen DrawHealthBar(); FVector2D ScreenSize = FVector2D(Canvas->SizeX, Canvas->SizeY); Super::DrawHUD(); //Display the instructions for until the timer hits zero if (d5){ if (GetWorldTimerManager().GetTimerRemaining(Handle5) > 0){ DrawHUDText(ToyFont, "Controls:", x, y, FColorRed, 1.0F,true); DrawHUDText(SketchFont, "WASD to Move", x, y+43, FColorRed, .5F, true, LC_Red); DrawHUDText(SketchFont, "Mouse to aim", x, y+63, FColorRed, .5F, true, LC_Red); DrawHUDText(SketchFont, "Click to shoot", x, y+83, FColorRed, .5F, true, LC_Red); DrawHUDText(SketchFont, "Some walls are breakable", x, y+103, FColorRed, .5F, true, LC_Red); DrawHUDText(SketchFont, "Shoot health packs to gain health", x, y+123, FColorRed, .5F, true, LC_Red); DrawHUDText(SketchFont, "Press M for Menu", x, y + 143, FColorRed, .5F, true, LC_Red); DrawHUDText(ToyFont, "-----------------", x, y+180, FColorRed, .5F, true, LC_Red); DrawHUDText(ToyFont, "Goal:", x, y+220, FColorRed, 1.0F, true); DrawHUDText(SketchFont, "Destroy every enemy", x, y+263, FColorRed, .5F, true,LC_Red); DrawHUDText(SketchFont, "Be the sole survivor", x, y+283, FColorRed, .5F, true,LC_Red); } else { GetWorldTimerManager().ClearTimer(Handle5); d5 = false; d1 = true; GetWorldTimerManager().SetTimer(Handle1, 1.0f, false); } } //When the instructions are finished display 3,2,1, Go! on the screen based on timers if (d1){ if (GetWorldTimerManager().GetTimerRemaining(Handle1) > 0) DrawText(TEXT("3"), FColor::Blue, Canvas->SizeX / 2 -70, Canvas->SizeY / 2 -50, SketchFont, 2.0F, false); else { GetWorldTimerManager().ClearTimer(Handle1); d1 = false; d2 = true; GetWorldTimerManager().SetTimer(Handle2, 1.0f, false); } } if (d2){ if (GetWorldTimerManager().GetTimerRemaining(Handle2) > 0) DrawText(TEXT("2"), FColor::Blue, Canvas->SizeX / 2-70, Canvas->SizeY / 2-50, SketchFont, 2.0F, false); else{ GetWorldTimerManager().ClearTimer(Handle2); d2 = false; d3 = true; GetWorldTimerManager().SetTimer(Handle3, 1.0f, false); } } if (d3){ if (GetWorldTimerManager().GetTimerRemaining(Handle3) > 0) DrawText(TEXT("1"), FColor::Blue, Canvas->SizeX / 2-70, Canvas->SizeY / 2-50, SketchFont, 2.0F, false); else{ GetWorldTimerManager().ClearTimer(Handle3); d3 = false; d4 = true; GetWorldTimerManager().SetTimer(Handle4, 1.0f, false); } } if (d4){ if (GetWorldTimerManager().GetTimerRemaining(Handle4) > 0) DrawText(TEXT("GO!"), FColor::Blue, (Canvas->SizeX / 2)-95, (Canvas->SizeY / 2)-50, SketchFont, 2.0F, false); else{ d4 = false; GetWorldTimerManager().ClearTimer(Handle4); canMove = true; } } //gameover text when gameover is reached if (gameover){ DrawText(TEXT("GAME OVER"), FColor::Red, (Canvas->SizeX / 2) - 250, (Canvas->SizeY / 2) - 30, ToyFont, 2.0F, false); DontDrawHUD = false; ThePC->ConsoleCommand("Pause"); } //win text when all the bots are killed if (botsKilled){ DrawText(TEXT("YOU WON!"), FColor::Red, (Canvas->SizeX / 2) - 250, (Canvas->SizeY / 2) - 30, ToyFont, 2.0F, false); DontDrawHUD = false; ThePC->ConsoleCommand("Pause"); } //Draw HUD? //DrawHUD is only turned on when the menu button is pressed if (DontDrawHUD) return; //Super Super::DrawHUD(); //No Canvas? if (!Canvas) return; //Reset States DrawHUD_Reset(); //Get New Mouse Position ThePC->GetMousePosition(MouseLocation.X, MouseLocation.Y); //Cursor In Buttons DrawHUD_CheckCursorInButtons(); //Draw Dialogs DrawHUD_DrawDialogs(); //} }
void AHUD::PostRender() { // Theres nothing we can really do without a canvas or a world - so leave now in that case if ( (GetWorld() == nullptr) || (Canvas == nullptr)) { return; } // Set up delta time RenderDelta = GetWorld()->TimeSeconds - LastHUDRenderTime; if ( PlayerOwner != NULL ) { // draw any debug text in real-time DrawDebugTextList(); } if ( bShowDebugInfo ) { if (DebugCanvas) { DebugCanvas->DisplayDebugManager.Initialize(DebugCanvas, GEngine->GetTinyFont(), FVector2D(4.f, 50.f)); ShowDebugInfo(DebugCanvas->DisplayDebugManager.GetMaxCharHeightRef(), DebugCanvas->DisplayDebugManager.GetYPosRef()); } } else if ( bShowHUD && FApp::CanEverRender() ) { DrawHUD(); // No need to do work to determine hit box candidates if there will never be any if (HitBoxMap.Num() > 0) { ULocalPlayer* LocalPlayer = GetOwningPlayerController() ? Cast<ULocalPlayer>(GetOwningPlayerController()->Player) : NULL; if (LocalPlayer && LocalPlayer->ViewportClient) { TArray<FVector2D> ContactPoints; if (!FSlateApplication::Get().IsFakingTouchEvents()) { FVector2D MousePosition; if (LocalPlayer->ViewportClient->GetMousePosition(MousePosition)) { ContactPoints.Add(MousePosition); } } for (int32 FingerIndex = 0; FingerIndex < EKeys::NUM_TOUCH_KEYS; ++FingerIndex) { FVector2D TouchLocation; bool bPressed = false; GetOwningPlayerController()->GetInputTouchState((ETouchIndex::Type)FingerIndex, TouchLocation.X, TouchLocation.Y, bPressed); if (bPressed) { ContactPoints.Add(TouchLocation); } } const FVector2D ContactPointOffset = GetCoordinateOffset(); if (!ContactPointOffset.IsZero()) { for (FVector2D& ContactPoint : ContactPoints) { ContactPoint += ContactPointOffset; } } UpdateHitBoxCandidates( MoveTemp(ContactPoints) ); } } else if (HitBoxesOver.Num() > 0) { // We still need to dispatch any end cursor over messages even if we don't have any hitboxes anymore for (const FName HitBoxName : HitBoxesOver) { NotifyHitBoxEndCursorOver(HitBoxName); } HitBoxesOver.Reset(); } } if( bShowHitBoxDebugInfo ) { RenderHitBoxes( Canvas->Canvas ); } DrawSafeZoneOverlay(); LastHUDRenderTime = GetWorld()->TimeSeconds; }