diff --git a/client/render/imgui_impl.cpp b/client/render/imgui_impl.cpp index 888e4865..85bfaa8e 100644 --- a/client/render/imgui_impl.cpp +++ b/client/render/imgui_impl.cpp @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +#define IMGUI_DEFINE_MATH_OPERATORS #include "imgui_impl.h" #include "implot.h" @@ -25,6 +26,7 @@ #include "image_loader.h" #include "openxr/openxr.h" #include "utils/ranges.h" +#include "utils/strings.h" #include "vulkan/vulkan_enums.hpp" #include "vulkan/vulkan_handles.hpp" #include "vulkan/vulkan_to_string.hpp" @@ -43,8 +45,6 @@ #include #include -#include "IconsFontAwesome6.h" - #ifdef __ANDROID__ #include #include @@ -178,6 +178,31 @@ static float distance_to_window(ImGuiWindow * window, ImVec2 position) return std::hypot(dx, dy); } +static const std::array layout_bindings = { + vk::DescriptorSetLayoutBinding{ + .binding = 0, + .descriptorType = vk::DescriptorType::eCombinedImageSampler, + .descriptorCount = 1, + .stageFlags = vk::ShaderStageFlagBits::eFragment, + }}; + +imgui_textures::imgui_textures( + vk::raii::PhysicalDevice physical_device, + vk::raii::Device & device, + uint32_t queue_family_index, + thread_safe & queue) : + physical_device(physical_device), + device(device), + queue(queue), + ds_layout(device, vk::DescriptorSetLayoutCreateInfo{.bindingCount = layout_bindings.size(), .pBindings = layout_bindings.data()}), + command_pool(device, + vk::CommandPoolCreateInfo{ + .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer | vk::CommandPoolCreateFlagBits::eTransient, + .queueFamilyIndex = queue_family_index, + }), + descriptor_pool(device, ds_layout, layout_bindings) +{} + std::vector> imgui_context::ray_plane_intersection(const imgui_context::controller_state & in) const { if (!in.active) @@ -289,14 +314,6 @@ imgui_context::imgui_frame & imgui_context::get_frame(vk::Image destination) return frame; } -static const std::array layout_bindings = { - vk::DescriptorSetLayoutBinding{ - .binding = 0, - .descriptorType = vk::DescriptorType::eCombinedImageSampler, - .descriptorCount = 1, - .stageFlags = vk::ShaderStageFlagBits::eFragment, - }}; - imgui_context::imgui_context( vk::raii::PhysicalDevice physical_device, vk::raii::Device & device, @@ -305,18 +322,13 @@ imgui_context::imgui_context( std::span controllers_, xr::swapchain & swapchain, std::vector layers) : - physical_device(physical_device), - device(device), + imgui_textures( + physical_device, + device, + queue_family_index, + queue), queue_family_index(queue_family_index), - queue(queue), - ds_layout(device, vk::DescriptorSetLayoutCreateInfo{.bindingCount = layout_bindings.size(), .pBindings = layout_bindings.data()}), - descriptor_pool(device, ds_layout, layout_bindings), renderpass(create_renderpass(device, swapchain.format(), true)), - command_pool(device, - vk::CommandPoolCreateInfo{ - .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer | vk::CommandPoolCreateFlagBits::eTransient, - .queueFamilyIndex = queue_family_index, - }), command_buffers(swapchain.images().size()), size(swapchain.extent().width, swapchain.extent().height), format(swapchain.format()), @@ -1020,12 +1032,12 @@ imgui_context::~imgui_context() ImGui::DestroyContext(context); } -ImTextureID imgui_context::load_texture(const std::string & filename, vk::raii::Sampler && sampler) +ImTextureID imgui_textures::load_texture(const std::string & filename, vk::raii::Sampler && sampler) { return load_texture(std::span{asset{filename}}, std::move(sampler)); } -ImTextureID imgui_context::load_texture(const std::span & bytes, vk::raii::Sampler && sampler) +ImTextureID imgui_textures::load_texture(const std::span & bytes, vk::raii::Sampler && sampler) { bool srgb = true; image_loader loader(physical_device, device, queue, command_pool); @@ -1061,7 +1073,7 @@ ImTextureID imgui_context::load_texture(const std::span & bytes return id; } -ImTextureID imgui_context::load_texture(const std::span & bytes) +ImTextureID imgui_textures::load_texture(const std::span & bytes) { return load_texture( bytes, @@ -1079,7 +1091,7 @@ ImTextureID imgui_context::load_texture(const std::span & bytes }); } -ImTextureID imgui_context::load_texture(const std::string & filename) +ImTextureID imgui_textures::load_texture(const std::string & filename) { return load_texture( filename, @@ -1097,15 +1109,8 @@ ImTextureID imgui_context::load_texture(const std::string & filename) }); } -void imgui_context::free_texture(ImTextureID texture) +void imgui_textures::free_texture(ImTextureID texture) { - // Make sure we are done using the texture - std::vector fences; - for (auto & f: command_buffers) - fences.push_back(*f.fence); - if (auto result = device.waitForFences(fences, true, 1'000'000'000); result != vk::Result::eSuccess) - spdlog::error("vkWaitForfences: {}", vk::to_string(result)); - textures.erase(texture); } @@ -1182,3 +1187,78 @@ void imgui_context::tooltip(std::string_view text) viewport->Pos = pos_backup; viewport->Size = size_backup; } + +// https://github.com/ocornut/imgui/issues/3379#issuecomment-2943903877 +void ScrollWhenDragging() +{ + ImVec2 delta{0.0f, -ImGui::GetIO().MouseDelta.y}; + const ImGuiMouseButton mouse_button = ImGuiMouseButton_Left; + + ImGuiContext & g = *ImGui::GetCurrentContext(); + ImGuiWindow * window = g.CurrentWindow; + ImGuiID id = window->GetID("##scrolldraggingoverlay"); + ImGui::KeepAliveID(id); + + static int active_id; + static ImVec2 cumulated_delta; + + bool HoveredIdAllowOverlap_backup = std::exchange(g.HoveredIdAllowOverlap, true); + bool ActiveIdAllowOverlap_backup = std::exchange(g.ActiveIdAllowOverlap, true); + if (active_id == 0 and ImGui::ItemHoverable(window->Rect(), 0, g.CurrentItemFlags) and ImGui::IsMouseClicked(mouse_button, ImGuiInputFlags_None, /*id*/ ImGuiKeyOwner_Any)) + { + active_id = id; + + // Don't scroll on the first step, in case the active controller just changed + delta = {}; + cumulated_delta = {}; + } + + if (not g.IO.MouseDown[mouse_button]) + active_id = 0; + + if (active_id == id) + { + if (delta.x != 0.0f) + ImGui::SetScrollX(window, window->Scroll.x + delta.x); + if (delta.y != 0.0f) + ImGui::SetScrollY(window, window->Scroll.y + delta.y); + + cumulated_delta += delta; + if (std::max(std::abs(cumulated_delta.x), std::abs(cumulated_delta.y)) > 50) + ImGui::ClearActiveID(); + } + + g.HoveredIdAllowOverlap = HoveredIdAllowOverlap_backup; + g.ActiveIdAllowOverlap = ActiveIdAllowOverlap_backup; +} + +void CenterTextH(const std::string & text) +{ + float win_width = ImGui::GetWindowSize().x; + float text_width = ImGui::CalcTextSize(text.c_str()).x; + ImGui::SetCursorPosX((win_width - text_width) / 2); + + ImGui::Text("%s", text.c_str()); +} + +void CenterTextHV(const std::string & text) +{ + ImVec2 size = ImGui::GetWindowSize(); + + std::vector lines = utils::split(text); + + float text_height = 0; + for (const auto & i: lines) + text_height += ImGui::CalcTextSize(i.c_str()).y; + + ImGui::SetCursorPosY((size.y - text_height) / 2); + + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, {0, 0}); + for (const auto & i: lines) + { + float text_width = ImGui::CalcTextSize(i.c_str()).x; + ImGui::SetCursorPosX((size.x - text_width) / 2); + ImGui::Text("%s", i.c_str()); + } + ImGui::PopStyleVar(); +} diff --git a/client/render/imgui_impl.h b/client/render/imgui_impl.h index cd5b2b78..1054e1f6 100644 --- a/client/render/imgui_impl.h +++ b/client/render/imgui_impl.h @@ -36,14 +36,8 @@ #include #include -class imgui_context +class imgui_textures { - struct command_buffer - { - vk::raii::CommandBuffer command_buffer = nullptr; - vk::raii::Fence fence = nullptr; - }; - struct texture_data { vk::raii::Sampler sampler; @@ -51,6 +45,38 @@ class imgui_context std::shared_ptr descriptor_set; }; +protected: + vk::raii::PhysicalDevice physical_device; + vk::raii::Device & device; + thread_safe & queue; + vk::raii::DescriptorSetLayout ds_layout; + vk::raii::CommandPool command_pool; + +private: + growable_descriptor_pool descriptor_pool; + std::unordered_map textures; + +public: + imgui_textures( + vk::raii::PhysicalDevice physical_device, + vk::raii::Device & device, + uint32_t queue_family_index, + thread_safe & queue); + ImTextureID load_texture(const std::string & filename, vk::raii::Sampler && sampler); + ImTextureID load_texture(const std::string & filename); + ImTextureID load_texture(const std::span & bytes, vk::raii::Sampler && sampler); + ImTextureID load_texture(const std::span & bytes); + void free_texture(ImTextureID); +}; + +class imgui_context : public imgui_textures +{ + struct command_buffer + { + vk::raii::CommandBuffer command_buffer = nullptr; + vk::raii::Fence fence = nullptr; + }; + public: struct imgui_frame { @@ -117,18 +143,10 @@ public: }; private: - vk::raii::PhysicalDevice physical_device; - vk::raii::Device & device; uint32_t queue_family_index; - thread_safe & queue; vk::raii::Pipeline pipeline = nullptr; - vk::raii::DescriptorSetLayout ds_layout; - growable_descriptor_pool descriptor_pool; vk::raii::RenderPass renderpass; - vk::raii::CommandPool command_pool; - - std::unordered_map textures; std::vector frames; imgui_frame & get_frame(vk::Image destination); @@ -207,11 +225,6 @@ public: // Convert position from viewport coordinates to real-world glm::vec3 rw_from_vp(const ImVec2 & position); - ImTextureID load_texture(const std::string & filename, vk::raii::Sampler && sampler); - ImTextureID load_texture(const std::string & filename); - ImTextureID load_texture(const std::span & bytes, vk::raii::Sampler && sampler); - ImTextureID load_texture(const std::span & bytes); - void free_texture(ImTextureID); void set_current(); bool is_modal_popup_shown() const; @@ -221,3 +234,7 @@ public: void set_controllers_enabled(bool value); void tooltip(std::string_view text); }; + +void ScrollWhenDragging(); +void CenterTextH(const std::string & text); +void CenterTextHV(const std::string & text); diff --git a/client/scene.cpp b/client/scene.cpp index 684922ee..82f00526 100644 --- a/client/scene.cpp +++ b/client/scene.cpp @@ -47,8 +47,8 @@ scene::scene(key, const meta & current_meta, std::span support device(application::instance().vk_device), physical_device(application::instance().vk_physical_device), queue(application::instance().vk_queue), - commandpool(application::instance().vk_cmdpool), queue_family_index(application::instance().vk_queue_family_index), + commandpool(application::instance().vk_cmdpool), current_meta(current_meta) { swapchain_format = vk::Format::eUndefined; diff --git a/client/scene.h b/client/scene.h index 9593100c..df277a7f 100644 --- a/client/scene.h +++ b/client/scene.h @@ -86,13 +86,16 @@ protected: XrViewConfigurationType viewconfig; bool focused = false; +public: vk::raii::Instance & vk_instance; vk::raii::Device & device; vk::raii::PhysicalDevice & physical_device; thread_safe & queue; - vk::raii::CommandPool & commandpool; uint32_t queue_family_index; +protected: + vk::raii::CommandPool & commandpool; + const meta & current_meta; std::optional renderer; diff --git a/client/scenes/app_launcher.cpp b/client/scenes/app_launcher.cpp new file mode 100644 index 00000000..4445d1a9 --- /dev/null +++ b/client/scenes/app_launcher.cpp @@ -0,0 +1,279 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2025 Guillaume Meunier + * Copyright (C) 2025 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#define IMGUI_DEFINE_MATH_OPERATORS +#include "app_launcher.h" + +#include "constants.h" +#include "imgui.h" +#include "imgui_internal.h" +#include "scenes/stream.h" +#include "utils/i18n.h" +#include "utils/ranges.h" + +#include +#include +#include + +using namespace std::chrono_literals; + +// Display a button with an image and a text centred horizontally +static bool icon( + const std::string & text, + ImTextureRef tex_ref, + const ImVec2 & image_size, + ImGuiButtonFlags flags = 0, + const ImVec2 & size_arg = ImVec2(0, 0), + const ImVec2 & uv0 = ImVec2(0, 0), + const ImVec2 & uv1 = ImVec2(1, 1), + const ImVec4 & tint_col = ImVec4(1, 1, 1, 1)) +{ + // Based on ImGui::ButtonEx and ImGui::ImageButtonEx + ImGuiWindow * window = ImGui::GetCurrentWindow(); + const ImGuiStyle & style = ImGui::GetStyle(); + + if (window->SkipItems) + return false; + + const ImVec2 label_size = ImGui::CalcTextSize(text.c_str(), nullptr, true); + + ImVec2 size = ImGui::CalcItemSize( + size_arg, + std::max(image_size.x, label_size.x) + style.FramePadding.x * 2.0f, + image_size.y + style.ItemInnerSpacing.y + label_size.y + style.FramePadding.y * 2.0f); + + const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); + + ImRect image_pos( + {(bb.Min.x + bb.Max.x - image_size.x) / 2, bb.Min.y + style.FramePadding.y}, + {(bb.Min.x + bb.Max.x + image_size.x) / 2, bb.Min.y + style.FramePadding.y + image_size.y}); + + ImRect label_pos( + {bb.Min.x + style.FramePadding.x, image_pos.Max.y + style.ItemInnerSpacing.y}, + {bb.Max.x - style.FramePadding.x, bb.Max.y - style.FramePadding.y}); + + ImGui::ItemSize(bb); + + ImGuiID id = window->GetID(text.c_str()); + if (!ImGui::ItemAdd(bb, id)) + return false; + + bool hovered, held; + bool pressed = ImGui::ButtonBehavior(bb, id, &hovered, &held, flags); + + // Render + const ImU32 col = ImGui::GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered + : ImGuiCol_Button); + ImGui::RenderNavCursor(bb, id); + ImGui::RenderFrame(bb.Min, bb.Max, col, true, style.FrameRounding); + + window->DrawList->AddImage(tex_ref, image_pos.Min, image_pos.Max, uv0, uv1, ImGui::GetColorU32(tint_col)); + ImGui::RenderTextClipped(label_pos.Min, label_pos.Max, text.c_str(), NULL, &label_size, style.ButtonTextAlign, &bb); + + return pressed; +} + +app_launcher::app_launcher( + scenes::stream & stream, + std::string server_name) : + server_name(std::move(server_name)), + stream(stream), + textures( + stream.physical_device, + stream.device, + stream.queue_family_index, + stream.queue) +{ + try + { + default_icon = textures.load_texture("default_icon.ktx2"); + } + catch (...) + { + default_icon = textures.load_texture("default_icon.png"); + } +} + +app_launcher::~app_launcher() +{ + stream.device.waitIdle(); + for (const auto & [app_id, app_icon]: app_icons) + textures.free_texture(app_icon); + textures.free_texture(default_icon); +} + +app_launcher::clicked app_launcher::draw_gui(imgui_context & imgui_ctx, const std::string & cancel) +{ + auto res = clicked::None; + auto t0 = std::chrono::steady_clock::now(); + bool app_starting = start_time != std::chrono::steady_clock::time_point{} and t0 - start_time < 10s; + + auto cancel_size = ImGui::CalcTextSize(cancel.c_str()); + + auto apps = applications.lock(); + + ImGui::PushFont(nullptr, constants::gui::font_size_large); + if (server_name.empty()) + CenterTextH(_("Connected to WiVRn server")); + else + CenterTextH(fmt::format(_F("Connected to {}"), server_name)); + ImGui::PopFont(); + + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, {20, 20}); + if (apps->empty()) + { + CenterTextHV(_("Start an application on the server to start streaming.")); + } + else + { + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {20, 0}); + ImGui::BeginDisabled(app_starting); + ImGui::BeginChild("Main", ImGui::GetWindowSize() - ImGui::GetCursorPos() - ImVec2(0, cancel_size.y + 80), 0, app_starting ? ImGuiWindowFlags_NoScrollWithMouse : 0); + + ImGui::Indent(20); + if (server_name.empty()) + ImGui::Text("%s", _S("Start an application on your computer or select one to start streaming.")); + else + ImGui::Text("%s", fmt::format(_F("Start an application on {} or select one to start streaming."), server_name).c_str()); + ImGui::Unindent(); + + float icon_width = 400; + float icon_spacing = ImGui::GetStyle().ItemSpacing.x; + float usable_window_width = ImGui::GetWindowSize().x - ImGui::GetCurrentWindow()->ScrollbarSizes.x; + + int icons_per_line = (usable_window_width + icon_spacing) / (icon_width + icon_spacing); + float icon_line_width = icons_per_line * icon_width + (icons_per_line - 1) * icon_spacing; + + ImGui::Indent((usable_window_width - icon_line_width) / 2); + + for (const auto [index, app]: utils::enumerate(*apps)) + { + ImTextureID texture = [&]() -> ImTextureID { + if (app.image.empty()) + return default_icon; + else + { + auto it = app_icons.find(app.id); + if (it == app_icons.end()) + { + // Don't load too many textures at the same time to keep the GUI responsive + if (std::chrono::steady_clock::now() - t0 > 10ms) + return default_icon; + + try + { + it = app_icons.emplace(app.id, textures.load_texture(app.image)).first; + } + catch (std::exception & e) + { + spdlog::warn("Unable to load icon for \"{}\": {}", app.id, e.what()); + + app.image.clear(); + return default_icon; + } + } + return it->second; + } + }(); + + ImGui::PushStyleColor(ImGuiCol_Button, 0); + if (icon(app.name + "##" + app.id, texture, {256, 256}, ImGuiButtonFlags_PressedOnClickRelease, {icon_width, 0})) + { + res = clicked::Start; + start_time = t0; + stream.start_application(app.id); + } + imgui_ctx.vibrate_on_hover(); + ImGui::PopStyleColor(); // ImGuiCol_Button + + if ((index + 1) % icons_per_line) + ImGui::SameLine(); + } + ImGui::Unindent(); + + std::vector> to_be_removed; + for (const auto & [app_id, app_icon]: app_icons) + { + if (not std::ranges::contains(*apps, app_id, &app::id)) + to_be_removed.emplace_back(app_id, app_icon); + } + for (const auto & [app_id, app_icon]: to_be_removed) + { + imgui_ctx.free_texture(app_icon); + app_icons.erase(app_id); + } + + ScrollWhenDragging(); + ImGui::EndChild(); + ImGui::EndDisabled(); + + if (app_starting) + { + ImGui::SetCursorPos(ImGui::GetWindowSize() / 2 - ImVec2{200, 200} - ImGui::GetStyle().FramePadding); + ImSpinner::SpinnerAng("App starting spinner", + 200, // Radius + 40, // Thickness + ImColor{1.f, 1.f, 1.f, 1.f}, // Colour + ImColor{1.f, 1.f, 1.f, 0.f}, // Background + 6, // Velocity + 0.75f * 2 * M_PI // Angle + ); + } + + ImGui::PopStyleVar(); // ImGuiStyleVar_WindowPadding + } + ImGui::PopStyleVar(); + + ImGui::SetCursorPos(ImGui::GetWindowSize() - cancel_size - ImVec2{50, 50}); + + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.8f, 0.2f, 0.2f, 0.40f)); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.8f, 0.2f, 0.2f, 1.00f)); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(1.0f, 0.1f, 0.1f, 1.00f)); + if (ImGui::Button(cancel.c_str())) + res = clicked::Cancel; + imgui_ctx.vibrate_on_hover(); + ImGui::PopStyleColor(3); // ImGuiCol_Button, ImGuiCol_ButtonHovered, ImGuiCol_ButtonActive + return res; +} + +void app_launcher::operator()(to_headset::application_list && apps) +{ + std::ranges::sort(apps.applications, [](auto & l, auto & r) { + return una::casesens::collate_utf8(l.name, r.name) < 0; + }); + + auto locked = applications.lock(); + + locked->clear(); + locked->reserve(apps.applications.size()); + for (const auto & i: apps.applications) + { + locked->push_back(app{ + .id = std::move(i.id), + .name = std::move(i.name), + }); + } +} + +void app_launcher::operator()(to_headset::application_icon && icon) +{ + auto locked = applications.lock(); + if (auto it = std::ranges::find(*locked, icon.id, &app::id); it != locked->end()) + it->image = std::move(icon.image); +} diff --git a/client/scenes/app_launcher.h b/client/scenes/app_launcher.h new file mode 100644 index 00000000..b9ac228b --- /dev/null +++ b/client/scenes/app_launcher.h @@ -0,0 +1,65 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2025 Guillaume Meunier + * Copyright (C) 2025 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include "imgui.h" +#include "render/imgui_impl.h" +#include "wivrn_packets.h" +#include +#include + +namespace scenes +{ +class stream; +} + +class app_launcher +{ + struct app + { + std::string id; + std::string name; + std::vector image; + }; + + std::chrono::steady_clock::time_point start_time{}; + std::string server_name; + scenes::stream & stream; + imgui_textures textures; + ImTextureID default_icon; + std::unordered_map app_icons; + // Last application list received from server + thread_safe> applications; + +public: + app_launcher(scenes::stream &, std::string server_name); + ~app_launcher(); + enum clicked + { + None, + Cancel, + Start, + }; + // cancel: text to display on the quit/disconnect/cancel button + clicked draw_gui(imgui_context &, const std::string & cancel); + + void operator()(wivrn::to_headset::application_list && apps); + void operator()(wivrn::to_headset::application_icon && icon); +}; diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index 2b9154ab..b5b10d54 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -572,7 +572,7 @@ void scenes::lobby::render(const XrFrameState & frame_state) { auto session = async_session.get(); if (session) - next_scene = stream::create(std::move(session), 1'000'000'000.f / frame_state.predictedDisplayPeriod); + next_scene = stream::create(std::move(session), 1'000'000'000.f / frame_state.predictedDisplayPeriod, server_name); async_session.reset(); } @@ -923,15 +923,6 @@ void scenes::lobby::on_focused() about_picture = imgui_ctx->load_texture(image + ".png"); } - try - { - default_icon = imgui_ctx->load_texture("default_icon.ktx2"); - } - catch (...) - { - default_icon = imgui_ctx->load_texture("default_icon.png"); - } - setup_passthrough(); session.set_refresh_rate(application::get_config().preferred_refresh_rate.value_or(0)); multicast = application::get_wifi_lock().get_multicast_lock(); @@ -952,8 +943,6 @@ void scenes::lobby::on_unfocused() renderer->wait_idle(); // Must be before the scene data because the renderer uses its descriptor sets about_picture = 0; - default_icon = 0; - app_icons.clear(); imgui_ctx.reset(); world.clear(); // Must be cleared before the renderer so that the descriptor sets are freed before their pools diff --git a/client/scenes/lobby.h b/client/scenes/lobby.h index d527ec9c..f26b8c6c 100644 --- a/client/scenes/lobby.h +++ b/client/scenes/lobby.h @@ -127,9 +127,6 @@ class lobby : public scene_impl tab last_current_tab = tab::server_list; int optional_feature_index = 0; // Which step of the first run screen are we in ImTextureID about_picture; - ImTextureID default_icon; - std::unordered_map app_icons; - std::optional timestamp_start_application; virtual_keyboard keyboard; diff --git a/client/scenes/lobby_gui.cpp b/client/scenes/lobby_gui.cpp index fce6fce7..1834a320 100644 --- a/client/scenes/lobby_gui.cpp +++ b/client/scenes/lobby_gui.cpp @@ -30,15 +30,13 @@ #include "imgui.h" #include "imgui_internal.h" #include "lobby.h" -#include "stream.h" +#include "scenes/stream.h" #include "utils/i18n.h" #include "utils/overloaded.h" -#include "utils/ranges.h" #include "version.h" #include "xr/body_tracker.h" #include #include -#include #include #include #include @@ -52,81 +50,6 @@ using namespace std::chrono_literals; -// https://github.com/ocornut/imgui/issues/3379#issuecomment-2943903877 -static void ScrollWhenDragging() -{ - ImVec2 delta{0.0f, -ImGui::GetIO().MouseDelta.y}; - const ImGuiMouseButton mouse_button = ImGuiMouseButton_Left; - - ImGuiContext & g = *ImGui::GetCurrentContext(); - ImGuiWindow * window = g.CurrentWindow; - ImGuiID id = window->GetID("##scrolldraggingoverlay"); - ImGui::KeepAliveID(id); - - static int active_id; - static ImVec2 cumulated_delta; - - bool HoveredIdAllowOverlap_backup = std::exchange(g.HoveredIdAllowOverlap, true); - bool ActiveIdAllowOverlap_backup = std::exchange(g.ActiveIdAllowOverlap, true); - if (active_id == 0 and ImGui::ItemHoverable(window->Rect(), 0, g.CurrentItemFlags) and ImGui::IsMouseClicked(mouse_button, ImGuiInputFlags_None, /*id*/ ImGuiKeyOwner_Any)) - { - active_id = id; - - // Don't scroll on the first step, in case the active controller just changed - delta = {}; - cumulated_delta = {}; - } - - if (not g.IO.MouseDown[mouse_button]) - active_id = 0; - - if (active_id == id) - { - if (delta.x != 0.0f) - ImGui::SetScrollX(window, window->Scroll.x + delta.x); - if (delta.y != 0.0f) - ImGui::SetScrollY(window, window->Scroll.y + delta.y); - - cumulated_delta += delta; - if (std::max(std::abs(cumulated_delta.x), std::abs(cumulated_delta.y)) > 50) - ImGui::ClearActiveID(); - } - - g.HoveredIdAllowOverlap = HoveredIdAllowOverlap_backup; - g.ActiveIdAllowOverlap = ActiveIdAllowOverlap_backup; -} - -static void CenterTextH(const std::string & text) -{ - float win_width = ImGui::GetWindowSize().x; - float text_width = ImGui::CalcTextSize(text.c_str()).x; - ImGui::SetCursorPosX((win_width - text_width) / 2); - - ImGui::Text("%s", text.c_str()); -} - -static void CenterTextHV(const std::string & text) -{ - ImVec2 size = ImGui::GetWindowSize(); - - std::vector lines = utils::split(text); - - float text_height = 0; - for (const auto & i: lines) - text_height += ImGui::CalcTextSize(i.c_str()).y; - - ImGui::SetCursorPosY((size.y - text_height) / 2); - - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, {0, 0}); - for (const auto & i: lines) - { - float text_width = ImGui::CalcTextSize(i.c_str()).x; - ImGui::SetCursorPosX((size.x - text_width) / 2); - ImGui::Text("%s", i.c_str()); - } - ImGui::PopStyleVar(); -} - static void InputText(const char * label, std::string & text, const ImVec2 & size, ImGuiInputTextFlags flags) { auto callback = [](ImGuiInputTextCallbackData * data) -> int { @@ -175,54 +98,6 @@ static void display_recentering_tip(imgui_context & ctx, const std::string & tip ImGui::PopFont(); } -// Display a button with an image and a text centred horizontally -static bool icon(const std::string & text, ImTextureRef tex_ref, const ImVec2 & image_size, ImGuiButtonFlags flags = 0, const ImVec2 & size_arg = ImVec2(0, 0), const ImVec2 & uv0 = ImVec2(0, 0), const ImVec2 & uv1 = ImVec2(1, 1), const ImVec4 & tint_col = ImVec4(1, 1, 1, 1)) -{ - // Based on ImGui::ButtonEx and ImGui::ImageButtonEx - ImGuiWindow * window = ImGui::GetCurrentWindow(); - const ImGuiStyle & style = ImGui::GetStyle(); - - if (window->SkipItems) - return false; - - const ImVec2 label_size = ImGui::CalcTextSize(text.c_str(), nullptr, true); - - ImVec2 size = ImGui::CalcItemSize( - size_arg, - std::max(image_size.x, label_size.x) + style.FramePadding.x * 2.0f, - image_size.y + style.ItemInnerSpacing.y + label_size.y + style.FramePadding.y * 2.0f); - - const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); - - ImRect image_pos( - {(bb.Min.x + bb.Max.x - image_size.x) / 2, bb.Min.y + style.FramePadding.y}, - {(bb.Min.x + bb.Max.x + image_size.x) / 2, bb.Min.y + style.FramePadding.y + image_size.y}); - - ImRect label_pos( - {bb.Min.x + style.FramePadding.x, image_pos.Max.y + style.ItemInnerSpacing.y}, - {bb.Max.x - style.FramePadding.x, bb.Max.y - style.FramePadding.y}); - - ImGui::ItemSize(bb); - - ImGuiID id = window->GetID(text.c_str()); - if (!ImGui::ItemAdd(bb, id)) - return false; - - bool hovered, held; - bool pressed = ImGui::ButtonBehavior(bb, id, &hovered, &held, flags); - - // Render - const ImU32 col = ImGui::GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered - : ImGuiCol_Button); - ImGui::RenderNavCursor(bb, id); - ImGui::RenderFrame(bb.Min, bb.Max, col, true, style.FrameRounding); - - window->DrawList->AddImage(tex_ref, image_pos.Min, image_pos.Max, uv0, uv1, ImGui::GetColorU32(tint_col)); - ImGui::RenderTextClipped(label_pos.Min, label_pos.Max, text.c_str(), NULL, &label_size, style.ButtonTextAlign, &bb); - - return pressed; -} - std::string openxr_post_processing_flag_name(XrCompositionLayerSettingsFlagsFB flag) { switch (flag) @@ -248,7 +123,6 @@ void scenes::lobby::gui_connecting(locked_notifiable & pin_req if (next_scene) { current_tab = tab::connected; - timestamp_start_application.reset(); ImGui::CloseCurrentPopup(); return; } @@ -384,144 +258,14 @@ void scenes::lobby::gui_connected(XrTime predicted_display_time) if (not next_scene) { current_tab = tab::server_list; - - for (const auto & [app_id, app_icon]: app_icons) - imgui_ctx->free_texture(app_icon); - app_icons.clear(); - return; } - ImGui::PushFont(nullptr, constants::gui::font_size_large); - if (server_name.empty()) - CenterTextH(_("Connected to WiVRn server")); - else - CenterTextH(fmt::format(_F("Connected to {}"), server_name)); - ImGui::PopFont(); - - auto disconnect = _("Disconnect"); - auto disconnect_size = ImGui::CalcTextSize(disconnect.c_str()); - - auto apps = next_scene->get_applications(); - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, {20, 20}); - if (apps->empty()) - { - CenterTextHV(_("Start an application on the server to start streaming.")); - } - else - { - bool app_starting = timestamp_start_application and (predicted_display_time - *timestamp_start_application < 10'000'000'000); - - ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {20, 0}); - ImGui::BeginDisabled(app_starting); - ImGui::BeginChild("Main", ImGui::GetWindowSize() - ImGui::GetCursorPos() - ImVec2(0, disconnect_size.y + 80), 0, app_starting ? ImGuiWindowFlags_NoScrollWithMouse : 0); - - ImGui::Indent(20); - if (server_name.empty()) - ImGui::Text("%s", _S("Start an application on your computer or select one to start streaming.")); - else - ImGui::Text("%s", fmt::format(_F("Start an application on {} or select one to start streaming."), server_name).c_str()); - ImGui::Unindent(); - - float icon_width = 400; - float icon_spacing = ImGui::GetStyle().ItemSpacing.x; - float usable_window_width = ImGui::GetWindowSize().x - ImGui::GetCurrentWindow()->ScrollbarSizes.x; - - int icons_per_line = (usable_window_width + icon_spacing) / (icon_width + icon_spacing); - float icon_line_width = icons_per_line * icon_width + (icons_per_line - 1) * icon_spacing; - - ImGui::Indent((usable_window_width - icon_line_width) / 2); - - auto t0 = std::chrono::steady_clock::now(); - for (const auto [index, app]: utils::enumerate(*apps)) - { - ImTextureID texture = [&]() -> ImTextureID { - if (app.image.empty()) - return default_icon; - else - { - auto it = app_icons.find(app.id); - if (it == app_icons.end()) - { - // Don't load too many textures at the same time to keep the GUI responsive - if (std::chrono::steady_clock::now() - t0 > 10ms) - return default_icon; - - try - { - it = app_icons.emplace(app.id, imgui_ctx->load_texture(app.image)).first; - } - catch (std::exception & e) - { - spdlog::warn("Unable to load icon for \"{}\": {}", app.id, e.what()); - - app.image.clear(); - return default_icon; - } - } - return it->second; - } - }(); - - ImGui::PushStyleColor(ImGuiCol_Button, 0); - if (icon(app.name + "##" + app.id, texture, {256, 256}, ImGuiButtonFlags_PressedOnClickRelease, {icon_width, 0})) - { - timestamp_start_application = predicted_display_time; - next_scene->start_application(app.id); - } - imgui_ctx->vibrate_on_hover(); - ImGui::PopStyleColor(); // ImGuiCol_Button - - if (index % 3 != 2) - ImGui::SameLine(); - } - ImGui::Unindent(); - - std::vector> to_be_removed; - for (const auto & [app_id, app_icon]: app_icons) - { - if (not std::ranges::contains(*apps, app_id, &scenes::stream::app::id)) - to_be_removed.emplace_back(app_id, app_icon); - } - for (const auto & [app_id, app_icon]: to_be_removed) - { - imgui_ctx->free_texture(app_icon); - app_icons.erase(app_id); - } - - ScrollWhenDragging(); - ImGui::EndChild(); - ImGui::EndDisabled(); - - if (app_starting) - { - ImGui::SetCursorPos(ImGui::GetWindowSize() / 2 - ImVec2{200, 200} - ImGui::GetStyle().FramePadding); - ImSpinner::SpinnerAng("App starting spinner", - 200, // Radius - 40, // Thickness - ImColor{1.f, 1.f, 1.f, 1.f}, // Colour - ImColor{1.f, 1.f, 1.f, 0.f}, // Background - 6, // Velocity - 0.75f * 2 * M_PI // Angle - ); - } - - ImGui::PopStyleVar(); // ImGuiStyleVar_WindowPadding - } - ImGui::PopStyleVar(); - - ImGui::SetCursorPos(ImGui::GetWindowSize() - disconnect_size - ImVec2{50, 50}); - - ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.8f, 0.2f, 0.2f, 0.40f)); - ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.8f, 0.2f, 0.2f, 1.00f)); - ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(1.0f, 0.1f, 0.1f, 1.00f)); - if (ImGui::Button(disconnect.c_str())) + if (next_scene->apps.draw_gui(*imgui_ctx, _("Disconnect")) == app_launcher::Cancel) { next_scene.reset(); current_tab = tab::server_list; } - imgui_ctx->vibrate_on_hover(); - ImGui::PopStyleColor(3); // ImGuiCol_Button, ImGuiCol_ButtonHovered, ImGuiCol_ButtonActive } void scenes::lobby::gui_new_server() diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index 72d3ec3a..e1924428 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -151,8 +151,9 @@ static const std::array supported_depth_formats{ vk::Format::eX8D24UnormPack32, }; -scenes::stream::stream() : +scenes::stream::stream(std::string server_name) : scene_impl(supported_color_formats, supported_depth_formats), + apps{*this, std::move(server_name)}, blitters{blitter(device, 0), blitter(device, 1)} { } @@ -185,9 +186,9 @@ static from_headset::visibility_mask_changed::masks get_visibility_mask(xr::inst return res; } -std::shared_ptr scenes::stream::create(std::unique_ptr network_session, float guessed_fps) +std::shared_ptr scenes::stream::create(std::unique_ptr network_session, float guessed_fps, std::string server_name) { - std::shared_ptr self{new stream}; + std::shared_ptr self{new stream{std::move(server_name)}}; self->network_session = std::move(network_session); self->network_session->send_control([&]() { diff --git a/client/scenes/stream.h b/client/scenes/stream.h index 736de971..fce46eb3 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -19,6 +19,7 @@ #pragma once +#include "app_launcher.h" #include "audio/audio.h" #include "decoder/shard_accumulator.h" #include "render/imgui_impl.h" @@ -49,12 +50,7 @@ public: }; static const size_t image_buffer_size = 3; - struct app - { - std::string id; - std::string name; - std::vector image; - }; + app_launcher apps; private: static const size_t view_count = 2; @@ -172,18 +168,18 @@ private: // Keep a reference to the resources needed to blit the images until vkWaitForFences std::vector> current_blit_handles; - // Last application list received from server - thread_safe> applications; - XrTime running_application_req = 0; thread_safe running_applications; - stream(); + stream(std::string server_name); public: ~stream(); - static std::shared_ptr create(std::unique_ptr session, float guessed_fps); + static std::shared_ptr create( + std::unique_ptr session, + float guessed_fps, + std::string server_name); void render(const XrFrameState &) override; void on_focused() override; @@ -220,11 +216,6 @@ public: return !exiting; } - auto get_applications() - { - return applications.lock(); - } - void start_application(std::string appid); static meta & get_meta_scene(); diff --git a/client/scenes/stream_gui.cpp b/client/scenes/stream_gui.cpp index e27ab3c3..5253a0f5 100644 --- a/client/scenes/stream_gui.cpp +++ b/client/scenes/stream_gui.cpp @@ -83,15 +83,6 @@ ImPlotPoint getter(int index, void * data_) return ImPlotPoint(index, *(float *)(data.data + index * data.stride) * data.multiplier); } - -void CenterTextH(const std::string & text) -{ - float win_width = ImGui::GetWindowSize().x; - float text_width = ImGui::CalcTextSize(text.c_str()).x; - ImGui::SetCursorPosX((win_width - text_width) / 2); - - ImGui::Text("%s", text.c_str()); -} } // namespace void scenes::stream::accumulate_metrics(XrTime predicted_display_time, const std::vector> & blit_handles, const gpu_timestamps & timestamps) diff --git a/client/scenes/stream_network.cpp b/client/scenes/stream_network.cpp index 91511f2f..59cd2472 100644 --- a/client/scenes/stream_network.cpp +++ b/client/scenes/stream_network.cpp @@ -106,31 +106,14 @@ void scenes::stream::send_feedback(const wivrn::from_headset::feedback & feedbac } } -void scenes::stream::operator()(to_headset::application_list && apps) +void scenes::stream::operator()(to_headset::application_list && l) { - std::ranges::sort(apps.applications, [](auto & l, auto & r) { - return una::casesens::collate_utf8(l.name, r.name) < 0; - }); - - auto locked = applications.lock(); - - locked->clear(); - locked->reserve(apps.applications.size()); - for (const auto & i: apps.applications) - { - locked->push_back(app{ - .id = std::move(i.id), - .name = std::move(i.name), - }); - } + apps(std::move(l)); } void scenes::stream::operator()(to_headset::application_icon && icon) { - auto locked = applications.lock(); - - if (auto it = std::ranges::find(*locked, icon.id, &app::id); it != locked->end()) - it->image = std::move(icon.image); + apps(std::move(icon)); } void scenes::stream::operator()(to_headset::running_applications && apps)