refactor chord_state
This commit is contained in:
+10
-23
@@ -565,8 +565,8 @@ void scenes::stream::on_focused()
|
|||||||
|
|
||||||
plots_toggle_1 = get_action("plots_toggle_1").first;
|
plots_toggle_1 = get_action("plots_toggle_1").first;
|
||||||
plots_toggle_2 = get_action("plots_toggle_2").first;
|
plots_toggle_2 = get_action("plots_toggle_2").first;
|
||||||
gamepad_mode_1 = get_action("gamepad_mode_1").first;
|
gamepad_chord.a = get_action("gamepad_mode_1").first;
|
||||||
gamepad_mode_2 = get_action("gamepad_mode_2").first;
|
gamepad_chord.b = get_action("gamepad_mode_2").first;
|
||||||
recenter_left = get_action("recenter_left").first;
|
recenter_left = get_action("recenter_left").first;
|
||||||
recenter_right = get_action("recenter_right").first;
|
recenter_right = get_action("recenter_right").first;
|
||||||
gui_distance_left = get_action("gui_distance_left").first;
|
gui_distance_left = get_action("gui_distance_left").first;
|
||||||
@@ -1263,29 +1263,16 @@ void scenes::stream::render(const XrFrameState & frame_state)
|
|||||||
show_gamepad_toast(false);
|
show_gamepad_toast(false);
|
||||||
spdlog::info("Controller gamepad emulation disabled due to setting change");
|
spdlog::info("Controller gamepad emulation disabled due to setting change");
|
||||||
}
|
}
|
||||||
gamepad_chord = {};
|
gamepad_chord.reset();
|
||||||
}
|
}
|
||||||
else
|
else if (gamepad_chord.check(instance.now()))
|
||||||
{
|
{
|
||||||
XrActionStateGetInfo get_info{
|
const bool enabled = not gamepad_mode.load();
|
||||||
.type = XR_TYPE_ACTION_STATE_GET_INFO,
|
gamepad_mode = enabled;
|
||||||
.action = gamepad_mode_1,
|
if (not enabled)
|
||||||
};
|
scheduled_derived_pose = instance.now();
|
||||||
XrActionStateBoolean state_1{XR_TYPE_ACTION_STATE_BOOLEAN};
|
show_gamepad_toast(enabled);
|
||||||
CHECK_XR(xrGetActionStateBoolean(session, &get_info, &state_1));
|
spdlog::info("Controller gamepad mode {}", enabled ? "enabled" : "disabled");
|
||||||
get_info.action = gamepad_mode_2;
|
|
||||||
XrActionStateBoolean state_2{XR_TYPE_ACTION_STATE_BOOLEAN};
|
|
||||||
CHECK_XR(xrGetActionStateBoolean(session, &get_info, &state_2));
|
|
||||||
|
|
||||||
if (check_gamepad_mode_chord(state_1, state_2))
|
|
||||||
{
|
|
||||||
const bool enabled = not gamepad_mode.load();
|
|
||||||
gamepad_mode = enabled;
|
|
||||||
if (not enabled)
|
|
||||||
scheduled_derived_pose = instance.now();
|
|
||||||
show_gamepad_toast(enabled);
|
|
||||||
spdlog::info("Controller gamepad mode {}", enabled ? "enabled" : "disabled");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
read_actions();
|
read_actions();
|
||||||
|
|||||||
+18
-7
@@ -83,13 +83,27 @@ private:
|
|||||||
std::array<std::atomic<interaction_profile>, 3> interaction_profiles; // left hand, right hand, gamepad
|
std::array<std::atomic<interaction_profile>, 3> interaction_profiles; // left hand, right hand, gamepad
|
||||||
std::atomic<bool> gamepad_mode = false;
|
std::atomic<bool> gamepad_mode = false;
|
||||||
|
|
||||||
struct gamepad_chord_state
|
struct chord_state
|
||||||
{
|
{
|
||||||
|
XrAction a = XR_NULL_HANDLE;
|
||||||
|
XrAction b = XR_NULL_HANDLE;
|
||||||
|
uint32_t clicks_needed = 1;
|
||||||
|
|
||||||
bool both_released = false;
|
bool both_released = false;
|
||||||
bool waiting_for_second_tap = false;
|
uint32_t clicks_done = 0;
|
||||||
XrTime first_tap_time = 0;
|
XrTime first_click_time = 0;
|
||||||
|
|
||||||
|
void reset()
|
||||||
|
{
|
||||||
|
both_released = false;
|
||||||
|
clicks_done = 0;
|
||||||
|
first_click_time = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read actions a and b and determine if they were activated together clicks_needed times in a row
|
||||||
|
bool check(XrTime now);
|
||||||
};
|
};
|
||||||
gamepad_chord_state gamepad_chord = {};
|
chord_state gamepad_chord = {.clicks_needed = 2};
|
||||||
|
|
||||||
bool using_frame_gamepad() const
|
bool using_frame_gamepad() const
|
||||||
{
|
{
|
||||||
@@ -97,7 +111,6 @@ private:
|
|||||||
interaction_profiles[0].load() == interaction_profile::valve_frame_controller_valve and
|
interaction_profiles[0].load() == interaction_profile::valve_frame_controller_valve and
|
||||||
interaction_profiles[1].load() == interaction_profile::valve_frame_controller_valve;
|
interaction_profiles[1].load() == interaction_profile::valve_frame_controller_valve;
|
||||||
}
|
}
|
||||||
bool check_gamepad_mode_chord(XrActionStateBoolean state_1, XrActionStateBoolean state_2);
|
|
||||||
std::atomic<bool> interaction_profile_changed = false;
|
std::atomic<bool> interaction_profile_changed = false;
|
||||||
std::atomic<XrTime> scheduled_derived_pose = 0; // Tracking thread will compute derived pose when time is reached
|
std::atomic<XrTime> scheduled_derived_pose = 0; // Tracking thread will compute derived pose when time is reached
|
||||||
std::atomic<bool> recenter_requested = false;
|
std::atomic<bool> recenter_requested = false;
|
||||||
@@ -192,8 +205,6 @@ private:
|
|||||||
|
|
||||||
XrAction plots_toggle_1 = XR_NULL_HANDLE;
|
XrAction plots_toggle_1 = XR_NULL_HANDLE;
|
||||||
XrAction plots_toggle_2 = XR_NULL_HANDLE;
|
XrAction plots_toggle_2 = XR_NULL_HANDLE;
|
||||||
XrAction gamepad_mode_1 = XR_NULL_HANDLE;
|
|
||||||
XrAction gamepad_mode_2 = XR_NULL_HANDLE;
|
|
||||||
XrAction recenter_left = XR_NULL_HANDLE;
|
XrAction recenter_left = XR_NULL_HANDLE;
|
||||||
XrAction recenter_right = XR_NULL_HANDLE;
|
XrAction recenter_right = XR_NULL_HANDLE;
|
||||||
XrAction gui_distance_left = XR_NULL_HANDLE;
|
XrAction gui_distance_left = XR_NULL_HANDLE;
|
||||||
|
|||||||
@@ -98,56 +98,58 @@ constexpr std::array frame_gamepad_ids = {
|
|||||||
};
|
};
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
bool scenes::stream::check_gamepad_mode_chord(XrActionStateBoolean state_1, XrActionStateBoolean state_2)
|
bool scenes::stream::chord_state::check(XrTime now)
|
||||||
{
|
{
|
||||||
constexpr XrDuration gamepad_chord_press_tolerance = 150'000'000; // 150ms between grip presses
|
constexpr XrDuration press_tolerance = 150'000'000; // 150ms between button presses
|
||||||
constexpr XrDuration gamepad_chord_timeout = 400'000'000; // 400ms between chord taps
|
constexpr XrDuration click_timeout = 400'000'000; // 400ms between chord clicks
|
||||||
|
|
||||||
if (not state_1.isActive or not state_2.isActive)
|
auto state_1 = application::read_action_bool(a);
|
||||||
|
auto state_2 = application::read_action_bool(b);
|
||||||
|
if (not state_1 or not state_2)
|
||||||
{
|
{
|
||||||
gamepad_chord = {};
|
reset();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool down_1 = state_1.currentState;
|
if (clicks_done != 0 and now - first_click_time > click_timeout)
|
||||||
const bool down_2 = state_2.currentState;
|
clicks_done = 0;
|
||||||
|
|
||||||
|
const bool down_1 = state_1->second;
|
||||||
|
const bool down_2 = state_2->second;
|
||||||
|
|
||||||
if (not down_1 and not down_2)
|
if (not down_1 and not down_2)
|
||||||
{
|
{
|
||||||
gamepad_chord.both_released = true;
|
both_released = true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// require both inputs to be released between the double-tap
|
// require both inputs to be released between taps
|
||||||
// to avoid accidental activation
|
// to avoid accidental activation
|
||||||
if (not(down_1 and down_2) or not gamepad_chord.both_released)
|
if (not(down_1 and down_2) or not both_released)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// both grips are down
|
// both inputs are down
|
||||||
gamepad_chord.both_released = false;
|
both_released = false;
|
||||||
const XrTime press_1 = state_1.lastChangeTime;
|
const XrTime press_1 = state_1->first;
|
||||||
const XrTime press_2 = state_2.lastChangeTime;
|
const XrTime press_2 = state_2->first;
|
||||||
const XrDuration press_separation = press_1 >= press_2 ? press_1 - press_2 : press_2 - press_1;
|
const XrDuration press_separation = press_1 >= press_2 ? press_1 - press_2 : press_2 - press_1;
|
||||||
|
|
||||||
// grips were not pressed together
|
// inputs were not pressed together
|
||||||
if (press_separation > gamepad_chord_press_tolerance)
|
if (press_separation > press_tolerance)
|
||||||
{
|
{
|
||||||
gamepad_chord.waiting_for_second_tap = false;
|
clicks_done = 0;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const XrTime tap_time = std::max(press_1, press_2);
|
if (clicks_done == 0)
|
||||||
if (gamepad_chord.waiting_for_second_tap and
|
first_click_time = std::max(press_1, press_2);
|
||||||
tap_time > gamepad_chord.first_tap_time and
|
|
||||||
tap_time - gamepad_chord.first_tap_time <= gamepad_chord_timeout)
|
|
||||||
{
|
|
||||||
gamepad_chord.waiting_for_second_tap = false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
gamepad_chord.first_tap_time = tap_time;
|
++clicks_done;
|
||||||
gamepad_chord.waiting_for_second_tap = true;
|
if (clicks_done < clicks_needed)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
reset();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void scenes::stream::read_actions()
|
void scenes::stream::read_actions()
|
||||||
|
|||||||
Reference in New Issue
Block a user