terravisor/source/interface.cpp

107 lines
3.4 KiB
C++
Raw Normal View History

#include "interface.h"
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <algorithm>
#include <iostream>
2024-08-28 21:07:15 -04:00
#include <filesystem>
namespace Interface {
2024-08-28 21:07:15 -04:00
void ApplyUIScaling(GLFWwindow* window) {
float xScale, yScale;
glfwGetWindowContentScale(window, &xScale, &yScale);
float scale = (xScale + yScale) * 0.5f;
ImGui::GetIO().FontGlobalScale = scale;
ImGuiStyle& style = ImGui::GetStyle();
style.ScaleAllSizes(scale);
ImGuiIO& io = ImGui::GetIO();
io.Fonts->Clear();
#ifdef _WIN32
2024-08-28 21:07:15 -04:00
io.Fonts->AddFontFromFileTTF("imgui/misc/fonts/JetBrainsMono/JetBrainsMono-Medium.ttf", 8.0f * scale);
#else
io.Fonts->AddFontFromFileTTF("../imgui/misc/fonts/JetBrainsMono/JetBrainsMono-Medium.ttf", 8.0f * scale);
#endif
2024-08-28 21:07:15 -04:00
io.Fonts->Build();
}
void AddWindow(std::vector<Window>& windows, const std::string& name, std::function<void()> renderFunc) {
windows.emplace_back(name, renderFunc);
}
Window* GetWindowByName(std::vector<Window>& windows, const std::string& name) {
auto it = std::find_if(windows.begin(), windows.end(), [&name](const Window& window) {
return window.name == name;
});
if (it != windows.end()) {
return &(*it); // Return pointer to the found window
} else {
return nullptr; // Return nullptr if the window was not found
}
}
void RenderWindows(std::vector<Window>& windows) {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
ImGui::NewFrame();
static ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_PassthruCentralNode;
ImGuiWindowFlags window_flags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking;
// Style windows
const ImGuiViewport* viewport = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(viewport->WorkPos);
ImGui::SetNextWindowSize(viewport->WorkSize);
ImGui::SetNextWindowViewport(viewport->ID);
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove;
window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus;
window_flags |= ImGuiWindowFlags_NoBackground;
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
ImGui::Begin("TerraVisor", nullptr, window_flags);
ImGui::PopStyleVar(3);
// Submit the DockSpace
if (io.ConfigFlags & ImGuiConfigFlags_DockingEnable) {
ImGuiID dockspace_id = ImGui::GetID("MyDockSpace");
ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags);
}
if (ImGui::BeginMenuBar()) {
if (ImGui::BeginMenu("Options")) {
ImGui::Text("Nothing Here Yet, Check Back Later!");
ImGui::EndMenu();
}
ImGui::EndMenuBar();
}
ImGui::End();
for (auto& window : windows) {
if (window.isOpen && ImGui::Begin(window.name.c_str(), &window.isOpen)) {
window.size = ImGui::GetWindowSize();
window.position = ImGui::GetWindowPos();
if (window.renderContent) {
window.renderContent();
}
}
ImGui::End();
}
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
}