From 863bef30abd3f23a17e00a8e2fe83a1a24f950fc Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Sat, 23 Nov 2024 00:23:36 +0100 Subject: [PATCH] Redo the PIN input window --- client/constants.h | 5 + client/scenes/lobby.cpp | 14 +-- client/scenes/lobby.h | 3 +- client/scenes/lobby_gui.cpp | 230 ++++++++++++++++++++++-------------- 4 files changed, 154 insertions(+), 98 deletions(-) diff --git a/client/constants.h b/client/constants.h index 3680a71e..ad0b8222 100644 --- a/client/constants.h +++ b/client/constants.h @@ -112,10 +112,15 @@ constexpr float window_border_size = 2; constexpr float window_rounding = 10; constexpr ImVec2 tooltip_padding = {5, 5}; +constexpr float tooltip_rounding = 0; constexpr float tooltip_distance = 10; constexpr ImVec2 button_size = {220, 80}; constexpr ImVec2 icon_button_size = {80, 80}; constexpr float connection_popup_width = 1000; +constexpr ImVec2 pin_entry_key_size = {90, 70}; +constexpr ImVec2 pin_entry_item_spacing = {10, 10}; +constexpr float pin_entry_popup_width = 3 * pin_entry_key_size.x + 2 * pin_entry_item_spacing.x; + } // namespace constants::style diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index f5468929..6efa0d2d 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -31,15 +31,14 @@ #include "stream.h" #include "utils/overloaded.h" #include "wivrn_client.h" +#include "wivrn_discover.h" #include "wivrn_sockets.h" #include "xr/passthrough.h" #include "xr/space.h" -#include -#include -#include #include -#include "wivrn_discover.h" +#include +#include // IWYU pragma: keep #include #include #include @@ -50,7 +49,6 @@ #include #include #include -#include #include #include @@ -1139,16 +1137,16 @@ void scenes::lobby::on_focused() { // Pop up window .space = xr::spaces::world, - .size = {0.6, 0.24}, + .size = {0.6, 0.28}, .vp_origin = {1500, 0}, - .vp_size = {1500, 600}, + .vp_size = {1500, 700}, .z_index = constants::lobby::zindex_gui, }, { // Virtual keyboard .space = xr::spaces::world, .size = {0.6, 0.2}, - .vp_origin = {1500, 600}, + .vp_origin = {1500, 700}, .vp_size = {1500, 500}, .always_show_cursor = true, .z_index = constants::lobby::zindex_gui, diff --git a/client/scenes/lobby.h b/client/scenes/lobby.h index 3b82fd40..c613c1c8 100644 --- a/client/scenes/lobby.h +++ b/client/scenes/lobby.h @@ -144,7 +144,8 @@ class lobby : public scene_impl std::string pin_buffer; void draw_features_status(XrTime predicted_display_time); - void gui_connecting(); + void gui_connecting(locked_notifiable & request); + void gui_enter_pin(locked_notifiable & request); void gui_server_list(); void gui_new_server(); void gui_settings(); diff --git a/client/scenes/lobby_gui.cpp b/client/scenes/lobby_gui.cpp index ea84b6b7..a2b57113 100644 --- a/client/scenes/lobby_gui.cpp +++ b/client/scenes/lobby_gui.cpp @@ -135,6 +135,7 @@ void scenes::lobby::tooltip(std::string_view text) }; ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, constants::style::tooltip_padding); + ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, constants::style::tooltip_rounding); // Clamp position to avoid overflowing on the right auto & style = ImGui::GetStyle(); @@ -151,7 +152,7 @@ void scenes::lobby::tooltip(std::string_view text) ImGui::EndTooltip(); } - ImGui::PopStyleVar(); + ImGui::PopStyleVar(2); viewport->Pos = pos_backup; viewport->Size = size_backup; @@ -163,101 +164,148 @@ void scenes::lobby::vibrate_on_hover() hovered_item = ImGui::GetItemID(); } -void scenes::lobby::gui_connecting() +void scenes::lobby::gui_connecting(locked_notifiable & pin_request) { using constants::style::button_size; - ImVec2 size{600, 0}; + std::string close_button_label = _("Cancel"); - auto pin_request = this->pin_request.lock(); - - std::string button_label = _("Cancel"); - - if (pin_request->pin_requested) + std::string status; + if (next_scene) { - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, {30, 30}); - ImGui::PushFont(imgui_ctx->large_font); - ImGui::Text("%s", _S("Enter the PIN")); - if (ImGui::IsWindowAppearing()) - ImGui::SetKeyboardFocusHere(); - InputText("##PIN", pin_buffer, size, ImGuiInputTextFlags_CharsDecimal); - ImGui::PopFont(); - - ImGui::BeginDisabled(pin_buffer.size() != 6); - if (ImGui::Button("OK", button_size)) - { - pin_request->pin = pin_buffer; - pin_request->pin_requested = false; - pin_request.notify_one(); - } - vibrate_on_hover(); - ImGui::EndDisabled(); - - ImGui::SameLine(); - ImGui::Dummy({size.x - 2 * button_size.x - 2 * ImGui::GetStyle().ItemSpacing.x, 0}); - - ImGui::SameLine(); - if (ImGui::Button(button_label.c_str(), button_size)) - { - async_session.cancel(); - next_scene.reset(); - pin_request->pin_cancelled = true; - pin_request.notify_one(); - - ImGui::CloseCurrentPopup(); - } - vibrate_on_hover(); - ImGui::PopStyleVar(); // ItemSpacing + if (next_scene->current_state() == scenes::stream::state::stalled) + status = _("Video stream interrupted"); + else if (server_name == "") + status = fmt::format(_F("Connection ready\nStart a VR application on your computer")); + else + status = fmt::format(_F("Connection ready\nStart a VR application on {}"), server_name); } + else if (async_session.valid()) + status = async_session.get_progress(); + else if (async_error) + status = *async_error; else { - std::string status; - if (next_scene) - { - if (next_scene->current_state() == scenes::stream::state::stalled) - status = _("Video stream interrupted"); - else if (server_name == "") - status = fmt::format(_F("Connection ready\nStart a VR application on your computer")); - else - status = fmt::format(_F("Connection ready\nStart a VR application on {}"), server_name); - } - else if (async_session.valid()) - status = async_session.get_progress(); - else if (async_error) - status = *async_error; - else - { - ImGui::CloseCurrentPopup(); - return; - } - - if (async_error) - button_label = _("Close"); - - ImGui::Dummy({1000, 1}); - - ImGui::PushFont(imgui_ctx->large_font); - if (server_name == "") - CenterTextH(fmt::format(_F("Connection"))); - else - CenterTextH(fmt::format(_F("Connection to {}"), server_name)); - ImGui::PopFont(); - - // ImGui::TextWrapped("%s", status.first.c_str()); - ImGui::Text("%s", status.c_str()); - - ImGui::SetCursorPosX(ImGui::GetWindowContentRegionMax().x - button_size.x - ImGui::GetStyle().WindowPadding.x); - - if (ImGui::Button(button_label.c_str(), button_size)) - { - async_session.cancel(); - next_scene.reset(); - pin_request->pin_cancelled = true; - - ImGui::CloseCurrentPopup(); - } - vibrate_on_hover(); + ImGui::CloseCurrentPopup(); + return; } + + if (async_error) + close_button_label = _("Close"); + + ImGui::Dummy({1000, 1}); + + ImGui::PushFont(imgui_ctx->large_font); + if (server_name == "") + CenterTextH(fmt::format(_F("Connection"))); + else + CenterTextH(fmt::format(_F("Connection to {}"), server_name)); + ImGui::PopFont(); + + // ImGui::TextWrapped("%s", status.first.c_str()); + ImGui::Text("%s", status.c_str()); + + ImGui::SetCursorPosX(ImGui::GetWindowContentRegionMax().x - button_size.x - ImGui::GetStyle().WindowPadding.x); + + if (ImGui::Button(close_button_label.c_str(), button_size)) + { + async_session.cancel(); + next_scene.reset(); + + pin_request->pin_cancelled = true; + + ImGui::CloseCurrentPopup(); + } + vibrate_on_hover(); +} + +void scenes::lobby::gui_enter_pin(locked_notifiable & pin_request) +{ + const int pin_size = 6; + + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, constants::style::pin_entry_item_spacing); + ImGui::PushFont(imgui_ctx->large_font); + + const auto & style = ImGui::GetStyle(); + const auto window = ImGui::GetCurrentWindow(); + + const std::string displayed_text = pin_buffer == "" ? _("PIN") : pin_buffer; + + const ImGuiID id = window->GetID("PIN"); + const ImVec2 label_size = ImGui::CalcTextSize(displayed_text.c_str(), nullptr, true); + const ImVec2 size = {constants::style::pin_entry_popup_width, label_size.y + style.FramePadding.y * 2.0f}; + + const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); + + ImGui::ItemSize(size, style.FramePadding.y); + if (!ImGui::ItemAdd(bb, id)) + return; + + const ImU32 col = ImGui::GetColorU32(ImGuiCol_FrameBg); + ImGui::RenderFrame(bb.Min, bb.Max, col, true, style.FrameRounding); + + ImGui::BeginDisabled(pin_buffer == ""); + const ImVec2 alignment = pin_buffer == "" ? ImVec2{0.5, 0.5} : ImVec2{0, 0.5}; + ImGui::RenderTextClipped(bb.Min + style.FramePadding, bb.Max - style.FramePadding, displayed_text.c_str(), nullptr, &label_size, alignment, &bb); + ImGui::EndDisabled(); + + ImGui::PopFont(); + if (ImGui::IsItemHovered()) + tooltip(_("Input the PIN displayed on the dashboard")); + + ImGui::BeginDisabled(pin_buffer.size() == pin_size); + for (int i = 1; i <= 9;) + { + for (int j = 0; j < 3; j++, i++) + { + char button_text[2]; + sprintf(button_text, "%d", i); + if (ImGui::Button(button_text, constants::style::pin_entry_key_size)) + pin_buffer += button_text; + vibrate_on_hover(); + + if (j < 2) + ImGui::SameLine(); + } + } + + 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(ICON_FA_RECTANGLE_XMARK, constants::style::pin_entry_key_size)) + { + async_session.cancel(); + next_scene.reset(); + pin_request->pin_cancelled = true; + pin_request.notify_one(); + + ImGui::CloseCurrentPopup(); + } + vibrate_on_hover(); + ImGui::PopStyleColor(3); + + ImGui::SameLine(); + if (ImGui::Button("0", constants::style::pin_entry_key_size)) + pin_buffer += "0"; + vibrate_on_hover(); + ImGui::EndDisabled(); + + ImGui::SameLine(); + ImGui::BeginDisabled(pin_buffer.size() == 0); + if (ImGui::Button(ICON_FA_DELETE_LEFT, constants::style::pin_entry_key_size)) + pin_buffer.resize(pin_buffer.size() - 1); + vibrate_on_hover(); + ImGui::EndDisabled(); + + ImGui::PopStyleVar(); // ImGuiStyleVar_ItemSpacing + + if (pin_buffer.size() == pin_size) + { + pin_request->pin = pin_buffer; + pin_request->pin_requested = false; + pin_request.notify_one(); + } + vibrate_on_hover(); } void scenes::lobby::gui_new_server() @@ -495,7 +543,7 @@ void scenes::lobby::gui_server_list() } // Check if an automatic connection has started - if ((async_session.valid() || next_scene) && !ImGui::IsPopupOpen("connecting")) + if ((async_session.valid() || next_scene) and not ImGui::IsPopupOpen("connecting")) ImGui::OpenPopup("connecting"); const auto & popup_layer = imgui_ctx->layers()[1]; @@ -506,7 +554,11 @@ void scenes::lobby::gui_server_list() ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, constants::style::window_border_size); if (ImGui::BeginPopupModal("connecting", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysAutoResize)) { - gui_connecting(); + auto pin_request = this->pin_request.lock(); + if (pin_request->pin_requested) + gui_enter_pin(pin_request); + else + gui_connecting(pin_request); ImGui::EndPopup(); }