Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom keybinds #724

Open
wants to merge 30 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
9ad0e0b
add: WIP first working attempt
ToeKneeRED Sep 24, 2024
8bcfd78
add: RebindService
ToeKneeRED Sep 25, 2024
e474c22
tweak: cleanup + add shared_ptrs
ToeKneeRED Sep 26, 2024
fb3bbf9
refactor: `GetKey`s
ToeKneeRED Sep 26, 2024
78a06eb
add: fully dynamic UI key setting
ToeKneeRED Sep 30, 2024
921ae2c
tweak: rename to KeybindService
ToeKneeRED Sep 30, 2024
676442e
refactor: remove unused
ToeKneeRED Sep 30, 2024
a052a45
add: KeyPressEvent, debug key rebinding, UI toggle instability, share…
ToeKneeRED Oct 3, 2024
345d870
add: KeyPressEvent, UI toggle stability, rebinding additions, cleanup
ToeKneeRED Oct 5, 2024
e516f5a
Merge branch 'dev' into add/rebind-keys
ToeKneeRED Oct 5, 2024
345bc8a
tweak: cleanup + clang format
ToeKneeRED Oct 5, 2024
b34a1f8
add: wide char support, UI stability
ToeKneeRED Oct 16, 2024
dd335c7
fix: rebinding to wide char key, cleanup
ToeKneeRED Oct 16, 2024
5debd2e
refactor: key name
ToeKneeRED Oct 16, 2024
d635c23
tweak: bump tiltedhooks ref
ToeKneeRED Oct 23, 2024
1b65220
fix: build
ToeKneeRED Oct 23, 2024
c90fa54
fix: ft build
ToeKneeRED Oct 23, 2024
6978cb0
fix: able to type toggle key when input field is focused
ToeKneeRED Nov 11, 2024
1f149dd
refactor: more straightforward and generic BindKey/SetKey functions
ToeKneeRED Nov 14, 2024
fe8ad68
refactor: some `SetKey`s
ToeKneeRED Nov 18, 2024
0fb294c
Merge branch 'dev' into add/rebind-keys
ToeKneeRED Nov 18, 2024
8eb4286
fix: KeybindView
ToeKneeRED Nov 18, 2024
f83de8e
Merge branch 'dev' into add/rebind-keys
ToeKneeRED Nov 21, 2024
9e53de0
fix: WIP reveal players
ToeKneeRED Nov 22, 2024
0f3461b
Merge branch 'dev' into add/rebind-keys
ToeKneeRED Nov 23, 2024
c679ae8
fix: reveal players keybind and merge conflicts, refactors
ToeKneeRED Nov 26, 2024
e913636
Merge branch 'tiltedphoques:dev' into add/rebind-keys
ToeKneeRED Nov 26, 2024
1aea8f3
fix: using disable keybind to cancel rebind no longer closes UI and r…
ToeKneeRED Nov 27, 2024
1c04883
tweak: clang format
ToeKneeRED Nov 27, 2024
8774cce
Merge branch 'dev' into add/rebind-keys
ToeKneeRED Nov 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Code/client/Events/KeyPressEvent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

/**
* @brief Dispatched when VirtualKey is pressed.
*/
struct KeyPressEvent
{
KeyPressEvent(const uint16_t acKeyCode)
: VirtualKey(acKeyCode)
{
}

uint16_t VirtualKey{};
};
20 changes: 18 additions & 2 deletions Code/client/Services/Debug/DebugService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,11 @@ void DebugService::OnUpdate(const UpdateEvent& acUpdateEvent) noexcept
static std::atomic<bool> s_f7Pressed = false;
static std::atomic<bool> s_f6Pressed = false;

if (GetAsyncKeyState(VK_F3) & 0x01)
// Bool state handled via KeybindService due to sometimes missing the key state changes
if (m_debugKeyPressed)
{
m_showDebugStuff = !m_showDebugStuff;
m_debugKeyPressed = false;
}

#if (!IS_MASTER)
Expand Down Expand Up @@ -201,7 +203,7 @@ void DebugService::OnUpdate(const UpdateEvent& acUpdateEvent) noexcept
{
s_f8Pressed = true;

//PlaceActorInWorld();
// PlaceActorInWorld();
}
}
else
Expand All @@ -226,6 +228,7 @@ static bool g_enableWeatherWindow{false};
static bool g_enableCombatWindow{false};
static bool g_enableCalendarWindow{false};
static bool g_enableDragonSpawnerWindow{false};
static bool g_enableKeybindWindow{false};

void DebugService::DrawServerView() noexcept
{
Expand Down Expand Up @@ -315,13 +318,24 @@ void DebugService::OnDraw() noexcept
ImGui::EndMenu();
}
#endif
if (ImGui::BeginMenu("Keybinds"))
{
ImGui::MenuItem("Show keybinds menu", nullptr, &g_enableKeybindWindow);
if (ImGui::Button("Reset all binds to default"))
{
m_world.GetKeybindService().ResetKeybinds(KeybindService::Keybind::All);
}

ImGui::EndMenu();
}
if (ImGui::BeginMenu("Debuggers"))
{
ImGui::MenuItem("Quests", nullptr, &g_enableQuestWindow);
ImGui::MenuItem("Entities", nullptr, &g_enableEntitiesWindow);
ImGui::MenuItem("Server", nullptr, &g_enableServerWindow);
ImGui::MenuItem("Party", nullptr, &g_enablePartyWindow);
ImGui::MenuItem("Dragon spawner", nullptr, &g_enableDragonSpawnerWindow);
ImGui::MenuItem("Keybinds", nullptr, &g_enableKeybindWindow);

#if (!IS_MASTER)
ImGui::MenuItem("Network", nullptr, &g_enableNetworkWindow);
Expand Down Expand Up @@ -368,6 +382,8 @@ void DebugService::OnDraw() noexcept
DrawPartyView();
if (g_enableDragonSpawnerWindow)
DrawDragonSpawnerView();
if (g_enableKeybindWindow)
DrawKeybindView();

#if (!IS_MASTER)
if (g_enableNetworkWindow)
Expand Down
143 changes: 143 additions & 0 deletions Code/client/Services/Debug/Views/KeybindView.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#include <Services/DebugService.h>
#include <Services/KeybindService.h>

#include "World.h"

#include <imgui.h>

TiltedPhoques::String ConvertWstringToString(const TiltedPhoques::WString& acWideString)
{
if (acWideString.empty())
{
return {};
}

int size_needed = WideCharToMultiByte(CP_UTF8, 0, acWideString.c_str(), static_cast<int>(acWideString.length()), NULL, 0, NULL, NULL);

TiltedPhoques::String str(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, acWideString.c_str(), static_cast<int>(acWideString.length()), &str[0], size_needed, NULL, NULL);

return str;
}

TiltedPhoques::WString BindKey(const TiltedPhoques::WString& acKeyName, bool& aBindActive, const KeybindService::Keybind& acKeyType)
{
auto& keybindService = World::Get().GetKeybindService();
TiltedPhoques::WString keyName = acKeyName;

ImGui::SameLine(0);
aBindActive = true;

for (uint16_t key = 255; key > 1; key--)
{
if (GetAsyncKeyState(key) & 0x8000)
{
if (key == VK_ESCAPE)
{
aBindActive = false;
break;
}
if (key == VK_LBUTTON || key == VK_RBUTTON)
{
continue;
}

aBindActive = false;

switch (acKeyType)
{
default: break;
case KeybindService::None: break;
case KeybindService::UI:
keybindService.BindKey(KeybindService::UI, key);
break;
case KeybindService::Debug:
keybindService.BindKey(KeybindService::Debug, key);
break;
case KeybindService::RevealPlayers:
keybindService.BindKey(KeybindService::RevealPlayers, key);
break;
}

break;
}
}

if (aBindActive)
ImGui::Text("Press a key...");
else
{
const auto& narrowString = ConvertWstringToString(keyName);
ImGui::Text("%s", narrowString.c_str());
}

return keyName;
}


void DebugService::DrawKeybindView()
{
auto& keybindService = World::Get().GetKeybindService();

ImGui::SetNextWindowSize(ImVec2(350, 150), ImGuiCond_FirstUseEver);
ImGui::Begin("Keybinds");

if (IsRebinding())
ImGui::Text("Press Escape to cancel");

TiltedPhoques::WString uiKeyName = keybindService.GetKey(KeybindService::Keybind::UI).first;
if (ImGui::Button("Show/Hide STR UI", ImVec2(200, 30)) || m_rebindUI)
{
m_rebindUI = true;
m_rebindDebug = false;
m_rebindRevealPlayers = false;

uiKeyName = BindKey(uiKeyName, m_rebindUI, KeybindService::Keybind::UI);
}
else
{
ImGui::SameLine(0);
const auto& narrowString = ConvertWstringToString(uiKeyName);
ImGui::Text("%s", narrowString.c_str());

m_rebindUI = false;
}

TiltedPhoques::WString debugKeyName = keybindService.GetKey(KeybindService::Keybind::Debug).first;
if (ImGui::Button("Show/Hide Debug UI", ImVec2(200, 30)) || m_rebindDebug)
{
m_rebindDebug = true;
m_rebindUI = false;
m_rebindRevealPlayers = false;

debugKeyName = BindKey(debugKeyName, m_rebindDebug, KeybindService::Keybind::Debug);
}
else
{
ImGui::SameLine(0);
const auto& narrowString = ConvertWstringToString(debugKeyName);
ImGui::Text("%s", narrowString.c_str());

m_rebindDebug = false;
}

TiltedPhoques::WString revealKeyName = keybindService.GetKey(KeybindService::Keybind::RevealPlayers).first;
if (ImGui::Button("Reveal Players", ImVec2(200, 30)) || m_rebindRevealPlayers)
{
m_rebindRevealPlayers = true;
m_rebindUI = false;
m_rebindDebug = false;

debugKeyName = BindKey(debugKeyName, m_rebindRevealPlayers, KeybindService::Keybind::RevealPlayers);
}
else
{
ImGui::SameLine(0);
const auto& narrowString = ConvertWstringToString(revealKeyName);
ImGui::Text("%s", narrowString.c_str());

m_rebindRevealPlayers = false;
}

ImGui::End();
}
14 changes: 14 additions & 0 deletions Code/client/Services/DebugService.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "KeybindService.h"

#include <Actor.h>

struct World;
Expand Down Expand Up @@ -30,6 +32,11 @@ struct DebugService

void SetDebugId(const uint32_t aFormId) noexcept;

const KeybindService::Key& GetDebugKey() const noexcept { return m_debugKeybind; }
void SetDebugKey(const TiltedPhoques::SharedPtr<KeybindService::Key>& acpKey) noexcept { m_debugKeybind = *acpKey; }
bool IsRebinding() const noexcept { return m_rebindUI || m_rebindDebug || m_rebindRevealPlayers; }
void DebugPressed() noexcept { m_debugKeyPressed = !m_debugKeyPressed; }

protected:
void OnDraw() noexcept;

Expand Down Expand Up @@ -60,6 +67,7 @@ struct DebugService
void DrawCombatView();
void DrawCalendarView();
void DrawDragonSpawnerView();
void DrawKeybindView();

public:
bool m_showDebugStuff = false;
Expand All @@ -80,6 +88,12 @@ struct DebugService
String SubtitleText = "";
uint32_t TopicID = 0;

KeybindService::Key m_debugKeybind{};
bool m_debugKeyPressed{false};
bool m_rebindUI{false};
bool m_rebindDebug{false};
bool m_rebindRevealPlayers{false};

entt::scoped_connection m_updateConnection;
entt::scoped_connection m_drawImGuiConnection;
entt::scoped_connection m_dialogueConnection;
Expand Down
Loading
Loading