Skip to content

Commit c4be286

Browse files
authored
Merge pull request A-SunsetMkt-Forks#1 from Shichiha/thwemes
add theme functionality
2 parents 290567e + df702b9 commit c4be286

File tree

4 files changed

+156
-74
lines changed

4 files changed

+156
-74
lines changed

cheat-base/src/cheat-base/cheat/misc/Settings.cpp

+101-28
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,97 @@
11
#include <pch.h>
22
#include "Settings.h"
33

4-
#include <cheat-base/render/gui-util.h>
5-
#include <cheat-base/render/renderer.h>
64
#include <cheat-base/cheat/CheatManagerBase.h>
5+
#include <cheat-base/render/renderer.h>
6+
#include <cheat-base/render/gui-util.h>
7+
#include <misc/cpp/imgui_stdlib.h>
8+
#include <cheat-base/util.h>
9+
10+
#include "shlwapi.h"
11+
#pragma comment(lib, "shlwapi.lib")
712

8-
namespace cheat::feature
13+
namespace cheat::feature
914
{
10-
Settings::Settings() : Feature(),
15+
Settings::Settings() : Feature(),
1116
NF(f_MenuKey, "Show Cheat Menu Key", "General", Hotkey(VK_F1)),
1217
NF(f_HotkeysEnabled, "Hotkeys Enabled", "General", true),
1318
NF(f_FontSize, "Font size", "General", 16.0f),
1419
NF(f_ShowStyleEditor, "Show Style Editor", "General", false),
1520

1621
NF(f_StatusMove, "Move Status Window", "General::StatusWindow", true),
1722
NF(f_StatusShow, "Show Status Window", "General::StatusWindow", true),
18-
19-
NF(f_InfoMove, "Move Info Window", "General::InfoWindow", true),
20-
NF(f_InfoShow, "Show Info Window", "General::InfoWindow", true),
21-
23+
24+
NF(f_InfoMove, "Move Info Window", "General::InfoWindow", true),
25+
NF(f_InfoShow, "Show Info Window", "General::InfoWindow", true),
26+
2227
NF(f_FpsMove, "Move FPS Indicator", "General::FPS", false),
2328
NF(f_FpsShow, "Show FPS Indicator", "General::FPS", true),
2429

25-
NF(f_NotificationsShow, "Show Notifications", "General::Notify", true),
30+
NF(f_NotificationsShow, "Show Notifications", "General::Notify", true),
2631
NF(f_NotificationsDelay, "Notifications Delay", "General::Notify", 500),
27-
28-
NF(f_FileLogging, "File Logging", "General::Logging", false),
32+
33+
NF(f_FileLogging, "File Logging", "General::Logging", false),
2934
NF(f_ConsoleLogging, "Console Logging", "General::Logging", true),
3035

3136
NF(f_FastExitEnable, "Fast Exit", "General::FastExit", false),
32-
NF(f_HotkeyExit, "Hotkeys", "General::FastExit", Hotkey(VK_F12))
33-
34-
{
37+
NF(f_HotkeyExit, "Hotkeys", "General::FastExit", Hotkey(VK_F12)),
38+
NFS(f_DefaultTheme, "Theme", "General::Colors", "Default"),
39+
themesDir(util::GetCurrentPath() / "themes")
40+
41+
{
3542
renderer::SetGlobalFontSize(static_cast<float>(f_FontSize));
3643
f_HotkeyExit.value().PressedEvent += MY_METHOD_HANDLER(Settings::OnExitKeyPressed);
37-
}
44+
if (!std::filesystem::exists(themesDir))
45+
std::filesystem::create_directory(themesDir);
3846

39-
const FeatureGUIInfo& Settings::GetGUIInfo() const
40-
{
41-
static const FeatureGUIInfo info{ "", "Settings", false };
42-
return info;
43-
}
47+
}
48+
49+
bool inited = false;
50+
void Settings::Init() {
51+
if (this->f_DefaultTheme.value() != "Default" && !inited)
52+
{
53+
LOG_INFO("Loading theme: %s", themesDir / (f_DefaultTheme.value() + ".json").c_str());
54+
if (!std::filesystem::exists(themesDir / (f_DefaultTheme.value() + ".json")))
55+
f_DefaultTheme = "Default";
56+
else Colors_Import(f_DefaultTheme.value());
57+
inited = true;
58+
}
59+
}
60+
61+
const FeatureGUIInfo& Settings::GetGUIInfo() const
62+
{
63+
static const FeatureGUIInfo info{ "", "Settings", false };
64+
return info;
65+
}
66+
67+
void Settings::Colors_Export(std::string name)
68+
{
69+
ImGuiStyle& style = ImGui::GetStyle();
70+
auto colors = style.Colors;
71+
72+
nlohmann::json json;
73+
for (int i = 0; i < ImGuiCol_COUNT; i++)
74+
json[ImGui::GetStyleColorName((ImGuiCol)i)] = { colors[i].x, colors[i].y, colors[i].z, colors[i].w };
75+
std::ofstream file(themesDir / (name + ".json"));
76+
file << std::setw(4) << json << std::endl;
77+
}
78+
79+
void Settings::Colors_Import(std::string name)
80+
{
81+
ImGuiStyle& style = ImGui::GetStyle();
82+
auto colors = style.Colors;
83+
nlohmann::json json;
84+
std::ifstream file(themesDir / (name + ".json"));
85+
file >> json;
86+
for (int i = 0; i < ImGuiCol_COUNT; i++)
87+
{
88+
auto color = json[ImGui::GetStyleColorName((ImGuiCol)i)];
89+
colors[i].x = color[0];
90+
colors[i].y = color[1];
91+
colors[i].z = color[2];
92+
colors[i].w = color[3];
93+
}
94+
}
4495

4596
void Settings::DrawMain()
4697
{
@@ -103,15 +154,15 @@ namespace cheat::feature
103154
ImGui::BeginGroupPanel("Show Notifications");
104155
{
105156
ConfigWidget(f_NotificationsShow, "Notifications on the bottom-right corner of the window will be displayed.");
106-
ConfigWidget(f_NotificationsDelay, 1,1,10000, "Delay in milliseconds between notifications.");
157+
ConfigWidget(f_NotificationsDelay, 1, 1, 10000, "Delay in milliseconds between notifications.");
107158
}
108159
ImGui::EndGroupPanel();
109160

110161
ImGui::BeginGroupPanel("Fast Exit");
111162
{
112163
ConfigWidget("Enabled",
113164
f_FastExitEnable,
114-
"Enable Fast Exit.\n"
165+
"Enable Fast Exit.\n"
115166
);
116167
if (!f_FastExitEnable)
117168
ImGui::BeginDisabled();
@@ -123,13 +174,36 @@ namespace cheat::feature
123174
ImGui::EndDisabled();
124175
}
125176
ImGui::EndGroupPanel();
177+
178+
ImGui::BeginGroupPanel("Colors");
179+
{
180+
static std::string nameBuffer_;
181+
ImGui::InputText("Name", &nameBuffer_);
182+
if (std::filesystem::exists(themesDir / (nameBuffer_ + ".json")))
183+
{
184+
if (this->f_DefaultTheme.value() != nameBuffer_)
185+
if (ImGui::Button("Set as default"))
186+
f_DefaultTheme = nameBuffer_;
187+
if (ImGui::Button("Load"))
188+
{
189+
Colors_Import(nameBuffer_);
190+
}
191+
}
192+
else
193+
{
194+
ImGui::Text("Theme does not exist.");
195+
}
196+
if (ImGui::Button("Save"))
197+
Colors_Export(nameBuffer_);
198+
}
199+
ImGui::EndGroupPanel();
126200
}
127201

128-
Settings& Settings::GetInstance()
129-
{
130-
static Settings instance;
131-
return instance;
132-
}
202+
Settings& Settings::GetInstance()
203+
{
204+
static Settings instance;
205+
return instance;
206+
}
133207

134208
void Settings::OnExitKeyPressed()
135209
{
@@ -139,4 +213,3 @@ namespace cheat::feature
139213
ExitProcess(0);
140214
}
141215
}
142-

cheat-base/src/cheat-base/cheat/misc/Settings.h

+11-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
#include <cheat-base/cheat/Feature.h>
33
#include <cheat-base/config/config.h>
44

5-
namespace cheat::feature
5+
namespace cheat::feature
66
{
77

88
class Settings : public Feature
9-
{
9+
{
1010
public:
1111
config::Field<Hotkey> f_MenuKey;
1212
config::Field<bool> f_HotkeysEnabled;
@@ -18,10 +18,10 @@ namespace cheat::feature
1818

1919
config::Field<bool> f_InfoMove;
2020
config::Field<bool> f_InfoShow;
21-
21+
2222
config::Field<bool> f_FpsShow;
2323
config::Field<bool> f_FpsMove;
24-
24+
2525
config::Field<bool> f_NotificationsShow;
2626
config::Field<int> f_NotificationsDelay;
2727

@@ -31,11 +31,17 @@ namespace cheat::feature
3131
config::Field<bool> f_FastExitEnable;
3232
config::Field<Hotkey> f_HotkeyExit;
3333

34+
std::filesystem::path themesDir;
35+
config::Field<std::string> f_DefaultTheme;
36+
3437
static Settings& GetInstance();
3538

3639
const FeatureGUIInfo& GetGUIInfo() const override;
3740
void DrawMain() override;
38-
41+
void Init();
42+
void Colors_Export(std::string name);
43+
void Colors_Import(std::string name);
44+
3945
private:
4046

4147
void OnExitKeyPressed();

cheat-base/src/cheat-base/render/renderer.cpp

+10-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <cheat-base/render/backend/dx12-hook.h>
1313

1414
#include <cheat-base/ResourceLoader.h>
15+
#include <cheat-base/cheat/misc/Settings.h>
1516

1617
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
1718

@@ -33,14 +34,14 @@ namespace renderer
3334
static constexpr int _fontsCount = _fontSizeMax / _fontSizeStep;
3435
static std::array<ImFont*, _fontsCount> _fonts;
3536

36-
static Data _customFontData {};
37+
static Data _customFontData{};
3738

3839
static WNDPROC OriginalWndProcHandler;
3940
static ID3D11RenderTargetView* mainRenderTargetView;
4041

4142
static void OnRenderDX11(ID3D11DeviceContext* pContext);
4243
static void OnInitializeDX11(HWND window, ID3D11Device* pDevice, ID3D11DeviceContext* pContext, IDXGISwapChain* pChain);
43-
44+
4445
static void OnPreRenderDX12();
4546
static void OnPostRenderDX12(ID3D12GraphicsCommandList* commandList);
4647
static void OnInitializeDX12(HWND window, ID3D12Device* pDevice, UINT buffersCounts, ID3D12DescriptorHeap* pDescriptorHeapImGuiRender);
@@ -106,7 +107,7 @@ namespace renderer
106107
return io.FontDefault;
107108
}
108109
int fontSizeInt = static_cast<int>(fontSize);
109-
int fontIndex = fontSizeInt / _fontSizeStep +
110+
int fontIndex = fontSizeInt / _fontSizeStep +
110111
(fontSizeInt % _fontSizeStep > (_fontSizeStep / 2) ? 1 : 0) - 1;
111112
fontIndex = std::clamp(fontIndex, 0, _fontsCount - 1);
112113
return _fonts[fontIndex];
@@ -122,7 +123,7 @@ namespace renderer
122123

123124
int fontSizeInt = static_cast<int>(fontSize);
124125
int fontIndex = fontSizeInt / _fontSizeStep;
125-
int fontAligned = fontIndex * _fontSizeStep +
126+
int fontAligned = fontIndex * _fontSizeStep +
126127
((fontSizeInt % _fontSizeStep) > _fontSizeStep / 2 ? _fontSizeStep : 0);
127128
fontAligned = std::clamp(fontAligned, _fontSizeStep, _fontSizeMax);
128129

@@ -138,7 +139,7 @@ namespace renderer
138139
{
139140
return _globalFontSize;
140141
}
141-
142+
142143
static void LoadCustomFont()
143144
{
144145
if (_customFontData.data == nullptr)
@@ -195,7 +196,7 @@ namespace renderer
195196
reinterpret_cast<LONG_PTR>(hWndProc)));
196197

197198
ImGui_ImplWin32_Init(window);
198-
ImGui_ImplDX12_Init(pDevice, buffersCounts, DXGI_FORMAT_R8G8B8A8_UNORM,
199+
ImGui_ImplDX12_Init(pDevice, buffersCounts, DXGI_FORMAT_R8G8B8A8_UNORM,
199200
pDescriptorHeapImGuiRender,
200201
pDescriptorHeapImGuiRender->GetCPUDescriptorHandleForHeapStart(),
201202
pDescriptorHeapImGuiRender->GetGPUDescriptorHandleForHeapStart());
@@ -253,6 +254,9 @@ namespace renderer
253254

254255
pContext->OMSetRenderTargets(1, &mainRenderTargetView, nullptr);
255256
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
257+
258+
auto& themes = cheat::feature::Settings::GetInstance();
259+
themes.Init();
256260
}
257261

258262
static LRESULT CALLBACK hWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

0 commit comments

Comments
 (0)