Rewrite application::read_action functions

This commit is contained in:
Guillaume Meunier
2024-01-18 22:38:52 +01:00
parent 90501ad463
commit 7f6523fcc0
3 changed files with 25 additions and 32 deletions
+12 -18
View File
@@ -904,10 +904,10 @@ void application::poll_actions()
instance().xr_session.sync_actions(instance().xr_actionset); instance().xr_session.sync_actions(instance().xr_actionset);
} }
bool application::read_action(XrAction action, bool & value, XrTime & last_change_time) std::optional<std::pair<XrTime, bool>> application::read_action_bool(XrAction action)
{ {
if (!is_focused()) if (!is_focused())
return false; return {};
XrActionStateGetInfo get_info{ XrActionStateGetInfo get_info{
.type = XR_TYPE_ACTION_STATE_GET_INFO, .type = XR_TYPE_ACTION_STATE_GET_INFO,
@@ -918,17 +918,15 @@ bool application::read_action(XrAction action, bool & value, XrTime & last_chang
CHECK_XR(xrGetActionStateBoolean(instance().xr_session, &get_info, &state)); CHECK_XR(xrGetActionStateBoolean(instance().xr_session, &get_info, &state));
if (!state.isActive) if (!state.isActive)
return false; return {};
value = state.currentState; return std::make_pair(state.lastChangeTime, state.currentState);
last_change_time = state.lastChangeTime;
return true;
} }
bool application::read_action(XrAction action, float & value, XrTime & last_change_time) std::optional<std::pair<XrTime, float>> application::read_action_float(XrAction action)
{ {
if (!is_focused()) if (!is_focused())
return false; return {};
XrActionStateGetInfo get_info{ XrActionStateGetInfo get_info{
.type = XR_TYPE_ACTION_STATE_GET_INFO, .type = XR_TYPE_ACTION_STATE_GET_INFO,
@@ -939,17 +937,15 @@ bool application::read_action(XrAction action, float & value, XrTime & last_chan
CHECK_XR(xrGetActionStateFloat(instance().xr_session, &get_info, &state)); CHECK_XR(xrGetActionStateFloat(instance().xr_session, &get_info, &state));
if (!state.isActive) if (!state.isActive)
return false; return {};
value = state.currentState; return std::make_pair(state.lastChangeTime, state.currentState);
last_change_time = state.lastChangeTime;
return true;
} }
bool application::read_action(XrAction action, XrVector2f & value, XrTime & last_change_time) std::optional<std::pair<XrTime, XrVector2f>> application::read_action_vec2(XrAction action)
{ {
if (!is_focused()) if (!is_focused())
return false; return {};
XrActionStateGetInfo get_info{ XrActionStateGetInfo get_info{
.type = XR_TYPE_ACTION_STATE_GET_INFO, .type = XR_TYPE_ACTION_STATE_GET_INFO,
@@ -960,11 +956,9 @@ bool application::read_action(XrAction action, XrVector2f & value, XrTime & last
CHECK_XR(xrGetActionStateVector2f(instance().xr_session, &get_info, &state)); CHECK_XR(xrGetActionStateVector2f(instance().xr_session, &get_info, &state));
if (!state.isActive) if (!state.isActive)
return false; return {};
value = state.currentState; return std::make_pair(state.lastChangeTime, state.currentState);
last_change_time = state.lastChangeTime;
return true;
} }
void application::haptic_start(XrAction action, XrPath subpath, int64_t duration, float frequency, float amplitude) void application::haptic_start(XrAction action, XrPath subpath, int64_t duration, float frequency, float amplitude)
+3 -3
View File
@@ -174,9 +174,9 @@ public:
}; };
static void poll_actions(); static void poll_actions();
static bool read_action(XrAction action, float & value, XrTime& last_change_time); static std::optional<std::pair<XrTime, float>> read_action_float(XrAction action);
static bool read_action(XrAction action, bool & value, XrTime& last_change_time); static std::optional<std::pair<XrTime, bool>> read_action_bool(XrAction action);
static bool read_action(XrAction action, XrVector2f & value, XrTime& last_change_time); static std::optional<std::pair<XrTime, XrVector2f>> read_action_vec2(XrAction action);
static void haptic_start(XrAction action, XrPath subpath, int64_t duration, float frequency, float amplitude); static void haptic_start(XrAction action, XrPath subpath, int64_t duration, float frequency, float amplitude);
static void haptic_stop(XrAction action, XrPath subpath); static void haptic_stop(XrAction action, XrPath subpath);
+10 -11
View File
@@ -29,27 +29,26 @@ void scenes::stream::read_actions()
{ {
switch (action_type) switch (action_type)
{ {
XrTime last_change_time;
case XR_ACTION_TYPE_BOOLEAN_INPUT: { case XR_ACTION_TYPE_BOOLEAN_INPUT: {
bool value; auto value = application::read_action_bool(action);
if (application::read_action(action, value, last_change_time)) if (value)
inputs.values.push_back({id, (float)value, (uint64_t)last_change_time}); inputs.values.push_back({id, (float)value->second, (uint64_t)value->first});
} }
break; break;
case XR_ACTION_TYPE_FLOAT_INPUT: { case XR_ACTION_TYPE_FLOAT_INPUT: {
float value; auto value = application::read_action_float(action);
if (application::read_action(action, value, last_change_time)) if (value)
inputs.values.push_back({id, value, (uint64_t)last_change_time}); inputs.values.push_back({id, value->second, (uint64_t)value->first});
} }
break; break;
case XR_ACTION_TYPE_VECTOR2F_INPUT: { case XR_ACTION_TYPE_VECTOR2F_INPUT: {
XrVector2f value; auto value = application::read_action_vec2(action);
if (application::read_action(action, value, last_change_time)) if (value)
{ {
inputs.values.push_back({id, value.x, (uint64_t)last_change_time}); inputs.values.push_back({id, value->second.x, (uint64_t)value->first});
inputs.values.push_back({(device_id)((int)id + 1), value.y, (uint64_t)last_change_time}); inputs.values.push_back({(device_id)((int)id + 1), value->second.y, (uint64_t)value->first});
} }
} }
break; break;