Refactor hotkey class

This commit is contained in:
Grinch_ 2022-06-08 03:55:49 +06:30
parent 219738d556
commit 7b0fa7c7f6
9 changed files with 89 additions and 203 deletions

View File

@ -290,7 +290,7 @@ void Game::FreeCam()
m_Freecam::m_fTotalMouse.y = -150; m_Freecam::m_fTotalMouse.y = -150;
} }
if (freeCamTeleportPlayer.Pressed()) if (KeyPressed(VK_RETURN))
{ {
CPlayerPed* player = FindPlayerPed(); CPlayerPed* player = FindPlayerPed();
CVector pos = m_Freecam::m_pPed->GetPosition(); CVector pos = m_Freecam::m_pPed->GetPosition();
@ -321,9 +321,9 @@ void Game::FreeCam()
speed *= 2; speed *= 2;
} }
if (freeCamForward.Pressed() || freeCamBackward.Pressed()) if (freeCamForward.PressedBasic() || freeCamBackward.PressedBasic())
{ {
if (freeCamForward.Pressed()) if (freeCamBackward.PressedBasic())
{ {
speed *= -1; speed *= -1;
} }
@ -335,9 +335,9 @@ void Game::FreeCam()
pos.z += speed * 2 * sin(m_Freecam::m_fTotalMouse.y / 3 * 3.14159f / 180.0f); pos.z += speed * 2 * sin(m_Freecam::m_fTotalMouse.y / 3 * 3.14159f / 180.0f);
} }
if (freeCamLeft.Pressed() || freeCamRight.Pressed()) if (freeCamLeft.PressedBasic() || freeCamRight.PressedBasic())
{ {
if (freeCamLeft.Pressed()) if (freeCamLeft.PressedBasic())
{ {
speed *= -1; speed *= -1;
} }

View File

@ -1,17 +1,31 @@
#include "pch.h" #include "pch.h"
#include "hotkeys.h" #include "hotkeys.h"
const size_t HOTKEY_START = 3;
const size_t HOTKEY_END = 135;
Hotkey::Hotkey(int key1, int key2, const std::string& configPath)
: m_key1(key1), m_key2(key2), m_ConfigPath("Hotkeys." + configPath)
{
if (m_ConfigPath != "")
{
m_key1 = gConfig.GetValue(m_ConfigPath + ".1", m_key1);
m_key2 = gConfig.GetValue(m_ConfigPath + ".2", m_key2);
}
}
bool Hotkey::DrawUI(const char* label) bool Hotkey::DrawUI(const char* label)
{ {
bool active = (m_CurrentHotkey == label); bool active = (m_CurrentHotkey == label);
bool state = false; bool state = false;
// Check for pressed keys
if (active) if (active)
{ {
ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetStyle().Colors[ImGuiCol_ButtonActive]); ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetStyle().Colors[ImGuiCol_ButtonActive]);
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImGui::GetStyle().Colors[ImGuiCol_ButtonActive]); ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImGui::GetStyle().Colors[ImGuiCol_ButtonActive]);
for (int key = 3; key != 135; ++key) for (size_t key = HOTKEY_START; key != HOTKEY_END; ++key)
{ {
if (KeyPressed(key)) if (KeyPressed(key))
{ {
@ -20,7 +34,7 @@ bool Hotkey::DrawUI(const char* label)
} }
} }
for (int key = 135; key != 3; --key) for (size_t key = HOTKEY_END; key != HOTKEY_START; --key)
{ {
if (KeyPressed(key)) if (KeyPressed(key))
{ {
@ -30,24 +44,9 @@ bool Hotkey::DrawUI(const char* label)
} }
} }
std::string text; std::string btnText = GetNameString();
ImVec2 size {ImGui::GetWindowContentRegionWidth() / 3.5f, ImGui::GetFrameHeight()*1.35f};
if (m_key1 != VK_NONE) if (ImGui::Button(std::format("{}##{}", btnText, label).c_str(), size))
{
text = key_names[m_key1 - 1];
}
else
{
text = "None";
}
if (m_key1 != m_key2)
{
text += (" + " + key_names[m_key2 - 1]);
}
if (ImGui::Button((text + std::string("##") + std::string(label)).c_str(),
ImVec2(ImGui::GetWindowContentRegionWidth() / 3.5, ImGui::GetFrameHeight()*1.35f)))
{ {
if (!active) if (!active)
{ {
@ -59,8 +58,17 @@ bool Hotkey::DrawUI(const char* label)
{ {
m_CurrentHotkey = ""; m_CurrentHotkey = "";
state = true; state = true;
// Save the hotkeys in config file
if (m_ConfigPath != "")
{
gConfig.SetValue(m_ConfigPath + "1", m_key1);
gConfig.SetValue(m_ConfigPath + "2", m_key2);
}
} }
// Reset hotkeys to NONE on right click
if (ImGui::IsMouseClicked(ImGuiMouseButton_Right)) if (ImGui::IsMouseClicked(ImGuiMouseButton_Right))
{ {
if (ImGui::IsItemHovered()) if (ImGui::IsItemHovered())
@ -104,23 +112,17 @@ bool Hotkey::Pressed()
return false; return false;
} }
bool Hotkey::PressedBasic()
{
return (m_CurrentHotkey == "") ? KeyPressed(m_key1) && KeyPressed(m_key2) : false;
}
std::string Hotkey::GetNameString() std::string Hotkey::GetNameString()
{ {
std::string text; std::string text = (m_key1 == VK_NONE) ? "None" : key_names[m_key1 - 1];
if (m_key1 != VK_NONE)
{
text = key_names[m_key1 - 1];
}
else
{
text = "None";
}
if (m_key1 != m_key2) if (m_key1 != m_key2)
{ {
text += (" + " + key_names[m_key2 - 1]); text += (" + " + key_names[m_key2 - 1]);
} }
return text; return text;
} }

View File

@ -2,24 +2,29 @@
#include <string> #include <string>
/* /*
ImGui HotKey Implementation class HotKey Implementation Class
Handles hotkeys and hotkey-related functions Handles events, save-load, name-fetch, ui etc.
*/ */
class Hotkey class Hotkey
{ {
private: private:
static inline std::string m_CurrentHotkey; static inline std::string m_CurrentHotkey;
bool m_bPressed; bool m_bPressed;
int m_key1, m_key2;
std::string m_ConfigPath; // config save path
public: public:
int m_key1, m_key2; Hotkey(int key1 = -1, int key2 = -1, const std::string& configPath = "");
Hotkey(int key1 = -1, int key2 = -1)
: m_key1(key1), m_key2(key2)
{}
// Draws ui to change the hotkeys from frontend // Draws ui to change the hotkeys from frontend
bool DrawUI(const char* label); bool DrawUI(const char* label);
bool Pressed();
// Returns formatted name of the hotkeys
std::string GetNameString(); std::string GetNameString();
// Returns true if the hotkeys are pressed
bool Pressed();
// Returns true if the hotkeys are pressed, with no checks
bool PressedBasic();
}; };

View File

@ -20,7 +20,7 @@ public:
// specialize since typeid(std::string) doesn't work // specialize since typeid(std::string) doesn't work
template <typename T> template <typename T>
T inline GetValue(std::string&& key, T&& defaultVal) T inline GetValue(std::string&& key, const T& defaultVal)
{ {
try try
{ {

View File

@ -34,55 +34,6 @@ void Menu::Init()
m_Overlay::textColor[3] = gConfig.GetValue("overlay.text_color.a", 1.0f); m_Overlay::textColor[3] = gConfig.GetValue("overlay.text_color.a", 1.0f);
m_bDiscordRPC = gConfig.GetValue("menu.discord_rpc", false); m_bDiscordRPC = gConfig.GetValue("menu.discord_rpc", false);
// Hotkeys
aimSkinChanger.m_key1 = gConfig.GetValue("hotkey.aim_skin_changer.key1", VK_RETURN);
aimSkinChanger.m_key2 = gConfig.GetValue("hotkey.aim_skin_changer.key2", VK_RETURN);
freeCam.m_key1 = gConfig.GetValue("hotkey.freecam.key1", VK_F6);
freeCam.m_key2 = gConfig.GetValue("hotkey.freecam.key2", VK_F6);
freeCamForward.m_key1 = gConfig.GetValue("hotkey.freeCamForward.key1", VK_KEY_I);
freeCamForward.m_key2 = gConfig.GetValue("hotkey.freeCamForward.key2", VK_KEY_I);
freeCamBackward.m_key1 = gConfig.GetValue("hotkey.freeCamBackward.key1", VK_KEY_K);
freeCamBackward.m_key2 = gConfig.GetValue("hotkey.freeCamBackward.key2", VK_KEY_K);
freeCamLeft.m_key1 = gConfig.GetValue("hotkey.freeCamLeft.key1", VK_KEY_J);
freeCamLeft.m_key2 = gConfig.GetValue("hotkey.freeCamLeft.key2", VK_KEY_J);
freeCamRight.m_key1 = gConfig.GetValue("hotkey.freeCamRight.key1", VK_KEY_L);
freeCamRight.m_key2 = gConfig.GetValue("hotkey.freeCamRight.key2", VK_KEY_L);
quickSceenShot.m_key1 = gConfig.GetValue("hotkey.quick_screenshot.key1", VK_F5);
quickSceenShot.m_key2 = gConfig.GetValue("hotkey.quick_screenshot.key2", VK_F5);
quickTeleport.m_key1 = gConfig.GetValue("hotkey.quick_tp.key1", VK_KEY_X);
quickTeleport.m_key2 = gConfig.GetValue("hotkey.quick_tp.key2", VK_KEY_Y);
menuOpen.m_key1 = gConfig.GetValue("hotkey.menu_open.key1", VK_LCONTROL);
menuOpen.m_key2 = gConfig.GetValue("hotkey.menu_open.key2", VK_KEY_M);
commandWindow.m_key1 = gConfig.GetValue("hotkey.command_window.key1", VK_LMENU);
commandWindow.m_key2 = gConfig.GetValue("hotkey.command_window.key2", VK_KEY_C);
flipVeh.m_key1 = gConfig.GetValue("hotkey.flip_veh.key1", VK_NONE);
flipVeh.m_key2 = gConfig.GetValue("hotkey.flip_veh.key2", VK_NONE);
fixVeh.m_key1 = gConfig.GetValue("hotkey.fix_veh.key1", VK_NONE);
fixVeh.m_key2 = gConfig.GetValue("hotkey.fix_veh.key2", VK_NONE);
godMode.m_key1 = gConfig.GetValue("hotkey.god_mode.key1", VK_NONE);
godMode.m_key2 = gConfig.GetValue("hotkey.god_mode.key2", VK_NONE);
vehEngine.m_key1 = gConfig.GetValue("hotkey.veh_engine.key1", VK_NONE);
vehEngine.m_key2 = gConfig.GetValue("hotkey.veh_engine.key2", VK_NONE);
vehInstantStart.m_key1 = gConfig.GetValue("hotkey.veh_instant_start.key1", VK_NONE);
vehInstantStart.m_key2 = gConfig.GetValue("hotkey.veh_instant_start.key2", VK_NONE);
vehInstantStop.m_key1 = gConfig.GetValue("hotkey.veh_instant_stop.key1", VK_NONE);
vehInstantStop.m_key2 = gConfig.GetValue("hotkey.veh_instant_stop.key2", VK_NONE);
Util::GetCPUUsageInit(); Util::GetCPUUsageInit();
MEMORYSTATUSEX memInfo; MEMORYSTATUSEX memInfo;
memInfo.dwLength = sizeof(MEMORYSTATUSEX); memInfo.dwLength = sizeof(MEMORYSTATUSEX);
@ -499,97 +450,28 @@ void Menu::ShowPage()
Ui::ShowTooltip(TEXT("Menu.UsageText")); Ui::ShowTooltip(TEXT("Menu.UsageText"));
ImGui::Spacing(); ImGui::Spacing();
ImGui::BeginChild("Hotkeys"); ImGui::BeginChild("Hotkeys");
if (menuOpen.DrawUI(TEXT("Menu.OpenMenuKey"))) menuOpen.DrawUI(TEXT("Menu.OpenMenuKey"));
{ commandWindow.DrawUI(TEXT("Menu.OpenCMDKey"));
gConfig.SetValue("hotkey.menu_open.key1", menuOpen.m_key1);
gConfig.SetValue("hotkey.menu_open.key2", menuOpen.m_key2);
}
if (commandWindow.DrawUI(TEXT("Menu.OpenCMDKey")))
{
gConfig.SetValue("hotkey.command_window.key1", commandWindow.m_key1);
gConfig.SetValue("hotkey.command_window.key2", commandWindow.m_key2);
}
ImGui::Dummy(ImVec2(0, 10)); ImGui::Dummy(ImVec2(0, 10));
if (aimSkinChanger.DrawUI(TEXT("Menu.SkinChangerKey"))) aimSkinChanger.DrawUI(TEXT("Menu.SkinChangerKey"));
{ quickSceenShot.DrawUI(TEXT("Menu.QuickSSKey"));
gConfig.SetValue("hotkey.aim_skin_changer.key1", aimSkinChanger.m_key1); freeCam.DrawUI(TEXT("Menu.FreecamKey"));
gConfig.SetValue("hotkey.aim_skin_changer.key2", aimSkinChanger.m_key2); freeCamForward.DrawUI(TEXT("Menu.FreecamForwardKey"));
} freeCamBackward.DrawUI(TEXT("Menu.FreecamBackwardKey"));
if (quickSceenShot.DrawUI(TEXT("Menu.QuickSSKey"))) freeCamLeft.DrawUI(TEXT("Menu.FreecamLeftKey"));
{ freeCamRight.DrawUI(TEXT("Menu.FreecamRightKey"));
gConfig.SetValue("hotkey.quick_screenshot.key1", quickSceenShot.m_key1); quickTeleport.DrawUI(TEXT("Menu.QuickTPKey"));
gConfig.SetValue("hotkey.quick_screenshot.key2", quickSceenShot.m_key2);
}
if (freeCam.DrawUI(TEXT("Menu.FreecamKey")))
{
gConfig.SetValue("hotkey.freecam.key1", freeCam.m_key1);
gConfig.SetValue("hotkey.freecam.key2", freeCam.m_key2);
}
if (freeCamForward.DrawUI(TEXT("Menu.FreecamForwardKey")))
{
gConfig.SetValue("hotkey.freeCamForward.key1", freeCamForward.m_key1);
gConfig.SetValue("hotkey.freeCamForward.key2", freeCamForward.m_key2);
}
if (freeCamBackward.DrawUI(TEXT("Menu.FreecamBackwardKey")))
{
gConfig.SetValue("hotkey.freeCamBackward.key1", freeCamBackward.m_key1);
gConfig.SetValue("hotkey.freeCamBackward.key2", freeCamBackward.m_key2);
}
if (freeCamLeft.DrawUI(TEXT("Menu.FreecamLeftKey")))
{
gConfig.SetValue("hotkey.freeCamLeft.key1", freeCamLeft.m_key1);
gConfig.SetValue("hotkey.freeCamLeft.key2", freeCamLeft.m_key2);
}
if (freeCamRight.DrawUI(TEXT("Menu.FreecamRightKey")))
{
gConfig.SetValue("hotkey.freeCamRight.key1", freeCamRight.m_key1);
gConfig.SetValue("hotkey.freeCamRight.key2", freeCamRight.m_key2);
}
if (quickTeleport.DrawUI(TEXT("Menu.QuickTPKey")))
{
gConfig.SetValue("hotkey.quick_tp.key1", quickTeleport.m_key1);
gConfig.SetValue("hotkey.quick_tp.key2", quickTeleport.m_key2);
}
ImGui::Dummy(ImVec2(0, 10)); ImGui::Dummy(ImVec2(0, 10));
if (fixVeh.DrawUI(TEXT("Menu.FixVehKey"))) fixVeh.DrawUI(TEXT("Menu.FixVehKey"));
{ flipVeh.DrawUI(TEXT("Menu.FlipVehKey"));
gConfig.SetValue("hotkey.fix_veh.key1", fixVeh.m_key1); godMode.DrawUI(TEXT("Menu.GodModeKey"));
gConfig.SetValue("hotkey.fix_veh.key2", fixVeh.m_key2); vehEngine.DrawUI(TEXT("Menu.VehEngineKey"));
} vehInstantStart.DrawUI(TEXT("Menu.VehStartKey"));
vehInstantStop.DrawUI(TEXT("Menu.VehStopKey"));
if (flipVeh.DrawUI(TEXT("Menu.FlipVehKey")))
{
gConfig.SetValue("hotkey.flip_veh.key1", flipVeh.m_key1);
gConfig.SetValue("hotkey.flip_veh.key2", flipVeh.m_key2);
}
if (godMode.DrawUI(TEXT("Menu.GodModeKey")))
{
gConfig.SetValue("hotkey.god_mode.key1", godMode.m_key1);
gConfig.SetValue("hotkey.god_mode.key2", godMode.m_key2);
}
if (vehEngine.DrawUI(TEXT("Menu.VehEngineKey")))
{
gConfig.SetValue("hotkey.veh_engine.key1", vehEngine.m_key1);
gConfig.SetValue("hotkey.veh_engine.key2", vehEngine.m_key2);
}
if (vehInstantStart.DrawUI(TEXT("Menu.VehStartKey")))
{
gConfig.SetValue("hotkey.veh_instant_start.key1", vehInstantStart.m_key1);
gConfig.SetValue("hotkey.veh_instant_start.key2", vehInstantStart.m_key2);
}
if (vehInstantStop.DrawUI(TEXT("Menu.VehStopKey")))
{
gConfig.SetValue("hotkey.veh_instant_stop.key1", vehInstantStop.m_key1);
gConfig.SetValue("hotkey.veh_instant_stop.key2", vehInstantStop.m_key2);
}
ImGui::Dummy(ImVec2(0, 10)); ImGui::Dummy(ImVec2(0, 10));

View File

@ -3,20 +3,19 @@ eRenderer gRenderer = Render_Unknown;
std::ofstream gLog = std::ofstream("CheatMenu.log"); std::ofstream gLog = std::ofstream("CheatMenu.log");
CJson gConfig = CJson("config"); CJson gConfig = CJson("config");
Hotkey aimSkinChanger; Hotkey aimSkinChanger {VK_RETURN, VK_RETURN, "AimSkinChanger"};
Hotkey freeCam; Hotkey freeCam {VK_F6, VK_F6, "Freecam.Toggle"};
Hotkey freeCamForward; Hotkey freeCamForward {VK_KEY_I, VK_KEY_I, "Freecam.Forward"};;
Hotkey freeCamBackward; Hotkey freeCamBackward {VK_KEY_K, VK_KEY_K, "Freecam.Backward"};;
Hotkey freeCamLeft; Hotkey freeCamLeft {VK_KEY_J, VK_KEY_J, "Freecam.Left"};;
Hotkey freeCamRight; Hotkey freeCamRight {VK_KEY_L, VK_KEY_L, "Freecam.Right"};;
Hotkey commandWindow; Hotkey commandWindow {VK_LCONTROL, VK_KEY_C, "CommandWindowToggle"};;
Hotkey fixVeh; Hotkey fixVeh {VK_NONE, VK_NONE, "Vehicle.Fix"};
Hotkey flipVeh; Hotkey flipVeh {VK_NONE, VK_NONE, "Vehicle.Flip"};
Hotkey freeCamTeleportPlayer; Hotkey godMode {VK_NONE, VK_NONE, "GodMode"};
Hotkey godMode; Hotkey menuOpen {VK_LCONTROL, VK_KEY_M, "MenuToggle"};
Hotkey menuOpen; Hotkey quickSceenShot {VK_F5, VK_F5, "QuickScreenshot"};
Hotkey quickSceenShot; Hotkey quickTeleport {VK_KEY_X, VK_KEY_Y, "QuickTeleport"};
Hotkey quickTeleport; Hotkey vehEngine {VK_NONE, VK_NONE, "Vehicle.EngineToggle"};
Hotkey vehEngine; Hotkey vehInstantStart {VK_NONE, VK_NONE, "Vehicle.InstantStart"};
Hotkey vehInstantStart; Hotkey vehInstantStop {VK_NONE, VK_NONE, "Vehicle.InstantStop"};
Hotkey vehInstantStop;

View File

@ -117,7 +117,6 @@ extern Hotkey freeCamRight;
extern Hotkey commandWindow; extern Hotkey commandWindow;
extern Hotkey fixVeh; extern Hotkey fixVeh;
extern Hotkey flipVeh; extern Hotkey flipVeh;
extern Hotkey freeCamTeleportPlayer;
extern Hotkey godMode; extern Hotkey godMode;
extern Hotkey menuOpen; extern Hotkey menuOpen;
extern Hotkey quickSceenShot; extern Hotkey quickSceenShot;

View File

@ -1,5 +1,4 @@
#pragma once #pragma once
#include <cstdint>
class RPC class RPC
{ {
@ -8,8 +7,8 @@ private:
{ {
const char* state; const char* state;
const char* details; const char* details;
int64_t startTimestamp; long long startTimestamp;
int64_t endTimestamp; long long endTimestamp;
const char* largeImageKey; const char* largeImageKey;
const char* largeImageText; const char* largeImageText;
const char* smallImageKey; const char* smallImageKey;
@ -20,7 +19,7 @@ private:
const char* matchSecret; const char* matchSecret;
const char* joinSecret; const char* joinSecret;
const char* spectateSecret; const char* spectateSecret;
int8_t instance; char instance;
} DiscordRichPresence; } DiscordRichPresence;
static inline FARPROC f_Init, f_ShutDown, f_Update; static inline FARPROC f_Init, f_ShutDown, f_Update;

View File

@ -2,5 +2,5 @@
#define MENU_NAME "Cheat Menu" #define MENU_NAME "Cheat Menu"
#define MENU_VERSION_NUMBER "3.3" #define MENU_VERSION_NUMBER "3.3"
#define MENU_VERSION MENU_VERSION_NUMBER"-beta" #define MENU_VERSION MENU_VERSION_NUMBER"-beta"
#define BUILD_NUMBER "20220606" #define BUILD_NUMBER "20220523"
#define MENU_TITLE MENU_NAME " v" MENU_VERSION #define MENU_TITLE MENU_NAME " v" MENU_VERSION