Misc UI updates and fixes:

- Vertically center the server name for manually added servers
- Add padding for feature icons
- Use a popup for adding a new server
- Display pointers for both controllers
- Avoid jumps in scrolling when the focused controller changes
- Grab the GUI by the pointed position instead of the center
- Add battery level in lobby
- Use the correct width in the server list if there is a scrollbar
- Make sure the tooltip does not overflow on other layers
- Save the virtual keyboard layout in the config file
- Don't display the IP address for USB connections
- Allow receiving intents when WiVRn is already running
- Add recentering tip
This commit is contained in:
Guillaume Meunier
2024-11-01 14:13:58 +01:00
parent dca3254df9
commit e1ce484c27
20 changed files with 1191 additions and 692 deletions
+202 -104
View File
@@ -32,11 +32,14 @@
#include "wivrn_client.h"
#include "wivrn_packets.h"
#include "xr/passthrough.h"
#include "xr/space.h"
#include <algorithm>
#include <glm/gtc/matrix_access.hpp>
#include "wivrn_discover.h"
#include <cstdint>
#include <glm/gtc/quaternion.hpp>
#include <glm/matrix.hpp>
#include <magic_enum.hpp>
#include <ranges>
#include <simdjson.h>
@@ -101,15 +104,21 @@ static const std::array supported_depth_formats{
vk::Format::eX8D24UnormPack32,
};
void scenes::lobby::move_gui(glm::vec3 head_position, glm::vec3 new_gui_position)
static glm::quat compute_gui_orientation(glm::vec3 head_position, glm::vec3 new_gui_position)
{
const float gui_pitch = -0.2;
const float keyboard_pitch = -0.6;
glm::vec3 gui_direction = new_gui_position - head_position;
float gui_yaw = atan2(gui_direction.x, gui_direction.z) + M_PI;
auto q = glm::quat(cos(gui_yaw / 2), 0, sin(gui_yaw / 2), 0) * glm::quat(cos(gui_pitch / 2), sin(gui_pitch / 2), 0, 0);
return glm::quat(cos(gui_yaw / 2), 0, sin(gui_yaw / 2), 0) * glm::quat(cos(gui_pitch / 2), sin(gui_pitch / 2), 0, 0);
}
void scenes::lobby::move_gui(glm::vec3 head_position, glm::vec3 new_gui_position)
{
const float keyboard_pitch = -0.6;
auto q = compute_gui_orientation(head_position, new_gui_position);
auto M = glm::mat3_cast(q); // plane-to-world transform
// Main window
@@ -198,6 +207,8 @@ scenes::lobby::lobby()
strcpy(add_server_window_hostname, "");
add_server_window_port = wivrn::default_port;
add_server_tcp_only = false;
keyboard.set_layout(application::get_config().virtual_keyboard_layout);
}
static std::string ip_address_to_string(const in_addr & addr)
@@ -424,7 +435,7 @@ std::optional<glm::vec3> scenes::lobby::check_recenter_gesture(const std::array<
return std::nullopt;
}
std::optional<glm::vec3> scenes::lobby::check_recenter_action(XrTime predicted_display_time)
std::optional<glm::vec3> scenes::lobby::check_recenter_action(XrTime predicted_display_time, glm::vec3 head_position)
{
std::optional<std::pair<glm::vec3, glm::quat>> aim;
@@ -440,31 +451,69 @@ std::optional<glm::vec3> scenes::lobby::check_recenter_action(XrTime predicted_d
if (aim)
{
if (not gui_recenter_distance)
if (not gui_recenter_position or not gui_recenter_distance)
{
auto intersection = imgui_ctx->ray_plane_intersection({
// First frame where the recenter action is active
imgui_context::controller_state state{
.active = true,
.aim_position = aim->first,
.aim_orientation = aim->second,
});
};
if (not intersection.empty())
imgui_ctx->compute_pointer_position(state);
if (state.pointer_position)
{
gui_recenter_distance = std::clamp(intersection.front().second, 0.05f, 1.f);
spdlog::info("recentering at {}m", intersection.front().second);
auto M = glm::mat3_cast(imgui_ctx->layers()[0].orientation);
// Pointer position in world
glm::vec3 world_pointer_position = imgui_ctx->rw_from_vp(*state.pointer_position);
// Pointer position in GUI
gui_recenter_position = glm::transpose(M) * (world_pointer_position - imgui_ctx->layers()[0].position);
gui_recenter_distance = glm::length(state.aim_position - world_pointer_position);
}
else
{
gui_recenter_position = glm::vec3(0, 0, 0);
gui_recenter_distance = 0.3;
}
}
const auto & v = aim->first;
const auto & q = aim->second;
return v + q * glm::vec3(0, 0, -*gui_recenter_distance);
else
{
// Subsequent frames: find the GUI position that gives the correct world pointer position
glm::vec3 controller_direction = -glm::column(glm::mat3_cast(aim->second), 2);
glm::vec3 wanted_world_pointer_position = aim->first + controller_direction * *gui_recenter_distance;
// I'm sure there's an analytical solution but I can't be bothered to write it so
// let's use a gradient descent instead
auto f = [&](glm::vec3 new_gui_position) {
auto q = compute_gui_orientation(head_position, new_gui_position);
auto M = glm::mat3_cast(q); // plane-to-world transform
glm::vec3 world_pointer_position = new_gui_position + M * *gui_recenter_position;
return world_pointer_position - wanted_world_pointer_position;
};
// One step is usually enough, the solution will continuously improve in the next frames
glm::vec3 gui_position = imgui_ctx->layers()[0].position;
float eps = 0.01;
glm::vec3 obj = f(gui_position);
glm::vec3 obj_dx = (f(gui_position + glm::vec3(eps, 0, 0)) - obj) / eps;
glm::vec3 obj_dy = (f(gui_position + glm::vec3(0, eps, 0)) - obj) / eps;
glm::vec3 obj_dz = (f(gui_position + glm::vec3(0, 0, eps)) - obj) / eps;
glm::mat3 jacobian{obj_dx, obj_dy, obj_dz};
gui_position -= glm::inverse(jacobian) * obj;
return gui_position;
}
}
else
{
gui_recenter_distance.reset();
gui_recenter_position.reset();
}
return std::nullopt;
@@ -610,14 +659,24 @@ void scenes::lobby::render(const XrFrameState & frame_state)
imgui_ctx->set_current();
if (!async_session.valid() && !next_scene && !ImGui::IsPopupOpen("", ImGuiPopupFlags_AnyPopup))
{
const auto & servers = application::get_config().servers;
for (auto && [cookie, data]: servers)
if (auto intent = application::get_intent())
{
if (data.visible && (data.autoconnect || force_autoconnect) && data.compatible && autoconnect_enabled)
connect(configuration::server_data{
.manual = true,
.service = *intent,
});
}
else
{
const auto & servers = application::get_config().servers;
for (auto && [cookie, data]: servers)
{
autoconnect_enabled = false;
connect(data);
break;
if (data.visible && (data.autoconnect || force_autoconnect) && data.compatible && autoconnect_enabled)
{
autoconnect_enabled = false;
connect(data);
break;
}
}
}
}
@@ -644,8 +703,8 @@ void scenes::lobby::render(const XrFrameState & frame_state)
if (head_position)
new_gui_position = check_recenter_gui(head_position->first, head_position->second);
if (!new_gui_position)
new_gui_position = check_recenter_action(frame_state.predictedDisplayTime);
if (!new_gui_position and head_position)
new_gui_position = check_recenter_action(frame_state.predictedDisplayTime, head_position->first);
if (application::get_hand_tracking_supported())
{
@@ -685,33 +744,46 @@ void scenes::lobby::render(const XrFrameState & frame_state)
move_gui(head_position->first, *new_gui_position);
}
std::vector<XrCompositionLayerQuad> imgui_layers = draw_gui(frame_state.predictedDisplayTime);
std::vector<std::pair<int, XrCompositionLayerQuad>> imgui_layers = draw_gui(frame_state.predictedDisplayTime);
assert(renderer);
renderer->start_frame();
std::vector<XrCompositionLayerProjectionView> lobby_layer_views;
std::vector<XrCompositionLayerDepthInfoKHR> depthinfo_layer_views;
std::vector<XrCompositionLayerDepthTestFB> depthtest_layer_views;
std::vector<XrCompositionLayerDepthInfoKHR> lobby_depth_layer_views;
std::vector<XrCompositionLayerProjectionView> controllers_layer_views;
std::vector<XrCompositionLayerDepthInfoKHR> controllers_depth_layer_views;
std::array<float, 4> clear_color;
lobby_handle->visible = not application::get_config().passthrough_enabled;
if (application::get_config().passthrough_enabled)
clear_color = {0, 0, 0, 0};
else
clear_color = {0, 0.25, 0.5, 1};
if (composition_layer_depth_test_supported)
{
std::array<float, 4> clear_color;
lobby_handle->visible = not application::get_config().passthrough_enabled;
if (application::get_config().passthrough_enabled)
clear_color = {0, 0, 0, 0};
else
clear_color = {0, 0.25, 0.5, 1};
std::tie(lobby_layer_views, depthinfo_layer_views) = render_layer(views, swapchains_lobby, swapchains_depth, *renderer, *lobby_scene, clear_color);
for (auto [color, depth]: std::views::zip(lobby_layer_views, depthinfo_layer_views))
{
std::tie(lobby_layer_views, lobby_depth_layer_views) = render_layer(
views,
swapchains_lobby,
swapchains_lobby_depth,
*renderer,
*lobby_scene,
clear_color);
for (auto [color, depth]: std::views::zip(lobby_layer_views, lobby_depth_layer_views))
color.next = &depth;
std::tie(controllers_layer_views, controllers_depth_layer_views) = render_layer(
views,
swapchains_controllers,
swapchains_controllers_depth,
*renderer,
*controllers_scene,
{0, 0, 0, 0});
for (auto [color, depth]: std::views::zip(controllers_layer_views, controllers_depth_layer_views))
color.next = &depth;
}
renderer->end_frame();
@@ -719,23 +791,36 @@ void scenes::lobby::render(const XrFrameState & frame_state)
for (auto & swapchain: swapchains_lobby)
swapchain.release();
for (auto & swapchain: swapchains_depth)
for (auto & swapchain: swapchains_lobby_depth)
swapchain.release();
for (auto & swapchain: swapchains_controllers)
swapchain.release();
for (auto & swapchain: swapchains_controllers_depth)
swapchain.release();
}
else
{
if (not application::get_config().passthrough_enabled)
lobby_layer_views = render_layer(views, swapchains_lobby, *renderer, *lobby_scene, {0, 0.25, 0.5, 1});
lobby_layer_views = render_layer(
views,
swapchains_lobby,
*renderer,
*lobby_scene,
clear_color);
controllers_layer_views = render_layer(
views,
swapchains_controllers,
*renderer,
*controllers_scene,
{0, 0, 0, 0});
controllers_layer_views = render_layer(views, swapchains_controllers, *renderer, *controllers_scene, {0, 0, 0, 0});
renderer->end_frame();
// After end_frame because the command buffers are submitted in end_frame
if (not application::get_config().passthrough_enabled)
{
for (auto & swapchain: swapchains_lobby)
swapchain.release();
}
for (auto & swapchain: swapchains_lobby)
swapchain.release();
for (auto & swapchain: swapchains_controllers)
swapchain.release();
@@ -743,7 +828,7 @@ void scenes::lobby::render(const XrFrameState & frame_state)
XrCompositionLayerProjection lobby_layer{
.type = XR_TYPE_COMPOSITION_LAYER_PROJECTION,
.layerFlags = 0,
.layerFlags = XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT,
.space = world_space,
.viewCount = (uint32_t)lobby_layer_views.size(),
.views = lobby_layer_views.data(),
@@ -758,7 +843,7 @@ void scenes::lobby::render(const XrFrameState & frame_state)
};
XrEnvironmentBlendMode blend_mode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE;
std::vector<XrCompositionLayerBaseHeader *> layers_base;
std::vector<std::pair<int, XrCompositionLayerBaseHeader *>> layers_with_z_index;
if (application::get_config().passthrough_enabled)
{
@@ -771,7 +856,7 @@ void scenes::lobby::render(const XrFrameState & frame_state)
blend_mode = XR_ENVIRONMENT_BLEND_MODE_ALPHA_BLEND;
},
[&](auto & p) {
layers_base.push_back(p.layer());
layers_with_z_index.emplace_back(-2, p.layer());
}},
passthrough);
}
@@ -793,54 +878,51 @@ void scenes::lobby::render(const XrFrameState & frame_state)
},
};
// Add XrCompositionLayerDepthTestFB to lobby_layer and imgui_layer
// The sky in the lobby layer and the passthrough layer have the same depth, so the operation must be:
// - LESS when passthrough is enabled (to avoid overwriting the passthrough)
// - LESS_OR_EQUAL when passthrough is disabled (so that the sky is visible)
XrCompositionLayerDepthTestFB layer_depth_test{
.type = XR_TYPE_COMPOSITION_LAYER_DEPTH_TEST_FB,
.next = nullptr,
.depthMask = true,
.compareOp = application::get_config().passthrough_enabled ? XR_COMPARE_OP_LESS_FB : XR_COMPARE_OP_LESS_OR_EQUAL_FB,
};
// if (composition_layer_depth_test_supported or not application::get_config().passthrough_enabled)
layers_with_z_index.emplace_back(-1, reinterpret_cast<XrCompositionLayerBaseHeader *>(&lobby_layer));
for (auto & [z_index, layer]: imgui_layers)
layers_with_z_index.emplace_back(z_index, reinterpret_cast<XrCompositionLayerBaseHeader *>(&layer));
layers_with_z_index.emplace_back(1000, reinterpret_cast<XrCompositionLayerBaseHeader *>(&controllers_layer));
if (composition_layer_depth_test_supported)
{
layers_base.push_back(reinterpret_cast<XrCompositionLayerBaseHeader *>(&lobby_layer));
for (auto & layer: imgui_layers)
layers_base.push_back(reinterpret_cast<XrCompositionLayerBaseHeader *>(&layer));
// Add XrCompositionLayerDepthTestFB to lobby_layer and imgui_layer
// The sky in the lobby layer and the passthrough layer have the same depth, so the operation must be:
// - LESS when passthrough is enabled (to avoid overwriting the passthrough)
// - LESS_OR_EQUAL when passthrough is disabled (so that the sky is visible)
XrCompositionLayerDepthTestFB layer_depth_test{
.type = XR_TYPE_COMPOSITION_LAYER_DEPTH_TEST_FB,
.next = nullptr,
.depthMask = true,
.compareOp = application::get_config().passthrough_enabled ? XR_COMPARE_OP_LESS_FB : XR_COMPARE_OP_LESS_OR_EQUAL_FB};
lobby_layer.next = &layer_depth_test;
for (auto & layer: imgui_layers)
for (auto & [z_index, 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);
controllers_layer.next = &layer_depth_test;
}
else
if (imgui_ctx->is_modal_popup_shown() and composition_layer_color_scale_bias_supported)
{
if (not application::get_config().passthrough_enabled)
layers_base.push_back(reinterpret_cast<XrCompositionLayerBaseHeader *>(&lobby_layer));
for (auto & layer: imgui_layers)
layers_base.push_back(reinterpret_cast<XrCompositionLayerBaseHeader *>(&layer));
if (not composition_layer_depth_test_supported)
layers_base.push_back(reinterpret_cast<XrCompositionLayerBaseHeader *>(&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);
color_scale_bias.next = imgui_layers.front().second.next;
imgui_layers.front().second.next = &color_scale_bias;
}
std::ranges::stable_sort(layers_with_z_index);
std::vector<XrCompositionLayerBaseHeader *> layers;
layers.reserve(layers_with_z_index.size());
for (auto & [z_index, layer]: layers_with_z_index)
layers.push_back(layer);
session.end_frame(frame_state.predictedDisplayTime, layers, blend_mode);
}
void scenes::lobby::on_focused()
@@ -853,11 +935,10 @@ void scenes::lobby::on_focused()
uint32_t height = views[0].recommendedImageRectHeight;
swapchains_lobby.reserve(views.size());
swapchains_controllers.reserve(views.size());
if (composition_layer_depth_test_supported)
swapchains_depth.reserve(views.size());
else
swapchains_controllers.reserve(views.size());
swapchains_lobby_depth.reserve(views.size());
for ([[maybe_unused]] auto view: views)
{
@@ -865,10 +946,13 @@ void scenes::lobby::on_focused()
assert(view.recommendedImageRectHeight == height);
swapchains_lobby.emplace_back(session, device, swapchain_format, width, height);
swapchains_controllers.emplace_back(session, device, swapchain_format, width, height);
if (composition_layer_depth_test_supported)
swapchains_depth.emplace_back(session, device, depth_format, width, height);
else
swapchains_controllers.emplace_back(session, device, swapchain_format, width, height);
{
swapchains_lobby_depth.emplace_back(session, device, depth_format, width, height);
swapchains_controllers_depth.emplace_back(session, device, depth_format, width, height);
}
}
spdlog::info("Created lobby swapchains: {}x{}", width, height);
@@ -883,8 +967,7 @@ void scenes::lobby::on_focused()
lobby_handle = lobby_scene->new_node();
lobby_scene->import(loader("ground.gltf"), lobby_handle);
if (not composition_layer_depth_test_supported)
controllers_scene.emplace();
controllers_scene.emplace();
scene_data & controllers_scene_data = composition_layer_depth_test_supported ? *lobby_scene : *controllers_scene;
input = input_profile("controllers/" + choose_webxr_profile() + "/profile.json", loader, controllers_scene_data);
@@ -928,28 +1011,42 @@ void scenes::lobby::on_focused()
});
}
// 0.4mm / pixel
std::vector<imgui_context::viewport> vps{
{
// Main window
.space = xr::spaces::world,
.size = {0.6, 0.4},
.vp_origin = {0, 0},
.vp_size = {1500, 1000},
},
{
// Pop up window
.size = {0.6, 0.2},
.space = xr::spaces::world,
.size = {0.6, 0.24},
.vp_origin = {1500, 0},
.vp_size = {1500, 500},
.vp_size = {1500, 600},
},
{
// Virtual keyboard
.space = xr::spaces::world,
.size = {0.6, 0.2},
.vp_origin = {1500, 500},
.vp_origin = {1500, 600},
.vp_size = {1500, 500},
.always_show_cursor = true,
},
};
{
// Recenter tip
.space = xr::spaces::view,
.position = {0, -0.4, -1.0},
.orientation = {1, 0, 0, 0},
.size = {0.6, 0.12},
.vp_origin = {0, 1000},
.vp_size = {1500, 300},
.z_index = 2000,
}};
swapchain_imgui = xr::swapchain(session, device, swapchain_format, 3000, 1000);
swapchain_imgui = xr::swapchain(session, device, swapchain_format, 3000, 1300);
imgui_ctx.emplace(physical_device, device, queue_family_index, queue, imgui_inputs, swapchain_imgui, vps);
@@ -1008,7 +1105,8 @@ void scenes::lobby::on_unfocused()
renderer.reset();
swapchains_lobby.clear();
swapchains_controllers.clear();
swapchains_depth.clear();
swapchains_lobby_depth.clear();
swapchains_controllers_depth.clear();
swapchain_imgui = xr::swapchain();
passthrough.emplace<std::monostate>();
multicast.reset();
+14 -4
View File
@@ -39,6 +39,10 @@
#include "render/scene_renderer.h"
#include "utils/async.h"
#ifdef __ANDROID__
#include "android/battery.h"
#endif
class wivrn_session;
namespace scenes
@@ -79,8 +83,9 @@ class lobby : public scene_impl<lobby>
ImGuiID hovered_item;
std::vector<xr::swapchain> swapchains_lobby;
std::vector<xr::swapchain> swapchains_lobby_depth;
std::vector<xr::swapchain> swapchains_controllers;
std::vector<xr::swapchain> swapchains_depth;
std::vector<xr::swapchain> swapchains_controllers_depth;
xr::swapchain swapchain_imgui;
vk::Format swapchain_format;
vk::Format depth_format;
@@ -90,18 +95,23 @@ class lobby : public scene_impl<lobby>
void update_server_list();
std::vector<XrCompositionLayerQuad> draw_gui(XrTime predicted_display_time);
std::vector<std::pair<int, XrCompositionLayerQuad>> draw_gui(XrTime predicted_display_time);
XrAction recenter_left_action = XR_NULL_HANDLE;
XrAction recenter_right_action = XR_NULL_HANDLE;
std::optional<glm::vec3> gui_recenter_position;
std::optional<float> gui_recenter_distance;
bool recenter_gui = true;
void move_gui(glm::vec3 head_position, glm::vec3 new_gui_position);
void tooltip(std::string_view text);
#ifdef __ANDROID__
battery battery_tracker;
#endif
enum class tab
{
server_list,
new_server,
settings,
about,
licenses,
@@ -130,7 +140,7 @@ class lobby : public scene_impl<lobby>
void connect(const configuration::server_data & data);
std::optional<glm::vec3> check_recenter_gesture(const std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT> & joints);
std::optional<glm::vec3> check_recenter_action(XrTime predicted_display_time);
std::optional<glm::vec3> check_recenter_action(XrTime predicted_display_time, glm::vec3 head_position);
std::optional<glm::vec3> check_recenter_gui(glm::vec3 head_position, glm::quat head_orientation);
public:
+250 -43
View File
@@ -21,12 +21,15 @@
#include "application.h"
#include "asset.h"
#include "configuration.h"
#include "imgui.h"
#include "imgui_internal.h"
#include "implot.h"
#include "lobby.h"
#include "stream.h"
#include "version.h"
#include <glm/gtc/quaternion.hpp>
#include <ranges>
#include <spdlog/fmt/fmt.h>
#include <utils/strings.h>
@@ -63,6 +66,74 @@ static void CenterTextHV(const std::string & text)
ImGui::PopStyleVar();
}
static void display_recentering_tip(imgui_context & ctx, const std::string & tip)
{
ImGui::PushFont(ctx.large_font);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {20, 20});
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 2);
ImGui::SetNextWindowPos(ctx.layers()[3].vp_center(), ImGuiCond_Always, {0.5, 0.5});
ImGui::Begin("Recentering tip", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysAutoResize);
std::vector<std::string> lines = utils::split(tip);
std::vector<float> widths;
float max_width = 0;
for (auto & line: lines)
{
widths.push_back(ImGui::CalcTextSize(line.c_str()).x);
max_width = std::max(max_width, widths.back());
}
for (auto [width, line]: std::views::zip(widths, lines))
{
ImGui::Dummy({(max_width - width) / 2, 0});
ImGui::SameLine();
ImGui::TextUnformatted(line.data(), line.data() + line.size());
}
ImGui::End();
ImGui::PopStyleVar(2);
ImGui::PopFont();
}
void scenes::lobby::tooltip(std::string_view text)
{
// FIXME: this is incorrect if we use the docking branch of imgui
ImGuiViewport * viewport = ImGui::GetMainViewport();
auto & layer = imgui_ctx->layer(ImGui::GetMousePos());
auto pos_backup = viewport->Pos;
auto size_backup = viewport->Size;
viewport->Pos = ImVec2(layer.vp_origin.x, layer.vp_origin.y);
viewport->Size = ImVec2(layer.vp_size.x, layer.vp_size.y);
ImVec2 pos{
(ImGui::GetItemRectMin().x + ImGui::GetItemRectMax().x) / 2,
ImGui::GetItemRectMin().y - 10,
};
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {5, 5});
// Clamp position to avoid overflowing on 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::min(pos.x, viewport->Pos.x + viewport->Size.x - size.x / 2);
ImGui::SetNextWindowPos(pos, ImGuiCond_Always, {0.5, 1});
if (ImGui::BeginTooltip())
{
ImGui::PushStyleColor(ImGuiCol_Text, 0xffffffff);
ImGui::TextUnformatted(text.data(), text.data() + text.size());
ImGui::PopStyleColor();
ImGui::EndTooltip();
}
ImGui::PopStyleVar();
viewport->Pos = pos_backup;
viewport->Size = size_backup;
}
void scenes::lobby::vibrate_on_hover()
{
if (ImGui::IsItemHovered())
@@ -78,6 +149,8 @@ void scenes::lobby::gui_connecting()
{
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);
}
@@ -101,7 +174,10 @@ void scenes::lobby::gui_connecting()
ImGui::Dummy({1000, 1});
ImGui::PushFont(imgui_ctx->large_font);
CenterTextH(fmt::format(_F("Connection to {}"), server_name));
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());
@@ -122,13 +198,14 @@ void scenes::lobby::gui_connecting()
void scenes::lobby::gui_new_server()
{
const ImVec2 button_size(220, 80);
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 10);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, {20, 20});
ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, {10, 10});
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, {10, 10});
ImGui::Dummy({1000, 1});
const ImVec2 button_size(220, 80);
ImGui::BeginTable("table", 2);
ImGui::TableSetupColumn("Field name", ImGuiTableColumnFlags_WidthFixed);
@@ -166,26 +243,27 @@ void scenes::lobby::gui_new_server()
ImGui::EndTable();
ImGui::Checkbox("TCP only", &add_server_tcp_only);
ImGui::Checkbox(_S("TCP only"), &add_server_tcp_only);
vibrate_on_hover();
auto top_left = ImGui::GetWindowContentRegionMin();
auto bottom_right = ImGui::GetWindowContentRegionMax();
ImGui::SetCursorPosX(top_left.x);
if (ImGui::Button("Cancel", button_size))
if (ImGui::Button(_S("Cancel"), button_size))
{
current_tab = tab::server_list;
strcpy(add_server_window_prettyname, "");
strcpy(add_server_window_hostname, "");
add_server_window_port = wivrn::default_port;
add_server_tcp_only = false;
ImGui::CloseCurrentPopup();
}
vibrate_on_hover();
ImGui::SameLine(bottom_right.x - button_size.x);
if (ImGui::Button("Save", button_size))
if (ImGui::Button(_S("Save"), button_size))
{
current_tab = tab::server_list;
configuration::server_data data{
@@ -206,10 +284,11 @@ void scenes::lobby::gui_new_server()
strcpy(add_server_window_hostname, "");
add_server_window_port = wivrn::default_port;
add_server_tcp_only = false;
ImGui::CloseCurrentPopup();
}
vibrate_on_hover();
ImGui::PopStyleVar(3);
ImGui::PopStyleVar(4); // ImGuiStyleVar_FrameRounding, ImGuiStyleVar_ItemSpacing, ImGuiStyleVar_CellPadding, ImGuiStyleVar_FramePadding
}
void scenes::lobby::gui_server_list()
@@ -244,17 +323,20 @@ void scenes::lobby::gui_server_list()
for (const auto & [name, cookie]: sorted_cookies)
{
configuration::server_data & data = config.servers.at(cookie);
bool is_selected = (cookie == selected_item);
// bool is_selected = (cookie == selected_item);
ImGui::SetCursorPos(pos);
ImGui::SetNextItemAllowOverlap();
// ImGui::SetNextItemAllowOverlap();
// TODO custom widget
if (ImGui::Selectable(("##" + cookie).c_str(), is_selected, ImGuiSelectableFlags_None, ImVec2(0, list_item_height)))
selected_item = cookie;
// if (ImGui::Selectable(("##" + cookie).c_str(), is_selected, ImGuiSelectableFlags_None, ImVec2(0, list_item_height)))
// selected_item = cookie;
ImGui::SetCursorPos(ImVec2(pos.x, pos.y));
if (data.manual)
ImGui::SetCursorPos(ImVec2(pos.x, pos.y + 25)); // FIXME compute the position correctly
else
ImGui::SetCursorPos(ImVec2(pos.x, pos.y));
ImGui::Text("%s", name.c_str());
if (!data.manual)
@@ -266,13 +348,7 @@ void scenes::lobby::gui_server_list()
vibrate_on_hover();
}
// TODO
// if (ImGui::IsItemHovered())
// {
// ImGui::SetTooltip("Tooltip");
// }
ImVec2 button_position(ImGui::GetWindowContentRegionMax().x - style.WindowPadding.x, pos.y + (list_item_height - button_size.y) / 2);
ImVec2 button_position(ImGui::GetWindowContentRegionMax().x, pos.y + (list_item_height - button_size.y) / 2);
button_position.x -= button_size.x + style.WindowPadding.x;
ImGui::SetCursorPos(button_position);
@@ -302,9 +378,9 @@ void scenes::lobby::gui_server_list()
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
{
if (!data.compatible && !data.manual)
ImGui::SetTooltip("%s", _S("Incompatible server version"));
tooltip(_("Incompatible server version"));
else if (!data.visible && !data.manual)
ImGui::SetTooltip("%s", _S("Server not available"));
tooltip(_("Server not available"));
}
ImGui::PopStyleColor(3);
@@ -350,6 +426,14 @@ void scenes::lobby::gui_server_list()
gui_connecting();
ImGui::EndPopup();
}
ImGui::SetNextWindowSize({800, 0});
ImGui::SetNextWindowPos({popup_layer_center.x, popup_layer_center.y}, ImGuiCond_FirstUseEver, ImVec2(0.5f, 0.5f));
if (ImGui::BeginPopupModal("add server", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysAutoResize))
{
gui_new_server();
ImGui::EndPopup();
}
ImGui::PopStyleVar(3);
}
@@ -459,7 +543,7 @@ void scenes::lobby::gui_settings()
config.save();
vibrate_on_hover();
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", _S("Overlay can be toggled by pressing both thumbsticks"));
tooltip(_("Overlay can be toggled by pressing both thumbsticks"));
ImGui::PopStyleVar();
@@ -626,21 +710,29 @@ static bool RadioButtonWithoutCheckBox(const std::string & label, T * v, T v_but
return pressed;
}
static void ScrollWhenDraggingOnVoid(ImVec2 delta)
static void ScrollWhenDraggingOnVoid()
{
ImVec2 delta{0.0f, -ImGui::GetIO().MouseDelta.y};
// https://github.com/ocornut/imgui/issues/3379#issuecomment-1678718752
ImGuiContext & g = *ImGui::GetCurrentContext();
ImGuiWindow * window = g.CurrentWindow;
bool hovered = false;
bool held = false;
static bool held_prev = false;
// Don't drag for the first frame because the current controller might have just changed and have a large delta
ImGuiID id = window->GetID("##scrolldraggingoverlay");
ImGui::KeepAliveID(id);
if (g.HoveredId == 0) // If nothing hovered so far in the frame (not same as IsAnyItemHovered()!)
ImGui::ButtonBehavior(window->Rect(), id, &hovered, &held, ImGuiButtonFlags_MouseButtonLeft);
if (held && delta.x != 0.0f)
if (held and held_prev and delta.x != 0.0f)
ImGui::SetScrollX(window, window->Scroll.x + delta.x);
if (held && delta.y != 0.0f)
if (held and held_prev and delta.y != 0.0f)
ImGui::SetScrollY(window, window->Scroll.y + delta.y);
held_prev = held;
}
static auto face_weights()
@@ -848,13 +940,15 @@ static const char * get_face_icon(XrTime predicted_display_time)
void scenes::lobby::draw_features_status(XrTime predicted_display_time)
{
const float win_width = ImGui::GetWindowSize().x;
const float win_width = ImGui::GetContentRegionAvail().x;
float text_width = 0;
auto & config = application::get_config();
struct item
{
feature f;
std::string tooltip_enabled;
std::string tooltip_disabled;
const char * icon_enabled;
const char * icon_disabled = ICON_FA_SLASH;
bool enabled;
@@ -864,6 +958,8 @@ void scenes::lobby::draw_features_status(XrTime predicted_display_time)
items.push_back({
.f = feature::microphone,
.tooltip_enabled = _("Microphone is enabled"),
.tooltip_disabled = _("Microphone is disabled"),
.icon_enabled = ICON_FA_MICROPHONE,
.icon_disabled = ICON_FA_MICROPHONE_SLASH,
});
@@ -872,6 +968,8 @@ void scenes::lobby::draw_features_status(XrTime predicted_display_time)
{
items.push_back({
.f = feature::hand_tracking,
.tooltip_enabled = _("Hand tracking is enabled"),
.tooltip_disabled = _("Hand tracking is disabled"),
.icon_enabled = ICON_FA_HAND,
});
}
@@ -880,6 +978,8 @@ void scenes::lobby::draw_features_status(XrTime predicted_display_time)
{
items.push_back({
.f = feature::eye_gaze,
.tooltip_enabled = _("Eye tracking is enabled"),
.tooltip_disabled = _("Eye tracking is disabled"),
.icon_enabled = ICON_FA_EYE,
.icon_disabled = ICON_FA_EYE_SLASH,
});
@@ -889,6 +989,8 @@ void scenes::lobby::draw_features_status(XrTime predicted_display_time)
{
items.push_back({
.f = feature::face_tracking,
.tooltip_enabled = _("Face tracking is enabled"),
.tooltip_disabled = _("Face tracking is disabled"),
.icon_enabled = get_face_icon(predicted_display_time),
.icon_disabled = ICON_FA_FACE_MEH_BLANK,
});
@@ -904,6 +1006,13 @@ void scenes::lobby::draw_features_status(XrTime predicted_display_time)
const ImGuiStyle & style = ImGui::GetStyle();
text_width += items.size() * style.FramePadding.x * 2;
// New server button
if (ImGui::Button(_S("Add server")) && !ImGui::IsPopupOpen("add server"))
ImGui::OpenPopup("add server");
vibrate_on_hover();
ImGui::SameLine();
// Enabled features
ImGui::SetCursorPosX((win_width - text_width) / 2);
for (auto & i: items)
{
@@ -919,25 +1028,121 @@ void scenes::lobby::draw_features_status(XrTime predicted_display_time)
config.set_feature(i.f, not i.enabled);
config.save();
}
vibrate_on_hover();
if (ImGui::IsItemHovered())
tooltip(i.enabled ? i.tooltip_enabled : i.tooltip_disabled);
if (i.icon_disabled == std::string_view(ICON_FA_SLASH) and not i.enabled)
{
auto save = ImGui::GetCursorPos();
auto save = ImGui::GetCurrentWindow()->DC;
ImGui::SetCursorPos(pos + ImGui::GetStyle().FramePadding);
ImGui::Text("%s", i.icon_enabled);
ImGui::SetCursorPos(save);
ImGui::GetCurrentWindow()->DC = save;
}
vibrate_on_hover();
ImGui::PopStyleColor(2);
ImGui::PopStyleVar();
}
float PrevLineSize = ImGui::GetCurrentWindow()->DC.PrevLineSize.y;
float PrevLineTextBaseOffset = ImGui::GetCurrentWindow()->DC.PrevLineTextBaseOffset;
#ifdef __ANDROID__
// Battery status
auto status = battery_tracker.get();
const char * battery_icon = nullptr;
if (status.charge)
{
int icon_nr;
if (status.charging)
{
if (*status.charge > 0.995)
icon_nr = 5;
else
icon_nr = application::now() / 500'000'000 % 5;
}
else
icon_nr = std::round((*status.charge) * 4);
switch (icon_nr)
{
case 0:
battery_icon = ICON_FA_BATTERY_EMPTY;
break;
case 1:
battery_icon = ICON_FA_BATTERY_QUARTER;
break;
case 2:
battery_icon = ICON_FA_BATTERY_HALF;
break;
case 3:
battery_icon = ICON_FA_BATTERY_THREE_QUARTERS;
break;
case 4:
battery_icon = ICON_FA_BATTERY_FULL;
break;
case 5:
battery_icon = ICON_FA_PLUG;
}
ImGui::SameLine();
// Always use the longest width for layout
float max_battery_width = ImGui::CalcTextSize(ICON_FA_BATTERY_FULL "100%").x;
ImVec4 battery_color = ImGui::GetStyleColorVec4(ImGuiCol_Text);
if (*status.charge < 0.2)
battery_color = ImVec4(1, 0, 0, 1);
ImGui::SetCursorPosX(win_width - max_battery_width - style.WindowPadding.x);
ImGui::TextColored(battery_color, "%s %d%%", battery_icon, (int)std::round(*status.charge * 100));
}
#endif
ImGui::Dummy({0, 15});
}
void scenes::lobby::gui_keyboard()
{
keyboard.display(hovered_item);
auto & config = application::get_config();
if (keyboard.get_layout() != config.virtual_keyboard_layout)
{
config.virtual_keyboard_layout = keyboard.get_layout();
config.save();
}
}
std::vector<XrCompositionLayerQuad> scenes::lobby::draw_gui(XrTime predicted_display_time)
static bool is_gui_visible(imgui_context & ctx, XrTime predicted_display_time)
{
// Get the GUI position in the view reference frame
if (auto pos = application::locate_controller(application::space(xr::spaces::world), application::space(xr::spaces::view), predicted_display_time))
{
glm::vec3 view_gui_position = pos->first + pos->second * ctx.layers()[0].position;
float gui_distance = glm::length(view_gui_position);
glm::vec3 direction = view_gui_position / gui_distance;
if (view_gui_position.z > 0 or view_gui_position.z < -1.5)
return false;
if (std::abs(direction.x) > 0.8)
return false;
if (std::abs(direction.y) > 0.8)
return false;
return true;
}
return true;
}
std::vector<std::pair<int, XrCompositionLayerQuad>> scenes::lobby::draw_gui(XrTime predicted_display_time)
{
for (const auto & [key, server]: application::get_config().servers)
{
@@ -955,7 +1160,6 @@ std::vector<XrCompositionLayerQuad> scenes::lobby::draw_gui(XrTime predicted_dis
if (ImGui::GetIO().WantTextInput)
{
ImGui::SetNextWindowPos(imgui_ctx->layers()[2].vp_center(), ImGuiCond_Always, {0.5, 0.5});
gui_keyboard();
}
@@ -972,18 +1176,18 @@ std::vector<XrCompositionLayerQuad> scenes::lobby::draw_gui(XrTime predicted_dis
{
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 10);
ImGui::BeginChild("Main", ImVec2(ImGui::GetWindowSize().x - ImGui::GetCursorPosX() - 20, 0));
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {20, 20});
ImGui::BeginChild("Main", ImVec2(ImGui::GetWindowSize().x - ImGui::GetCursorPosX(), 0));
ImGui::SetCursorPosY(20);
switch (current_tab)
{
case tab::server_list:
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, {10, 10});
draw_features_status(predicted_display_time);
gui_server_list();
break;
ImGui::PopStyleVar();
case tab::new_server:
gui_new_server();
gui_server_list();
break;
case tab::settings:
@@ -1011,15 +1215,13 @@ std::vector<XrCompositionLayerQuad> scenes::lobby::draw_gui(XrTime predicted_dis
ImGui::Dummy(ImVec2(0, 20));
ImVec2 mouse_delta = ImGui::GetIO().MouseDelta;
ScrollWhenDraggingOnVoid(ImVec2(0.0f, -mouse_delta.y));
ScrollWhenDraggingOnVoid();
ImGui::EndChild();
ImGui::PopStyleVar(); // ImGuiStyleVar_FrameRounding
ImGui::PopStyleVar(2); // ImGuiStyleVar_FrameRounding, ImGuiStyleVar_WindowPadding
}
ImGui::PushStyleColor(ImGuiCol_ChildBg, IM_COL32(0, 0, 0, 255));
ImGui::SetCursorPos(style.WindowPadding);
{
ImGui::BeginChild("Tabs", {TabWidth, ImGui::GetContentRegionMax().y - ImGui::GetWindowContentRegionMin().y});
@@ -1027,9 +1229,6 @@ std::vector<XrCompositionLayerQuad> scenes::lobby::draw_gui(XrTime predicted_dis
RadioButtonWithoutCheckBox(ICON_FA_COMPUTER " " + _("Server list"), &current_tab, tab::server_list, {TabWidth, 0});
vibrate_on_hover();
RadioButtonWithoutCheckBox(ICON_FA_COMPUTER " " + _("New server"), &current_tab, tab::new_server, {TabWidth, 0});
vibrate_on_hover();
RadioButtonWithoutCheckBox(ICON_FA_GEARS " " + _("Settings"), &current_tab, tab::settings, {TabWidth, 0});
vibrate_on_hover();
@@ -1051,6 +1250,14 @@ std::vector<XrCompositionLayerQuad> scenes::lobby::draw_gui(XrTime predicted_dis
ImGui::PopStyleColor(); // ImGuiCol_WindowBg
ImGui::PopStyleVar(2); // ImGuiStyleVar_WindowPadding, ImGuiStyleVar_ScrollbarSize
if (not is_gui_visible(*imgui_ctx, predicted_display_time))
{
if (application::get_hand_tracking_supported())
display_recentering_tip(*imgui_ctx, _("Press A or X or put your palm up\nto move the main window"));
else
display_recentering_tip(*imgui_ctx, _("Press A or X to move the main window"));
}
if (hovered_item != last_hovered && hovered_item != 0)
{
size_t controller = imgui_ctx->get_focused_controller();
+299 -303
View File
@@ -19,148 +19,148 @@
#define IMGUI_DEFINE_MATH_OPERATORS
#include <array>
#include <cassert>
#include <cctype>
#include <limits>
#include <spdlog/spdlog.h>
#include <string_view>
#include <unicode/uchar.h>
#include <uni_algo/case.h>
#include <uni_algo/ranges_grapheme.h>
#include <utility>
#include "IconsFontAwesome6.h"
#include "imgui.h"
#include "imgui_internal.h"
#include "lobby_keyboard.h"
// #include <stdexcept>
// #include <cwchar>
// #include <ranges>
// #include <unicode/ustring.h> // u_strToUpper
static const ImGuiKey key_layout = (ImGuiKey)(ImGuiKey_NamedKey_END + 1);
static const ImGuiKey key_symbols_letter = (ImGuiKey)(ImGuiKey_NamedKey_END + 2);
// See https://github.com/qt/qtvirtualkeyboard/blob/dev/src/layouts/fallback/main.qml
virtual_keyboard::layout qwerty = {
{
{0.5, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
{1, u"q"},
{1, u"w"},
{1, u"eéèêë"},
{1, u"rŕrř"},
{1, u"tţtŧť"},
{1, u"yÿyýŷ"},
{1, u"uűūũûüuùú"},
{1, u"iîïīĩiìí"},
{1, u"oœøõôöòó"},
{1, u"p"},
{1.5, u"", ImGuiKey_Backspace, ICON_FA_DELETE_LEFT, virtual_keyboard::key_flag_repeat},
{0.5},
{1, "q"},
{1, "w"},
{1, "eéèêë"},
{1, "rŕrř"},
{1, "tţtŧť"},
{1, "yÿyýŷ"},
{1, "uűūũûüuùú"},
{1, "iîïīĩiìí"},
{1, "oœøõôöòó"},
{1, "p"},
{1.5, ICON_FA_DELETE_LEFT, ImGuiKey_Backspace, virtual_keyboard::key_flag_repeat},
}, // 12
{
{1, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
{1, u"aaäåãâàá"},
{1, u"sšsşś"},
{1, u"ddđď"},
{1, u"f"},
{1, u"gġgģĝğ"},
{1, u"h"},
{1, u"j"},
{1, u"k"},
{1, u"lĺŀłļľl"},
{2, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
{1},
{1, "aaäåãâàá"},
{1, "sšsşś"},
{1, "ddđď"},
{1, "f"},
{1, "gġgģĝğ"},
{1, "h"},
{1, "j"},
{1, "k"},
{1, "lĺŀłļľl"},
{2},
}, // 12
{
{1.5, u"", ImGuiKey_LeftShift},
{1, u"zzžż"},
{1, u"x"},
{1, u"cçcċčć"},
{1, u"v"},
{1, u"b"},
{1, u"nņńnň"},
{1, u"m"},
{1, u","},
{1, u"."},
{1.5, "", ImGuiKey_LeftShift},
{1, "zzžż"},
{1, "x"},
{1, "cçcċčć"},
{1, "v"},
{1, "b"},
{1, "nņńnň"},
{1, "m"},
{1, ","},
{1, "."},
// {1, u"-"},
{1.5, u"", ImGuiKey_RightShift},
{1.5, "", ImGuiKey_RightShift},
}, // 12
{
{2, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
{1, u"", key_symbols_letter, "?123"},
{1, u"", key_layout, ICON_FA_GLOBE},
{5, u" "},
{3, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
{2},
{1, "?123", key_symbols_letter},
{1, ICON_FA_GLOBE, key_layout},
{5, " "},
{3},
} // 12
};
// See https://github.com/qt/qtvirtualkeyboard/blob/dev/src/layouts/fr_FR/main.qml
virtual_keyboard::layout azerty = {
{
{0.5, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
{1, u"aàâæ"},
{1, u"z"},
{1, u"eéèêë"},
{1, u"r"},
{1, u"t"},
{1, u"yÿ"},
{1, u"uùûü"},
{1, u"iîï"},
{1, u"oôœ"},
{1, u"p"},
{1.5, u"", ImGuiKey_Backspace, ICON_FA_DELETE_LEFT, virtual_keyboard::key_flag_repeat},
{0.5},
{1, "aàâæ"},
{1, "z"},
{1, "eéèêë"},
{1, "r"},
{1, "t"},
{1, "yÿ"},
{1, "uùûü"},
{1, "iîï"},
{1, "oôœ"},
{1, "p"},
{1.5, ICON_FA_DELETE_LEFT, ImGuiKey_Backspace, virtual_keyboard::key_flag_repeat},
}, // 12
{
{1, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
{1, u"q"},
{1, u"s"},
{1, u"d"},
{1, u"f"},
{1, u"g"},
{1, u"h"},
{1, u"j"},
{1, u"k"},
{1, u"l"},
{1, u"m"},
{1, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
{1},
{1, "q"},
{1, "s"},
{1, "d"},
{1, "f"},
{1, "g"},
{1, "h"},
{1, "j"},
{1, "k"},
{1, "l"},
{1, "m"},
// {1, "é"},
{1},
}, // 12
{
{1.5, u"", ImGuiKey_LeftShift},
{1, u"w"},
{1, u"x"},
{1, u"cç"},
{1, u"v"},
{1, u"b"},
{1, u"n"},
{1, u","},
{1, u"."},
{1, u"-"},
{1.5, u"", ImGuiKey_RightShift},
{1.5, "", ImGuiKey_LeftShift},
{1, "w"},
{1, "x"},
{1, "cç"},
{1, "v"},
{1, "b"},
{1, "n"},
{1, ","},
{1, "."},
{1, "-"},
{1.5, "", ImGuiKey_RightShift},
}, // 12
{
{2, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
{1, u"", key_symbols_letter, "?123"},
{1, u"", key_layout, ICON_FA_GLOBE},
{5, u" "},
{3, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
{2},
{1, "?123", key_symbols_letter},
{1, ICON_FA_GLOBE, key_layout},
{5, " "},
{3},
} // 12
};
virtual_keyboard::layout digits = {
{
{1, u"1"},
{1, u"2"},
{1, u"3"},
{1, "1"},
{1, "2"},
{1, "3"},
}, // 3
{
{1, u"4"},
{1, u"5"},
{1, u"6"},
{1, "4"},
{1, "5"},
{1, "6"},
}, // 3
{
{1, u"7"},
{1, u"8"},
{1, u"9"},
{1, "7"},
{1, "8"},
{1, "9"},
}, // 3
{
{2, u"0"},
{1, u"", ImGuiKey_Backspace, ICON_FA_DELETE_LEFT, virtual_keyboard::key_flag_repeat},
{2, "0"},
{1, ICON_FA_DELETE_LEFT, ImGuiKey_Backspace, virtual_keyboard::key_flag_repeat},
} // 3
};
@@ -168,120 +168,126 @@ virtual_keyboard::layout digits = {
// virtual_keyboard::layout qwertz = {
// };
std::array layouts = {&qwerty, &azerty};
virtual_keyboard::layout symbols = {
{
{0.5, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
{1, u"1"},
{1, u"2"},
{1, u"3"},
{1, u"4"},
{1, u"5"},
{1, u"6"},
{1, u"7"},
{1, u"8"},
{1, u"9"},
{1, u"0"},
{1.5, u"", ImGuiKey_Backspace, ICON_FA_DELETE_LEFT, virtual_keyboard::key_flag_repeat},
{0.5},
{1, "1"},
{1, "2"},
{1, "3"},
{1, "4"},
{1, "5"},
{1, "6"},
{1, "7"},
{1, "8"},
{1, "9"},
{1, "0"},
{1.5, ICON_FA_DELETE_LEFT, ImGuiKey_Backspace, virtual_keyboard::key_flag_repeat},
}, // 12
{
{1, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
{1, u"@"},
{1, u"#"},
{1, u"%"},
{1, u"&"},
{1, u"*"},
{1, u"_"},
{1, u"-"},
{1, u"+"},
{1, u"("},
{1, u")"},
{1, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
{1},
{1, "@"},
{1, "#"},
{1, "%"},
{1, "&"},
{1, "*"},
{1, "_"},
{1, "-"},
{1, "+"},
{1, "("},
{1, ")"},
{1},
}, // 12
{
{1.5, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
{1, u"", ImGuiKey_None, "1/2", virtual_keyboard::key_flag_hidden},
{1, u"\""},
{1, u"<"},
{1, u">"},
{1, u"'"},
{1, u":"},
{1, u"/"},
{1, u"!"},
{1, u"?"},
{1.5, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
{1.5},
{1}, // {1, "", ImGuiKey_None, "1/2", virtual_keyboard::key_flag_hidden},
{1, "\""},
{1, "<"},
{1, ">"},
{1, "'"},
{1, ":"},
{1, "/"},
{1, "!"},
{1, "?"},
{1.5},
}, // 12
{
{1, u"", key_symbols_letter, "ABC"},
{1, u"", key_layout, ICON_FA_GLOBE},
{5, u" "},
{3, u"", ImGuiKey_None, "", virtual_keyboard::key_flag_hidden},
{1, "ABC", key_symbols_letter},
{1, ICON_FA_GLOBE, key_layout},
{5, " "},
{3},
},
};
static std::string to_utf8(char32_t c)
{
std::string s;
std::array layouts = {
std::make_pair<std::string_view, virtual_keyboard::layout *>("QWERTY", &qwerty),
std::make_pair<std::string_view, virtual_keyboard::layout *>("AZERTY", &azerty),
};
if (c < 0x80)
virtual_keyboard::key::key(float width, std::string_view characters, ImGuiKey keycode, key_flags flag) :
width(width),
keycode(keycode),
flag(flag)
{
if (keycode == ImGuiKey_None)
{
s += (char)c;
}
else if (c < 0x800)
{
s += (char)(0xc0 | ((c >> 6) & 0x1f));
s += (char)(0x80 | (c & 0x3f));
}
else if (c < 0x10000)
{
s += (char)(0xe0 | ((c >> 12) & 0x0f));
s += (char)(0x80 | ((c >> 6) & 0x3f));
s += (char)(0x80 | (c & 0x3f));
// Normal key: the first character is displayed on the key
characters_lower = characters;
characters_upper = una::cases::to_uppercase_utf8(characters);
glyph_lower = *una::ranges::views::grapheme::utf8(characters_lower).begin();
glyph_upper = *una::ranges::views::grapheme::utf8(characters_upper).begin();
}
else
{
s += (char)(0xf0 | ((c >> 18) & 0x07));
s += (char)(0x80 | ((c >> 12) & 0x3f));
s += (char)(0x80 | ((c >> 6) & 0x3f));
s += (char)(0x80 | (c & 0x3f));
// Special key: characters contains the key label, except shift which depends
// on caps lock
assert(not characters.empty() or keycode == ImGuiKey_LeftShift or keycode == ImGuiKey_RightShift);
glyph_lower = (std::string)characters;
glyph_upper = (std::string)characters;
}
return s;
}
// static std::string to_utf8(std::u16string_view str)
// {
// std::string s;
// for(char16_t c: str)
// s += to_utf8(c);
//
// return s;
// }
//
// static std::u16string str_to_upper(std::u16string_view text)
// {
// UErrorCode err = U_ZERO_ERROR;
//
// size_t size = u_strToUpper(nullptr, 0, text.data(), text.size(), "" /* locale*/, &err);
//
// if (U_FAILURE(err))
// throw std::runtime_error(std::string("u_strToUpper: ") + u_errorName(err));
//
// std::u16string upper;
// upper.resize(size);
//
// err = U_ZERO_ERROR;
// u_strToUpper(nullptr, 0, text.data(), text.size(), "" /* locale*/, &err);
//
// if (U_FAILURE(err))
// throw std::runtime_error(std::string("u_strToUpper: ") + u_errorName(err));
//
// return upper;
// }
virtual_keyboard::key::key(float width) :
width(width),
keycode(ImGuiKey_None),
flag(virtual_keyboard::key_flag_hidden)
{
}
// Reimplemented because ImGui::IsWindowHovered() returns false if a popup is open
bool virtual_keyboard::is_window_hovered()
{
int i = 0;
ImGuiContext & g = *GImGui;
if (not ImGui::IsMouseHoveringRect(g.CurrentWindow->Pos, g.CurrentWindow->Pos + g.CurrentWindow->Size))
return false;
// Ignore any window below the keyboard
while (g.Windows[i] != g.CurrentWindow and i < g.Windows.Size)
i++;
// Cannot find the keyboard window
if (i >= g.Windows.Size)
return false;
// Skip the keyboard window
i++;
// Check if another window is over the keyboard
while (i < g.Windows.Size)
{
if (ImGui::IsMouseHoveringRect(g.Windows[i]->Pos, g.Windows[i]->Pos + g.Windows[i]->Size))
return false;
i++;
}
return true;
}
// Mostly copied from ImGui::ButtonBehavior, massively simplified for the keyboard use case, don't take focus when clicked
bool virtual_keyboard::button_behavior(const ImRect & bb, ImGuiID id, bool * out_hovered, bool * out_held, ImGuiButtonFlags flags)
virtual_keyboard::key_status virtual_keyboard::button_behavior(const ImRect & bb, ImGuiID id, bool window_hovered, ImGuiButtonFlags flags)
{
ImGuiContext & g = *GImGui;
@@ -294,37 +300,31 @@ bool virtual_keyboard::button_behavior(const ImRect & bb, ImGuiID id, bool * out
}
bool pressed = false;
bool hovered = g.HoveredWindow == g.CurrentWindow and ImGui::IsMouseHoveringRect(bb.Min, bb.Max);
// ImGui::ItemHoverable;
bool hovered = window_hovered and ImGui::IsMouseHoveringRect(bb.Min, bb.Max);
bool mouse_down = ImGui::IsMouseDown(0, id);
// Mouse handling
if (hovered)
if (hovered and mouse_down and active_id != id)
{
// Changed wrt original imgui: only 1 button is present
bool mouse_button_clicked = ImGui::IsMouseClicked(0, ImGuiInputFlags_None, id);
active_id = id;
held_duration = 0;
// Process initial action
if (mouse_button_clicked && active_id != id)
{
active_id = id;
held_duration = 0;
if (flags & ImGuiButtonFlags_PressedOnClick)
pressed = true;
}
if (flags & ImGuiButtonFlags_PressedOnClick)
pressed = true;
}
// Process while held
bool held = false;
if (active_id == id)
{
if (ImGui::IsMouseDown(0, id))
if (mouse_down)
{
held = true;
float prev_held_duration = held_duration;
held_duration += g.IO.DeltaTime;
if (flags & ImGuiButtonFlags_Repeat and held_duration > g.IO.KeyRepeatDelay)
if ((flags & ImGuiButtonFlags_Repeat) and held_duration > g.IO.KeyRepeatDelay)
{
int n1 = (prev_held_duration - g.IO.KeyRepeatDelay) / g.IO.KeyRepeatRate;
int n2 = (held_duration - g.IO.KeyRepeatDelay) / g.IO.KeyRepeatRate;
@@ -335,54 +335,41 @@ bool virtual_keyboard::button_behavior(const ImRect & bb, ImGuiID id, bool * out
}
else
{
if (hovered && (flags & ImGuiButtonFlags_PressedOnClickRelease))
if (hovered and (flags & ImGuiButtonFlags_PressedOnClickRelease))
pressed = true;
active_id = 0;
}
}
if (out_hovered)
*out_hovered = hovered;
if (out_held)
*out_held = held;
return pressed;
return key_status{
.hovered = hovered,
.held = held,
.pressed = pressed,
.hold_duration = held ? held_duration : 0};
}
bool virtual_keyboard::draw_single_key(const key & k, int key_id, ImVec2 size_arg, bool & hovered)
virtual_keyboard::key_status virtual_keyboard::draw_single_key(const key & k, int key_id, ImVec2 size_arg, bool window_hovered)
{
std::string key_label;
bool is_shift = k.keycode == ImGuiKey_LeftShift or k.keycode == ImGuiKey_RightShift;
bool is_shift = k.key == ImGuiKey_LeftShift or k.key == ImGuiKey_RightShift;
if (k.characters.empty())
{
if (is_shift)
key_label = (current_case_mode == case_mode::lower) ? ICON_FA_CHEVRON_UP : ICON_FA_CIRCLE_CHEVRON_UP;
else
key_label = k.glyph;
}
else if (current_case_mode == case_mode::lower)
{
key_label = to_utf8(k.characters[0]);
}
else
{
// FIXME: use ICU
key_label = to_utf8(/*u_*/ towupper(k.characters[0]));
}
const std::string & key_label = is_shift
? (current_case_mode == case_mode::lower)
? ICON_FA_CHEVRON_UP
: ICON_FA_CIRCLE_CHEVRON_UP
: ((current_case_mode == case_mode::lower)
? k.glyph_lower
: k.glyph_upper);
std::string key_label_id;
if (k.characters.empty())
key_label_id = "virtual_keyboard_key_" + std::to_string(k.key);
if (k.keycode != ImGuiKey_None)
key_label_id = "virtual_keyboard_key_" + std::to_string(k.keycode);
else
key_label_id = "virtual_keyboard_" + to_utf8(k.characters[0]);
key_label_id = "virtual_keyboard_" + std::string(k.glyph_lower);
ImGuiWindow * window = ImGui::GetCurrentWindow();
if (window->SkipItems)
return false;
return {};
ImGuiContext & g = *GImGui;
const ImGuiStyle & style = g.Style;
@@ -395,25 +382,23 @@ bool virtual_keyboard::draw_single_key(const key & k, int key_id, ImVec2 size_ar
const ImRect bb(pos, pos + size);
ImGui::ItemSize(size, style.FramePadding.y);
if (!ImGui::ItemAdd(bb, id))
return false;
return {};
ImGuiButtonFlags flags = ImGuiButtonFlags_None;
if (k.flag & key_flag_repeat)
flags |= ImGuiButtonFlags_Repeat;
bool held;
bool pressed = button_behavior(bb, id, &hovered, &held, flags);
auto status = button_behavior(bb, id, window_hovered, flags);
// Render
bool active = (held and hovered) or (is_shift and current_case_mode == case_mode::caps_lock);
bool active = (status.held and status.hovered) or (is_shift and current_case_mode == case_mode::caps_lock);
const ImU32 col = ImGui::GetColorU32(active ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered
: ImGuiCol_Button);
const ImU32 col = ImGui::GetColorU32(active ? ImGuiCol_ButtonActive : status.hovered ? ImGuiCol_ButtonHovered
: ImGuiCol_Button);
ImGui::RenderNavHighlight(bb, id);
ImGui::RenderFrame(bb.Min, bb.Max, col, true, style.FrameRounding);
// ImGui::RenderTextClipped(bb.Min + style.FramePadding, bb.Max - style.FramePadding, key_label.c_str(), nullptr, &label_size, style.ButtonTextAlign, &bb);
ImGui::RenderTextClippedEx(
g.CurrentWindow->DrawList,
bb.Min + style.FramePadding,
@@ -424,52 +409,61 @@ bool virtual_keyboard::draw_single_key(const key & k, int key_id, ImVec2 size_ar
style.ButtonTextAlign,
&bb);
if (pressed)
{
if (status.pressed)
press_single_key(k);
}
return pressed;
return status;
}
void virtual_keyboard::press_single_key(const key & k)
{
if (k.key == ImGuiKey_LeftShift or k.key == ImGuiKey_RightShift)
switch ((int)k.keycode) // cast to int to avoid -Werror=switch
{
switch (current_case_mode)
{
case case_mode::lower:
current_case_mode = case_mode::upper;
break;
case case_mode::upper:
current_case_mode = case_mode::caps_lock;
break;
case case_mode::caps_lock:
current_case_mode = case_mode::lower;
break;
}
}
else if (k.key != ImGuiKey_None)
{
ImGui::GetIO().AddKeyEvent(k.key, true);
ImGui::GetIO().AddKeyEvent(k.key, false);
}
else if (not k.characters.empty())
{
switch (current_case_mode)
{
case case_mode::lower:
ImGui::GetIO().AddInputCharactersUTF8(to_utf8(k.characters[0]).c_str());
break;
case ImGuiKey_LeftShift:
case ImGuiKey_RightShift:
switch (current_case_mode)
{
case case_mode::lower:
current_case_mode = case_mode::upper;
break;
case case_mode::upper:
current_case_mode = case_mode::caps_lock;
break;
case case_mode::caps_lock:
current_case_mode = case_mode::lower;
break;
}
break;
case case_mode::upper:
current_case_mode = case_mode::lower;
[[fallthrough]];
case key_layout:
current_layout = (current_layout + 1) % layouts.size();
break;
case case_mode::caps_lock:
ImGui::GetIO().AddInputCharactersUTF8(to_utf8(towupper(k.characters[0])).c_str());
break;
}
case key_symbols_letter:
symbols_shown = not symbols_shown;
break;
case ImGuiKey_None:
switch (current_case_mode)
{
case case_mode::lower:
ImGui::GetIO().AddInputCharactersUTF8(std::string(*una::ranges::views::grapheme::utf8(k.characters_lower).begin()).c_str());
break;
case case_mode::upper:
current_case_mode = case_mode::lower;
[[fallthrough]];
case case_mode::caps_lock:
ImGui::GetIO().AddInputCharactersUTF8(std::string(*una::ranges::views::grapheme::utf8(k.characters_upper).begin()).c_str());
break;
}
break;
default:
ImGui::GetIO().AddKeyEvent(k.keycode, true);
ImGui::GetIO().AddKeyEvent(k.keycode, false);
break;
}
}
@@ -481,7 +475,7 @@ void virtual_keyboard::display(ImGuiID & hovered_id)
bool want_digits = input_state and (input_state->Flags & ImGuiInputTextFlags_CharsDecimal) != 0;
auto & layout = want_digits ? digits : symbols_shown ? symbols
: *layouts[current_layout];
: *layouts[current_layout].second;
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {8, 8});
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 16);
@@ -499,7 +493,7 @@ void virtual_keyboard::display(ImGuiID & hovered_id)
ImVec2 padding = ImGui::GetStyle().WindowPadding;
size = {size.x - 2 * padding.x, size.y - 2 * padding.y};
// Compute keys size;
// Compute keys size:
float key_height = [&]() {
int N = layout.size();
@@ -526,6 +520,8 @@ void virtual_keyboard::display(ImGuiID & hovered_id)
ImVec2 row_position = ImGui::GetCursorPos();
int id = 0;
bool window_hovered = is_window_hovered();
for (const auto & row: layout)
{
ImVec2 key_position = row_position;
@@ -541,25 +537,8 @@ void virtual_keyboard::display(ImGuiID & hovered_id)
{
ImGui::SetCursorPos(key_position);
bool is_hovered;
if (draw_single_key(key, id++, ImVec2{base_key_width * key.width - style.ItemSpacing.x, key_height}, is_hovered))
{
switch ((int)key.key) // cast to int to avoid -Werror=switch
{
case key_layout:
current_layout = (current_layout + 1) % layouts.size();
break;
case key_symbols_letter:
symbols_shown = not symbols_shown;
break;
default:
break;
}
}
if (is_hovered)
auto status = draw_single_key(key, id++, ImVec2{base_key_width * key.width - style.ItemSpacing.x, key_height}, window_hovered);
if (status.hovered)
hovered_id = ImGui::GetItemID();
}
@@ -573,7 +552,7 @@ void virtual_keyboard::display(ImGuiID & hovered_id)
ImGui::SetCursorPos(row_position);
if (ImGui::IsWindowHovered())
if (is_window_hovered())
{
ImGui::GetIO().MouseDown[0] = false;
ImGui::GetIO().MouseClicked[0] = false;
@@ -583,3 +562,20 @@ void virtual_keyboard::display(ImGuiID & hovered_id)
ImGui::PopStyleColor(); // ImGuiCol_WindowBg
ImGui::PopStyleVar(4); // ImGuiStyleVar_WindowPadding, ImGuiStyleVar_WindowRounding, ImGuiStyleVar_ItemSpacing, ImGuiStyleVar_FrameRounding
}
const std::string_view virtual_keyboard::get_layout() const
{
return layouts[current_layout].first;
}
void virtual_keyboard::set_layout(std::string_view layout_name)
{
for (int i = 0, n = layouts.size(); i < n; ++i)
{
if (layouts[i].first == layout_name)
{
current_layout = i;
return;
}
}
}
+23 -7
View File
@@ -20,7 +20,7 @@
#pragma once
#include "imgui.h"
#include <string_view>
#include <string>
#include <vector>
struct ImRect;
@@ -38,14 +38,20 @@ public:
struct key
{
float width;
const std::u16string_view characters;
ImGuiKey key = ImGuiKey_None;
const char * glyph = nullptr;
std::string characters_lower;
std::string characters_upper;
ImGuiKey keycode = ImGuiKey_None;
std::string glyph_lower;
std::string glyph_upper;
key_flags flag = key_flag_none;
key(float width, std::string_view characters, ImGuiKey keycode = ImGuiKey_None, key_flags flag = key_flag_none);
key(float width);
};
using layout = std::vector<std::vector<key>>;
private:
enum class case_mode
{
lower,
@@ -53,10 +59,18 @@ public:
caps_lock
};
private:
bool button_behavior(const ImRect & bb, ImGuiID id, bool * out_hovered, bool * out_held, ImGuiButtonFlags flags = 0);
bool draw_single_key(const key & k, int key_id, ImVec2 size_arg, bool & hovered);
struct key_status
{
bool hovered = false;
bool held = false;
bool pressed = false;
float hold_duration = 0;
};
key_status button_behavior(const ImRect & bb, ImGuiID id, bool window_hovered, ImGuiButtonFlags flags = 0);
key_status draw_single_key(const key & k, int key_id, ImVec2 size_arg, bool window_hovered);
void press_single_key(const key & k);
bool is_window_hovered();
ImGuiID active_id = 0;
float held_duration = 0;
@@ -67,4 +81,6 @@ private:
public:
void display(ImGuiID & hovered);
const std::string_view get_layout() const;
void set_layout(std::string_view layout_name);
};
+5 -1
View File
@@ -320,5 +320,9 @@ std::vector<XrCompositionLayerQuad> scenes::stream::plot_performance_metrics(XrT
}
ImGui::End();
return imgui_ctx->end_frame();
std::vector<XrCompositionLayerQuad> layers;
for (auto & layer: imgui_ctx->end_frame())
layers.push_back(layer.second);
return layers;
}
+8 -32
View File
@@ -24,6 +24,7 @@
#include <thread>
#ifdef __ANDROID__
#include "android/battery.h"
#include "android/jnipp.h"
#endif
@@ -159,22 +160,8 @@ void scenes::stream::tracking()
#ifdef __ANDROID__
// Runtime may use JNI and needs the thread to be attached
application::instance().setup_jni();
jni::object<""> act(application::native_app()->activity->clazz);
auto app = act.call<jni::object<"android/app/Application">>("getApplication");
auto ctx = app.call<jni::object<"android/content/Context">>("getApplicationContext");
jni::string filter_jstr("android.intent.action.BATTERY_CHANGED");
jni::string level_jstr("level");
jni::string scale_jstr("scale");
jni::string plugged_jstr("plugged");
jni::Int default_jint(-1);
auto receiver_jobj = jni::object<"android/content/BroadcastReceiver">(NULL);
auto filter_jobj = jni::new_object<"android/content/IntentFilter">(filter_jstr);
auto register_receiver = jni::klass("android/content/Context")
.method<jni::object<"android/content/Intent">>("registerReceiver", receiver_jobj, filter_jobj);
auto get_int_extra = jni::klass("android/content/Intent").method<jni::Int>("getIntExtra", level_jstr, default_jint);
battery battery_tracker;
XrTime next_battery_check = 0;
const XrDuration battery_check_interval = 30'000'000'000; // 30s
@@ -313,24 +300,13 @@ void scenes::stream::tracking()
if (next_battery_check < now and control.enabled[size_t(tid::battery)])
{
timer t2(instance);
from_headset::battery battery{};
auto intent = ctx.call<jni::object<"android/content/Intent">>(register_receiver, receiver_jobj, filter_jobj);
if (intent)
{
auto level_jint = intent.call<jni::Int>(get_int_extra, level_jstr, default_jint);
auto scale_jint = intent.call<jni::Int>(get_int_extra, scale_jstr, default_jint);
if (level_jint && level_jint.value >= 0 && scale_jint && scale_jint.value >= 0)
{
battery.present = true;
battery.charge = (float)(level_jint.value) / (float)(scale_jint.value);
}
auto plugged_jint = intent.call<jni::Int>(get_int_extra, plugged_jstr, default_jint);
battery.charging = plugged_jint && plugged_jint.value > 0;
}
network_session->send_stream(battery);
auto status = battery_tracker.get();
network_session->send_stream(from_headset::battery{
.charge = status.charge.value_or(-1),
.present = status.charge.has_value(),
.charging = status.charging,
});
next_battery_check = now + battery_check_interval;
XrDuration battery_dur = t2.count();