From 8e4088280e6c1ab3d9aa9f63164617bf44755f65 Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Fri, 18 Oct 2024 00:16:39 +0200 Subject: [PATCH] Dim the main window when a popup is shown --- client/application.cpp | 3 +- client/render/imgui_impl.cpp | 380 +++++++++++++++++++++++------------ client/render/imgui_impl.h | 11 +- client/scenes/lobby.cpp | 36 +++- client/scenes/lobby.h | 1 + 5 files changed, 292 insertions(+), 139 deletions(-) diff --git a/client/application.cpp b/client/application.cpp index e9eddd30..e6f2f6b4 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -758,6 +758,7 @@ void application::initialize() opt_extensions.push_back(XR_EXT_PALM_POSE_EXTENSION_NAME); opt_extensions.push_back(XR_KHR_COMPOSITION_LAYER_DEPTH_EXTENSION_NAME); opt_extensions.push_back(XR_FB_COMPOSITION_LAYER_DEPTH_TEST_EXTENSION_NAME); + opt_extensions.push_back(XR_KHR_COMPOSITION_LAYER_COLOR_SCALE_BIAS_EXTENSION_NAME); for (const auto & i: interaction_profiles) { @@ -797,7 +798,7 @@ void application::initialize() spdlog::info(" Vendor ID: {:#x}", properties.vendorId); spdlog::info(" System name: {}", properties.systemName); spdlog::info(" Graphics properties:"); - spdlog::info(" Maximum swapchain image size: {}x{}", properties.graphicsProperties.maxSwapchainImageWidth, properties.graphicsProperties.maxSwapchainImageWidth); + spdlog::info(" Maximum swapchain image size: {}x{}", properties.graphicsProperties.maxSwapchainImageWidth, properties.graphicsProperties.maxSwapchainImageHeight); spdlog::info(" Maximum layer count: {}", properties.graphicsProperties.maxLayerCount); spdlog::info(" Tracking properties:"); spdlog::info(" Orientation tracking: {}", (bool)properties.trackingProperties.orientationTracking); diff --git a/client/render/imgui_impl.cpp b/client/render/imgui_impl.cpp index 858103ee..bb823c73 100644 --- a/client/render/imgui_impl.cpp +++ b/client/render/imgui_impl.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -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::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> imgui_context::ray_plane_intersection(const imgui_context::controller_state & in) const { if (!in.active) @@ -129,12 +187,7 @@ std::vector> 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 imgui_context::get_pointer_position_in_imgui_frame() +std::vector 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 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::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::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 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::infinity(); + for (ImGuiWindow * window: context->Windows) + { + distance = std::min(distance, distance_to_window(window, *position)); + } - float radius = 10; // std::clamp(distance_to_border / 4, 0, 10); - float alpha = std::clamp((distance_to_border - 10) / 50, 0, 0.8); + float radius = 10; + float alpha = std::clamp((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 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 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; +} diff --git a/client/render/imgui_impl.h b/client/render/imgui_impl.h index f26aeefa..e8ca47fd 100644 --- a/client/render/imgui_impl.h +++ b/client/render/imgui_impl.h @@ -75,14 +75,14 @@ public: glm::quat aim_orientation = {1, 0, 0, 0}; float trigger_value = 0; - float squeeze_value = 0; glm::vec2 scroll_value = {0, 0}; + std::optional pointer_position; float hover_distance = 1e10; bool squeeze_clicked = false; bool trigger_clicked = false; - bool fingertip_hovered = false; + bool fingertip_hovering = false; bool fingertip_touching = false; ImGuiMouseSource source = ImGuiMouseSource_Mouse; }; @@ -148,14 +148,14 @@ private: ImVector glyph_ranges; bool glyph_range_dirty = true; - std::vector> pointer_position; // first: coordinates from imgui pov, second: distance from the plane, sorted by increasing distance - #if WIVRN_SHOW_IMGUI_DEMO_WINDOW bool show_demo_window = true; #endif void initialize_fonts(); - std::optional get_pointer_position_in_imgui_frame(); + + std::vector read_controllers_state(XrTime display_time); + void compute_pointer_position(controller_state & state); public: imgui_context( @@ -191,4 +191,5 @@ public: void set_current(); void add_chars(std::string_view sv); + bool is_modal_popup_shown() const; }; diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index 63ee16b5..e3ae88de 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -186,6 +186,12 @@ scenes::lobby::lobby() spdlog::info("Composition layer depth test supported"); else spdlog::info("Composition layer depth test NOT supported"); + + composition_layer_color_scale_bias_supported = instance.has_extension(XR_KHR_COMPOSITION_LAYER_COLOR_SCALE_BIAS_EXTENSION_NAME); + if (composition_layer_color_scale_bias_supported) + spdlog::info("Composition layer color scale/bias supported"); + else + spdlog::info("Composition layer color scale/bias NOT supported"); } static std::string ip_address_to_string(const in_addr & addr) @@ -522,7 +528,6 @@ static std::pair, std::vector(&lobby_layer)); @@ -786,6 +808,12 @@ void scenes::lobby::render(const XrFrameState & frame_state) for (auto & layer: imgui_layers) layer.next = &layer_depth_test; + if (imgui_ctx->is_modal_popup_shown() and composition_layer_color_scale_bias_supported) + { + color_scale_bias.next = imgui_layers.front().next; + imgui_layers.front().next = &color_scale_bias; + } + session.end_frame(frame_state.predictedDisplayTime, layers_base, blend_mode); } else @@ -799,6 +827,12 @@ void scenes::lobby::render(const XrFrameState & frame_state) if (not composition_layer_depth_test_supported) layers_base.push_back(reinterpret_cast(&controllers_layer)); + if (imgui_ctx->is_modal_popup_shown() and composition_layer_color_scale_bias_supported) + { + color_scale_bias.next = imgui_layers.front().next; + imgui_layers.front().next = &color_scale_bias; + } + session.end_frame(frame_state.predictedDisplayTime, layers_base, blend_mode); } } diff --git a/client/scenes/lobby.h b/client/scenes/lobby.h index cdf1c0ef..f4e10028 100644 --- a/client/scenes/lobby.h +++ b/client/scenes/lobby.h @@ -67,6 +67,7 @@ class lobby : public scene_impl std::optional right_hand; node_handle lobby_handle; bool composition_layer_depth_test_supported; + bool composition_layer_color_scale_bias_supported; std::optional imgui_ctx; std::array haptic_output;