feat: add DevPanel

This commit is contained in:
2026-04-25 14:24:50 +08:00
parent ada0603a2f
commit 4ca2133ff3
13 changed files with 404 additions and 39 deletions

View File

@@ -3,6 +3,13 @@
#include <Cubed/tools/cubed_assert.hpp>
#include <Cubed/tools/log.hpp>
#include <Cubed/window.hpp>
#include <Cubed/tools/font.hpp>
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
namespace Cubed {
static int windowed_xpos = 0, windowed_ypos = 0;
@@ -15,6 +22,11 @@ Window::Window(Renderer& renderer) :
}
Window::~Window() {
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
if (m_window) {
glfwDestroyWindow(m_window);
m_window = nullptr;
@@ -44,6 +56,7 @@ void Window::update_viewport() {
config.set("window.width", windowed_width);
config.set("window.height", windowed_height);
}
void Window::init() {
@@ -84,7 +97,6 @@ void Window::init() {
GLFWmonitor* primary = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(primary);
glfwSetWindowPos(m_window, static_cast<int>(mode->width / 2.0f) - 400, static_cast<int>(mode->height / 2.0f) - 300);
//update_viewport();
}
@@ -100,23 +112,7 @@ void Window::hot_reload() {
windowed_width = config.get<int>("window.width");
windowed_height = config.get<int>("window.height");
if (config.get<bool>("window.fullscreen.V-Sync")) {
GLFWmonitor* monitor = glfwGetWindowMonitor(m_window);
if (monitor != nullptr) {
glfwSetWindowMonitor(
m_window,
nullptr,
windowed_xpos,
windowed_ypos,
windowed_width,
windowed_height,
0
);
} else {
}
config.set("window.fullscreen", false);
} else {
if (config.get<bool>("window.fullscreen")) {
glfwGetWindowPos(m_window, &windowed_xpos, &windowed_ypos);
glfwGetWindowSize(m_window, &windowed_width, &windowed_height);
@@ -132,11 +128,28 @@ void Window::hot_reload() {
mode->height,
GL_DONT_CARE
);
config.set("window.fullscreen", true);
} else {
GLFWmonitor* monitor = glfwGetWindowMonitor(m_window);
if (monitor != nullptr) {
glfwSetWindowMonitor(
m_window,
nullptr,
windowed_xpos,
windowed_ypos,
windowed_width,
windowed_height,
0
);
} else {
Logger::error("Can't Find Monitor");
}
}
update_viewport();
if (!m_mouse_enable) {
glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
} else {
glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
}
void Window::toggle_fullscreen() {
@@ -178,13 +191,54 @@ void Window::toggle_fullscreen() {
}
void Window::toggle_mouse_able() {
//auto& io = ImGui::GetIO();
if (m_mouse_enable) {
//io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange;
//Logger::info("ImGuiConfigFlags_NoMouseCursorChange");
//ImGui::SetMouseCursor(ImGuiMouseCursor_None);
//glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
m_mouse_enable = false;
} else {
//io.ConfigFlags &= ~ImGuiConfigFlags_NoMouseCursorChange;
//Logger::info("Disable ImGuiConfigFlags_NoMouseCursorChange");
glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
m_mouse_enable = true;
}
}
void Window::imgui_init() {
float dpi_scale_x, dpi_scale_y;
glfwGetWindowContentScale(m_window, &dpi_scale_x, &dpi_scale_y);
//float main_scale = ImGui_ImplGlfw_GetContentScaleForMonitor(glfwGetPrimaryMonitor());
float main_scale = dpi_scale_x;
Logger::info("Main Scale {}", main_scale);
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange; // Prevent ImGui from automatically changing the system cursor shape, allowing the game to fully control cursor appearance (e.g., hidden/disabled custom cursor).
auto theme = Config::get().get<int>("devpanel.theme");
if (theme == 0) {
ImGui::StyleColorsDark();
} else if (theme == 1) {
ImGui::StyleColorsLight();
} else {
ImGui::StyleColorsDark();
Config::get().set("devpanel.theme", 0);
}
ImGuiStyle& style = ImGui::GetStyle();
style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
style.FontScaleDpi = main_scale;
style.FontSizeBase = 20.0f;
ImFont* font = io.Fonts->AddFontFromFileTTF(Font::font_path().c_str());
ASSERT_MSG(font != nullptr, "Font Load Fail");
// Setup Platform/Renderer backends
ImGui_ImplGlfw_InitForOpenGL(m_window, false);
ImGui_ImplOpenGL3_Init();
}
}