Refactor code & add stuff

This commit is contained in:
Grinch_ 2022-01-20 13:04:45 +06:00
parent fe979d413f
commit 7995ca7614
26 changed files with 246 additions and 146 deletions

View File

@ -245,7 +245,7 @@ Animation::Animation()
#endif
}
void Animation::Draw()
void Animation::ShowPage()
{
if (ImGui::BeginTabBar("Animation", ImGuiTabBarFlags_NoTooltip + ImGuiTabBarFlags_FittingPolicyScroll))
{

View File

@ -48,5 +48,5 @@ private:
public:
Animation();
static void Draw();
static void ShowPage();
};

View File

@ -4,6 +4,8 @@
#include "ui.h"
#include "updater.h"
#include "d3dhook.h"
#include "../depend/imgui/imgui_internal.h"
#include "util.h"
void CheatMenu::DrawWindow()
{
@ -35,28 +37,13 @@ void CheatMenu::DrawWindow()
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding,
ImVec2(ImGui::GetWindowWidth() / 85, ImGui::GetWindowHeight() / 200));
if (Updater::IsUpdateAvailable())
{
ShowUpdateScreen();
}
else
{
Ui::DrawHeaders(header);
if (Ui::m_HeaderId == -1)
{
ShowWelcomeScreen();
}
}
ProcessMenuPages();
if (m_bSizeChangedExternal)
{
m_bSizeChangedExternal = false;
}
else
{
m_fMenuSize = ImGui::GetWindowSize();
}
gConfig.SetValue("window.sizeX", m_fMenuSize.x);
gConfig.SetValue("window.sizeY", m_fMenuSize.y);
@ -75,6 +62,108 @@ void CheatMenu::DrawWindow()
DrawOverlay();
}
void CheatMenu::ProcessMenuPages()
{
static void* pCallback;
ImVec2 size = Ui::GetSize(3, false);
ImGuiStyle &style = ImGui::GetStyle();
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
ImGui::PushFont(FontMgr::GetFont("header"));
m_nMenuPage = Updater::IsUpdateAvailable() ? eMenuPages::UPDATE : m_nMenuPage;
// Check once if it's anniversary day
static bool aniCheckDone;
if (!aniCheckDone)
{
SYSTEMTIME st;
GetSystemTime(&st);
if (st.wMonth == 3 && st.wDay == 28)
{
/*
* We don't want to be annoying and
* show anniversary screen on every game start
*/
bool flag = gConfig.GetValue("window.anniversaryShown", false);
if (!flag)
{
gConfig.SetValue("window.anniversaryShown", true);
m_nMenuPage = eMenuPages::ANNIVERSARY;
}
}
aniCheckDone = true;
}
ImDrawList *pDrawList = ImGui::GetWindowDrawList();
for (size_t i = 0; i < m_headerList.size(); ++i)
{
/*
* For Welcome & Update pages
* They don't need to add item in the header list
*/
if (m_headerList[i].skipHeader)
{
if (m_nMenuPage == m_headerList[i].page)
pCallback = m_headerList[i].pFunc;
continue;
}
const char* text = m_headerList[i].name.c_str();
ImVec4 color;
if (m_headerList[i].page == m_nMenuPage)
{
color = style.Colors[ImGuiCol_ButtonActive];
pCallback = m_headerList[i].pFunc;
}
else
color = style.Colors[ImGuiCol_Button];
if (ImGui::InvisibleButton(text, size))
{
m_nMenuPage = m_headerList[i].page;
size_t curPage = static_cast<size_t>(m_headerList[i].page);
gConfig.SetValue("window.idnum", curPage);
pCallback = m_headerList[i].pFunc;
Updater::ResetUpdaterState();
}
if (ImGui::IsItemHovered())
color = style.Colors[ImGuiCol_ButtonHovered];
/*
* Window rounding flags
* TODO: hardcoded atm
*/
ImDrawFlags flags = ImDrawFlags_RoundCornersNone;
if (i == 0) flags = ImDrawFlags_RoundCornersTopLeft;
if (i == 2) flags = ImDrawFlags_RoundCornersTopRight;
if (i == 6) flags = ImDrawFlags_RoundCornersBottomLeft;
if (i == 8) flags = ImDrawFlags_RoundCornersBottomRight;
ImVec2 min = ImGui::GetItemRectMin();
ImVec2 max = ImGui::GetItemRectMax();
ImVec2 size = ImGui::CalcTextSize(text);
pDrawList->AddRectFilled(min, max, ImGui::GetColorU32(color), style.FrameRounding, flags);
ImGui::RenderTextClipped(min + style.FramePadding, max - style.FramePadding, text, NULL, &size, style.ButtonTextAlign);
if (i % 3 != 2)
ImGui::SameLine();
}
ImGui::PopFont();
ImGui::PopStyleVar();
ImGui::Dummy(ImVec2(0, 10));
if (pCallback != nullptr && ImGui::BeginChild("HEADERCONTENT"))
{
static_cast<void(*)()>(pCallback)();
ImGui::EndChild();
}
}
CheatMenu::CheatMenu()
{
if (!D3dHook::InjectHook(DrawWindow))
@ -85,7 +174,7 @@ CheatMenu::CheatMenu()
ApplyStyle();
// Load menu settings
Ui::m_HeaderId = gConfig.GetValue("window.idnum", -1);
m_nMenuPage = (eMenuPages)gConfig.GetValue("window.idnum", (size_t)eMenuPages::WELCOME);
m_fMenuSize.x = gConfig.GetValue("window.sizeX", screen::GetScreenWidth() / 4.0f);
m_fMenuSize.y = gConfig.GetValue("window.sizeY", screen::GetScreenHeight() / 1.2f);
srand(CTimer::m_snTimeInMilliseconds);
@ -128,9 +217,79 @@ CheatMenu::~CheatMenu()
D3dHook::RemoveHook();
}
void CheatMenu::ShowWelcomeScreen()
/*
* YIKES YOU AREN"T SUPPOSED TO FIND THIS YOU KNOW!!!
* Probably a good easter egg for the upcoming anniversary ;)
*/
void CheatMenu::ShowAnniversaryPage()
{
Ui::CenterdText("Happy Anniversary!");
ImGui::NewLine();
ImGui::TextWrapped("On this day, in 2019, the first public version of menu was released in MixMods Forum."
" It's been a blast working on it and I've learned a lot in the process.\n\nThanks to you and everyone who used or"
" contributed to the modification in any form or shape.");
ImGui::NewLine();
ImGui::TextWrapped("Feel free to star the GitHub repo or join the discord server and provide feedback, ideas, or suggestions.");
ImGui::NewLine();
if (ImGui::Button("Discord server", ImVec2(Ui::GetSize(2))))
{
ShellExecute(nullptr, "open", DISCORD_INVITE, nullptr, nullptr, SW_SHOWNORMAL);
}
ImGui::SameLine();
if (ImGui::Button("GitHub repo", ImVec2(Ui::GetSize(2))))
{
ShellExecute(nullptr, "open", GITHUB_LINK, nullptr, nullptr, SW_SHOWNORMAL);
}
ImGui::NewLine();
static bool showHistory = false;
ImGui::Checkbox("Show backstory", &showHistory);
if (showHistory)
{
ImGui::BeginChild("BACKSTORY");
ImGui::TextWrapped("I wanted to share the backstory behind the initial idea or plan behind the menu."
" This is gonna be long so feel free to skip it if you're not interested.");
ImGui::NewLine();
ImGui::TextWrapped("The original idea of the menu comes way back from 2016! The inspiration for the menu"
" is from the 'CheatMenu by UNRATED69'. I wanted something that had some more features and worked with SAxVCxLC."
" But there not being any other CheatMenu's back then, I wanted to make something myself but lacked the knowledge to do so.");
ImGui::NewLine();
ImGui::TextWrapped("In 2018, I finally got an opportunity to learn CLEO or GTA3Script after Junior released"
" his tutorial. I started from basics but it soon became apparent that due to the limitations of CLEO, creating menus were"
"really tedious.");
ImGui::NewLine();
ImGui::TextWrapped("Later that year I found Moonloader, which had ImGui support. Meaning I could make menus"
" without brainfucking myself (kudos to everyone who writes 100s of lines in CLEO). I recall starting working on"
" the menu in October/November that same year.");
ImGui::NewLine();
ImGui::TextWrapped("I had high hopes the mod would succeed and the menu was nowhere near the state I wanted"
" it to be. But over a hot conversation with KKJJ, I finally decided to add the absolute bare minimum of features and"
" see what happens. And to my surprise, it even surpassed all of my expectations and became my most popular mod to this day.");
ImGui::NewLine();
ImGui::TextWrapped("A part of me is already cringing telling the story but it is what it is. I've learned"
" a lot working on this mod and I'm grateful. If you made it through all this way, kudos, you're awesome.");
ImGui::NewLine();
ImGui::TextWrapped("Again, thanks to you and everyone who used or helped me along the way. Enjoy ;)");
ImGui::EndChild();
}
}
void CheatMenu::ShowWelcomePage()
{
ImGui::BeginChild("WelcomeScreen");
ImGui::NewLine();
Ui::CenterdText("Welcome to Cheat Menu");
@ -155,45 +314,33 @@ void CheatMenu::ShowWelcomeScreen()
ImGui::TextWrapped("If you find bugs or have suggestions, you can let me know on discord :)");
ImGui::Dummy(ImVec2(0, 30));
Ui::CenterdText("Copyright Grinch_ 2019-2022. All rights reserved.");
ImGui::EndChild();
}
void CheatMenu::ShowUpdateScreen()
void CheatMenu::ShowUpdatePage()
{
ImGui::BeginChild("UPdateScreen");
std::string ver = Updater::GetUpdateVersion();
ImGui::Dummy(ImVec2(0, 20));
Ui::CenterdText("A new version of the mod is available.");
Ui::CenterdText("A new version of the menu is available.");
Ui::CenterdText(std::string("Current version: ") + MENU_VERSION);
Ui::CenterdText("Latest version: " + ver);
ImGui::Dummy(ImVec2(0, 10));
ImGui::TextWrapped("In order to keep using the menu, you need to update to the latest version."
" This is to ensure everything is using the most up-to-date version.");
ImGui::TextWrapped("It's highly recommanded to update to the latest version."
" Newer versions may contain new features and bug fixes.");
ImGui::Dummy(ImVec2(0, 10));
ImGui::TextWrapped("To know what changes are made or to download, click on the \"Download page\" button."
ImGui::TextWrapped("To know what changes are made or to download, click on the 'Download page' button."
" Follow the instructions there. If you're still having issues, let me know on discord.");
ImGui::Dummy(ImVec2(0, 5));
if (ImGui::Button("Discord server", ImVec2(Ui::GetSize(3))))
{
if (ImGui::Button("Discord server", ImVec2(Ui::GetSize(2))))
ShellExecute(NULL, "open", DISCORD_INVITE, NULL, NULL, SW_SHOWNORMAL);
}
ImGui::SameLine();
if (ImGui::Button("Download page", Ui::GetSize(3)))
if (ImGui::Button("Download page", Ui::GetSize(2)))
{
ShellExecute(NULL, "open", std::string("https://github.com/user-grinch/Cheat-Menu/releases/tag/" +
ver).c_str(), NULL, NULL, SW_SHOWNORMAL);
}
ImGui::SameLine();
if (ImGui::Button("Hide page", Ui::GetSize(3)))
{
Updater::ResetUpdaterState();
}
ImGui::EndChild();
}
void CheatMenu::ApplyStyle()

View File

@ -22,21 +22,45 @@
class CheatMenu : Animation, Game, Menu, Ped, Player, Teleport, Vehicle, Visual, Weapon
{
private:
enum class eMenuPages
{
ANIMATION, ANNIVERSARY, GAME, MENU, NONE, PED, PLAYER, TELEPORT, UPDATE, VEHICLE, VISUAL, WEAPON, WELCOME
};
struct HeaderData
{
std::string name;
void *pFunc;
eMenuPages page;
bool skipHeader = false;
};
static inline eMenuPages m_nMenuPage = eMenuPages::WELCOME;
static inline bool m_bShowMenu = false;
static inline ImVec2 m_fMenuSize = ImVec2(screen::GetScreenWidth() / 4, screen::GetScreenHeight() / 1.2);
static inline bool m_bSizeChangedExternal = false;
static inline CallbackTable header
{
{"Teleport", &Teleport::Draw}, {"Player", &Player::Draw}, {"Ped", &Ped::Draw},
{"Animation", &Animation::Draw}, {"Vehicle", &Vehicle::Draw}, {"Weapon", &Weapon::Draw},
{"Game", &Game::Draw}, {"Visual", &Visual::Draw}, {"Menu", &Menu::Draw}
};
static void ApplyStyle();
static void DrawWindow();
static void ShowUpdateScreen();
static void ShowWelcomeScreen();
static void ShowAnniversaryPage();
static void ShowUpdatePage();
static void ShowWelcomePage();
static void ProcessMenuPages();
static inline std::vector<HeaderData> m_headerList
{
{"Teleport", &Teleport::ShowPage, eMenuPages::TELEPORT},
{"Player", &Player::ShowPage, eMenuPages::PLAYER},
{"Ped", &Ped::ShowPage, eMenuPages::PED},
{"Animation", &Animation::ShowPage, eMenuPages::ANIMATION},
{"Vehicle", &Vehicle::ShowPage, eMenuPages::VEHICLE},
{"Weapon", &Weapon::ShowPage, eMenuPages::WEAPON},
{"Game", &Game::ShowPage, eMenuPages::GAME},
{"Visual", &Visual::ShowPage, eMenuPages::VISUAL},
{"Menu", &Menu::ShowPage, eMenuPages::MENU},
{"Welcome", &ShowWelcomePage, eMenuPages::WELCOME, true},
{"Update", &ShowUpdatePage, eMenuPages::UPDATE, true},
{"Anniversary", &ShowAnniversaryPage, eMenuPages::ANNIVERSARY, true}
};
public:
CheatMenu();

View File

@ -63,12 +63,12 @@ void MenuThread(void* param)
// Checking for updates once a day
time_t now = time(0);
struct tm tstruct = *localtime(&now);
if (gConfig.GetValue("config.update_date", 0) != tstruct.tm_mday)
SYSTEMTIME st;
GetSystemTime(&st);
if (gConfig.GetValue("config.update_date", 0) != st.wDay)
{
Updater::CheckUpdate();
gConfig.SetValue("config.update_date", tstruct.tm_mday);
gConfig.SetValue("config.update_date", st.wDay);
}
while (true)

View File

@ -414,7 +414,7 @@ void Game::ClearFreecamStuff()
Command<Commands::RESTORE_CAMERA_JUMPCUT>();
}
void Game::Draw()
void Game::ShowPage()
{
ImGui::Spacing();
CPlayerPed* pPlayer = FindPlayerPed();

View File

@ -63,6 +63,6 @@ public:
static inline bool m_bSyncTime;
Game();
static void Draw();
static void ShowPage();
};

View File

@ -75,7 +75,7 @@ public:
Example: "Menu.Window.X"
*/
template <typename T>
void SetValue(std::string&& key, T& val)
void SetValue(std::string&& key, const T& val)
{
std::stringstream ss(key);
std::string line;
@ -100,7 +100,7 @@ public:
}
template <>
void SetValue(std::string&& key, std::string& val)
void SetValue(std::string&& key, const std::string& val)
{
std::stringstream ss(key);
std::string line;

View File

@ -348,7 +348,7 @@ void Menu::ProcessCommands()
#endif
}
void Menu::Draw()
void Menu::ShowPage()
{
ImGui::Spacing();
if (ImGui::Button("Reset config", ImVec2(Ui::GetSize(2))))

View File

@ -1,4 +1,5 @@
#pragma once
#include "pch.h"
class Menu
{
@ -45,7 +46,7 @@ public:
};
Menu();
static void Draw();
static void ShowPage();
static void DrawOverlay();
static void DrawShortcutsWindow();
static void ProcessCommands();

View File

@ -74,7 +74,6 @@
#include "resourcestore.h"
#include "fontmgr.h"
using CallbackTable = std::vector<std::pair<std::string, void(*)()>>;
using namespace plugin;
enum eRenderer

View File

@ -166,7 +166,7 @@ void Ped::SpawnPed(std::string& cat, std::string& name, std::string& model)
}
}
void Ped::Draw()
void Ped::ShowPage()
{
if (ImGui::BeginTabBar("Ped", ImGuiTabBarFlags_NoTooltip + ImGuiTabBarFlags_FittingPolicyScroll))
{

View File

@ -59,5 +59,5 @@ public:
Ped();
~Ped();
static void Draw();
static void ShowPage();
};

View File

@ -360,7 +360,7 @@ void Player::ChangePlayerModel(std::string& cat, std::string& key, std::string&
}
#endif
void Player::Draw()
void Player::ShowPage()
{
CPlayerPed* pPlayer = FindPlayerPed();
int hplayer = CPools::GetPedRef(pPlayer);

View File

@ -53,5 +53,5 @@ private:
public:
Player();
static void Draw();
static void ShowPage();
};

View File

@ -188,7 +188,7 @@ void Teleport::RemoveTeleportEntry(std::string& category, std::string& key, std:
}
}
void Teleport::Draw()
void Teleport::ShowPage()
{
if (ImGui::BeginTabBar("Teleport", ImGuiTabBarFlags_NoTooltip + ImGuiTabBarFlags_FittingPolicyScroll))
{

View File

@ -39,5 +39,5 @@ public:
Teleport();
static void TeleportPlayer(bool get_marker = false, CVector pos = CVector(0, 0, 0), int interior_id = 0);
static void Draw();
static void ShowPage();
};

View File

@ -224,75 +224,6 @@ void Ui::CenterdText(const std::string& text)
ImGui::Text(text.c_str());
}
void Ui::DrawHeaders(CallbackTable& data)
{
static void* pCallback;
ImVec2 size = GetSize(3, false);
ImGuiStyle &style = ImGui::GetStyle();
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
ImGui::PushFont(FontMgr::GetFont("header"));
ImDrawList *pDrawList = ImGui::GetWindowDrawList();
for (size_t i = 0; i < data.size(); ++i)
{
const char* btn_text = data[i].first.c_str();
ImVec4 color;
if (i == m_HeaderId)
{
color = style.Colors[ImGuiCol_ButtonActive];
pCallback = data[i].second;
}
else
{
color = style.Colors[ImGuiCol_Button];
}
if (ImGui::InvisibleButton(btn_text, size))
{
m_HeaderId = i;
gConfig.SetValue("window.idnum", m_HeaderId);
pCallback = data[i].second;
}
if (ImGui::IsItemHovered())
{
color = style.Colors[ImGuiCol_ButtonHovered];
}
// hardcoded
ImDrawFlags flags = ImDrawFlags_RoundCornersNone;
if (i == 0) flags = ImDrawFlags_RoundCornersTopLeft;
if (i == 2) flags = ImDrawFlags_RoundCornersTopRight;
if (i == 6) flags = ImDrawFlags_RoundCornersBottomLeft;
if (i == 8) flags = ImDrawFlags_RoundCornersBottomRight;
ImVec2 min = ImGui::GetItemRectMin();
ImVec2 max = ImGui::GetItemRectMax();
ImVec2 size = ImGui::CalcTextSize(btn_text);
pDrawList->AddRectFilled(min, max, ImGui::GetColorU32(color), style.FrameRounding, flags);
ImGui::RenderTextClipped(min + style.FramePadding, max - style.FramePadding, btn_text, NULL, &size, style.ButtonTextAlign);
if (i % 3 != 2)
{
ImGui::SameLine();
}
}
ImGui::PopFont();
ImGui::PopStyleVar();
ImGui::Dummy(ImVec2(0, 10));
if (m_HeaderId != -1)
{
if (pCallback != nullptr && ImGui::BeginChild("TABSBAR"))
{
static_cast<void(*)()>(pCallback)();
ImGui::EndChild();
}
}
}
void Ui::ShowTooltip(const char* text)
{
ImGui::SameLine();

View File

@ -48,7 +48,6 @@ public:
const char* hint = nullptr);
static bool CheckboxBitFlag(const char* label, uint flag, const char* hint = nullptr);
static bool CheckboxWithHint(const char* label, bool* state, const char* hint = nullptr, bool is_disabled = false);
static void DrawHeaders(CallbackTable& data);
static void DrawJSON(ResourceStore& data,
std::function<void(std::string&, std::string&, std::string&)> func_left_click,

View File

@ -19,7 +19,6 @@ public:
static void ClearCharTasksVehCheck(CPed* ped);
static int GetLargestGangInZone();
#endif
static void SetCarForwardSpeed(CVehicle *pVeh, float speed);
static CPed* GetClosestPed();
static CVehicle* GetClosestVehicle();

View File

@ -558,7 +558,7 @@ int Vehicle::GetModelFromName(const char* name)
}
}
void Vehicle::Draw()
void Vehicle::ShowPage()
{
ImGui::Spacing();
CPlayerPed* pPlayer = FindPlayerPed();

View File

@ -100,5 +100,5 @@ public:
#endif
static std::string GetNameFromModel(int model);
static int GetModelFromName(const char* name);
static void Draw();
static void ShowPage();
};

View File

@ -346,7 +346,7 @@ bool Visual::TimeCycColorEdit4(const char* label, T* r, T* g, T* b, T* a, ImGuiC
return rtn;
}
void Visual::Draw()
void Visual::ShowPage()
{
if (ImGui::BeginTabBar("Visual", ImGuiTabBarFlags_NoTooltip + ImGuiTabBarFlags_FittingPolicyScroll))
{

View File

@ -41,5 +41,5 @@ private:
public:
Visual();
static void Draw();
static void ShowPage();
};

View File

@ -225,7 +225,7 @@ void Weapon::GiveWeaponToPlayer(std::string& rootkey, std::string& name, std::st
}
#endif
void Weapon::Draw()
void Weapon::ShowPage()
{
CPlayerPed* pPlayer = FindPlayerPed();
uint hplayer = CPools::GetPedRef(pPlayer);

View File

@ -53,5 +53,5 @@ public:
#endif
Weapon();
static void Draw();
static void ShowPage();
};