From b5d4f678602c083c5850ed34211d93479f920945 Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Tue, 10 Feb 2026 21:35:03 +0100 Subject: [PATCH] Put the tooltips in a different layer --- client/constants.h | 5 ++-- client/render/imgui_impl.cpp | 50 +++++++++++++++++++++--------------- client/render/imgui_impl.h | 1 + client/scenes/lobby.cpp | 12 ++++++++- client/scenes/stream.cpp | 25 ++++++++++++------ client/scenes/stream_gui.cpp | 9 ++++--- 6 files changed, 67 insertions(+), 35 deletions(-) diff --git a/client/constants.h b/client/constants.h index 60f24d5f..eb64fdf8 100644 --- a/client/constants.h +++ b/client/constants.h @@ -108,7 +108,8 @@ constexpr int zindex_passthrough = -2; constexpr int zindex_lobby = -1; constexpr int zindex_gui = 0; constexpr int zindex_controllers = 1; -constexpr int zindex_recenter_tip = 2; +constexpr int zindex_tooltip = 2; +constexpr int zindex_recenter_tip = 3; } // namespace constants::lobby namespace constants::stream @@ -129,7 +130,7 @@ constexpr float window_rounding = 10; constexpr ImVec2 tooltip_padding = {5, 5}; constexpr float tooltip_rounding = 0; -constexpr float tooltip_distance = 10; +constexpr glm::vec3 tooltip_distance = {0, 0.004, 0.005}; constexpr ImVec2 button_size = {220, 80}; constexpr ImVec2 icon_button_size = {80, 80}; diff --git a/client/render/imgui_impl.cpp b/client/render/imgui_impl.cpp index 722d42d9..c1c315e2 100644 --- a/client/render/imgui_impl.cpp +++ b/client/render/imgui_impl.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -214,7 +215,7 @@ std::vector> imgui_context::ray_plane_intersection(cons for (const auto & i: layers_) { - if (i.space != xr::spaces::world) + if (i.space != xr::spaces::world or i.tooltip_viewport) continue; auto M = glm::transpose(glm::mat3_cast(i.orientation)); // world-to-plane transform @@ -1227,34 +1228,31 @@ void imgui_context::tooltip(std::string_view text) // FIXME: this is incorrect if we use the docking branch of imgui ImGuiViewport * viewport = ImGui::GetMainViewport(); auto & current_layer = layer(ImGui::GetMousePos()); - auto pos_backup = viewport->Pos; - auto size_backup = viewport->Size; - viewport->Pos = ImVec2(current_layer.vp_origin.x, current_layer.vp_origin.y); - viewport->Size = ImVec2(current_layer.vp_size.x, current_layer.vp_size.y); - ImVec2 pos{ + assert(std::ranges::contains(layers_, true, &viewport::tooltip_viewport)); + auto & tooltip_layer = *std::ranges::find(layers_, true, &viewport::tooltip_viewport); + + // Get the item position before drawing the tooltip (top center) + ImVec2 item_position{ (ImGui::GetItemRectMin().x + ImGui::GetItemRectMax().x) / 2, - ImGui::GetItemRectMin().y - constants::style::tooltip_distance, + ImGui::GetItemRectMin().y, }; + auto pos_backup = viewport->Pos; + auto size_backup = viewport->Size; + viewport->Pos = ImVec2(tooltip_layer.vp_origin.x, tooltip_layer.vp_origin.y); + viewport->Size = ImVec2(tooltip_layer.vp_size.x, tooltip_layer.vp_size.y); + + // Draw the tooltip in the tooltip layer + // Clamp position to avoid overflowing on the left or the right ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, constants::style::tooltip_padding); ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, constants::style::tooltip_rounding); - // Clamp position to avoid overflowing on the left or the right auto & style = ImGui::GetStyle(); const ImVec2 text_size = ImGui::CalcTextSize(text.data(), text.data() + text.size(), true); - const ImVec2 size = {text_size.x + style.WindowPadding.x * 2.0f, text_size.y + style.WindowPadding.y * 2.0f}; - pos.x = std::clamp(pos.x, viewport->Pos.x + size.x / 2, viewport->Pos.x + viewport->Size.x - size.x / 2); - ImVec2 pivot = {0.5, 1}; + const ImVec2 tooltip_size = {text_size.x + style.WindowPadding.x * 2.0f, text_size.y + style.WindowPadding.y * 2.0f}; - // Move tooltip below the item if it overflows on the top - if (pos.y - size.y <= viewport->Pos.y) - { - pos.y = ImGui::GetItemRectMax().y + constants::style::tooltip_distance; - pivot.y = 0; - } - - ImGui::SetNextWindowPos(pos, ImGuiCond_Always, pivot); + ImGui::SetNextWindowPos(viewport->Pos, ImGuiCond_Always); if (ImGui::BeginTooltip()) { ImGui::PushStyleColor(ImGuiCol_Text, 0xffffffff); @@ -1262,11 +1260,23 @@ void imgui_context::tooltip(std::string_view text) ImGui::PopStyleColor(); ImGui::EndTooltip(); } - ImGui::PopStyleVar(2); viewport->Pos = pos_backup; viewport->Size = size_backup; + + // Compute the tooltip position + glm::quat tooltip_orientation = current_layer.orientation; + auto M = glm::mat3_cast(tooltip_orientation); + + float pixel_size = current_layer.size.y / current_layer.vp_size.y; + glm::vec3 tooltip_position_centre = rw_from_vp(item_position) + M * (glm::vec3(0, tooltip_size.y / 2, 0) * pixel_size + constants::style::tooltip_distance); + + // Position the tooltip layer + tooltip_layer.position = tooltip_position_centre; + tooltip_layer.orientation = tooltip_orientation; + tooltip_layer.vp_size = {tooltip_size.x, tooltip_size.y}; + tooltip_layer.size = {tooltip_size.x * pixel_size, tooltip_size.y * pixel_size}; } // https://github.com/ocornut/imgui/issues/3379#issuecomment-2943903877 diff --git a/client/render/imgui_impl.h b/client/render/imgui_impl.h index 086392fd..b659dd18 100644 --- a/client/render/imgui_impl.h +++ b/client/render/imgui_impl.h @@ -136,6 +136,7 @@ public: glm::ivec2 vp_size; bool always_show_cursor = false; // Show the cursor in this viewport even if there is a modal popup elsewhere (eg. this is a virtual keyboard) + bool tooltip_viewport = false; // Choose this viewport to display the tooltip, ignore mouse events int z_index = 0; diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index 46b7c9fc..a99887cb 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -1245,7 +1245,17 @@ void scenes::lobby::on_focused() .vp_origin = {0, 1000}, .vp_size = {1500, 300}, .z_index = constants::lobby::zindex_recenter_tip, - }}; + }, + { + // Tooltip + .space = xr::spaces::world, + .size = {0.6, 0.08}, + .vp_origin = {0, 1300}, + .vp_size = {1500, 200}, + .tooltip_viewport = true, + .z_index = constants::lobby::zindex_tooltip, + }, + }; xr::swapchain swapchain_imgui(instance, session, device, swapchain_format, 3000, 1500); diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index ef9754fc..0222ae67 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -476,14 +476,23 @@ void scenes::stream::on_focused() device, swapchain_format, 1800, - 1000); + 1200); - imgui_context::viewport vp{ - .space = xr::spaces::world, - // Position and orientation are set at each frame - .size = {1.2, 0.6666}, - .vp_origin = {0, 0}, - .vp_size = {1800, 1000}, + std::vector vps{ + { + .space = xr::spaces::world, + // Position and orientation are set at each frame + .size = {1.2, 0.6666}, + .vp_origin = {0, 0}, + .vp_size = {1800, 1000}, + }, + { + .space = xr::spaces::world, + .size = {1.2, 0.1333}, + .vp_origin = {0, 1000}, + .vp_size = {1800, 200}, + .tooltip_viewport = true, + }, }; imgui_ctx.emplace(physical_device, @@ -492,7 +501,7 @@ void scenes::stream::on_focused() queue, imgui_inputs, std::move(swapchain_imgui), - std::vector{vp}, + std::move(vps), image_cache); if (application::get_config().enable_stream_gui) diff --git a/client/scenes/stream_gui.cpp b/client/scenes/stream_gui.cpp index feaef556..78d44f3a 100644 --- a/client/scenes/stream_gui.cpp +++ b/client/scenes/stream_gui.cpp @@ -835,7 +835,8 @@ void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicte const ImGuiStyle & style = ImGui::GetStyle(); imgui_ctx->new_frame(predicted_display_time); - ImVec2 content_size{ImGui::GetMainViewport()->Size - ImVec2{tab_width, 0} - margin_around_window * 2}; + ImVec2 viewport_size(imgui_ctx->layers()[0].vp_size.x, imgui_ctx->layers()[0].vp_size.y); + ImVec2 content_size{viewport_size - ImVec2{tab_width, 0} - margin_around_window * 2}; ImVec2 content_center = margin_around_window + content_size / 2 + ImVec2{tab_width, 0}; bool display_tabs, always_auto_resize; @@ -851,7 +852,7 @@ void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicte case gui_status::hidden: case gui_status::bitrate_settings: case gui_status::foveation_settings: - ImGui::SetNextWindowPos(ImGui::GetMainViewport()->Size / 2, ImGuiCond_Always, ImVec2(0.5f, 0.5f)); + ImGui::SetNextWindowPos(viewport_size / 2, ImGuiCond_Always, ImVec2(0.5f, 0.5f)); always_auto_resize = true; display_tabs = false; break; @@ -866,13 +867,13 @@ void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicte case gui_status::settings: case gui_status::applications: ImGui::SetNextWindowPos(margin_around_window); - ImGui::SetNextWindowSize(ImGui::GetMainViewport()->Size - margin_around_window * 2); + ImGui::SetNextWindowSize(viewport_size - margin_around_window * 2); always_auto_resize = false; display_tabs = true; break; case gui_status::application_launcher: ImGui::SetNextWindowPos(margin_around_window); - ImGui::SetNextWindowSize(ImGui::GetMainViewport()->Size - margin_around_window * 2); + ImGui::SetNextWindowSize(viewport_size - margin_around_window * 2); always_auto_resize = false; display_tabs = false; break;