Add a random number generator in test_menu2.
Add minimum and maximum for random number generator. Add open current directory button to test_menu2. Add some new functions for directories in imgui_functions. Comment more code in imgui_functions, clean it up a bit. Add some new items into imgui_defines.h Add float to string, address to string, generate random number to misc_util. Add template to convert all values that work with it to a string.
This commit is contained in:
parent
e13d74732e
commit
5e6d5f0cfc
@ -248,6 +248,7 @@
|
||||
<ClCompile Include="src\test\opengl_test.cpp" />
|
||||
<ClCompile Include="src\util\helpers.cpp" />
|
||||
<ClCompile Include="src\util\keystates.cpp" />
|
||||
<ClCompile Include="src\util\misc_util.cpp" />
|
||||
<ClCompile Include="src\util\text_file_functions.cpp" />
|
||||
<ClCompile Include="src\util\text_functions.cpp" />
|
||||
</ItemGroup>
|
||||
@ -268,6 +269,7 @@
|
||||
<ClInclude Include="src\util\helpers.h" />
|
||||
<ClInclude Include="src\util\imgui_defines.h" />
|
||||
<ClInclude Include="src\util\keystates.h" />
|
||||
<ClInclude Include="src\util\misc_util.h" />
|
||||
<ClInclude Include="src\util\text_file_functions.h" />
|
||||
<ClInclude Include="src\util\text_functions.h" />
|
||||
<ClInclude Include="lib\ImGui\backends\imgui_impl_dx9.h" />
|
||||
|
@ -73,6 +73,9 @@
|
||||
<ClCompile Include="src\menus\test_menu1.cpp">
|
||||
<Filter>src\menus</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\util\misc_util.cpp">
|
||||
<Filter>src\util</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="lib\ImGui\imconfig.h">
|
||||
@ -157,6 +160,9 @@
|
||||
<ClInclude Include="src\menus\test_menu1.h">
|
||||
<Filter>src\menus</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\util\misc_util.h">
|
||||
<Filter>src\util</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Filter Include="src">
|
||||
|
@ -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 <string>
|
||||
#include <iostream>
|
||||
|
||||
#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 <direct.h>
|
||||
#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 -------------/
|
||||
|
||||
/// <summary>
|
||||
/// Destory the ImGui window
|
||||
@ -63,6 +157,11 @@ void ImGuiFunctions::Main::ShowWindow(HWND hwnd)
|
||||
// }
|
||||
//}
|
||||
|
||||
//-------------- End ImGui Window handling -------------/
|
||||
|
||||
|
||||
//-------------- ImGui Setup and fonts -------------/
|
||||
|
||||
/// <summary>
|
||||
/// Setup ImGui
|
||||
/// </summary>
|
||||
@ -105,21 +204,7 @@ void ImGuiFunctions::Main::SetupContext()
|
||||
//IM_ASSERT(font != nullptr);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a help marker
|
||||
/// </summary>
|
||||
/// <param name="desc"></param>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//-------------- End ImGui Setup and fonts -------------/
|
||||
|
||||
//-------------- Misc ImGui functions -------------/
|
||||
|
||||
/// <summary>
|
||||
/// Add a help marker
|
||||
/// </summary>
|
||||
/// <param name="desc"></param>
|
||||
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 -------------/
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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.
|
||||
|
@ -1,6 +1,13 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
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();
|
||||
};
|
||||
|
@ -13,4 +13,16 @@
|
||||
|
||||
// This works for shortening the ImGui text.
|
||||
// Add it to files that use ImGui text.
|
||||
#define IMGUITEXT ImGui::Text
|
||||
#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
|
54
src/util/misc_util.cpp
Normal file
54
src/util/misc_util.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
#include "misc_util.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
#include <string>
|
||||
|
||||
#define NEW_RANDOM_NUMBER_GEN
|
||||
|
||||
/// <summary>
|
||||
/// Convert a float to a string, instead of using std::to_string everywhere.
|
||||
/// </summary>
|
||||
/// <param name="value">The float to convert</param>
|
||||
/// <returns>The float converted to string</returns>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate a random number, this takes a minimum and maximum value
|
||||
/// https://stackoverflow.com/questions/29381843/generate-random-number-in-range-min-max
|
||||
/// </summary>
|
||||
/// <param name="min">The minimum number to generate.</param>
|
||||
/// <param name="max">The maximum number to generate.</param>
|
||||
/// <returns>A random number between min and max.</returns>
|
||||
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
|
||||
}
|
||||
|
32
src/util/misc_util.h
Normal file
32
src/util/misc_util.h
Normal file
@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
// 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 <typename T>
|
||||
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);
|
||||
|
||||
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user