CogWindow: Move Registering of default command bindings to WindowManager

This commit is contained in:
Arnaud Jamin
2023-10-19 23:31:42 -04:00
parent e4ad41601e
commit 804f7c6ba5
7 changed files with 140 additions and 120 deletions
@@ -1,5 +1,6 @@
#include "CogEngineWindow_CommandBindings.h"
#include "CogWindowManager.h"
#include "CogWindowWidgets.h"
#include "GameFramework/PlayerInput.h"
#include "imgui.h"
@@ -18,20 +19,6 @@ UCogEngineWindow_CommandBindings::UCogEngineWindow_CommandBindings()
{
}
//--------------------------------------------------------------------------------------------------------------------------
void UCogEngineWindow_CommandBindings::PostInitProperties()
{
Super::PostInitProperties();
if (bRegisterDefaultCommands)
{
if (RegisterDefaultCommands())
{
bRegisterDefaultCommands = false;
}
}
}
//--------------------------------------------------------------------------------------------------------------------------
void UCogEngineWindow_CommandBindings::RenderContent()
{
@@ -61,7 +48,7 @@ void UCogEngineWindow_CommandBindings::RenderContent()
ImGui::SameLine();
if (FCogWindowWidgets::ButtonWithTooltip("Sort", "Sort the array"))
{
Sort(PlayerInput);
UCogWindowManager::SortCommands(PlayerInput);
PlayerInput->SaveConfig();
}
@@ -77,7 +64,7 @@ void UCogEngineWindow_CommandBindings::RenderContent()
"[F5] Cog.ToggleSelectionMode\n"
))
{
RegisterDefaultCommands();
Owner->RegisterDefaultCommands();
}
for (FKeyBind& KeyBind : PlayerInput->DebugExecBindings)
@@ -105,68 +92,3 @@ void UCogEngineWindow_CommandBindings::RenderContent()
PlayerInput->SaveConfig();
}
}
//--------------------------------------------------------------------------------------------------------------------------
void UCogEngineWindow_CommandBindings::Sort(UPlayerInput* PlayerInput)
{
PlayerInput->DebugExecBindings.Sort([](const FKeyBind& Key1, const FKeyBind& Key2)
{
return Key1.Command.Compare(Key2.Command) < 0;
});
}
//--------------------------------------------------------------------------------------------------------------------------
bool UCogEngineWindow_CommandBindings::RegisterDefaultCommands()
{
APlayerController* PlayerController = GetLocalPlayerController();
if (PlayerController == nullptr)
{
return false;
}
UPlayerInput* PlayerInput = PlayerController->PlayerInput;
if (PlayerInput == nullptr)
{
return false;
}
AddCogCommand(PlayerInput, "Cog.ToggleInput", EKeys::Tab);
AddCogCommand(PlayerInput, "Cog.LoadLayout 1", EKeys::F1);
AddCogCommand(PlayerInput, "Cog.LoadLayout 2", EKeys::F2);
AddCogCommand(PlayerInput, "Cog.LoadLayout 3", EKeys::F3);
AddCogCommand(PlayerInput, "Cog.LoadLayout 4", EKeys::F4);
AddCogCommand(PlayerInput, "Cog.ToggleSelectionMode", EKeys::F5);
Sort(PlayerInput);
PlayerInput->SaveConfig();
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
void UCogEngineWindow_CommandBindings::AddCogCommand(UPlayerInput* PlayerInput, const FString& Command, const FKey& Key)
{
for (FKeyBind& KeyBind : PlayerInput->DebugExecBindings)
{
if (KeyBind.Key == Key && KeyBind.Command != Command)
{
KeyBind.Control = true;
KeyBind.bIgnoreCtrl = false;
}
}
FKeyBind* ExistingKeyBind = PlayerInput->DebugExecBindings.FindByPredicate([Command](const FKeyBind& KeyBind){ return KeyBind.Command == Command; });
if (ExistingKeyBind == nullptr)
{
ExistingKeyBind = &PlayerInput->DebugExecBindings.AddDefaulted_GetRef();
}
FKeyBind CogKeyBind;
CogKeyBind.Command = Command;
CogKeyBind.Control = false;
CogKeyBind.bIgnoreCtrl = true;
CogKeyBind.Key = Key;
*ExistingKeyBind = CogKeyBind;
}
@@ -18,20 +18,10 @@ public:
protected:
virtual void PostInitProperties() override;
virtual void RenderHelp() override;
virtual void RenderContent() override;
virtual bool RegisterDefaultCommands();
private:
UPROPERTY(Config)
bool bRegisterDefaultCommands = true;
void Sort(UPlayerInput* PlayerInput);
void AddCogCommand(UPlayerInput* PlayerInput, const FString& Command, const FKey& Key);
};
@@ -5,6 +5,7 @@
#include "GameFramework/GameUserSettings.h"
#include "GameFramework/InputSettings.h"
#include "GameFramework/PlayerController.h"
#include "GameFramework/PlayerInput.h"
#include "imgui_internal.h"
#include "InputCoreTypes.h"
@@ -227,7 +228,6 @@ bool FCogImguiInputHelper::IsKeyBoundToCommand(UWorld* World, const FKeyEvent& K
return false;
}
//--------------------------------------------------------------------------------------------------------------------------
bool FCogImguiInputHelper::IsConsoleEvent(const FKeyEvent& KeyEvent)
{
@@ -4,6 +4,7 @@
#include "CogImguiInputHelper.h"
#include "imgui.h"
class UPlayerInput;
struct FCogImGuiKeyInfo;
struct FKeyBind;
@@ -2,6 +2,7 @@
#include "CogDebugDrawImGui.h"
#include "CogImguiModule.h"
#include "CogImguiInputHelper.h"
#include "CogImguiWidget.h"
#include "CogWindow_Settings.h"
#include "CogWindow_Spacing.h"
@@ -15,6 +16,20 @@ UCogWindowManager::UCogWindowManager()
{
}
//--------------------------------------------------------------------------------------------------------------------------
void UCogWindowManager::PostInitProperties()
{
Super::PostInitProperties();
if (bRegisterDefaultCommands)
{
if (RegisterDefaultCommands())
{
bRegisterDefaultCommands = false;
}
}
}
//--------------------------------------------------------------------------------------------------------------------------
void UCogWindowManager::InitializeInternal()
{
@@ -515,4 +530,83 @@ void UCogWindowManager::ResetAllWindowsConfig()
{
Window->ResetConfig();
}
}
//--------------------------------------------------------------------------------------------------------------------------
bool UCogWindowManager::RegisterDefaultCommands()
{
UWorld* World = GetWorld();
if (World == nullptr)
{
return false;
}
APlayerController* PlayerController = FCogImguiInputHelper::GetFirstLocalPlayerController(*World);
if (PlayerController == nullptr)
{
return false;
}
UPlayerInput* PlayerInput = PlayerController->PlayerInput;
if (PlayerInput == nullptr)
{
return false;
}
AddCommand(PlayerInput, "Cog.ToggleInput", EKeys::Tab);
AddCommand(PlayerInput, "Cog.LoadLayout 1", EKeys::F1);
AddCommand(PlayerInput, "Cog.LoadLayout 2", EKeys::F2);
AddCommand(PlayerInput, "Cog.LoadLayout 3", EKeys::F3);
AddCommand(PlayerInput, "Cog.LoadLayout 4", EKeys::F4);
AddCommand(PlayerInput, "Cog.ToggleSelectionMode", EKeys::F5);
SortCommands(PlayerInput);
PlayerInput->SaveConfig();
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
void UCogWindowManager::AddCommand(UPlayerInput* PlayerInput, const FString& Command, const FKey& Key)
{
//---------------------------------------------------
// Reassign conflicting commands
//---------------------------------------------------
for (FKeyBind& KeyBind : PlayerInput->DebugExecBindings)
{
if (KeyBind.Key == Key && KeyBind.Command != Command)
{
KeyBind.Control = true;
KeyBind.bIgnoreCtrl = false;
}
}
//---------------------------------------------------
// Find or add desired command
//---------------------------------------------------
FKeyBind* ExistingKeyBind = PlayerInput->DebugExecBindings.FindByPredicate([Command](const FKeyBind& KeyBind) { return KeyBind.Command == Command; });
if (ExistingKeyBind == nullptr)
{
ExistingKeyBind = &PlayerInput->DebugExecBindings.AddDefaulted_GetRef();
}
//---------------------------------------------------
// Assign the key to the command
//---------------------------------------------------
FKeyBind CogKeyBind;
CogKeyBind.Command = Command;
CogKeyBind.Control = false;
CogKeyBind.bIgnoreCtrl = true;
CogKeyBind.Key = Key;
*ExistingKeyBind = CogKeyBind;
}
//--------------------------------------------------------------------------------------------------------------------------
void UCogWindowManager::SortCommands(UPlayerInput* PlayerInput)
{
PlayerInput->DebugExecBindings.Sort([](const FKeyBind& Key1, const FKeyBind& Key2)
{
return Key1.Command.Compare(Key2.Command) < 0;
});
}
@@ -21,13 +21,15 @@ class COGWINDOW_API UCogWindowManager : public UObject
public:
UCogWindowManager();
void Shutdown();
virtual void PostInitProperties() override;
void SortMainMenu();
virtual void Shutdown();
void Render(float DeltaTime);
virtual void SortMainMenu();
void Tick(float DeltaTime);
virtual void Render(float DeltaTime);
virtual void Tick(float DeltaTime);
template<class T>
T* CreateWindow(const FString& Name, bool AddToMainMenu = true)
@@ -39,41 +41,47 @@ public:
return Window;
}
void AddWindow(UCogWindow* Window, bool AddToMainMenu = true);
virtual void AddWindow(UCogWindow* Window, bool AddToMainMenu = true);
void AddMainMenuWidget(UCogWindow* Window);
virtual void AddMainMenuWidget(UCogWindow* Window);
UCogWindow* FindWindowByID(ImGuiID ID);
virtual UCogWindow* FindWindowByID(ImGuiID ID);
void CloseAllWindows();
virtual void CloseAllWindows();
void ResetLayout();
virtual void ResetLayout();
void LoadLayout(int32 LayoutIndex);
virtual void LoadLayout(int32 LayoutIndex);
void SaveLayout(int32 LayoutIndex);
virtual void SaveLayout(int32 LayoutIndex);
bool GetHideAllWindows() const { return bHideAllWindows; }
virtual bool GetHideAllWindows() const { return bHideAllWindows; }
void SetHideAllWindows(bool Value);
virtual void SetHideAllWindows(bool Value);
float GetDPIScale() const { return DPIScale; }
virtual float GetDPIScale() const { return DPIScale; }
void SetDPIScale(float Value);
virtual void SetDPIScale(float Value);
bool GetCompactMode() const { return bCompactMode; }
virtual bool GetCompactMode() const { return bCompactMode; }
void SetCompactMode(bool Value) { bCompactMode = Value; }
virtual void SetCompactMode(bool Value) { bCompactMode = Value; }
bool GetShowHelp() const { return bShowHelp; }
virtual bool GetShowHelp() const { return bShowHelp; }
void SetShowHelp(bool Value) { bShowHelp = Value; }
virtual void SetShowHelp(bool Value) { bShowHelp = Value; }
bool GetPreviewWindowsInMenu() const { return bShowWindowsInMainMenu; }
virtual bool GetPreviewWindowsInMenu() const { return bShowWindowsInMainMenu; }
void SetPreviewWindowsInMenu(bool Value) { bShowWindowsInMainMenu = Value; }
virtual void SetPreviewWindowsInMenu(bool Value) { bShowWindowsInMainMenu = Value; }
void ResetAllWindowsConfig();
virtual void ResetAllWindowsConfig();
virtual bool RegisterDefaultCommands();
static void AddCommand(UPlayerInput* PlayerInput, const FString& Command, const FKey& Key);
static void SortCommands(UPlayerInput* PlayerInput);
protected:
@@ -86,17 +94,17 @@ protected:
TArray<FMenu> SubMenus;
};
void InitializeInternal();
virtual void InitializeInternal();
void RefreshDPIScale();
virtual void RefreshDPIScale();
void RenderMainMenu();
virtual void RenderMainMenu();
FMenu* AddMenu(const FString& Name);
virtual FMenu* AddMenu(const FString& Name);
void RenderOptionMenu(FMenu& Menu);
virtual void RenderOptionMenu(FMenu& Menu);
void RenderMenuItem(UCogWindow& Window, const char* MenuItemName);
virtual void RenderMenuItem(UCogWindow& Window, const char* MenuItemName);
static void SettingsHandler_ClearAll(ImGuiContext* ctx, ImGuiSettingsHandler*);
@@ -137,6 +145,9 @@ protected:
UPROPERTY(Config)
bool bShowWindowsInMainMenu = true;
UPROPERTY(Config)
bool bRegisterDefaultCommands = true;
TSharedPtr<SCogImguiWidget> ImGuiWidget = nullptr;
FMenu MainMenu;
@@ -172,6 +172,8 @@ void UCogSampleFunctionLibrary_Damage::ExecuteDamageGameplayCue(
GameplayCueParameters.Location = HitResult.Location;
GameplayCueParameters.Normal = HitResult.ImpactNormal;
FGameplayTagContainer SpecAssetTags;
EffectSpec.GetAllAssetTags(SpecAssetTags);
TArray<FGameplayTag> Tags;
SpecAssetTags.GetGameplayTagArray(Tags);