Skip to content

Commit 1139da3

Browse files
committed
Added useful util functions and delegates; Removed redundant InGameWidget class
1 parent ce5ed82 commit 1139da3

File tree

17 files changed

+216
-173
lines changed

17 files changed

+216
-173
lines changed

Diff for: Content/Bomber/UI/InGame/WBP_InGame.uasset

-22.6 KB
Binary file not shown.

Diff for: Plugins/MyEditorUtils/Source/MyUtils/Private/MyUtilsLibraries/WidgetUtilsLibrary.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,30 @@ UUserWidget* FWidgetUtilsLibrary::GetParentWidgetOfClass(const UUserWidget* InWi
3535
return FoundWidget;
3636
}
3737

38+
// Returns first child widget found by specified class iterating all widget objects
39+
UUserWidget* FWidgetUtilsLibrary::GetChildWidgetOfClass(const UUserWidget* ParentWidget, TSubclassOf<UUserWidget> ChildWidgetClass)
40+
{
41+
if (!ParentWidget)
42+
{
43+
return nullptr;
44+
}
45+
46+
TArray<UWidget*> ChildWidgets;
47+
ParentWidget->WidgetTree->GetAllWidgets(ChildWidgets);
48+
49+
// Iterate through all child widgets to find the one of the specified class
50+
for (UWidget* Widget : ChildWidgets)
51+
{
52+
UUserWidget* UserWidget = Cast<UUserWidget>(Widget);
53+
if (UserWidget && UserWidget->IsA(ChildWidgetClass))
54+
{
55+
return UserWidget;
56+
}
57+
}
58+
59+
return nullptr;
60+
}
61+
3862
// Returns first widget by specified class iterating all widget objects
3963
UUserWidget* FWidgetUtilsLibrary::FindWidgetOfClass(UObject* WorldContextObject, TSubclassOf<UUserWidget> ParentWidgetClass)
4064
{

Diff for: Plugins/MyEditorUtils/Source/MyUtils/Public/MyUtilsLibraries/WidgetUtilsLibrary.h

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class MYUTILS_API FWidgetUtilsLibrary
1818
static FORCEINLINE T* GetParentWidgetOfClass(const UUserWidget* ChildWidget) { return Cast<T>(GetParentWidgetOfClass(ChildWidget, T::StaticClass())); }
1919
static UUserWidget* GetParentWidgetOfClass(const UUserWidget* InWidget, TSubclassOf<UUserWidget> ParentWidgetClass);
2020

21+
/** Returns first child widget found by specified class iterating all widget objects. */
22+
template <typename T>
23+
static FORCEINLINE T* GetChildWidgetOfClass(const UUserWidget* ParentWidget) { return Cast<T>(GetChildWidgetOfClass(ParentWidget, T::StaticClass())); }
24+
static UUserWidget* GetChildWidgetOfClass(const UUserWidget* ParentWidget, TSubclassOf<UUserWidget> ChildWidgetClass);
25+
2126
/** Returns first widget found by specified class iterating all widget objects. */
2227
template <typename T>
2328
static FORCEINLINE T* FindWidgetOfClass(UObject* WorldContextObject) { return Cast<T>(FindWidgetOfClass(WorldContextObject, T::StaticClass())); }

Diff for: Source/Bomber/Private/Controllers/MyPlayerController.cpp

+14-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "GameFramework/MyCheatManager.h"
1212
#include "GameFramework/MyGameStateBase.h"
1313
#include "GameFramework/MyPlayerState.h"
14+
#include "LevelActors/PlayerCharacter.h"
1415
#include "MyUtilsLibraries/InputUtilsLibrary.h"
1516
#include "Subsystems/GlobalEventsSubsystem.h"
1617
#include "UI/InGameMenuWidget.h"
@@ -140,19 +141,28 @@ void AMyPlayerController::OnPossess(APawn* InPawn)
140141
// Try to rebind inputs for possessed pawn on server
141142
constexpr bool bInvertRest = true;
142143
SetAllInputContextsEnabled(true, AMyGameStateBase::GetCurrentGameState(), bInvertRest);
144+
145+
// Notify client about pawn change
146+
if (APlayerCharacter* PlayerCharacter = Cast<APlayerCharacter>(InPawn))
147+
{
148+
UGlobalEventsSubsystem::Get().OnLocalPlayerReady.Broadcast(PlayerCharacter);
149+
}
143150
}
144151

145152
// Is overriden to notify the client when this controller possesses new player character
146153
void AMyPlayerController::OnRep_Pawn()
147154
{
148155
Super::OnRep_Pawn();
149156

150-
// Notify client about pawn change
151-
GetOnNewPawnNotifier().Broadcast(GetPawn());
152-
153157
// Try to rebind inputs for possessed pawn on client
154158
constexpr bool bInvertRest = true;
155159
SetAllInputContextsEnabled(true, AMyGameStateBase::GetCurrentGameState(), bInvertRest);
160+
161+
// Notify client about pawn change
162+
if (APlayerCharacter* PlayerCharacter = GetPawn<APlayerCharacter>())
163+
{
164+
UGlobalEventsSubsystem::Get().OnLocalPlayerReady.Broadcast(PlayerCharacter);
165+
}
156166
}
157167

158168
// Is overriden to notify the client when is set new player state
@@ -511,4 +521,4 @@ void AMyPlayerController::AddNewInputContexts(const TArray<const UMyInputMapping
511521
AllInputContextsInternal.AddUnique(InputContextIt);
512522
}
513523
}
514-
}
524+
}

Diff for: Source/Bomber/Private/GameFramework/MyGameStateBase.cpp

+51-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ AMyGameStateBase& AMyGameStateBase::Get()
3333

3434
/*********************************************************************************************
3535
* Current Game State enum
36-
* Can be tracked both on host and client by listening UMadGlobalEvents::Get().OnGameStateChanged
36+
* Can be tracked both on host and client by listening UGlobalEventsSubsystem::Get().OnGameStateChanged
3737
********************************************************************************************* */
3838

3939
// Returns the AMyGameState::CurrentGameState property.
@@ -128,6 +128,28 @@ void AMyGameStateBase::OnAnyCharacterDestroyed()
128128
* 3-2-1-GO
129129
********************************************************************************************* */
130130

131+
// Sets the left second of the 'Three-two-one-GO' timer
132+
void AMyGameStateBase::SetStartingTimerSecondsRemain(float NewStartingTimerSecRemain)
133+
{
134+
StartingTimerSecRemainInternal = NewStartingTimerSecRemain;
135+
ApplyStartingTimerSecondsRemain();
136+
}
137+
138+
// Is called on client when the 'Three-two-one-GO' timer was updated
139+
void AMyGameStateBase::OnRep_StartingTimerSecRemain()
140+
{
141+
ApplyStartingTimerSecondsRemain();
142+
}
143+
144+
// Updates current starting timer seconds remain
145+
void AMyGameStateBase::ApplyStartingTimerSecondsRemain()
146+
{
147+
if (OnStartingTimerSecRemainChanged.IsBound())
148+
{
149+
OnStartingTimerSecRemainChanged.Broadcast(StartingTimerSecRemainInternal);
150+
}
151+
}
152+
131153
// Is called during the Game Starting state to handle the 'Three-two-one-GO' timer
132154
void AMyGameStateBase::DecrementStartingCountdown()
133155
{
@@ -136,7 +158,8 @@ void AMyGameStateBase::DecrementStartingCountdown()
136158
return;
137159
}
138160

139-
StartingTimerSecRemainInternal -= UGameStateDataAsset::Get().GetTickInterval();
161+
const float NewValue = StartingTimerSecRemainInternal - UGameStateDataAsset::Get().GetTickInterval();
162+
SetStartingTimerSecondsRemain(NewValue);
140163

141164
if (IsStartingTimerElapsed())
142165
{
@@ -149,6 +172,28 @@ void AMyGameStateBase::DecrementStartingCountdown()
149172
* Runs during the match (120...0)
150173
********************************************************************************************* */
151174

175+
// Sets the left second to the end of the match
176+
void AMyGameStateBase::SetInGameTimerSecondsRemain(float NewInGameTimerSecRemain)
177+
{
178+
InGameTimerSecRemainInternal = NewInGameTimerSecRemain;
179+
ApplyInGameTimerSecondsRemain();
180+
}
181+
182+
// Is called on client when in-match timer was updated
183+
void AMyGameStateBase::OnRep_InGameTimerSecRemain()
184+
{
185+
ApplyInGameTimerSecondsRemain();
186+
}
187+
188+
// Updates current in-match timer seconds remain
189+
void AMyGameStateBase::ApplyInGameTimerSecondsRemain()
190+
{
191+
if (OnInGameTimerSecRemainChanged.IsBound())
192+
{
193+
OnInGameTimerSecRemainChanged.Broadcast(InGameTimerSecRemainInternal);
194+
}
195+
}
196+
152197
// Is called during the In-Game state to handle time consuming for the current match
153198
void AMyGameStateBase::DecrementInGameCountdown()
154199
{
@@ -158,7 +203,8 @@ void AMyGameStateBase::DecrementInGameCountdown()
158203
return;
159204
}
160205

161-
InGameTimerSecRemainInternal -= UGameStateDataAsset::Get().GetTickInterval();
206+
const float NewValue = InGameTimerSecRemainInternal - UGameStateDataAsset::Get().GetTickInterval();
207+
SetInGameTimerSecondsRemain(NewValue);
162208

163209
if (IsInGameTimerElapsed())
164210
{
@@ -186,8 +232,8 @@ void AMyGameStateBase::TriggerCountdowns()
186232
return;
187233
}
188234

189-
StartingTimerSecRemainInternal = UGameStateDataAsset::Get().GetStartingCountdown();
190-
InGameTimerSecRemainInternal = UGameStateDataAsset::Get().GetInGameCountdown();
235+
SetStartingTimerSecondsRemain(UGameStateDataAsset::Get().GetStartingCountdown());
236+
SetInGameTimerSecondsRemain(UGameStateDataAsset::Get().GetInGameCountdown());
191237

192238
constexpr bool bInLoop = true;
193239
const float InRate = UGameStateDataAsset::Get().GetTickInterval();

Diff for: Source/Bomber/Private/LevelActors/PlayerCharacter.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,6 @@ void APlayerCharacter::SetActorHiddenInGame(bool bNewHidden)
314314
{
315315
Super::SetActorHiddenInGame(bNewHidden);
316316

317-
ResetPowerups();
318-
319317
if (UMySkeletalMeshComponent* MySkeletalMeshComponent = GetMySkeletalMeshComponent())
320318
{
321319
const ECollisionEnabled::Type NewType = bNewHidden ? ECollisionEnabled::NoCollision : ECollisionEnabled::PhysicsOnly;
@@ -329,15 +327,15 @@ void APlayerCharacter::SetActorHiddenInGame(bool bNewHidden)
329327
ConstructPlayerCharacter();
330328

331329
TryPossessController();
332-
333-
return;
334330
}
335-
336-
// Is removed from Generated Map
337-
if (Controller)
331+
else if (Controller)
338332
{
333+
// Is removed from Generated Map
334+
339335
Controller->UnPossess();
340336
}
337+
338+
ResetPowerups();
341339
}
342340

343341
// Called when this Pawn is possessed. Only called on the server (or in standalone)
@@ -454,7 +452,13 @@ void APlayerCharacter::ApplyPowerups()
454452
MovementComponent->MaxWalkSpeed = SkateN;
455453
}
456454

457-
// Apply others
455+
// Apply others types of powerups
456+
457+
// Notify listeners
458+
if (OnPowerUpsChanged.IsBound())
459+
{
460+
OnPowerUpsChanged.Broadcast(PowerupsInternal);
461+
}
458462
}
459463

460464
// Reset all picked up powerups
@@ -687,4 +691,4 @@ void APlayerCharacter::MovePlayer(const FInputActionValue& ActionValue)
687691

688692
AddMovementInput(ForwardDirection, MovementVector.Y);
689693
AddMovementInput(RightDirection, MovementVector.X);
690-
}
694+
}

Diff for: Source/Bomber/Private/UI/InGameWidget.cpp

-67
This file was deleted.

Diff for: Source/Bomber/Private/UI/MyHUD.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
//---
55
#include "DataAssets/UIDataAsset.h"
66
#include "MyUtilsLibraries/UtilsLibrary.h"
7-
#include "UI/InGameWidget.h"
87
#include "UI/SettingsWidget.h"
98
//---
109
#include "UnrealClient.h"
@@ -101,7 +100,7 @@ void AMyHUD::InitWidgets()
101100

102101
const UUIDataAsset& UIDataAsset = UUIDataAsset::Get();
103102

104-
InGameWidgetInternal = CreateWidgetByClass<UInGameWidget>(UIDataAsset.GetInGameWidgetClass());
103+
InGameWidgetInternal = CreateWidgetByClass<UUserWidget>(UIDataAsset.GetInGameWidgetClass());
105104

106105
FPSCounterWidgetInternal = CreateWidgetByClass(UIDataAsset.GetFPSCounterWidgetClass());
107106

@@ -133,4 +132,4 @@ void AMyHUD::OnViewportResizedWhenInit(FViewport* Viewport, uint32 Index)
133132
}
134133

135134
InitWidgets();
136-
}
135+
}

Diff for: Source/Bomber/Private/UtilityLibraries/MyBlueprintFunctionLibrary.cpp

+12-5
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
#include "GameFramework/MyPlayerState.h"
1717
#include "LevelActors/PlayerCharacter.h"
1818
#include "MyUtilsLibraries/UtilsLibrary.h"
19+
#include "MyUtilsLibraries/WidgetUtilsLibrary.h"
1920
#include "Subsystems/GeneratedMapSubsystem.h"
2021
#include "UI/InGameMenuWidget.h"
21-
#include "UI/InGameWidget.h"
2222
#include "UI/MyHUD.h"
2323
#include "UtilityLibraries/CellsUtilsLibrary.h"
2424
//---
@@ -44,6 +44,13 @@ ELevelType UMyBlueprintFunctionLibrary::GetLevelType()
4444
return ELevelType::First;
4545
}
4646

47+
// Is used a lot by the UI View Models as 'Conversion Function' to show or hide own widget
48+
ESlateVisibility UMyBlueprintFunctionLibrary::GetVisibilityByGameState(const ECurrentGameState& GameStateProperty, int32 GameStates)
49+
{
50+
const bool bMatching = EnumHasAnyFlags(GameStateProperty, TO_ENUM(ECurrentGameState, GameStates));
51+
return bMatching ? ESlateVisibility::Visible : ESlateVisibility::Collapsed;
52+
}
53+
4754
/* ---------------------------------------------------
4855
* Framework pointer getters
4956
* --------------------------------------------------- */
@@ -125,7 +132,7 @@ AMyHUD* UMyBlueprintFunctionLibrary::GetMyHUD(const UObject* OptionalWorldContex
125132
}
126133

127134
// Returns the In-Game widget
128-
UInGameWidget* UMyBlueprintFunctionLibrary::GetInGameWidget(const UObject* OptionalWorldContext/* = nullptr*/)
135+
UUserWidget* UMyBlueprintFunctionLibrary::GetInGameWidget(const UObject* OptionalWorldContext/* = nullptr*/)
129136
{
130137
const AMyHUD* MyHUD = GetMyHUD(OptionalWorldContext);
131138
return MyHUD ? MyHUD->GetInGameWidget() : nullptr;
@@ -134,8 +141,8 @@ UInGameWidget* UMyBlueprintFunctionLibrary::GetInGameWidget(const UObject* Optio
134141
// Returns the In-Game Menu widget
135142
UInGameMenuWidget* UMyBlueprintFunctionLibrary::GetInGameMenuWidget(const UObject* OptionalWorldContext/* = nullptr*/)
136143
{
137-
const UInGameWidget* InGameWidget = GetInGameWidget(OptionalWorldContext);
138-
return InGameWidget ? InGameWidget->GetInGameMenuWidget() : nullptr;
144+
const UUserWidget* InGameWidget = GetInGameWidget(OptionalWorldContext);
145+
return InGameWidget ? FWidgetUtilsLibrary::GetChildWidgetOfClass<UInGameMenuWidget>(InGameWidget) : nullptr;
139146
}
140147

141148
// Returns specified player character, by default returns local player
@@ -209,4 +216,4 @@ bool UMyBlueprintFunctionLibrary::IsActorHasAnyMatchingType(const AActor* Actor,
209216
{
210217
const EActorType ActorType = GetActorType(Actor);
211218
return BitwiseActorTypes(TO_FLAG(ActorType), ActorsTypesBitmask);
212-
}
219+
}

Diff for: Source/Bomber/Public/Bomber.h

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ using EIT = EItemType;
126126

127127
/**
128128
* The replicated states of the game. It shares the state between all the players at the same time.
129+
* Can be tracked by listening UGlobalEventsSubsystem::Get().OnGameStateChanged
129130
*/
130131
UENUM(BlueprintType, meta = (Bitflags, UseEnumValuesAsMaskValuesInEditor = "true"))
131132
enum class ECurrentGameState : uint8

0 commit comments

Comments
 (0)