| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#ifndef OSGXR_OPENXR_ACTION |
| 5 |
#define OSGXR_OPENXR_ACTION 1 |
| 6 |
|
| 7 |
#include "ActionSet.h" |
| 8 |
#include "Path.h" |
| 9 |
|
| 10 |
#include <osg/Vec2f> |
| 11 |
#include <osg/ref_ptr> |
| 12 |
|
| 13 |
#include <cassert> |
| 14 |
#include <string> |
| 15 |
#include <vector> |
| 16 |
|
| 17 |
namespace osgXR { |
| 18 |
|
| 19 |
namespace OpenXR { |
| 20 |
|
| 21 |
class Path; |
| 22 |
class Space; |
| 23 |
|
| 24 |
class Action : public osg::Referenced |
| 25 |
{ |
| 26 |
public: |
| 27 |
|
| 28 |
Action(ActionSet *actionSet, |
| 29 |
const std::string &name, |
| 30 |
const std::string &localizedName, |
| 31 |
XrActionType type); |
| 32 |
virtual ~Action(); |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
void addSubaction(const Path &path); |
| 37 |
|
| 38 |
|
| 39 |
bool init(); |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
inline bool valid() const |
| 44 |
{ |
| 45 |
return _action != XR_NULL_HANDLE; |
| 46 |
} |
| 47 |
|
| 48 |
inline bool check(XrResult result, const char *actionMsg) const |
| 49 |
{ |
| 50 |
return _actionSet->check(result, actionMsg); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
inline const osg::ref_ptr<ActionSet> getActionSet() const |
| 56 |
{ |
| 57 |
return _actionSet; |
| 58 |
} |
| 59 |
|
| 60 |
inline const osg::ref_ptr<Instance> getInstance() const |
| 61 |
{ |
| 62 |
return _actionSet->getInstance(); |
| 63 |
} |
| 64 |
|
| 65 |
inline XrInstance getXrInstance() const |
| 66 |
{ |
| 67 |
return _actionSet->getXrInstance(); |
| 68 |
} |
| 69 |
|
| 70 |
inline XrActionSet getXrActionSet() const |
| 71 |
{ |
| 72 |
return _actionSet->getXrActionSet(); |
| 73 |
} |
| 74 |
|
| 75 |
inline XrAction getXrAction() const |
| 76 |
{ |
| 77 |
return _action; |
| 78 |
} |
| 79 |
|
| 80 |
protected: |
| 81 |
|
| 82 |
|
| 83 |
osg::ref_ptr<ActionSet> _actionSet; |
| 84 |
std::vector<XrPath> _subactionPaths; |
| 85 |
XrActionCreateInfo _createInfo; |
| 86 |
XrAction _action; |
| 87 |
}; |
| 88 |
|
| 89 |
|
| 90 |
class ActionStateBase : public osg::Referenced |
| 91 |
{ |
| 92 |
public: |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
ActionStateBase(Action *action, Session *session, |
| 97 |
Path subactionPath = Path()); |
| 98 |
virtual ~ActionStateBase(); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
inline bool valid() const |
| 103 |
{ |
| 104 |
return _valid; |
| 105 |
} |
| 106 |
|
| 107 |
inline bool check(XrResult result, const char *actionMsg) const |
| 108 |
{ |
| 109 |
return _action->check(result, actionMsg); |
| 110 |
} |
| 111 |
|
| 112 |
protected: |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
bool checkUpdate(); |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
osg::ref_ptr<Action> _action; |
| 122 |
osg::ref_ptr<Session> _session; |
| 123 |
Path _subactionPath; |
| 124 |
bool _valid; |
| 125 |
unsigned int _syncCount; |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
template <typename T> |
| 130 |
class ActionStateCommon : public ActionStateBase |
| 131 |
{ |
| 132 |
private: |
| 133 |
|
| 134 |
typedef ActionStateBase Base; |
| 135 |
|
| 136 |
public: |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
ActionStateCommon(Action *action, Session *session, |
| 141 |
Path subactionPath = Path()) : |
| 142 |
Base(action, session, subactionPath) |
| 143 |
{ |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
bool isActive() const |
| 149 |
{ |
| 150 |
assert(valid()); |
| 151 |
return _state.isActive; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
bool update() |
| 158 |
{ |
| 159 |
if (Base::checkUpdate()) |
| 160 |
return updateState(); |
| 161 |
return valid(); |
| 162 |
} |
| 163 |
|
| 164 |
protected: |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
bool updateState(); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
T _state; |
| 173 |
}; |
| 174 |
|
| 175 |
|
| 176 |
typedef ActionStateCommon<XrActionStateBoolean> ActionStateCommonBoolean; |
| 177 |
typedef ActionStateCommon<XrActionStateFloat> ActionStateCommonFloat; |
| 178 |
typedef ActionStateCommon<XrActionStateVector2f> ActionStateCommonVector2f; |
| 179 |
typedef ActionStateCommon<XrActionStatePose> ActionStateCommonPose; |
| 180 |
|
| 181 |
|
| 182 |
template <typename T> |
| 183 |
struct ActionTypeInfo; |
| 184 |
|
| 185 |
|
| 186 |
template <> |
| 187 |
struct ActionTypeInfo<XrActionStateBoolean> |
| 188 |
{ |
| 189 |
static bool convert(XrBool32 value) |
| 190 |
{ |
| 191 |
return value; |
| 192 |
} |
| 193 |
|
| 194 |
static bool defaultValue() |
| 195 |
{ |
| 196 |
return false; |
| 197 |
} |
| 198 |
}; |
| 199 |
|
| 200 |
|
| 201 |
template <> |
| 202 |
struct ActionTypeInfo<XrActionStateFloat> |
| 203 |
{ |
| 204 |
static float convert(float value) |
| 205 |
{ |
| 206 |
return value; |
| 207 |
} |
| 208 |
|
| 209 |
static float defaultValue() |
| 210 |
{ |
| 211 |
return 0.0f; |
| 212 |
} |
| 213 |
}; |
| 214 |
|
| 215 |
|
| 216 |
template <> |
| 217 |
struct ActionTypeInfo<XrActionStateVector2f> |
| 218 |
{ |
| 219 |
static osg::Vec2f convert(const XrVector2f &value) |
| 220 |
{ |
| 221 |
return osg::Vec2f(value.x, value.y); |
| 222 |
} |
| 223 |
|
| 224 |
static osg::Vec2f defaultValue() |
| 225 |
{ |
| 226 |
return osg::Vec2f(0.0f, 0.0f); |
| 227 |
} |
| 228 |
}; |
| 229 |
|
| 230 |
|
| 231 |
template <typename T> |
| 232 |
class ActionStateSimple : public ActionStateCommon<T> |
| 233 |
{ |
| 234 |
private: |
| 235 |
|
| 236 |
typedef ActionStateCommon<T> Base; |
| 237 |
|
| 238 |
public: |
| 239 |
|
| 240 |
typedef ActionTypeInfo<T> Info; |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
ActionStateSimple(Action *action, Session *session, |
| 245 |
Path subactionPath = Path()) : |
| 246 |
Base(action, session, subactionPath) |
| 247 |
{ |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
auto getCurrentState() const |
| 253 |
{ |
| 254 |
assert(this->valid()); |
| 255 |
return Info::convert(Base::_state.currentState); |
| 256 |
} |
| 257 |
|
| 258 |
bool hasChangedSinceLastSync() const |
| 259 |
{ |
| 260 |
assert(this->valid()); |
| 261 |
return Base::_state.changedSinceLastSync; |
| 262 |
} |
| 263 |
|
| 264 |
XrTime getLastChangedTime() const |
| 265 |
{ |
| 266 |
assert(this->valid()); |
| 267 |
return Base::_state.lastChangedTime; |
| 268 |
} |
| 269 |
}; |
| 270 |
|
| 271 |
|
| 272 |
typedef ActionStateSimple<XrActionStateBoolean> ActionStateBoolean; |
| 273 |
typedef ActionStateSimple<XrActionStateFloat> ActionStateFloat; |
| 274 |
typedef ActionStateSimple<XrActionStateVector2f> ActionStateVector2f; |
| 275 |
|
| 276 |
|
| 277 |
template <XrActionType type, typename T> |
| 278 |
class ActionTyped : public Action |
| 279 |
{ |
| 280 |
public: |
| 281 |
|
| 282 |
typedef T State; |
| 283 |
|
| 284 |
ActionTyped(ActionSet *actionSet, |
| 285 |
const std::string &name, |
| 286 |
const std::string &localizedName) : |
| 287 |
Action(actionSet, name, localizedName, type) |
| 288 |
{ |
| 289 |
} |
| 290 |
|
| 291 |
osg::ref_ptr<State> createState(Session *session, |
| 292 |
Path subactionPath = Path()) |
| 293 |
{ |
| 294 |
return new State(this, session, subactionPath); |
| 295 |
} |
| 296 |
}; |
| 297 |
|
| 298 |
|
| 299 |
class ActionStatePose; |
| 300 |
class ActionStateVibration; |
| 301 |
|
| 302 |
|
| 303 |
typedef ActionTyped<XR_ACTION_TYPE_BOOLEAN_INPUT, ActionStateBoolean> ActionBoolean; |
| 304 |
typedef ActionTyped<XR_ACTION_TYPE_FLOAT_INPUT, ActionStateFloat> ActionFloat; |
| 305 |
typedef ActionTyped<XR_ACTION_TYPE_VECTOR2F_INPUT, ActionStateVector2f> ActionVector2f; |
| 306 |
typedef ActionTyped<XR_ACTION_TYPE_POSE_INPUT, ActionStatePose> ActionPose; |
| 307 |
typedef ActionTyped<XR_ACTION_TYPE_VIBRATION_OUTPUT, ActionStateVibration> ActionVibration; |
| 308 |
|
| 309 |
|
| 310 |
class ActionStatePose : public ActionStateCommon<XrActionStatePose> |
| 311 |
{ |
| 312 |
private: |
| 313 |
|
| 314 |
typedef ActionStateCommon<XrActionStatePose> Base; |
| 315 |
|
| 316 |
public: |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
ActionStatePose(ActionPose *action, Session *session, |
| 321 |
Path subactionPath = Path()); |
| 322 |
~ActionStatePose(); |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
Space *getSpace() |
| 327 |
{ |
| 328 |
return _space.get(); |
| 329 |
} |
| 330 |
|
| 331 |
protected: |
| 332 |
|
| 333 |
osg::ref_ptr<Space> _space; |
| 334 |
}; |
| 335 |
|
| 336 |
class ActionStateVibration : public osg::Referenced |
| 337 |
{ |
| 338 |
public: |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
ActionStateVibration(ActionVibration *action, Session *session, |
| 343 |
Path subactionPath = Path()); |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
inline bool check(XrResult result, const char *actionMsg) const |
| 348 |
{ |
| 349 |
return _action->check(result, actionMsg); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
bool applyHapticFeedback(int64_t duration_ns, float frequency, |
| 355 |
float amplitude) const; |
| 356 |
bool stopHapticFeedback() const; |
| 357 |
|
| 358 |
protected: |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
osg::ref_ptr<Action> _action; |
| 363 |
osg::ref_ptr<Session> _session; |
| 364 |
Path _subactionPath; |
| 365 |
}; |
| 366 |
|
| 367 |
} |
| 368 |
|
| 369 |
} |
| 370 |
|
| 371 |
#endif |