diff --git a/plugins/hifiKinect/src/KinectPlugin.cpp b/plugins/hifiKinect/src/KinectPlugin.cpp index 2c837d83d3..0bff69ed57 100644 --- a/plugins/hifiKinect/src/KinectPlugin.cpp +++ b/plugins/hifiKinect/src/KinectPlugin.cpp @@ -227,15 +227,26 @@ void KinectPlugin::init() { static const QString KINECT_PLUGIN { "Kinect" }; { auto getter = [this]()->bool { return _enabled; }; - auto setter = [this](bool value) { _enabled = value; saveSettings(); }; + auto setter = [this](bool value) { + _enabled = value; saveSettings(); + if (!_enabled) { + auto userInputMapper = DependencyManager::get(); + userInputMapper->withLock([&, this]() { + _inputDevice->clearState(); + }); + } + }; auto preference = new CheckPreference(KINECT_PLUGIN, "Enabled", getter, setter); preferences->addPreference(preference); } } bool KinectPlugin::isSupported() const { - // FIXME -- check to see if there's a camera or not... - return true; + bool supported = false; +#ifdef HAVE_KINECT + supported = initializeDefaultSensor(); +#endif + return supported; } bool KinectPlugin::activate() { @@ -250,13 +261,29 @@ bool KinectPlugin::activate() { userInputMapper->registerDevice(_inputDevice); return initializeDefaultSensor(); - } else { - return false; } + return false; } -bool KinectPlugin::initializeDefaultSensor() { +bool KinectPlugin::isHandController() const { + bool sensorAvailable = false; #ifdef HAVE_KINECT + if (_kinectSensor) { + BOOLEAN sensorIsAvailable = FALSE; + HRESULT hr = _kinectSensor->get_IsAvailable(&sensorIsAvailable); + sensorAvailable = SUCCEEDED(hr) && (sensorIsAvailable == TRUE); + } +#endif + return _enabled && _initialized && sensorAvailable; +} + + +bool KinectPlugin::initializeDefaultSensor() const { +#ifdef HAVE_KINECT + if (_initialized) { + return true; + } + HRESULT hr; hr = GetDefaultKinectSensor(&_kinectSensor); @@ -289,6 +316,7 @@ bool KinectPlugin::initializeDefaultSensor() { return false; } + _initialized = true; return true; #else return false; @@ -527,3 +555,9 @@ void KinectPlugin::InputDevice::update(float deltaTime, const controller::InputC } } +void KinectPlugin::InputDevice::clearState() { + for (size_t i = 0; i < KinectJointIndex::Size; i++) { + int poseIndex = KinectJointIndexToPoseIndex((KinectJointIndex)i); + _poseStateMap[poseIndex] = controller::Pose(); + } +} diff --git a/plugins/hifiKinect/src/KinectPlugin.h b/plugins/hifiKinect/src/KinectPlugin.h index c66d746427..b10698fa31 100644 --- a/plugins/hifiKinect/src/KinectPlugin.h +++ b/plugins/hifiKinect/src/KinectPlugin.h @@ -41,7 +41,7 @@ template inline void SafeRelease(Interface *& pInterfaceToRelea class KinectPlugin : public InputPlugin { Q_OBJECT public: - bool isHandController() const override { return true; } + bool isHandController() const override; // Plugin functions virtual void init() override; @@ -79,6 +79,8 @@ protected: void update(float deltaTime, const controller::InputCalibrationData& inputCalibrationData, const std::vector& joints, const std::vector& prevJoints); + + void clearState(); }; std::shared_ptr _inputDevice { std::make_shared() }; @@ -86,7 +88,8 @@ protected: static const char* NAME; static const char* KINECT_ID_STRING; - bool _enabled; + bool _enabled { false }; + mutable bool _initialized { false }; // copy of data directly from the KinectDataReader SDK std::vector _joints; @@ -97,18 +100,18 @@ protected: // Kinect SDK related items... - bool KinectPlugin::initializeDefaultSensor(); + bool KinectPlugin::initializeDefaultSensor() const; void updateBody(); #ifdef HAVE_KINECT void ProcessBody(INT64 time, int bodyCount, IBody** bodies); // Current Kinect - IKinectSensor* _kinectSensor { nullptr }; - ICoordinateMapper* _coordinateMapper { nullptr }; + mutable IKinectSensor* _kinectSensor { nullptr }; + mutable ICoordinateMapper* _coordinateMapper { nullptr }; // Body reader - IBodyFrameReader* _bodyFrameReader { nullptr }; + mutable IBodyFrameReader* _bodyFrameReader { nullptr }; #endif };