Dim the main window when a popup is shown
This commit is contained in:
+248
-132
@@ -35,6 +35,7 @@
|
||||
#include <glm/gtc/matrix_access.hpp>
|
||||
#include <imgui.h>
|
||||
#include <imgui_internal.h>
|
||||
#include <limits>
|
||||
#include <optional>
|
||||
#include <ranges>
|
||||
#include <spdlog/spdlog.h>
|
||||
@@ -118,6 +119,63 @@ static void check_vk_result(VkResult result)
|
||||
}
|
||||
}
|
||||
|
||||
static bool in_window(ImGuiWindow * window, ImVec2 position)
|
||||
{
|
||||
if (window->Hidden or not window->Active)
|
||||
return false;
|
||||
|
||||
if (window->Pos.x > position.x or
|
||||
window->Pos.y > position.y or
|
||||
window->Pos.x + window->Size.x < position.x or
|
||||
window->Pos.y + window->Size.y < position.y)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool in_viewport(const imgui_context::viewport & viewport, ImVec2 position)
|
||||
{
|
||||
if (viewport.vp_origin.x > position.x or
|
||||
viewport.vp_origin.y > position.y or
|
||||
viewport.vp_origin.x + viewport.vp_size.x < position.x or
|
||||
viewport.vp_origin.y + viewport.vp_size.y < position.y)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool window_intersects_viewport(ImGuiWindow * window, imgui_context::viewport & viewport)
|
||||
{
|
||||
ImRect w{window->Pos.x, window->Pos.y, window->Pos.x + window->Size.x, window->Pos.y + window->Size.y};
|
||||
ImRect v(viewport.vp_origin.x, viewport.vp_origin.y, viewport.vp_origin.x + viewport.vp_size.x, viewport.vp_origin.y + viewport.vp_size.y);
|
||||
|
||||
return w.Overlaps(v);
|
||||
}
|
||||
|
||||
static float distance_to_window(ImGuiWindow * window, ImVec2 position)
|
||||
{
|
||||
if (window->Hidden or not window->Active)
|
||||
return std::numeric_limits<float>::infinity();
|
||||
|
||||
float dx;
|
||||
if (position.x < window->Pos.x)
|
||||
dx = window->Pos.x - position.x;
|
||||
else if (position.x > window->Pos.x + window->Size.x)
|
||||
dx = position.x - (window->Pos.x + window->Size.x);
|
||||
else
|
||||
dx = 0;
|
||||
|
||||
float dy;
|
||||
if (position.y < window->Pos.y)
|
||||
dy = window->Pos.y - position.y;
|
||||
else if (position.y > window->Pos.y + window->Size.y)
|
||||
dy = position.y - (window->Pos.y + window->Size.y);
|
||||
else
|
||||
dy = 0;
|
||||
|
||||
return std::hypot(dx, dy);
|
||||
}
|
||||
|
||||
std::vector<std::pair<ImVec2, float>> imgui_context::ray_plane_intersection(const imgui_context::controller_state & in) const
|
||||
{
|
||||
if (!in.active)
|
||||
@@ -129,12 +187,7 @@ std::vector<std::pair<ImVec2, float>> imgui_context::ray_plane_intersection(cons
|
||||
{
|
||||
auto M = glm::transpose(glm::mat3_cast(i.orientation)); // world-to-plane transform
|
||||
|
||||
glm::quat q;
|
||||
if (std::abs(glm::length(in.aim_orientation) - 1) < 0.01)
|
||||
q = in.aim_orientation;
|
||||
else
|
||||
q = i.orientation;
|
||||
|
||||
glm::quat q = (in.source == ImGuiMouseSource_VRHandTracking) ? i.orientation : in.aim_orientation;
|
||||
glm::vec3 controller_direction = glm::column(glm::mat3_cast(q), 2);
|
||||
|
||||
// Compute all vectors in the reference frame of the GUI plane
|
||||
@@ -540,49 +593,14 @@ void imgui_context::initialize_fonts()
|
||||
ImGui_ImplVulkan_CreateFontsTexture();
|
||||
}
|
||||
|
||||
std::optional<ImVec2> imgui_context::get_pointer_position_in_imgui_frame()
|
||||
std::vector<imgui_context::controller_state> imgui_context::read_controllers_state(XrTime display_time)
|
||||
{
|
||||
if (pointer_position.empty())
|
||||
return {};
|
||||
|
||||
for (auto [pos, distance]: pointer_position)
|
||||
{
|
||||
for (ImGuiWindow * window: context->Windows)
|
||||
{
|
||||
if (window->Hidden or not window->Active)
|
||||
continue;
|
||||
|
||||
if (window->Pos.x > pos.x)
|
||||
continue;
|
||||
if (window->Pos.y > pos.y)
|
||||
continue;
|
||||
if (window->Pos.x + window->Size.x < pos.x)
|
||||
continue;
|
||||
if (window->Pos.y + window->Size.y < pos.y)
|
||||
continue;
|
||||
|
||||
return pos;
|
||||
}
|
||||
}
|
||||
|
||||
return pointer_position.back().first;
|
||||
}
|
||||
|
||||
void imgui_context::new_frame(XrTime display_time)
|
||||
{
|
||||
ImGui::SetCurrentContext(context);
|
||||
ImPlot::SetCurrentContext(plot_context);
|
||||
|
||||
if (last_display_time)
|
||||
io.DeltaTime = std::min((display_time - last_display_time) * 1e-9f, 0.1f);
|
||||
last_display_time = display_time;
|
||||
|
||||
float scroll_scale = io.DeltaTime * 3;
|
||||
|
||||
size_t new_focused_controller = focused_controller;
|
||||
|
||||
std::vector<controller_state> new_states;
|
||||
|
||||
// Get the hand/controller state from OpenXR
|
||||
for (auto && [index, controller]: utils::enumerate(controllers))
|
||||
{
|
||||
auto & [ctrl, state] = controller;
|
||||
@@ -600,33 +618,13 @@ void imgui_context::new_frame(XrTime display_time)
|
||||
index_tip.pose.position.x,
|
||||
index_tip.pose.position.y,
|
||||
index_tip.pose.position.z};
|
||||
|
||||
// Use the GUI plane as orientation to have the finger point perpendicularly
|
||||
// to the plane instead of following the finger direction.
|
||||
// ray_plane_intersection() interprets a null quaternion as a request to use the layer
|
||||
// orientation.
|
||||
new_state.aim_orientation = glm::quat(0, 0, 0, 0);
|
||||
// aim_orientation is ignored by ray_plane_intersection() for hands
|
||||
|
||||
new_state.active = true;
|
||||
new_state.source = ImGuiMouseSource_VRHandTracking;
|
||||
auto position_distance = ray_plane_intersection(new_state);
|
||||
|
||||
if (not position_distance.empty())
|
||||
{
|
||||
new_state.hover_distance = position_distance.front().second;
|
||||
|
||||
if (std::abs(position_distance.front().second) < 0.1f)
|
||||
new_state.fingertip_hovered = true;
|
||||
else
|
||||
new_state.active = false;
|
||||
|
||||
if (new_state.fingertip_hovered && (new_state.hover_distance < 0.02))
|
||||
new_state.fingertip_touching = true;
|
||||
}
|
||||
else
|
||||
new_state.hover_distance = 1e10;
|
||||
}
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -637,52 +635,135 @@ void imgui_context::new_frame(XrTime display_time)
|
||||
new_state.active = true;
|
||||
new_state.aim_position = location->first;
|
||||
new_state.aim_orientation = location->second;
|
||||
}
|
||||
|
||||
if (ctrl.squeeze)
|
||||
{
|
||||
auto squeeze = application::read_action_float(ctrl.squeeze).value_or(std::pair{0, 0});
|
||||
new_state.squeeze_value = squeeze.second;
|
||||
if (ctrl.trigger)
|
||||
{
|
||||
auto trigger = application::read_action_float(ctrl.trigger).value_or(std::pair{0, 0});
|
||||
new_state.trigger_value = trigger.second;
|
||||
|
||||
// TODO tunable
|
||||
if (new_state.squeeze_value < 0.5)
|
||||
new_state.squeeze_clicked = false;
|
||||
else if (new_state.squeeze_value > 0.8)
|
||||
new_state.squeeze_clicked = true;
|
||||
}
|
||||
// TODO tunable
|
||||
/*if (new_state.trigger_value < 0.5)
|
||||
new_state.trigger_clicked = false;
|
||||
else */
|
||||
if (new_state.trigger_value > 0.8)
|
||||
new_state.trigger_clicked = true;
|
||||
}
|
||||
|
||||
if (ctrl.trigger)
|
||||
{
|
||||
auto trigger = application::read_action_float(ctrl.trigger).value_or(std::pair{0, 0});
|
||||
new_state.trigger_value = trigger.second;
|
||||
|
||||
// TODO tunable
|
||||
if (new_state.trigger_value < 0.5)
|
||||
new_state.trigger_clicked = false;
|
||||
else if (new_state.trigger_value > 0.8)
|
||||
new_state.trigger_clicked = true;
|
||||
}
|
||||
|
||||
if (ctrl.scroll)
|
||||
{
|
||||
if (auto act = application::read_action_vec2(ctrl.scroll); act)
|
||||
new_state.scroll_value = {-act->second.x * scroll_scale, act->second.y * scroll_scale};
|
||||
else
|
||||
new_state.scroll_value = {0, 0};
|
||||
if (ctrl.scroll)
|
||||
{
|
||||
if (auto act = application::read_action_vec2(ctrl.scroll); act)
|
||||
new_state.scroll_value = {-act->second.x * scroll_scale, act->second.y * scroll_scale};
|
||||
else
|
||||
new_state.scroll_value = {0, 0};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float closest_hover_distance = 1e10;
|
||||
for (auto && [index, new_state]: utils::enumerate(new_states))
|
||||
// Compute the position in imgui frame according to the currently displayed windows (from the last frame)
|
||||
for (auto & state: new_states)
|
||||
{
|
||||
if (std::abs(new_state.hover_distance) < closest_hover_distance && new_state.fingertip_hovered)
|
||||
compute_pointer_position(state);
|
||||
|
||||
if (state.source == ImGuiMouseSource_VRHandTracking)
|
||||
{
|
||||
new_focused_controller = index;
|
||||
closest_hover_distance = std::abs(new_state.hover_distance);
|
||||
// TODO tunable
|
||||
if (std::abs(state.hover_distance) < 0.1)
|
||||
state.fingertip_hovering = true;
|
||||
|
||||
if (std::abs(state.hover_distance) < 0.02)
|
||||
state.fingertip_touching = true;
|
||||
}
|
||||
else if (new_state.squeeze_clicked || new_state.trigger_clicked || glm::length(new_state.scroll_value) > 0.01f)
|
||||
}
|
||||
|
||||
return new_states;
|
||||
}
|
||||
|
||||
void imgui_context::compute_pointer_position(imgui_context::controller_state & state)
|
||||
{
|
||||
auto intersections = ray_plane_intersection(state);
|
||||
|
||||
if (intersections.empty())
|
||||
{
|
||||
state.hover_distance = std::numeric_limits<float>::infinity();
|
||||
state.pointer_position = std::nullopt;
|
||||
return;
|
||||
}
|
||||
|
||||
if (ImGuiWindow * modal_popup = ImGui::GetTopMostAndVisiblePopupModal())
|
||||
{
|
||||
// If there is a popup window, use the viewport of that window
|
||||
for (auto & i: layers_)
|
||||
{
|
||||
new_focused_controller = index;
|
||||
if (window_intersects_viewport(modal_popup, i))
|
||||
{
|
||||
for (auto [position, distance]: intersections)
|
||||
{
|
||||
if (in_viewport(i, position))
|
||||
{
|
||||
state.hover_distance = distance;
|
||||
state.pointer_position = position;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
state.hover_distance = std::numeric_limits<float>::infinity();
|
||||
state.pointer_position = std::nullopt;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Intersections are sorted by distance: take the closest one
|
||||
for (auto [position, distance]: intersections)
|
||||
{
|
||||
for (ImGuiWindow * window: context->Windows)
|
||||
{
|
||||
if (not window->Active or window->Hidden)
|
||||
continue;
|
||||
|
||||
if (in_window(window, position))
|
||||
{
|
||||
state.hover_distance = distance;
|
||||
state.pointer_position = position;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If the pointer isn't in any window, take the farthest one
|
||||
std::tie(state.pointer_position, state.hover_distance) = intersections.back();
|
||||
}
|
||||
}
|
||||
|
||||
void imgui_context::new_frame(XrTime display_time)
|
||||
{
|
||||
ImGui::SetCurrentContext(context);
|
||||
ImPlot::SetCurrentContext(plot_context);
|
||||
|
||||
if (last_display_time)
|
||||
io.DeltaTime = std::min((display_time - last_display_time) * 1e-9f, 0.1f);
|
||||
last_display_time = display_time;
|
||||
|
||||
size_t new_focused_controller = focused_controller;
|
||||
|
||||
// Uses the window list from last frame
|
||||
auto new_states = read_controllers_state(display_time);
|
||||
|
||||
// Set the currently active controller
|
||||
for (auto && [index, state]: utils::enumerate(new_states))
|
||||
{
|
||||
if (state.source == ImGuiMouseSource_VRController)
|
||||
{
|
||||
if (state.trigger_clicked or glm::length(state.scroll_value) > 0.01f)
|
||||
{
|
||||
new_focused_controller = index;
|
||||
}
|
||||
}
|
||||
|
||||
if (state.source == ImGuiMouseSource_VRHandTracking)
|
||||
{
|
||||
if (state.fingertip_hovering)
|
||||
new_focused_controller = index;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -702,7 +783,6 @@ void imgui_context::new_frame(XrTime display_time)
|
||||
|
||||
if (new_focused_controller != (size_t)-1)
|
||||
{
|
||||
pointer_position = ray_plane_intersection(new_states[new_focused_controller]);
|
||||
auto scroll = new_states[new_focused_controller].scroll_value;
|
||||
|
||||
bool last_trigger = controllers[new_focused_controller].second.trigger_clicked;
|
||||
@@ -711,7 +791,7 @@ void imgui_context::new_frame(XrTime display_time)
|
||||
bool last_touching = controllers[new_focused_controller].second.fingertip_touching;
|
||||
fingertip_touching = new_states[new_focused_controller].fingertip_touching;
|
||||
|
||||
if (auto position = get_pointer_position_in_imgui_frame())
|
||||
if (auto position = new_states[new_focused_controller].pointer_position)
|
||||
{
|
||||
io.AddMousePosEvent(position->x, position->y);
|
||||
|
||||
@@ -731,10 +811,6 @@ void imgui_context::new_frame(XrTime display_time)
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
pointer_position.clear();
|
||||
}
|
||||
|
||||
focused_controller = new_focused_controller;
|
||||
for (auto && [controller, next_state]: std::views::zip(controllers, new_states))
|
||||
@@ -786,37 +862,64 @@ std::vector<XrCompositionLayerQuad> imgui_context::end_frame()
|
||||
ImGui::SetCurrentContext(context);
|
||||
ImPlot::SetCurrentContext(plot_context);
|
||||
|
||||
if (auto position = get_pointer_position_in_imgui_frame())
|
||||
if (auto position = controllers[focused_controller].second.pointer_position)
|
||||
{
|
||||
// Clip in the right plane
|
||||
ImVec2 clip_rect_min(0, 0);
|
||||
ImVec2 clip_rect_max(size.width, size.height);
|
||||
|
||||
for (auto & i: layers_)
|
||||
// If there is a modal popup, only display the cursor in the viewport of the popup
|
||||
ImGuiWindow * modal_popup = ImGui::GetTopMostAndVisiblePopupModal();
|
||||
if (modal_popup)
|
||||
{
|
||||
if (position->x < i.vp_origin.x)
|
||||
continue;
|
||||
if (position->y < i.vp_origin.y)
|
||||
continue;
|
||||
if (position->x > i.vp_origin.x + i.vp_size.x)
|
||||
continue;
|
||||
if (position->y > i.vp_origin.y + i.vp_size.y)
|
||||
continue;
|
||||
for (auto & i: layers_)
|
||||
{
|
||||
if (window_intersects_viewport(modal_popup, i))
|
||||
{
|
||||
if (position->x < i.vp_origin.x or
|
||||
position->y < i.vp_origin.y or
|
||||
position->x > i.vp_origin.x + i.vp_size.x or
|
||||
position->y > i.vp_origin.y + i.vp_size.y)
|
||||
{
|
||||
// Cursor is not in the same viewport as the popup
|
||||
clip_rect_min = ImVec2(0, 0);
|
||||
clip_rect_max = ImVec2(0, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
clip_rect_min = ImVec2(i.vp_origin.x + 1, i.vp_origin.y + 1);
|
||||
clip_rect_max = ImVec2(i.vp_origin.x + i.vp_size.x, i.vp_origin.y + i.vp_size.y);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (auto & i: layers_)
|
||||
{
|
||||
if (position->x < i.vp_origin.x or
|
||||
position->y < i.vp_origin.y or
|
||||
position->x > i.vp_origin.x + i.vp_size.x or
|
||||
position->y > i.vp_origin.y + i.vp_size.y)
|
||||
continue;
|
||||
|
||||
clip_rect_min = ImVec2(i.vp_origin.x + 1, i.vp_origin.y + 1);
|
||||
clip_rect_max = ImVec2(i.vp_origin.x + i.vp_size.x, i.vp_origin.y + i.vp_size.y);
|
||||
clip_rect_min = ImVec2(i.vp_origin.x + 1, i.vp_origin.y + 1);
|
||||
clip_rect_max = ImVec2(i.vp_origin.x + i.vp_size.x, i.vp_origin.y + i.vp_size.y);
|
||||
|
||||
break;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO fix according to layers
|
||||
float distance_to_border = std::min({position->x,
|
||||
size.width - position->x,
|
||||
position->y,
|
||||
size.height - position->y});
|
||||
// Compute the distance to the closest window
|
||||
float distance = std::numeric_limits<float>::infinity();
|
||||
for (ImGuiWindow * window: context->Windows)
|
||||
{
|
||||
distance = std::min(distance, distance_to_window(window, *position));
|
||||
}
|
||||
|
||||
float radius = 10; // std::clamp<float>(distance_to_border / 4, 0, 10);
|
||||
float alpha = std::clamp<float>((distance_to_border - 10) / 50, 0, 0.8);
|
||||
float radius = 10;
|
||||
float alpha = std::clamp<float>((40 - distance) / 50, 0, 0.8);
|
||||
|
||||
ImU32 color_pressed = ImGui::GetColorU32(ImVec4(0, 0.2, 1, alpha));
|
||||
ImU32 color_unpressed = ImGui::GetColorU32(ImVec4(1, 1, 1, alpha));
|
||||
@@ -830,11 +933,6 @@ std::vector<XrCompositionLayerQuad> imgui_context::end_frame()
|
||||
draw_list->PopClipRect();
|
||||
}
|
||||
|
||||
if (not context->OpenPopupStack.empty())
|
||||
{
|
||||
// TODO dim main window
|
||||
}
|
||||
|
||||
ImGui::Render();
|
||||
|
||||
current_command_buffer = (current_command_buffer + 1) % command_buffers.size();
|
||||
@@ -880,6 +978,19 @@ std::vector<XrCompositionLayerQuad> imgui_context::end_frame()
|
||||
|
||||
for (auto & i: layers_)
|
||||
{
|
||||
bool visible = false;
|
||||
for (ImGuiWindow * window: context->Windows)
|
||||
{
|
||||
if (window->Active and not window->Hidden and window_intersects_viewport(window, i))
|
||||
{
|
||||
visible = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (not visible)
|
||||
continue;
|
||||
|
||||
quads.push_back(XrCompositionLayerQuad{
|
||||
.type = XR_TYPE_COMPOSITION_LAYER_QUAD,
|
||||
.layerFlags = XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT,
|
||||
@@ -1008,3 +1119,8 @@ void imgui_context::set_current()
|
||||
ImGui::SetCurrentContext(context);
|
||||
ImPlot::SetCurrentContext(plot_context);
|
||||
}
|
||||
|
||||
bool imgui_context::is_modal_popup_shown() const
|
||||
{
|
||||
return ImGui::GetTopMostAndVisiblePopupModal() != nullptr;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user