| 1261 | } |
| 1262 | |
| 1263 | void imgui_context::tooltip(std::string_view text) |
| 1264 | { |
| 1265 | // FIXME: this is incorrect if we use the docking branch of imgui |
| 1266 | ImGuiViewport * viewport = ImGui::GetMainViewport(); |
| 1267 | auto & current_layer = layer(ImGui::GetMousePos()); |
| 1268 | |
| 1269 | assert(std::ranges::contains(layers_, true, &viewport::tooltip_viewport)); |
| 1270 | auto & tooltip_layer = *std::ranges::find(layers_, true, &viewport::tooltip_viewport); |
| 1271 | |
| 1272 | // Get the item position before drawing the tooltip (top center) |
| 1273 | ImVec2 item_position{ |
| 1274 | (ImGui::GetItemRectMin().x + ImGui::GetItemRectMax().x) / 2, |
| 1275 | ImGui::GetItemRectMin().y, |
| 1276 | }; |
| 1277 | |
| 1278 | auto pos_backup = viewport->Pos; |
| 1279 | auto size_backup = viewport->Size; |
| 1280 | viewport->Pos = ImVec2(tooltip_layer.vp_origin.x, tooltip_layer.vp_origin.y); |
| 1281 | viewport->Size = ImVec2(tooltip_layer.vp_size.x, tooltip_layer.vp_size.y); |
| 1282 | |
| 1283 | // Draw the tooltip in the tooltip layer |
| 1284 | // Clamp position to avoid overflowing on the left or the right |
| 1285 | ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, constants::style::tooltip_padding); |
| 1286 | ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, constants::style::tooltip_rounding); |
| 1287 | |
| 1288 | auto & style = ImGui::GetStyle(); |
| 1289 | const ImVec2 text_size = ImGui::CalcTextSize(text.data(), text.data() + text.size(), true); |
| 1290 | const ImVec2 tooltip_size = {text_size.x + style.WindowPadding.x * 2.0f, text_size.y + style.WindowPadding.y * 2.0f}; |
| 1291 | |
| 1292 | ImGui::SetNextWindowPos(viewport->Pos + viewport->Size * 0.5, ImGuiCond_Always, {0.5, 0.5}); |
| 1293 | ImGui::SetNextWindowSizeConstraints({0, 0}, viewport->Size); |
| 1294 | if (ImGui::BeginTooltip()) |
| 1295 | { |
| 1296 | ImGui::PushStyleColor(ImGuiCol_Text, 0xffffffff); |
| 1297 | ImGui::TextUnformatted(text.data(), text.data() + text.size()); |
| 1298 | ImGui::PopStyleColor(); |
| 1299 | ImGui::EndTooltip(); |
| 1300 | } |
| 1301 | ImGui::PopStyleVar(2); |
| 1302 | |
| 1303 | viewport->Pos = pos_backup; |
| 1304 | viewport->Size = size_backup; |
| 1305 | |
| 1306 | // Compute the tooltip position |
| 1307 | glm::quat tooltip_orientation = current_layer.orientation; |
| 1308 | auto M = glm::mat3_cast(tooltip_orientation); |
| 1309 | |
| 1310 | float pixel_size = current_layer.size.y / current_layer.vp_size.y; |
| 1311 | 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); |
| 1312 | |
| 1313 | // Position the tooltip layer |
| 1314 | tooltip_layer.position = tooltip_position_centre; |
| 1315 | tooltip_layer.orientation = tooltip_orientation; |
| 1316 | } |
| 1317 | |
| 1318 | // https://github.com/ocornut/imgui/issues/3379#issuecomment-2943903877 |
| 1319 | void ScrollWhenDragging() |
no test coverage detected