2025-07-15 03:08:42 +03:00
|
|
|
#include <glad/glad.h>
|
|
|
|
|
#include <GLFW/glfw3.h>
|
2025-07-15 20:25:08 +03:00
|
|
|
|
2025-07-15 19:43:51 +03:00
|
|
|
#include <imgui.h>
|
|
|
|
|
#include <imgui_impl_glfw.h>
|
|
|
|
|
#include <imgui_impl_opengl3.h>
|
2025-07-15 20:25:08 +03:00
|
|
|
#include <iostream>
|
2025-07-15 03:08:42 +03:00
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
|
std::cout << "Hello working!" << std::endl;
|
2025-07-15 20:42:47 +03:00
|
|
|
|
2025-07-15 19:43:51 +03:00
|
|
|
|
2025-07-15 03:08:42 +03:00
|
|
|
glfwInit();
|
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
2025-07-15 21:54:05 +03:00
|
|
|
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
|
2025-07-15 20:56:12 +03:00
|
|
|
glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);
|
2025-07-15 03:08:42 +03:00
|
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
|
|
2025-07-15 20:56:12 +03:00
|
|
|
|
2025-07-15 20:25:08 +03:00
|
|
|
// make the window
|
2025-07-15 03:08:42 +03:00
|
|
|
GLFWwindow *window = glfwCreateWindow(640, 480, "OpenGL Demo", NULL, NULL);
|
|
|
|
|
if (!window) {
|
|
|
|
|
|
|
|
|
|
std::cout << "Failed to create GLFW window." << std::endl;
|
|
|
|
|
glfwTerminate();
|
2025-07-15 20:09:48 +03:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
glfwMakeContextCurrent(window);
|
2025-07-15 20:25:08 +03:00
|
|
|
// d
|
2025-07-15 19:43:51 +03:00
|
|
|
|
2025-07-15 20:25:08 +03:00
|
|
|
// load graphics
|
2025-07-15 20:09:48 +03:00
|
|
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
|
|
|
|
std::cerr << "Failed to initialize OpenGL loader!" << std::endl;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
glViewport(0, 0, 640, 480);
|
2025-07-15 20:25:08 +03:00
|
|
|
// d
|
2025-07-15 19:43:51 +03:00
|
|
|
|
2025-07-15 20:25:08 +03:00
|
|
|
// now make ImGui
|
2025-07-15 20:09:48 +03:00
|
|
|
IMGUI_CHECKVERSION();
|
|
|
|
|
ImGui::CreateContext();
|
2025-07-15 20:25:08 +03:00
|
|
|
ImGuiIO &io = ImGui::GetIO();
|
|
|
|
|
(void)io;
|
2025-07-15 20:42:47 +03:00
|
|
|
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
|
2025-07-15 20:09:48 +03:00
|
|
|
ImGui::StyleColorsDark();
|
|
|
|
|
ImGui_ImplGlfw_InitForOpenGL(window, true);
|
|
|
|
|
ImGui_ImplOpenGL3_Init("#version 130");
|
2025-07-15 20:25:08 +03:00
|
|
|
/// im a dumbass
|
|
|
|
|
///
|
2025-07-15 19:43:51 +03:00
|
|
|
|
2025-07-15 20:25:08 +03:00
|
|
|
// program
|
|
|
|
|
while (!glfwWindowShouldClose(window)) {
|
2025-07-15 21:31:54 +03:00
|
|
|
|
|
|
|
|
ImGui_ImplOpenGL3_NewFrame();
|
|
|
|
|
ImGui_ImplGlfw_NewFrame();
|
|
|
|
|
ImGui::NewFrame();
|
|
|
|
|
|
2025-07-15 20:42:47 +03:00
|
|
|
static bool dragging_window = false;
|
2025-07-15 21:31:54 +03:00
|
|
|
static double drag_start_global_x, drag_start_global_y;
|
|
|
|
|
static int drag_start_window_x, drag_start_window_y;
|
2025-07-15 03:08:42 +03:00
|
|
|
|
2025-07-15 20:25:08 +03:00
|
|
|
glClearColor(1.0f, .5f, .2f, 1.0f);
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2025-07-15 19:43:51 +03:00
|
|
|
|
2025-07-15 20:25:08 +03:00
|
|
|
glfwPollEvents();
|
2025-07-15 03:08:42 +03:00
|
|
|
|
|
|
|
|
|
2025-07-15 21:31:54 +03:00
|
|
|
if (ImGui::BeginMainMenuBar()) {
|
|
|
|
|
// --- DRAG LOGIC FIRST ---
|
|
|
|
|
bool is_hovered = ImGui::IsWindowHovered();
|
|
|
|
|
bool is_clicked = ImGui::IsMouseClicked(0);
|
|
|
|
|
bool is_held = ImGui::IsMouseDown(0);
|
2025-07-15 03:08:42 +03:00
|
|
|
|
2025-07-15 21:31:54 +03:00
|
|
|
// Get current window position
|
|
|
|
|
int window_x, window_y;
|
|
|
|
|
glfwGetWindowPos(window, &window_x, &window_y);
|
2025-07-15 03:08:42 +03:00
|
|
|
|
2025-07-15 21:31:54 +03:00
|
|
|
// Get current mouse position relative to window
|
|
|
|
|
double local_mouse_x, local_mouse_y;
|
|
|
|
|
glfwGetCursorPos(window, &local_mouse_x, &local_mouse_y);
|
2025-07-15 03:08:42 +03:00
|
|
|
|
2025-07-15 21:31:54 +03:00
|
|
|
// Convert to global mouse position
|
|
|
|
|
double global_mouse_x = window_x + local_mouse_x;
|
|
|
|
|
double global_mouse_y = window_y + local_mouse_y;
|
2025-07-15 20:42:47 +03:00
|
|
|
|
2025-07-15 21:31:54 +03:00
|
|
|
// ✅ Only start dragging if not over any ImGui menu item
|
|
|
|
|
if (is_hovered && is_clicked && !ImGui::IsAnyItemHovered()) {
|
|
|
|
|
dragging_window = true;
|
|
|
|
|
drag_start_global_x = global_mouse_x;
|
|
|
|
|
drag_start_global_y = global_mouse_y;
|
|
|
|
|
drag_start_window_x = window_x;
|
|
|
|
|
drag_start_window_y = window_y;
|
|
|
|
|
}
|
2025-07-15 20:09:48 +03:00
|
|
|
|
2025-07-15 21:54:05 +03:00
|
|
|
|
|
|
|
|
static bool is_maximized = false;
|
|
|
|
|
static int restore_x, restore_y, restore_w, restore_h;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-07-15 21:31:54 +03:00
|
|
|
if (dragging_window) {
|
|
|
|
|
if (is_held) {
|
|
|
|
|
double dx = global_mouse_x - drag_start_global_x;
|
|
|
|
|
double dy = global_mouse_y - drag_start_global_y;
|
|
|
|
|
|
2025-07-15 21:54:05 +03:00
|
|
|
int new_x = drag_start_window_x + static_cast<int>(dx);
|
|
|
|
|
int new_y = drag_start_window_y + static_cast<int>(dy);
|
|
|
|
|
|
|
|
|
|
glfwSetWindowPos(window, new_x, new_y);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GLFWmonitor* primaryMonitor = glfwGetPrimaryMonitor();
|
|
|
|
|
int monitor_x, monitor_y, monitor_w, monitor_h;
|
|
|
|
|
if (glfwGetMonitorWorkarea) {
|
|
|
|
|
glfwGetMonitorWorkarea(primaryMonitor, &monitor_x, &monitor_y, &monitor_w, &monitor_h);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const int tolerance = 2;
|
|
|
|
|
if (!is_maximized && abs(new_y - monitor_y) <= tolerance) {
|
|
|
|
|
|
|
|
|
|
glfwGetWindowPos(window, &restore_x, &restore_y);
|
|
|
|
|
glfwGetWindowSize(window, &restore_w, &restore_h);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glfwSetWindowPos(window, monitor_x, monitor_y);
|
|
|
|
|
glfwSetWindowSize(window, monitor_w, monitor_h);
|
|
|
|
|
is_maximized = true;
|
|
|
|
|
dragging_window = false;
|
|
|
|
|
}
|
2025-07-15 21:31:54 +03:00
|
|
|
} else {
|
|
|
|
|
dragging_window = false;
|
|
|
|
|
}
|
2025-07-15 21:54:05 +03:00
|
|
|
|
|
|
|
|
if (!dragging_window && is_maximized) {
|
|
|
|
|
double mx, my;
|
|
|
|
|
glfwGetCursorPos(window, &mx, &my);
|
|
|
|
|
if (ImGui::IsMouseDown(0) && my < 10.0) {
|
|
|
|
|
// Start unmaximize drag
|
|
|
|
|
is_maximized = false;
|
|
|
|
|
glfwSetWindowSize(window, restore_w, restore_h);
|
|
|
|
|
glfwSetWindowPos(window,
|
|
|
|
|
static_cast<int>(global_mouse_x - restore_w / 2),
|
|
|
|
|
static_cast<int>(global_mouse_y - 10));
|
|
|
|
|
dragging_window = true;
|
|
|
|
|
drag_start_global_x = global_mouse_x;
|
|
|
|
|
drag_start_global_y = global_mouse_y;
|
|
|
|
|
drag_start_window_x = static_cast<int>(global_mouse_x - restore_w / 2);
|
|
|
|
|
drag_start_window_y = static_cast<int>(global_mouse_y - 10);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-15 20:56:12 +03:00
|
|
|
}
|
2025-07-15 20:42:47 +03:00
|
|
|
|
2025-07-15 21:54:05 +03:00
|
|
|
|
2025-07-15 21:31:54 +03:00
|
|
|
// --- MENU ITEMS ALWAYS RENDERED ---
|
|
|
|
|
if (ImGui::BeginMenu("File")) {
|
|
|
|
|
if (ImGui::MenuItem("Quit")) {
|
|
|
|
|
glfwSetWindowShouldClose(window, true);
|
|
|
|
|
}
|
|
|
|
|
ImGui::EndMenu();
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-15 21:54:05 +03:00
|
|
|
float window_widthA = ImGui::GetWindowWidth();
|
2025-07-15 21:31:54 +03:00
|
|
|
ImVec2 size = ImGui::CalcTextSize("x");
|
|
|
|
|
float button_width = size.x + ImGui::GetStyle().FramePadding.x * 2;
|
2025-07-15 21:54:05 +03:00
|
|
|
ImGui::SameLine(window_widthA - button_width - ImGui::GetStyle().ItemSpacing.x);
|
2025-07-15 21:31:54 +03:00
|
|
|
|
|
|
|
|
if (ImGui::Button("x")) {
|
2025-07-15 21:54:05 +03:00
|
|
|
|
2025-07-15 21:31:54 +03:00
|
|
|
std::cout << "Exiting..." << std::endl;
|
2025-07-15 20:42:47 +03:00
|
|
|
glfwSetWindowShouldClose(window, true);
|
|
|
|
|
}
|
2025-07-15 21:31:54 +03:00
|
|
|
ImGui::EndMainMenuBar();
|
|
|
|
|
|
|
|
|
|
|
2025-07-15 20:09:48 +03:00
|
|
|
}
|
2025-07-15 21:31:54 +03:00
|
|
|
|
|
|
|
|
|
2025-07-15 21:54:05 +03:00
|
|
|
GLFWmonitor* primaryMonitor = glfwGetPrimaryMonitor();
|
|
|
|
|
int monitor_x, monitor_y, monitor_width, monitor_height;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (glfwGetMonitorWorkarea) {
|
|
|
|
|
glfwGetMonitorWorkarea(primaryMonitor, &monitor_x, &monitor_y, &monitor_width, &monitor_height);
|
|
|
|
|
} else {
|
|
|
|
|
// Fallback
|
|
|
|
|
const GLFWvidmode* mode = glfwGetVideoMode(primaryMonitor);
|
|
|
|
|
glfwGetMonitorPos(primaryMonitor, &monitor_x, &monitor_y);
|
|
|
|
|
monitor_width = mode->width;
|
|
|
|
|
monitor_height = mode->height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int win_x, win_y;
|
|
|
|
|
glfwGetWindowPos(window, &win_x, &win_y);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const int tolerance = 5;
|
|
|
|
|
if (abs(win_y - monitor_y) <= tolerance) {
|
|
|
|
|
std::cout << "Window is at the top of the main screen!" << std::endl;
|
|
|
|
|
}
|
2025-07-15 21:31:54 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-07-15 20:09:48 +03:00
|
|
|
|
|
|
|
|
ImGui::Begin("Editor");
|
|
|
|
|
ImGui::Text("Hello from editor");
|
|
|
|
|
ImGui::End();
|
|
|
|
|
|
|
|
|
|
ImGui::Render();
|
|
|
|
|
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
|
|
|
|
glfwSwapBuffers(window);
|
2025-07-15 03:08:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glfwDestroyWindow(window);
|
|
|
|
|
glfwTerminate();
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|