From 0a2007bb29fe5fecd7af31dd00c6792778f7c7f2 Mon Sep 17 00:00:00 2001 From: Yimura <24669514+Yimura@users.noreply.github.com> Date: Sat, 26 Nov 2022 22:12:21 +0100 Subject: [PATCH] fix(hotkey_service): duplicate key overwrite (#643) --- src/core/globals.hpp | 5 ++++- src/services/hotkey/hotkey_functions.cpp | 14 ++++++++++++++ src/services/hotkey/hotkey_functions.hpp | 6 ++++++ src/services/hotkey/hotkey_service.cpp | 19 ++++++++++--------- src/services/hotkey/hotkey_service.hpp | 2 +- src/views/settings/view_settings.cpp | 3 +++ 6 files changed, 38 insertions(+), 11 deletions(-) create mode 100644 src/services/hotkey/hotkey_functions.cpp create mode 100644 src/services/hotkey/hotkey_functions.hpp diff --git a/src/core/globals.hpp b/src/core/globals.hpp index c448e995..88a44433 100644 --- a/src/core/globals.hpp +++ b/src/core/globals.hpp @@ -226,6 +226,7 @@ namespace big int menu_toggle = VK_INSERT; int teleport_waypoint = 0; int teleport_objective = 0; + int noclip = 0; }; bool dev_dlc = false; @@ -665,6 +666,7 @@ namespace big this->settings.hotkeys.menu_toggle = j["settings"]["hotkeys"]["menu_toggle"]; this->settings.hotkeys.teleport_waypoint = j["settings"]["hotkeys"]["teleport_waypoint"]; this->settings.hotkeys.teleport_objective = j["settings"]["hotkeys"]["teleport_objective"]; + this->settings.hotkeys.noclip = j["settings"]["hotkeys"]["noclip"]; this->spawn_vehicle.preview_vehicle = j["spawn_vehicle"]["preview_vehicle"]; this->spawn_vehicle.spawn_inside = j["spawn_vehicle"]["spawn_inside"]; @@ -1012,7 +1014,8 @@ namespace big { "hotkeys", { { "menu_toggle", this->settings.hotkeys.menu_toggle }, { "teleport_waypoint", this->settings.hotkeys.teleport_waypoint }, - { "teleport_objective", this->settings.hotkeys.teleport_objective } + { "teleport_objective", this->settings.hotkeys.teleport_objective }, + { "noclip", this->settings.hotkeys.noclip } } } } diff --git a/src/services/hotkey/hotkey_functions.cpp b/src/services/hotkey/hotkey_functions.cpp new file mode 100644 index 00000000..34171919 --- /dev/null +++ b/src/services/hotkey/hotkey_functions.cpp @@ -0,0 +1,14 @@ +#include "common.hpp" +#include "hotkey_functions.hpp" +#include "services/notifications/notification_service.hpp" + +namespace big::hotkey_funcs +{ + void toggle_noclip() + { + const auto state = !g->self.noclip; + g_notification_service->push("Noclip", std::format("Noclip has been {}.", state ? "enabled" : "disabled")); + + g->self.noclip = state; + } +} \ No newline at end of file diff --git a/src/services/hotkey/hotkey_functions.hpp b/src/services/hotkey/hotkey_functions.hpp new file mode 100644 index 00000000..4f9e90b0 --- /dev/null +++ b/src/services/hotkey/hotkey_functions.hpp @@ -0,0 +1,6 @@ +#pragma once + +namespace big::hotkey_funcs +{ + extern void toggle_noclip(); +} \ No newline at end of file diff --git a/src/services/hotkey/hotkey_service.cpp b/src/services/hotkey/hotkey_service.cpp index 9a2ca773..ae2cad5e 100644 --- a/src/services/hotkey/hotkey_service.cpp +++ b/src/services/hotkey/hotkey_service.cpp @@ -1,6 +1,7 @@ #include "hotkey_service.hpp" #include "fiber_pool.hpp" #include "util/teleport.hpp" +#include "hotkey_functions.hpp" namespace big { @@ -8,6 +9,7 @@ namespace big { register_hotkey("waypoint", g->settings.hotkeys.teleport_waypoint, teleport::to_waypoint); register_hotkey("objective", g->settings.hotkeys.teleport_objective, teleport::to_objective); + register_hotkey("noclip", g->settings.hotkeys.noclip, hotkey_funcs::toggle_noclip); g_hotkey_service = this; } @@ -24,18 +26,17 @@ namespace big bool hotkey_service::update_hotkey(const std::string_view name, const key_t key) { - static auto update_hotkey_map = [key](hotkey_map& hotkey_map, rage::joaat_t name_hash) -> bool + static auto update_hotkey_map = [](hotkey_map& hotkey_map, rage::joaat_t name_hash, key_t new_key) -> bool { - if (const auto &it = std::find_if(hotkey_map.begin(), hotkey_map.end(), [name_hash](std::pair pair) -> bool - { - return pair.second.name_hash() == name_hash; - }); it != hotkey_map.end()) + for (auto it = hotkey_map.begin(); it != hotkey_map.end(); ++it) { auto hotkey = it->second; - hotkey.set_key(key); + if (hotkey.name_hash() != name_hash) + continue; - hotkey_map.emplace(key, hotkey); hotkey_map.erase(it); + hotkey.set_key(new_key); + hotkey_map.emplace(new_key, hotkey); return true; } @@ -43,8 +44,8 @@ namespace big }; const auto name_hash = rage::joaat(name); - return update_hotkey_map(m_hotkeys[1], name_hash) // released - && update_hotkey_map(m_hotkeys[0], name_hash); // down + return update_hotkey_map(m_hotkeys[1], name_hash, key) // released + && update_hotkey_map(m_hotkeys[0], name_hash, key); // down } void hotkey_service::wndproc(eKeyState state, key_t key) diff --git a/src/services/hotkey/hotkey_service.hpp b/src/services/hotkey/hotkey_service.hpp index af9c28a8..d41eb523 100644 --- a/src/services/hotkey/hotkey_service.hpp +++ b/src/services/hotkey/hotkey_service.hpp @@ -10,7 +10,7 @@ namespace big DOWN = WM_KEYDOWN }; - using hotkey_map = std::map; + using hotkey_map = std::multimap; class hotkey_service final { diff --git a/src/views/settings/view_settings.cpp b/src/views/settings/view_settings.cpp index 27cc6466..4e0af5d9 100644 --- a/src/views/settings/view_settings.cpp +++ b/src/views/settings/view_settings.cpp @@ -42,6 +42,9 @@ namespace big if (ImGui::Hotkey("Teleport to objective", &g->settings.hotkeys.teleport_objective)) g_hotkey_service->update_hotkey("objective", g->settings.hotkeys.teleport_objective); + if (ImGui::Hotkey("Toggle Noclip", &g->settings.hotkeys.noclip)) + g_hotkey_service->update_hotkey("noclip", g->settings.hotkeys.noclip); + ImGui::PopItemWidth();