Finally working window ez tbh

This commit is contained in:
2025-07-15 21:31:54 +03:00
parent 22ad2002e4
commit d4b450ebe6

View File

@@ -50,56 +50,90 @@ int main(int argc, char **argv) {
// program // program
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
static bool dragging_window = false; static bool dragging_window = false;
static double drag_start_mouse_x, drag_start_mouse_y; static double drag_start_global_x, drag_start_global_y;
static int drag_start_window_x,drag_start_window_y; static int drag_start_window_x, drag_start_window_y;
glClearColor(1.0f, .5f, .2f, 1.0f); glClearColor(1.0f, .5f, .2f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glfwPollEvents(); glfwPollEvents();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::BeginMainMenuBar(); if (ImGui::BeginMainMenuBar()) {
bool is_hovered = ImGui::IsWindowHovered(); // --- DRAG LOGIC FIRST ---
bool is_clicked = ImGui::IsMouseClicked(0); bool is_hovered = ImGui::IsWindowHovered();
bool is_held = ImGui::IsMouseDown(0); bool is_clicked = ImGui::IsMouseClicked(0);
bool is_held = ImGui::IsMouseDown(0);
if (is_hovered && is_clicked) { // Get current window position
dragging_window = true; int window_x, window_y;
glfwGetWindowPos(window, &window_x, &window_y);
// Get initial mouse and window positions // Get current mouse position relative to window
glfwGetCursorPos(window, &drag_start_mouse_x, &drag_start_mouse_y); double local_mouse_x, local_mouse_y;
glfwGetWindowPos(window, &drag_start_window_x, &drag_start_window_y); glfwGetCursorPos(window, &local_mouse_x, &local_mouse_y);
}
if (dragging_window) { // Convert to global mouse position
if (is_held) { double global_mouse_x = window_x + local_mouse_x;
double current_mouse_x, current_mouse_y; double global_mouse_y = window_y + local_mouse_y;
glfwGetCursorPos(window, &current_mouse_x, &current_mouse_y);
double dx = current_mouse_x - drag_start_mouse_x; // ✅ Only start dragging if not over any ImGui menu item
double dy = current_mouse_y - drag_start_mouse_y; if (is_hovered && is_clicked && !ImGui::IsAnyItemHovered()) {
dragging_window = true;
glfwSetWindowPos(window, drag_start_global_x = global_mouse_x;
drag_start_window_x + static_cast<int>(dx), drag_start_global_y = global_mouse_y;
drag_start_window_y + static_cast<int>(dy)); drag_start_window_x = window_x;
} else { drag_start_window_y = window_y;
dragging_window = false;
} }
}
// Menu items if (dragging_window) {
if (ImGui::BeginMenu("File")) { if (is_held) {
if (ImGui::MenuItem("Exit")) { double dx = global_mouse_x - drag_start_global_x;
double dy = global_mouse_y - drag_start_global_y;
glfwSetWindowPos(window,
drag_start_window_x + static_cast<int>(dx),
drag_start_window_y + static_cast<int>(dy));
} else {
dragging_window = false;
}
}
// --- MENU ITEMS ALWAYS RENDERED ---
if (ImGui::BeginMenu("File")) {
if (ImGui::MenuItem("Quit")) {
glfwSetWindowShouldClose(window, true);
}
ImGui::EndMenu();
}
float window_width = ImGui::GetWindowWidth();
ImVec2 size = ImGui::CalcTextSize("x");
float button_width = size.x + ImGui::GetStyle().FramePadding.x * 2;
ImGui::SameLine(window_width - button_width - ImGui::GetStyle().ItemSpacing.x);
if (ImGui::Button("x")) {
// Button clicked logic
std::cout << "Exiting..." << std::endl;
glfwSetWindowShouldClose(window, true); glfwSetWindowShouldClose(window, true);
} }
ImGui::EndMenu();
ImGui::EndMainMenuBar();
} }
ImGui::EndMainMenuBar();
ImGui::Begin("Editor"); ImGui::Begin("Editor");
ImGui::Text("Hello from editor"); ImGui::Text("Hello from editor");