Skip to content

Commit

Permalink
Allow mouse intersection tests to hit any ImGui window
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Oct 30, 2023
1 parent f4d7df9 commit 9b9fcd1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,7 @@ list(APPEND ue4poc_SOURCES
"src/ue4poc-imgui/imgui_impl_dx11.cpp"
"src/ue4poc-imgui/imgui_impl_dx12.cpp"
"src/ue4poc-imgui/imgui_impl_win32.cpp"
"src/utility/ImGui.cpp"
"src/ExceptionHandler.hpp"
"src/Framework.hpp"
"src/LicenseStrings.hpp"
Expand Down Expand Up @@ -835,6 +836,7 @@ list(APPEND ue4poc_SOURCES
"src/mods/vr/vtables/IXRTrackingSystemVTables.hpp"
"src/ue4poc-imgui/font_robotomedium.hpp"
"src/ue4poc-imgui/ue4poc_imconfig.hpp"
"src/utility/ImGui.hpp"
"src/utility/Logging.hpp"
"src/ue4poc-imgui/imgui_impl_dx11.h"
"src/ue4poc-imgui/imgui_impl_dx12.h"
Expand Down
5 changes: 2 additions & 3 deletions src/mods/vr/OverlayComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "Framework.hpp"
#include "../VR.hpp"
#include "../utility/ImGui.hpp"

#include "OverlayComponent.hpp"

Expand Down Expand Up @@ -101,9 +102,7 @@ void OverlayComponent::on_pre_imgui_frame() {
// lerp towards the intersection point
last_mouse_pos = glm::lerp(last_mouse_pos, glm::vec2{x, y}, delta_f * 10.0f);

if (last_mouse_pos.x >= window_pos.x && last_mouse_pos.x <= window_pos.x + window_size.x &&
last_mouse_pos.y >= window_pos.y && last_mouse_pos.y <= window_pos.y + window_size.y)
{
if (imgui::is_point_intersecting_any(last_mouse_pos.x, last_mouse_pos.y)) {
io.MousePos = ImVec2{
last_mouse_pos.x,
last_mouse_pos.y
Expand Down
34 changes: 34 additions & 0 deletions src/utility/ImGui.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <imgui.h>
#include <imgui_internal.h>

#include <glm/vec2.hpp>

#include "ImGui.hpp"

namespace imgui {
bool is_point_intersecting_any(float x, float y) {
const ImGuiIO& io = ImGui::GetIO();
const auto ctx = io.Ctx;

if (ctx == nullptr) {
return false;
}

for (int n = 0; n < ctx->Windows.Size; n++) {
const auto window = ctx->Windows[n];
const auto pos = glm::vec2{window->Pos.x, window->Pos.y};
const auto size = glm::vec2{window->Size.x, window->Size.y};
const auto mx = pos + size;

if (window->WasActive && window->Active) {
if (x >= window->Pos.x && x <= window->Pos.x + window->Size.x &&
y >= window->Pos.y && y <= window->Pos.y + window->Size.y)
{
return true;
}
}
}

return false;
}
}
5 changes: 5 additions & 0 deletions src/utility/ImGui.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

namespace imgui {
bool is_point_intersecting_any(float x, float y);
}

0 comments on commit 9b9fcd1

Please sign in to comment.