Switch between poke and pointer interaction in lobby

This commit is contained in:
Patrick Nicolas
2025-11-13 21:48:10 +01:00
parent a9907e0810
commit 3e8a78002e
7 changed files with 115 additions and 55 deletions
+53 -44
View File
@@ -555,84 +555,93 @@ std::vector<imgui_context::controller_state> imgui_context::read_controllers_sta
size_t new_focused_controller = focused_controller;
std::vector<controller_state> new_states;
new_states.resize(controllers.size());
if (not controllers_enabled)
return new_states;
aim_interaction = {true, true};
// Get the hand/controller state from OpenXR
for (auto && [index, controller]: utils::enumerate(controllers))
// First hands, so we can set aim_interaction
for (const auto & [controller, state]: std::ranges::zip_view(controllers, new_states))
{
auto & [ctrl, state] = controller;
controller_state & new_state = new_states.emplace_back();
if (not controllers_enabled)
const auto & ctrl = controller.first;
if (not ctrl.hand)
continue;
state.source = ImGuiMouseSource_VRHandTracking;
if (ctrl.hand)
if (auto joints = ctrl.hand->locate(world, display_time))
{
if (auto joints = ctrl.hand->locate(world, display_time))
XrHandJointLocationEXT & index_tip = (*joints)[XR_HAND_JOINT_INDEX_TIP_EXT].first;
if (index_tip.locationFlags & XR_SPACE_LOCATION_POSITION_TRACKED_BIT)
{
XrHandJointLocationEXT & index_tip = (*joints)[XR_HAND_JOINT_INDEX_TIP_EXT].first;
if (index_tip.locationFlags & XR_SPACE_LOCATION_POSITION_TRACKED_BIT)
{
new_state.aim_position = {
index_tip.pose.position.x,
index_tip.pose.position.y,
index_tip.pose.position.z};
// aim_orientation is ignored by ray_plane_intersection() for hands
state.aim_position = {
index_tip.pose.position.x,
index_tip.pose.position.y,
index_tip.pose.position.z};
// aim_orientation is ignored by ray_plane_intersection() for hands
new_state.active = true;
new_state.source = ImGuiMouseSource_VRHandTracking;
}
state.active = true;
}
compute_pointer_position(state);
if (state.hover_distance < constants::gui::fingertip_distance_hovering_thd)
{
state.fingertip_hovering = true;
aim_interaction[ctrl.index] = false;
}
else
{
state.pointer_position.reset();
}
continue;
if (state.hover_distance < constants::gui::fingertip_distance_touching_thd_hi and state.hover_distance > constants::gui::fingertip_distance_touching_thd_lo)
state.fingertip_touching = true;
}
}
new_state.source = ImGuiMouseSource_VRController;
// Then controllers
for (const auto & [controller, state]: std::ranges::zip_view(controllers, new_states))
{
const auto & ctrl = controller.first;
if (ctrl.hand)
continue;
state.source = ImGuiMouseSource_VRController;
if (not aim_interaction[ctrl.index])
continue;
if (auto location = application::locate_controller(ctrl.aim, world, display_time))
{
new_state.active = true;
new_state.aim_position = location->first + glm::mat3_cast(location->second * ctrl.offset.second) * ctrl.offset.first;
new_state.aim_orientation = location->second * ctrl.offset.second;
state.active = true;
state.aim_position = location->first + glm::mat3_cast(location->second * ctrl.offset.second) * ctrl.offset.first;
state.aim_orientation = location->second * ctrl.offset.second;
compute_pointer_position(state);
if (ctrl.trigger)
{
auto trigger = application::read_action_float(ctrl.trigger).value_or(std::pair{0, 0});
new_state.trigger_value = trigger.second;
state.trigger_value = trigger.second;
// TODO tunable
/*if (new_state.trigger_value < 0.5)
new_state.trigger_clicked = false;
else */
if (new_state.trigger_value > constants::gui::trigger_click_thd)
new_state.trigger_clicked = true;
if (state.trigger_value > constants::gui::trigger_click_thd)
state.trigger_clicked = true;
}
if (ctrl.scroll)
{
if (auto act = application::read_action_vec2(ctrl.scroll); act)
new_state.scroll_value = {-act->second.x * scroll_scale, act->second.y * scroll_scale};
state.scroll_value = {-act->second.x * scroll_scale, act->second.y * scroll_scale};
else
new_state.scroll_value = {0, 0};
state.scroll_value = {0, 0};
}
}
}
// Compute the position in imgui frame according to the currently displayed windows (from the last frame)
for (auto & state: new_states)
{
compute_pointer_position(state);
if (state.source == ImGuiMouseSource_VRHandTracking)
{
if (state.hover_distance < constants::gui::fingertip_distance_hovering_thd)
state.fingertip_hovering = true;
if (state.hover_distance < constants::gui::fingertip_distance_touching_thd_hi and state.hover_distance > constants::gui::fingertip_distance_touching_thd_lo)
state.fingertip_touching = true;
}
}
return new_states;
}
+7
View File
@@ -93,6 +93,7 @@ public:
struct controller
{
uint8_t index; // left = 0, right = 1
XrSpace aim;
std::pair<glm::vec3, glm::quat> offset;
@@ -186,6 +187,8 @@ private:
bool button_pressed = false;
bool fingertip_touching = false;
std::array<bool, 2> aim_interaction = {true, true}; // left, right
ImGuiID hovered_item = 0; // Hovered item in the current frame, reset at the beginning of the frame
ImGuiID hovered_item_prev = 0; // Hovered item at the previous frame
@@ -242,6 +245,10 @@ public:
void set_hovered_item();
void set_controllers_enabled(bool value);
void tooltip(std::string_view text);
std::array<bool, 2> is_aim_interaction() const
{
return aim_interaction;
}
};
void ScrollWhenDragging();
+13 -6
View File
@@ -397,15 +397,22 @@ static void set_clipping_planes(entt::registry & scene, entt::entity entity, std
}
}
void input_profile::apply(entt::registry & scene, XrSpace world_space, XrTime predicted_display_time, bool hide_left, bool hide_right, std::span<glm::vec4> pointer_limits)
void input_profile::apply(
entt::registry & scene,
XrSpace world_space,
XrTime predicted_display_time,
bool hide_left_controller,
bool hide_left_ray,
bool hide_right_controller,
bool hide_right_ray,
std::span<glm::vec4> pointer_limits)
{
for (auto && [entity, node, space]: scene.view<components::node, bound_space>().each())
{
if ((space.space == xr::spaces::grip_left or space.space == xr::spaces::aim_left) and hide_left)
{
node.visible = false;
}
else if ((space.space == xr::spaces::grip_right or space.space == xr::spaces::aim_right) and hide_right)
if ((space.space == xr::spaces::grip_left and hide_left_controller) or
(space.space == xr::spaces::aim_left and hide_left_ray) or
(space.space == xr::spaces::grip_right and hide_right_controller) or
(space.space == xr::spaces::aim_right and hide_right_ray))
{
node.visible = false;
}
+9 -1
View File
@@ -40,5 +40,13 @@ struct input_profile
input_profile(scene & scene, const std::filesystem::path & json_profile, uint32_t layer_mask_controller, uint32_t layer_mask_ray);
// application::poll_actions() must have been called before
void apply(entt::registry & scene, XrSpace world_space, XrTime predicted_display_time, bool hide_left, bool hide_right, std::span<glm::vec4> pointer_limits);
void apply(
entt::registry & scene,
XrSpace world_space,
XrTime predicted_display_time,
bool hide_left_controller,
bool hide_left_ray,
bool hide_right_controller,
bool hide_right_ray,
std::span<glm::vec4> pointer_limits);
};
+23 -3
View File
@@ -1065,7 +1065,14 @@ void scenes::lobby::render(const XrFrameState & frame_state)
ray_limits.push_back(compute_ray_limits(layer.pose));
}
input->apply(world, world_space, frame_state.predictedDisplayTime, hide_left_controller, hide_right_controller, ray_limits);
input->apply(world,
world_space,
frame_state.predictedDisplayTime,
hide_left_controller,
not imgui_ctx->is_aim_interaction()[0],
hide_right_controller,
not imgui_ctx->is_aim_interaction()[1],
ray_limits);
renderer::animate(world, frame_state.predictedDisplayPeriod * 1.0e-9);
@@ -1198,6 +1205,7 @@ void scenes::lobby::on_focused()
std::vector imgui_inputs{
imgui_context::controller{
.index = 0,
.aim = get_action_space("left_aim"),
.offset = input->offset[xr::spaces::aim_left],
.trigger = get_action("left_trigger").first,
@@ -1206,6 +1214,7 @@ void scenes::lobby::on_focused()
.haptic_output = get_action("left_haptic").first,
},
imgui_context::controller{
.index = 1,
.aim = get_action_space("right_aim"),
.offset = input->offset[xr::spaces::aim_right],
.trigger = get_action("right_trigger").first,
@@ -1221,8 +1230,8 @@ void scenes::lobby::on_focused()
right_hand = session.create_hand_tracker(XR_HAND_RIGHT_EXT);
hand_model::add_hand(*this, XR_HAND_LEFT_EXT, "assets://left-hand.glb", layer_controllers);
hand_model::add_hand(*this, XR_HAND_RIGHT_EXT, "assets://right-hand.glb", layer_controllers);
imgui_inputs.push_back({.hand = &*left_hand});
imgui_inputs.push_back({.hand = &*right_hand});
imgui_inputs.push_back({.index = 0, .hand = &*left_hand});
imgui_inputs.push_back({.index = 1, .hand = &*right_hand});
}
face_tracker = xr::make_face_tracker(instance, system, session);
@@ -1443,6 +1452,17 @@ scene::meta & scenes::lobby::get_meta_scene()
{"right_squeeze", "/user/hand/right/input/menu/click"},
},
},
suggested_binding{
{
"/interaction_profiles/ext/hand_interaction_ext",
},
{
{"left_aim", "/user/hand/left/input/aim/pose"},
{"left_trigger", "/user/hand/left/input/aim_activate_ext"},
{"right_aim", "/user/hand/right/input/aim/pose"},
{"right_trigger", "/user/hand/right/input/aim_activate_ext"},
},
},
}};
return m;
+2
View File
@@ -429,6 +429,7 @@ void scenes::stream::on_focused()
std::array imgui_inputs{
imgui_context::controller{
.index = 0,
.aim = get_action_space("left_aim"),
.offset = input->offset[xr::spaces::aim_left],
.trigger = get_action("left_trigger").first,
@@ -437,6 +438,7 @@ void scenes::stream::on_focused()
.haptic_output = get_action("left_haptic").first,
},
imgui_context::controller{
.index = 1,
.aim = get_action_space("right_aim"),
.offset = input->offset[xr::spaces::aim_right],
.trigger = get_action("right_trigger").first,
+8 -1
View File
@@ -1032,7 +1032,14 @@ void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicte
hide_right_controller = true;
}
input->apply(world, world_space, predicted_display_time, hide_left_controller, hide_right_controller, ray_limits);
input->apply(world,
world_space,
predicted_display_time,
hide_left_controller,
hide_left_controller,
hide_right_controller,
hide_right_controller,
ray_limits);
// Add the layer with the controllers
if (composition_layer_depth_test_supported)