Skip to content

Commit cebc813

Browse files
committed
Added the 'CheatSetting' console command for the 'Settings Widget Constructor' plugin, removed that cheat from the project level
1 parent e8ae8e4 commit cebc813

File tree

8 files changed

+131
-68
lines changed

8 files changed

+131
-68
lines changed

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

+13-4
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,30 @@
22

33
#include "WidgetUtilsLibrary.h"
44
//---
5+
#include "Blueprint/WidgetBlueprintLibrary.h"
56
#include "Blueprint/WidgetTree.h"
67

78
// Return the parent widget of a specific class in the widget tree hierarchy
8-
UWidget* UWidgetUtilsLibrary::GetParentWidgetOfClass(const UWidget* InWidget, TSubclassOf<UWidget> ParentWidgetClass)
9+
UUserWidget* UWidgetUtilsLibrary::GetParentWidgetOfClass(const UUserWidget* InWidget, TSubclassOf<UUserWidget> ParentWidgetClass)
910
{
1011
UObject* It = InWidget ? InWidget->GetParent() : nullptr;
1112
if (!It)
1213
{
1314
return nullptr;
1415
}
1516

16-
UWidget* FoundWidget = nullptr;
17+
UUserWidget* FoundWidget = nullptr;
1718
while ((It = It->GetOuter()) != nullptr)
1819
{
1920
// Check every outer until the desired one is found
2021
if (It->IsA(ParentWidgetClass))
2122
{
22-
FoundWidget = Cast<UWidget>(It);
23+
FoundWidget = Cast<UUserWidget>(It);
2324
break;
2425
}
2526

2627
if (!It->IsA<UWidgetTree>()
27-
&& !It->IsA<UWidget>())
28+
&& !It->IsA<UUserWidget>())
2829
{
2930
// No sense to iterate non-widget outers
3031
break;
@@ -34,3 +35,11 @@ UWidget* UWidgetUtilsLibrary::GetParentWidgetOfClass(const UWidget* InWidget, TS
3435
return FoundWidget;
3536
}
3637

38+
// Returns first widget by specified class iterating all widget objects
39+
UUserWidget* UWidgetUtilsLibrary::FindWidgetOfClass(UObject* WorldContextObject, TSubclassOf<UUserWidget> ParentWidgetClass)
40+
{
41+
TArray<UUserWidget*> FoundWidgets;
42+
UWidgetBlueprintLibrary::GetAllWidgetsOfClass(WorldContextObject, /*out*/FoundWidgets, ParentWidgetClass);
43+
return !FoundWidgets.IsEmpty()? FoundWidgets[0] : nullptr;
44+
}
45+

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

+12-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//---
99
#include "WidgetUtilsLibrary.generated.h"
1010

11-
class UWidget;
11+
class UUserWidget;
1212

1313
/**
1414
* The common functions library for widgets.
@@ -20,12 +20,20 @@ class MYUTILS_API UWidgetUtilsLibrary : public UBlueprintFunctionLibrary
2020

2121
public:
2222
/** Return the parent widget of a specific class in the widget tree hierarchy. */
23-
UFUNCTION(BlueprintPure, Category = "C++", meta = (DefaultToSelf = "InWidget"))
24-
static UWidget* GetParentWidgetOfClass(const UWidget* InWidget, TSubclassOf<UWidget> ParentWidgetClass);
23+
UFUNCTION(BlueprintPure, Category = "C++", meta = (DefaultToSelf = "InWidget", HidePin = "InWidget"))
24+
static UUserWidget* GetParentWidgetOfClass(const UUserWidget* InWidget, TSubclassOf<UUserWidget> ParentWidgetClass);
2525

2626
/** Return the parent widget of a specific class in the widget tree hierarchy. */
2727
template <typename T>
28-
static FORCEINLINE T* GetParentWidgetOfClass(const UWidget* ChildWidget) { return Cast<T>(GetParentWidgetOfClass(ChildWidget, T::StaticClass())); }
28+
static FORCEINLINE T* GetParentWidgetOfClass(const UUserWidget* ChildWidget) { return Cast<T>(GetParentWidgetOfClass(ChildWidget, T::StaticClass())); }
29+
30+
/** Returns first widget found by specified class iterating all widget objects. */
31+
UFUNCTION(BlueprintPure, Category = "C++", meta = (WorldContext = "WorldContextObject"))
32+
static UUserWidget* FindWidgetOfClass(UObject* WorldContextObject, TSubclassOf<UUserWidget> ParentWidgetClass);
33+
34+
/** Returns first widget found by specified class iterating all widget objects. */
35+
template <typename T>
36+
static FORCEINLINE T* FindWidgetOfClass(UObject* WorldContextObject) { return Cast<T>(FindWidgetOfClass(WorldContextObject, T::StaticClass())); }
2937

3038
/** Returns the slate widget from UMG widget.
3139
* As an example, it returns SCheckbox slate widget from UCheckBox widget. */
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) Yevhenii Selivanov
2+
3+
#include "SettingsCheatExtension.h"
4+
//---
5+
#include "WidgetUtilsLibrary.h"
6+
#include "UI/SettingsWidget.h"
7+
8+
// Default constructor
9+
USettingsCheatExtension::USettingsCheatExtension()
10+
{
11+
UCheatManager::RegisterForOnCheatManagerCreated(FOnCheatManagerCreated::FDelegate::CreateUObject(this, &ThisClass::OnCheatManagerCreated));
12+
}
13+
14+
// Register a delegate to call whenever a cheat manager is spawned; it will also be called immediately for cheat managers that already exist at this point
15+
void USettingsCheatExtension::OnCheatManagerCreated(UCheatManager* CheatManager)
16+
{
17+
if (!HasAllFlags(RF_ClassDefaultObject) // Continue if only is CDO object
18+
|| !CheatManager)
19+
{
20+
return;
21+
}
22+
23+
// Spawn the extension from its CDO and add it to the cheat manager
24+
USettingsCheatExtension* Extension = NewObject<ThisClass>(CheatManager);
25+
CheatManager->AddCheatManagerExtension(Extension);
26+
}
27+
28+
// Override the setting value with the cheat
29+
void USettingsCheatExtension::CheatSetting(const FString& TagByValue) const
30+
{
31+
USettingsWidget* SettingsWidget = UWidgetUtilsLibrary::FindWidgetOfClass<USettingsWidget>(GetWorld());
32+
if (!SettingsWidget)
33+
{
34+
return;
35+
}
36+
37+
if (TagByValue.IsEmpty())
38+
{
39+
return;
40+
}
41+
42+
static const FString Delimiter = TEXT("?");
43+
TArray<FString> SeparatedStrings;
44+
TagByValue.ParseIntoArray(SeparatedStrings, *Delimiter);
45+
46+
static constexpr int32 TagIndex = 0;
47+
FName TagName = NAME_None;
48+
if (SeparatedStrings.IsValidIndex(TagIndex))
49+
{
50+
TagName = *SeparatedStrings[TagIndex];
51+
}
52+
53+
if (TagName.IsNone())
54+
{
55+
return;
56+
}
57+
58+
// Extract value
59+
static constexpr int32 ValueIndex = 1;
60+
FString TagValue = TEXT("");
61+
if (SeparatedStrings.IsValidIndex(ValueIndex))
62+
{
63+
TagValue = SeparatedStrings[ValueIndex];
64+
}
65+
66+
SettingsWidget->SetSettingValue(TagName, TagValue);
67+
SettingsWidget->SaveSettings();
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) Yevhenii Selivanov
2+
3+
#pragma once
4+
5+
#include "GameFramework/CheatManager.h"
6+
//---
7+
#include "SettingsCheatExtension.generated.h"
8+
9+
/**
10+
* Automatically extends any cheat manager with settings-related console commands.
11+
*/
12+
UCLASS()
13+
class MYSETTINGSWIDGETCONSTRUCTOR_API USettingsCheatExtension : public UCheatManagerExtension
14+
{
15+
GENERATED_BODY()
16+
17+
public:
18+
/** Default constructor. */
19+
USettingsCheatExtension();
20+
21+
/** Register a delegate to call whenever a cheat manager is spawned; it will also be called immediately for cheat managers that already exist at this point. */
22+
void OnCheatManagerCreated(UCheatManager* CheatManager);
23+
24+
/** Override the setting value with the cheat.
25+
* [Types] [Example command]
26+
* Button: CheatSetting Close
27+
* Checkbox: CheatSetting VSync?1
28+
* Combobox index: CheatSetting Shadows?2
29+
* Combobox list: CheatSetting Shadows?Low,Medium,High
30+
* Slider: CheatSetting Audio?0.5
31+
* Text Line: CheatSetting Title?Settings
32+
* User Input: CheatSetting Player?JanSeliv
33+
*
34+
* @param TagByValue Tag?Value */
35+
UFUNCTION(Exec)
36+
void CheatSetting(const FString& TagByValue) const;
37+
};

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

-41
Original file line numberDiff line numberDiff line change
@@ -114,44 +114,3 @@ void UMyCheatManager::SetGodMode(bool bShouldEnable) const
114114
MapComponent->SetUndestroyable(bShouldEnable);
115115
}
116116
}
117-
118-
// Set new setting value
119-
void UMyCheatManager::SetSetting(const FString& TagByValue) const
120-
{
121-
if (TagByValue.IsEmpty())
122-
{
123-
return;
124-
}
125-
126-
static const FString Delimiter = TEXT("?");
127-
TArray<FString> SeparatedStrings;
128-
TagByValue.ParseIntoArray(SeparatedStrings, *Delimiter);
129-
130-
static constexpr int32 TagIndex = 0;
131-
FName TagName = NAME_None;
132-
if (SeparatedStrings.IsValidIndex(TagIndex))
133-
{
134-
TagName = *SeparatedStrings[TagIndex];
135-
}
136-
137-
if (TagName.IsNone())
138-
{
139-
return;
140-
}
141-
142-
// Extract value
143-
static constexpr int32 ValueIndex = 1;
144-
FString TagValue = TEXT("");
145-
if (SeparatedStrings.IsValidIndex(ValueIndex))
146-
{
147-
TagValue = SeparatedStrings[ValueIndex];
148-
}
149-
150-
const AMyHUD* MyHUD = USingletonLibrary::GetMyHUD();
151-
USettingsWidget* SettingsWidget = MyHUD ? MyHUD->GetSettingsWidget() : nullptr;
152-
if (SettingsWidget)
153-
{
154-
SettingsWidget->SetSettingValue(TagName, TagValue);
155-
SettingsWidget->SaveSettings();
156-
}
157-
}

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

-18
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,4 @@ class BOMBER_API UMyCheatManager : public UCheatManager
8383
/** Enable or disable the God mode to make a controllable player undestroyable. */
8484
UFUNCTION(Exec, meta = (OverrideNativeName = "Bomber.Player.SetGodMode"))
8585
void SetGodMode(bool bShouldEnable) const;
86-
87-
/* ---------------------------------------------------
88-
* UI
89-
* --------------------------------------------------- */
90-
91-
/** Override the setting value.
92-
* Types: Examples:
93-
* Button: Bomber.UI.SetSetting Close
94-
* Checkbox: Bomber.UI.SetSetting VSync?1
95-
* Combobox index: Bomber.UI.SetSetting Shadows?2
96-
* Combobox list: Bomber.UI.SetSetting Shadows?Low,Medium,High
97-
* Slider: Bomber.UI.SetSetting Audio?0.5
98-
* Text Line: Bomber.UI.SetSetting Title?Bomber
99-
* User Input: Bomber.UI.SetSetting Player?JanSeliv
100-
* @param TagByValue Tag?Value
101-
*/
102-
UFUNCTION(Exec, meta = (OverrideNativeName = "Bomber.UI.SetSetting"))
103-
void SetSetting(const FString& TagByValue) const;
10486
};

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class BOMBER_API AGeneratedMap final : public AActor
242242
const FCell& Cell,
243243
EPathType Pathfinder,
244244
int32 SideLength,
245-
UPARAM(meta = (Bitmask, BitmaskEnum = "/Script/Bomber.ECellDirection")) int32 DirectionsBitmask,
245+
int32 DirectionsBitmask,
246246
bool bBreakInputCells = false) const;
247247

248248
/**

0 commit comments

Comments
 (0)