diff --git a/KCNet-ImGui-Test.vcxproj b/KCNet-ImGui-Test.vcxproj index fb6421d..5aaadd1 100644 --- a/KCNet-ImGui-Test.vcxproj +++ b/KCNet-ImGui-Test.vcxproj @@ -248,6 +248,7 @@ + @@ -268,6 +269,7 @@ + diff --git a/KCNet-ImGui-Test.vcxproj.filters b/KCNet-ImGui-Test.vcxproj.filters index 31d5545..552ba71 100644 --- a/KCNet-ImGui-Test.vcxproj.filters +++ b/KCNet-ImGui-Test.vcxproj.filters @@ -73,6 +73,9 @@ src\menus + + src\util + @@ -157,6 +160,9 @@ src\menus + + src\util + diff --git a/src/functions/imgui_functions.cpp b/src/functions/imgui_functions.cpp index 8ed6fdf..3e07b7c 100644 --- a/src/functions/imgui_functions.cpp +++ b/src/functions/imgui_functions.cpp @@ -1,11 +1,105 @@ +//#include "pch.h" + +// https://stackoverflow.com/questions/22744262/cant-call-stdmax-because-minwindef-h-defines-max +// Fix for std::min and std::max +#define NOMINMAX + +#include +#include + #include "imgui_functions.h" + + // ImGui #include "imgui.h" #include "imgui_impl_dx9.h" #include "imgui_impl_win32.h" -// TODO Move ImGui functions for use in here. +#ifdef _WIN32 +// For _getcwd +#include +#endif + +//-------------- ImGui Directories -------------/ + +// TODO Make this work on Linux and Windows + +std::string +ImGuiFunctions::Folders::GetCurrentWorkingDirectory() +{ +#ifdef _WIN32 + char buffer[MAX_PATH]; + if (_getcwd(buffer, sizeof(buffer)) != nullptr) { + return std::string(buffer); + } + else { + return ""; // Or handle the error appropriately + } +#endif +} + + +void +ImGuiFunctions::Folders::OpenCurrentDirectoryButton(const char* buttonLabel) +{ +#ifdef _WIN32 + std::string currentDir = GetCurrentWorkingDirectory(); + if (!currentDir.empty() && ImGui::Button(buttonLabel)) + { + ShellExecuteA(nullptr, "explore", currentDir.c_str(), nullptr, nullptr, SW_SHOWNORMAL); + } + +#endif +} + +void +ImGuiFunctions::Folders::OpenDirectoryButton(std::string folder, const char* buttonLabel) +{ + //Defines defines = Defines(); +#ifdef _WIN32 + // std::string currentDir = GetCurrentWorkingDirectory() + folder; + if (!folder.empty() && ImGui::Button(buttonLabel)) { + // Leftover log function, ReVC + //LogFunctions::LogInfo(defines.logFile, "Folder path: " + folder); + // ShellExecuteA(nullptr, "explore", currentDir.c_str(), nullptr, nullptr, SW_SHOWNORMAL); + ShellExecuteA(nullptr, "explore", folder.c_str(), nullptr, nullptr, SW_SHOWNORMAL); + } +#endif +} + + + +//-------------- End ImGui Directories -------------/ + +//-------------- ImGui input -------------/ + + +void +ImGuiFunctions::Input::InputIntMax(const char* label, int* v, int step, int step_fast, int max_value) +{ + if (ImGui::InputInt(label, v, step, step_fast)) { + *v = std::min(*v, max_value); + //return true; // Value changed + } + //return false; // Value unchanged +} + +// If you also need to set a min value, use this function instead: +void +ImGuiFunctions::Input::InputIntClamp(const char* label, int* v, int step, int step_fast, int min_value, int max_value) +{ + if (ImGui::InputInt(label, v, step, step_fast)) { + *v = std::max(min_value, std::min(*v, max_value)); + //return true; // Value changed + } + //return false; // Value unchanged +} + +//-------------- End ImGui input -------------/ + + +//-------------- ImGui Window handling -------------/ /// /// Destory the ImGui window @@ -63,6 +157,11 @@ void ImGuiFunctions::Main::ShowWindow(HWND hwnd) // } //} +//-------------- End ImGui Window handling -------------/ + + +//-------------- ImGui Setup and fonts -------------/ + /// /// Setup ImGui /// @@ -105,21 +204,7 @@ void ImGuiFunctions::Main::SetupContext() //IM_ASSERT(font != nullptr); } -/// -/// Add a help marker -/// -/// -static void ImGuiFunctions::Main::HelpMarker(const char* desc) -{ - ImGui::TextDisabled("(?)"); - if (ImGui::BeginItemTooltip()) - { - ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f); - ImGui::TextUnformatted(desc); - ImGui::PopTextWrapPos(); - ImGui::EndTooltip(); - } -} + // Credit to user-grinch on github for the style code here. // https://github.com/user-grinch/Cheat-Menu/blob/master/src/cheatmenu.cpp#L271-L335 @@ -187,4 +272,27 @@ void ImGuiFunctions::Main::ApplyStyle() style->Colors[ImGuiCol_PlotHistogramHovered] = ImVec4(0.25f, 1.00f, 0.00f, 1.00f); style->Colors[ImGuiCol_TextSelectedBg] = ImVec4(0.06f, 0.05f, 0.06f, 0.95f); style->Colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.6f); -} \ No newline at end of file +} + + +//-------------- End ImGui Setup and fonts -------------/ + +//-------------- Misc ImGui functions -------------/ + +/// +/// Add a help marker +/// +/// +static void ImGuiFunctions::Main::HelpMarker(const char* desc) +{ + ImGui::TextDisabled("(?)"); + if (ImGui::BeginItemTooltip()) + { + ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f); + ImGui::TextUnformatted(desc); + ImGui::PopTextWrapPos(); + ImGui::EndTooltip(); + } +} + +//-------------- End Misc ImGui functions -------------/ diff --git a/src/functions/imgui_functions.h b/src/functions/imgui_functions.h index 01193d1..decbd88 100644 --- a/src/functions/imgui_functions.h +++ b/src/functions/imgui_functions.h @@ -14,6 +14,23 @@ namespace ImGuiFunctions { void ShutDown(); void DestroyWindow(HWND hwnd, WNDCLASSEXW wc); void ApplyStyle(); + + + + } + + namespace Input { + // For setting a min/max value on ImGui::InputInt +// Set it to just have a maximum value + void InputIntMax(const char* label, int* v, int step = 1, int step_fast = 10, int max_value = 100); + // Set it to have a minimum and maximum value + void InputIntClamp(const char* label, int* v, int step = 1, int step_fast = 10, int min_value = 0, int max_value = 100); + } + + namespace Folders { + std::string GetCurrentWorkingDirectory(); + void OpenCurrentDirectoryButton(const char* buttonLabel); + void OpenDirectoryButton(std::string folder, const char* buttonLabel); } } diff --git a/src/menus/test_menu2.cpp b/src/menus/test_menu2.cpp index 7fac040..c5ad4db 100644 --- a/src/menus/test_menu2.cpp +++ b/src/menus/test_menu2.cpp @@ -12,8 +12,25 @@ #include "test_menu2.h" +#include "imgui_defines.h" +// New for random numbers and other util +#include "misc_util.h" + +// Functions for clamping the inputs for values and other stuff +#include "imgui_functions.h" + void TestMenu2::TestMenu() { + // Init the random number to 0 by default + static int randomNumber = 0; + + // Set defualt min and max values + static int testMin = 0; + static int min = 0; + static int max = 1000; + + MiscUtil miscUtil = MiscUtil(); + const int vsprintBufferSize = 1024; char* vsPrintBuffer = new char[vsprintBufferSize]; if (ImGui::CollapsingHeader("test2")) @@ -28,8 +45,71 @@ void TestMenu2::TestMenu() call_vsprintf_s(vsPrintBuffer, 15, format); } #endif //_TEST1 + + //ImGui::SliderInt("Min/Max", &testMin, min, max); + + + // From ReVC + //ImGui::Text("Minimum number: 23, Maximum number: 59"); + // TODO Come up with a better solution for this. + //ImGuiFunctions::Input::InputIntMax("Minimum", &min, 1, 10, 23); + + //ImGuiFunctions::Input::InputIntClamp("Maximum", &max, 1, 10, 0, 59); + + IMGUITEXT("Random Number Generator"); + + IMGUI_INPUT_INT("Minimum", &min); + IMGUI_INPUT_INT("Maximum", &max); + + + + // This works outputting to a message box, I switched to using ImGui text. + if (IMGUIBUTTON("Generate random number")) + { + // Some random value + //int value = miscUtil.GenerateRandomNumber(1, 1000); + //randomNumber = miscUtil.GenerateRandomNumber(1, 1000); + randomNumber = miscUtil.GenerateRandomNumber(min, max); +//#ifdef _WIN32 +// // Convert the integer to a string +// std::stringstream ss; +// //ss << "Random number: " << value; +// ss << "Random number: " << randomNumber; +// std::string strValue = ss.str(); +// std::wstring wStrValue(strValue.begin(), strValue.end()); +// +// // Switch to using MessageBoxW +// MessageBoxW(NULL, wStrValue.c_str(), +// L"KCNet-ImGuiTest", MB_OK); +// +// +// //MessageBox(NULL, TEXT("Hello From ImGui, a MessageBox"), +// //MessageBox(NULL, TEXT("Random number: " + value), +// // TEXT("KCNet-ImGuiTest"), MB_OK); +//#endif + } + + std::string randomNumberText = "Random number: " + MiscUtil::toString(randomNumber); + IMGUITEXT(randomNumberText.c_str()); + + // End random number generator + + IMGUI_SEPERATOR(); + IMGUITEXT("Folder utils"); + ImGuiFunctions::Folders::OpenCurrentDirectoryButton("Open current directory"); + + IMGUI_SEPERATOR(); + + // End directory testing + + //if (IMGUIBUTTON("Open Current Directory")) + //{ + // + //} + + } - // + // End test2 header // Disabled, these are just for testing. // This works for a column, having multiple items on the same row. diff --git a/src/menus/test_menu2.h b/src/menus/test_menu2.h index 1fb23f0..b374d61 100644 --- a/src/menus/test_menu2.h +++ b/src/menus/test_menu2.h @@ -1,6 +1,13 @@ #pragma once +#include +#include + class TestMenu2 { +private: + // TODO Figure out how to use the randomNumber in a button without making it static. + //int randomNumber = 0; + //std::string randomNumberText = "Random number: 0"; public: void TestMenu(); }; diff --git a/src/util/imgui_defines.h b/src/util/imgui_defines.h index 8352e0b..8f9aa5a 100644 --- a/src/util/imgui_defines.h +++ b/src/util/imgui_defines.h @@ -13,4 +13,16 @@ // This works for shortening the ImGui text. // Add it to files that use ImGui text. -#define IMGUITEXT ImGui::Text \ No newline at end of file +#define IMGUITEXT ImGui::Text + +// Make a button +#define IMGUIBUTTON ImGui::Button + +// Make a input int value +#define IMGUI_INPUT_INT(LABEL, VALUE) ImGui::InputInt(LABEL, VALUE) + +// Make a input int with flags +#define IMGUI_INPUT_INT_FLAGS(LABEL, VALUE, FLAGS) ImGui::InputInt(LABEL, VALUE, FLAGS) + +// Make an ImGui separator +#define IMGUI_SEPERATOR ImGui::Separator \ No newline at end of file diff --git a/src/util/misc_util.cpp b/src/util/misc_util.cpp new file mode 100644 index 0000000..f503eb1 --- /dev/null +++ b/src/util/misc_util.cpp @@ -0,0 +1,54 @@ +#include "misc_util.h" + +#include +#include +#include + +#define NEW_RANDOM_NUMBER_GEN + +/// +/// Convert a float to a string, instead of using std::to_string everywhere. +/// +/// The float to convert +/// The float converted to string +std::string +MiscUtil::floatToString(float value) +{ + std::stringstream ss; + ss << value; + return ss.str(); +} + +std::string +MiscUtil::AddressToString(void* address) +{ + std::stringstream ss; + ss << address; + return ss.str(); +} + +/// +/// Generate a random number, this takes a minimum and maximum value +/// https://stackoverflow.com/questions/29381843/generate-random-number-in-range-min-max +/// +/// The minimum number to generate. +/// The maximum number to generate. +/// A random number between min and max. +int +MiscUtil::GenerateRandomNumber(int min, int max) +{ + +#ifdef NEW_RANDOM_NUMBER_GEN + // Use std::random_device to seed the generator + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution<> distrib(min, max); + return distrib(gen); +#else + srand(time(0)); + // Get a random number between min and max + int randomNum = min + rand() % (max - min + 1); + return randomNum; +#endif +} + diff --git a/src/util/misc_util.h b/src/util/misc_util.h new file mode 100644 index 0000000..0541fb9 --- /dev/null +++ b/src/util/misc_util.h @@ -0,0 +1,32 @@ +#pragma once +#include +#include +#include + +// Some of these came from my ReVC project + +class MiscUtil +{ +public: + // Convert a memory address to a string + // Takes a value like '&pPlayer->m_fHealth' or any variable. + std::string AddressToString(void* address); + + // Random number generator with a min and max value. + static int GenerateRandomNumber(int min, int max); + + + // Template to convert float, int, and other values that can be converted to a string. + template + static std::string toString(T value) { + std::stringstream ss; + ss << value; + return ss.str(); + } + + // Convert a float to a string + std::string floatToString(float value); + + +}; +