load settings on start
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <shellapi.h>
|
||||
@@ -28,12 +29,19 @@ UINT const WMAPP_HIDEFLYOUT = WM_APP + 2;
|
||||
|
||||
DrawData* hackyDrawData;
|
||||
ApplicationData* hackyAppData;
|
||||
bool justDocked = false;
|
||||
bool isHidden = false;
|
||||
|
||||
int main()
|
||||
{
|
||||
ApplicationData applicationData{};
|
||||
ImGuiCallbacks callbacks{};
|
||||
|
||||
startImgui<ApplicationData>(applicationData, init, draw, cleanup, "Asuro's Tool", 600, 400);
|
||||
callbacks.initFunc = std::bind(init, std::placeholders::_1, std::ref(applicationData));
|
||||
callbacks.drawFunc = std::bind(draw, std::placeholders::_1, std::ref(applicationData));
|
||||
callbacks.cleanupFunc = std::bind(cleanup, std::placeholders::_1, std::ref(applicationData));
|
||||
|
||||
startImgui(callbacks, "Asuro's Tool", 600, 400);
|
||||
}
|
||||
|
||||
LRESULT CALLBACK trayIconEventHandler(int code, WPARAM wParam, LPARAM lParam)
|
||||
@@ -74,20 +82,6 @@ void init(DrawData& drawData, ApplicationData& appData)
|
||||
icons_config.PixelSnapH = true;
|
||||
io.Fonts->AddFontFromFileTTF("remixicon.ttf", 14.0f, &icons_config, icons_ranges);
|
||||
|
||||
// Set up audio device api
|
||||
HRESULT audioResult;
|
||||
audioResult = CoInitializeEx(NULL, COINIT_MULTITHREADED);
|
||||
isError(audioResult, "Failed to initialize COM: ");
|
||||
|
||||
audioResult = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&appData.audioData->deviceEnumerator));
|
||||
isError(audioResult, "Failed to set up audio device enumerator: ");
|
||||
|
||||
appData.audioData->audioNotificationListener = new AudioNotificationListener(appData.audioData);
|
||||
audioResult = appData.audioData->deviceEnumerator->RegisterEndpointNotificationCallback(appData.audioData->audioNotificationListener);
|
||||
isError(audioResult, "Failed to register audio notification listener: ");
|
||||
|
||||
reloadDeviceLists(*appData.audioData);
|
||||
|
||||
// Set window icon
|
||||
HINSTANCE instance = GetModuleHandle(NULL);
|
||||
LPWSTR iconId = MAKEINTRESOURCE(IDI_ICON1);
|
||||
@@ -123,15 +117,61 @@ void init(DrawData& drawData, ApplicationData& appData)
|
||||
|
||||
// Set window minimize behavior
|
||||
glfwSetWindowIconifyCallback(drawData.window, [](GLFWwindow* window, int isIconified) {
|
||||
if (isIconified == GLFW_TRUE && hackyAppData->settings.minimizeToTray)
|
||||
if (isIconified && hackyAppData->settings.minimizeToTray)
|
||||
{
|
||||
glfwHideWindow(window);
|
||||
}
|
||||
isHidden = isIconified;
|
||||
});
|
||||
|
||||
glfwSetWindowFocusCallback(drawData.window, [](GLFWwindow* window, int isFocused) {
|
||||
if (!isFocused && hackyAppData->settings.docked && !justDocked)
|
||||
{
|
||||
glfwIconifyWindow(window);
|
||||
}
|
||||
});
|
||||
|
||||
// Load settings
|
||||
ImGuiSettingsHandler ini_handler;
|
||||
ini_handler.TypeName = "ApplicationSettings";
|
||||
ini_handler.TypeHash = ImHashStr("ApplicationSettings");
|
||||
ini_handler.ReadOpenFn = settingsReadOpen;
|
||||
ini_handler.ReadLineFn = settingsReadLine;
|
||||
ini_handler.WriteAllFn = settingsWriteAll;
|
||||
GImGui->SettingsHandlers.push_back(ini_handler);
|
||||
ImGui::LoadIniSettingsFromDisk("imgui.ini");
|
||||
updateSettings(drawData, appData);
|
||||
|
||||
// Set up audio device api
|
||||
HRESULT audioResult;
|
||||
audioResult = CoInitializeEx(NULL, COINIT_MULTITHREADED);
|
||||
isError(audioResult, "Failed to initialize COM: ");
|
||||
|
||||
audioResult = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&appData.audioData->deviceEnumerator));
|
||||
isError(audioResult, "Failed to set up audio device enumerator: ");
|
||||
|
||||
appData.audioData->audioNotificationListener = new AudioNotificationListener(appData.audioData);
|
||||
audioResult = appData.audioData->deviceEnumerator->RegisterEndpointNotificationCallback(appData.audioData->audioNotificationListener);
|
||||
isError(audioResult, "Failed to register audio notification listener: ");
|
||||
|
||||
reloadDeviceLists(*appData.audioData);
|
||||
}
|
||||
|
||||
void draw(DrawData& drawData, ApplicationData& appData)
|
||||
{
|
||||
justDocked = false;
|
||||
|
||||
// sad :(
|
||||
hackyDrawData = &drawData;
|
||||
hackyAppData = &appData;
|
||||
|
||||
// Actual Drawing
|
||||
if (isHidden)
|
||||
{
|
||||
glfwWaitEvents();
|
||||
return;
|
||||
}
|
||||
|
||||
float customYCursor = 0;
|
||||
ImVec2 viewportSize = ImGui::GetMainViewport()->Size;
|
||||
|
||||
@@ -176,6 +216,7 @@ void cleanup(DrawData& drawData, ApplicationData& appData)
|
||||
ImVec2 menuBar(DrawData& drawData, ApplicationData& appData)
|
||||
{
|
||||
ImVec2 size{};
|
||||
bool closeMenu = false;
|
||||
|
||||
if (ImGui::BeginMainMenuBar())
|
||||
{
|
||||
@@ -185,7 +226,8 @@ ImVec2 menuBar(DrawData& drawData, ApplicationData& appData)
|
||||
ImGui::Checkbox("Docked", &appData.settings.docked);
|
||||
if (appData.settings.docked != prevDocked)
|
||||
{
|
||||
glfwSetWindowAttrib(drawData.window, GLFW_DECORATED, !appData.settings.docked);
|
||||
closeMenu = true;
|
||||
updateDocked(drawData, appData);
|
||||
}
|
||||
|
||||
ImGui::Checkbox("Show Disabled Devices", &appData.settings.showDisabledDevices);
|
||||
@@ -223,6 +265,11 @@ ImVec2 menuBar(DrawData& drawData, ApplicationData& appData)
|
||||
ImGui::EndMainMenuBar();
|
||||
}
|
||||
|
||||
if (closeMenu)
|
||||
{
|
||||
ImGui::SetWindowFocus();
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
@@ -359,3 +406,43 @@ void drawCircle(float radius, ImU32 color)
|
||||
ImVec2 windowPos = ImGui::GetWindowPos();
|
||||
drawList->AddCircleFilled(ImVec2(cursorPos.x + windowPos.x, cursorPos.y + windowPos.y + ImGui::GetTextLineHeight() / 2.), radius, color);
|
||||
}
|
||||
|
||||
void updateSettings(DrawData& drawData, ApplicationData& appData)
|
||||
{
|
||||
updateDocked(drawData, appData);
|
||||
}
|
||||
|
||||
void updateDocked(DrawData& drawData, ApplicationData& appData)
|
||||
{
|
||||
justDocked = true;
|
||||
|
||||
glfwSetWindowAttrib(drawData.window, GLFW_DECORATED, !appData.settings.docked);
|
||||
ShowWindow(drawData.window_handle, SW_HIDE);
|
||||
SetWindowLongPtr(drawData.window_handle, GWL_EXSTYLE, appData.settings.docked ? WS_EX_TOOLWINDOW : 0);
|
||||
ShowWindow(drawData.window_handle, SW_SHOW);
|
||||
}
|
||||
|
||||
void* settingsReadOpen(ImGuiContext* ctx, ImGuiSettingsHandler* handler, const char* name)
|
||||
{
|
||||
ApplicationSettings* settings = &hackyAppData->settings;
|
||||
*settings = ApplicationSettings();
|
||||
return (void*)settings;
|
||||
}
|
||||
|
||||
void settingsReadLine(ImGuiContext* ctx, ImGuiSettingsHandler* handler, void* entry, const char* line)
|
||||
{
|
||||
ApplicationSettings* settings = (ApplicationSettings*)entry;
|
||||
|
||||
int docked;
|
||||
if (sscanf_s(line, "docked=%i", &docked))
|
||||
{
|
||||
settings->docked = (bool)docked;
|
||||
}
|
||||
}
|
||||
|
||||
void settingsWriteAll(ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* outBuf)
|
||||
{
|
||||
outBuf->appendf("[%s][%s]\n", "ApplicationSettings", "ApplicationSettings");
|
||||
outBuf->appendf("docked=%i\n", (int)hackyAppData->settings.docked);
|
||||
outBuf->append("\n");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user