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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
tweak: cleanup + add shared_ptrs
ToeKneeRED committed Sep 26, 2024
commit e474c22810b602949d8e8f2ab2b3c1a6a2dcaff1
22 changes: 12 additions & 10 deletions Code/client/Services/Debug/Views/RebindView.cpp
Original file line number Diff line number Diff line change
@@ -13,19 +13,18 @@ void DebugService::DrawRebindView()
auto& inputService = World::Get().GetInputService();
auto& rebindService = World::Get().GetRebindService();
auto uiKey = inputService.GetUIKey();
static bool s_bindingActive = false;

ImGui::SetNextWindowSize(ImVec2(250, 440), ImGuiCond_FirstUseEver);
ImGui::Begin("Rebind");

if (ImGui::CollapsingHeader("UI"))
{
if (ImGui::Button("Open/Close", ImVec2(100, 30)) || s_bindingActive)
if (ImGui::Button("Open/Close", ImVec2(100, 30)) || m_rebindActive)
{
ImGui::SameLine(0);
s_bindingActive = true;
m_rebindActive = true;

if (s_bindingActive)
if (m_rebindActive)
ImGui::Text("Press a key...");
else
ImGui::Text(uiKey.first.c_str());
@@ -36,16 +35,19 @@ void DebugService::DrawRebindView()
if (GetAsyncKeyState(key) & 0x8000)
{
if (key == VK_ESCAPE || key == VK_LBUTTON || key == VK_RBUTTON)
{
m_rebindActive = false;
break;
}

auto& newKey = rebindService.GetKeyFromVKKeyCode(key);

if (!newKey.first.empty())
auto newKey = rebindService.GetKeyFromVKKeyCode(key);
if (!newKey->first.empty())
{
s_bindingActive = false;
uiKey = newKey;
m_rebindActive = false;
uiKey = *newKey;

if(rebindService.SetUIKey(uiKey))
if(rebindService.SetUIKey(std::move(newKey)))
{
TiltedPhoques::DInputHook::Get().SetToggleKeys({DIK_RCONTROL, static_cast<unsigned>(uiKey.second.diKeyCode)});
break;
2 changes: 2 additions & 0 deletions Code/client/Services/DebugService.h
Original file line number Diff line number Diff line change
@@ -81,6 +81,8 @@ struct DebugService
String SubtitleText = "";
uint32_t TopicID = 0;

bool m_rebindActive = false;

entt::scoped_connection m_updateConnection;
entt::scoped_connection m_drawImGuiConnection;
entt::scoped_connection m_dialogueConnection;
6 changes: 3 additions & 3 deletions Code/client/Services/Generic/InputService.cpp
Original file line number Diff line number Diff line change
@@ -458,12 +458,12 @@ LRESULT CALLBACK InputService::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPAR
return 0;
}

bool InputService::SetUIKey(const RebindService::Key& acKey) noexcept
bool InputService::SetUIKey(std::shared_ptr<RebindService::Key> acKey) noexcept
{
if (acKey.first.empty() || acKey.second.diKeyCode == -1 || acKey.second.vkKeyCode == -1)
if (acKey->first.empty() || acKey->second.diKeyCode == -1 || acKey->second.vkKeyCode == -1)
return false;

m_pUiKey = acKey;
m_pUiKey = *acKey;
return true;
}

46 changes: 22 additions & 24 deletions Code/client/Services/Generic/RebindService.cpp
Original file line number Diff line number Diff line change
@@ -5,47 +5,46 @@ RebindService::RebindService(InputService& aInputService) :
{
}

const RebindService::Key& RebindService::GetKeyFromName(const TiltedPhoques::String& acKeyName) const noexcept
std::shared_ptr<RebindService::Key> RebindService::GetKeyFromName(const TiltedPhoques::String& acKeyName) const noexcept
{
const auto& key = std::find_if(m_keys.begin(), m_keys.end(), [acKeyName](const Key& aKey) { return aKey.first == acKeyName; });

Key pKey;
std::shared_ptr newKey = std::make_shared<Key>(*key);

if (key != m_keys.end())
{
pKey = *key;
*newKey = *key;
}

return pKey;
return newKey;
}


const RebindService::Key& RebindService::GetKeyFromVKKeyCode(int32_t aKeyCode) const noexcept
std::shared_ptr<RebindService::Key> RebindService::GetKeyFromVKKeyCode(int32_t aKeyCode) const noexcept
{
const auto& key = std::find_if(m_keys.begin(), m_keys.end(), [aKeyCode](const Key& aKey) { return aKey.second.vkKeyCode == aKeyCode; });

Key pKey;
std::shared_ptr newKey = std::make_shared<Key>(*key);

if (key != m_keys.end())
{
pKey = *key;
*newKey = *key;
}

return pKey;
return newKey;
}

const RebindService::Key& RebindService::GetKeyFromDIKeyCode(int32_t aKeyCode) const noexcept
std::shared_ptr<RebindService::Key> RebindService::GetKeyFromDIKeyCode(int32_t aKeyCode) const noexcept
{
const auto& key = std::find_if(m_keys.begin(), m_keys.end(), [aKeyCode](const Key& aKey) { return aKey.second.diKeyCode == aKeyCode; });

Key pKey;
std::shared_ptr newKey = std::make_shared<Key>(*key);

if (key != m_keys.end())
{
pKey = *key;
*newKey = *key;
}

return pKey;
return newKey;
}

int32_t RebindService::GetDIKeyCode(const TiltedPhoques::String& aKeyName) const noexcept
@@ -80,21 +79,21 @@ int32_t RebindService::GetVKKeyCode(const TiltedPhoques::String& aKeyName) const
return KeyCodes::Error;
}

bool RebindService::SetUIKey(const Key& apKey) noexcept
bool RebindService::SetUIKey(std::shared_ptr<RebindService::Key> apKey) noexcept
{
m_inputService.SetUIKey(apKey);
m_pUiKey = apKey;
m_pUiKey = *apKey;
m_inputService.SetUIKey(std::move(apKey));
return true;
}

bool RebindService::SetUIKeyFromVKKeyCode(int32_t aKeyCode) noexcept
{
const auto& key = GetKeyFromVKKeyCode(aKeyCode);

if (key != m_errorKey)
if (!key->first.empty())
{
m_inputService.SetUIKey(key);
m_pUiKey = key;
m_pUiKey = *key;
m_inputService.SetUIKey(std::move(key));
return true;
}

@@ -105,10 +104,10 @@ bool RebindService::SetUIKeyFromDIKeyCode(int32_t aKeyCode) noexcept
{
const auto& key = GetKeyFromDIKeyCode(aKeyCode);

if (key != m_errorKey)
if (!key->first.empty())
{
m_pUiKey = *key;
m_inputService.SetUIKey(key);
m_pUiKey = key;
return true;
}

@@ -117,11 +116,10 @@ bool RebindService::SetUIKeyFromDIKeyCode(int32_t aKeyCode) noexcept

bool RebindService::SetUIKeyFromKeyName(const TiltedPhoques::String& acKeyName) noexcept
{
if (const auto& key = GetKeyFromName(acKeyName); key != m_errorKey)
if (const auto& key = GetKeyFromName(acKeyName); !key->first.empty())
{
//World::Get().GetInputService().SetUIKey(key);
m_pUiKey = *key;
m_inputService.SetUIKey(key);
m_pUiKey = key;
return true;
}

2 changes: 1 addition & 1 deletion Code/client/Services/InputService.h
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ struct InputService

// Use this function to get the player's current UI key
RebindService::Key GetUIKey() const noexcept { return m_pUiKey; }
bool SetUIKey(const RebindService::Key& apKey) noexcept;
bool SetUIKey(std::shared_ptr<RebindService::Key> apKey) noexcept;

static LRESULT WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

84 changes: 17 additions & 67 deletions Code/client/Services/RebindService.h
Original file line number Diff line number Diff line change
@@ -8,87 +8,45 @@ struct RebindService
{
struct KeyCodes
{
enum
{
Error = -1
};
enum { Error = -1 };

int32_t vkKeyCode = Error;
int32_t diKeyCode = Error;

bool operator==(const KeyCodes& acKeyCodes) const
{
return this->vkKeyCode == acKeyCodes.vkKeyCode && this->diKeyCode == acKeyCodes.diKeyCode;
}
};

/*struct Key
{
TiltedPhoques::String keyName;
KeyCodes keyCodes;

Key() : keyName(""), keyCodes({KeyCodes::Error, KeyCodes::Error})
{
}

Key(const TiltedPhoques::String& acKeyName, const KeyCodes& acKeyCodes) :
keyName(acKeyName), keyCodes(acKeyCodes)
{
}

Key(const Key& acKey)
{
keyName = acKey.keyName;
keyCodes = acKey.keyCodes;
}

Key(const std::pair<TiltedPhoques::String, KeyCodes>& acKey)
{
keyName = acKey.first;
keyCodes = acKey.second;
}

bool operator==(const Key& acKey) const
{
return this->keyName == acKey.keyName && this->keyCodes == acKey.keyCodes;
}

bool operator==(const std::pair<TiltedPhoques::String, KeyCodes>& acKey) const
{
return this->keyName == acKey.first && this->keyCodes == acKey.second;
}
};*/

using Key = std::pair<TiltedPhoques::String, KeyCodes>;


RebindService(InputService& aInputService);
~RebindService() = default;

TP_NOCOPYMOVE(RebindService);

const Key& GetUIKey() const noexcept { return m_pUiKey; }
const Key& GetDebugKey() const noexcept { return m_pDebugKey; }
TiltedPhoques::Map<Key::first_type, Key::second_type> GetKeys() const noexcept { return m_keys; }

bool SetUIKey(const Key& apKey) noexcept;
// UI Key
const Key& GetUIKey() const noexcept { return m_pUiKey; }
bool SetUIKey(std::shared_ptr<RebindService::Key> apKey) noexcept;
bool SetUIKeyFromKeyName(const TiltedPhoques::String& acKeyName) noexcept;
bool SetUIKeyFromVKKeyCode(int32_t aKeyCode) noexcept;
bool SetUIKeyFromDIKeyCode(int32_t aKeyCode) noexcept;

const Key& GetKeyFromName(const TiltedPhoques::String& acKeyName) const noexcept;
const Key& GetKeyFromVKKeyCode(int32_t aKeyCode) const noexcept;
const Key& GetKeyFromDIKeyCode(int32_t aKeyCode) const noexcept;

TiltedPhoques::Map<Key::first_type, Key::second_type> GetKeys() const noexcept { return m_keys; }
//const TiltedPhoques::Map<TiltedPhoques::String, KeyCodes>& GetKeys() const noexcept { return m_keys; }
// Debug Key
const Key& GetDebugKey() const noexcept { return m_pDebugKey; }

// Get DirectInput KeyCode
int32_t GetDIKeyCode(const TiltedPhoques::String& aKeyName) const noexcept;
// Get VirtualKey KeyCode
// General Key
std::shared_ptr<RebindService::Key> GetKeyFromName(const TiltedPhoques::String& acKeyName) const noexcept;
std::shared_ptr<RebindService::Key> GetKeyFromVKKeyCode(int32_t aKeyCode) const noexcept;
int32_t GetVKKeyCode(const TiltedPhoques::String& aKeyName) const noexcept;
std::shared_ptr<RebindService::Key> GetKeyFromDIKeyCode(int32_t aKeyCode) const noexcept;
int32_t GetDIKeyCode(const TiltedPhoques::String& aKeyName) const noexcept;
bool IsValidKey(const TiltedPhoques::String& acKeyName) const noexcept { return m_keys.find(acKeyName) != m_keys.end(); }

private:
Key m_pUiKey = {"F2", {VK_F2, DIK_F2}};
Key m_pDebugKey = {"F3", {VK_F3, DIK_F3}};

InputService& m_inputService;

const TiltedPhoques::Map<Key::first_type, Key::second_type> m_keys
{
{"", {KeyCodes::Error, KeyCodes::Error}},
@@ -224,12 +182,4 @@ struct RebindService
{"]", {VK_OEM_6, DIK_RBRACKET}},
{"'", {VK_OEM_7, DIK_APOSTROPHE}},
};


private:
Key m_pUiKey = {"F2", {VK_F2, DIK_F2}};
Key m_pDebugKey = {"F3", {VK_F3, DIK_F3}};
Key m_errorKey = {"", {KeyCodes::Error, KeyCodes::Error}};

InputService& m_inputService;
};