Fix mouse interaction when hovering GUI

This commit is contained in:
Phireh 2023-10-21 20:53:58 +02:00
commit 6741e0ae9b
2 changed files with 19 additions and 15 deletions

View file

@ -1,5 +1,5 @@
[Window][Debug##Default]
Pos=60,60
Pos=38,60
Size=400,400
Collapsed=0
@ -9,8 +9,8 @@ Size=550,680
Collapsed=0
[Window][Hi there]
Pos=77,92
Size=450,349
Pos=158,190
Size=422,452
Collapsed=0
[Table][0xC9935533,3]

View file

@ -386,6 +386,7 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
double cursor_y;
camera_t the_camera;
bool mouse_pressed;
bool mouse_over_gui;
the_camera.position = glm::vec3(0.0f, 0.0f, 1.0f);
the_camera.size = glm::vec2(1.0f, 1.0f);
@ -425,13 +426,24 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
}
mouse_pressed = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS;
mouse_over_gui = ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem);
cursor_dx = cursor_x;
cursor_dy = cursor_y;
glfwGetCursorPos(window, &cursor_x, &cursor_y);
cursor_dx -= cursor_x;
cursor_dy -= cursor_y;
// Mouse movement
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS)
if (!mouse_over_gui && glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS)
{
fprintf(stdout, "Moving camera by %f %f\n", cursor_dx, cursor_dx);
// mouse delta!
the_camera.position.x += cursor_dx;
the_camera.position.y += cursor_dy;
the_camera.position.x += cursor_dx * the_camera.size.x/window_width;
// The Y coord is inverted
the_camera.position.y -= cursor_dy * the_camera.size.y/window_height;
}
if (the_camera.size.x < 0.1)
@ -439,14 +451,6 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
if (the_camera.size.y < 0.1)
the_camera.size.y = 0.1;
cursor_dx = cursor_x;
cursor_dy = cursor_y;
cursor_dx -= cursor_x;
cursor_dy -= cursor_y;
glfwGetCursorPos(window, &cursor_x, &cursor_y);
/* Rendering */
glClearColor(0.1f, 0.2f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
@ -475,7 +479,7 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
for (int j = 0; j < grid.columns; j++)
{
int idx = i * grid.columns + j;
if (inside_hex(&grid.hexes[idx], cursor_world)) // draw hovered hex differently
if (!mouse_over_gui && inside_hex(&grid.hexes[idx], cursor_world)) // draw hovered hex differently
{
hovered_hex = i * grid.columns + j;
if (mouse_pressed)