diff --git a/Examples/OpenVRController/OpenVRControllerExample.cpp b/Examples/OpenVRController/OpenVRControllerExample.cpp index deeb899735f84b184350dbc00b94d3a63eb35bd6..f6559cac3ab23a14313000a23ce4fdde750db65b 100644 --- a/Examples/OpenVRController/OpenVRControllerExample.cpp +++ b/Examples/OpenVRController/OpenVRControllerExample.cpp @@ -135,10 +135,11 @@ main() imstkNew<SceneObjectController> controller2(scalpelBlade10, viewer->getVRDeviceClient(OPENVR_RIGHT_CONTROLLER)); scene->addController(controller2); - // VRButtonPress emitted from viewer, recieve it in the scene that way the scene is not running whilst it is swapped + // This button event is emitted from the viewer's thread, thus it is queued to the scene so that we do not + // run it while the scene is updating bool blade10InHand = true; - queueConnect<VRButtonEvent>(viewer->getVRDeviceClient(OPENVR_RIGHT_CONTROLLER), EventType::VRButtonPress, sceneManager, - [&](VRButtonEvent* e) + queueConnect<ButtonEvent>(viewer->getVRDeviceClient(OPENVR_RIGHT_CONTROLLER), EventType::DeviceButtonPress, sceneManager, + [&](ButtonEvent* e) { // When any button pressed, swap blade // todo: distance metric not working diff --git a/Source/Common/imstkEventObject.h b/Source/Common/imstkEventObject.h index 8582fb7b82f241dd86325c680b0991a65f86e86c..877be3abaf4e6adfaf6d2493d48ca025798606a2 100644 --- a/Source/Common/imstkEventObject.h +++ b/Source/Common/imstkEventObject.h @@ -40,12 +40,11 @@ enum class EventType Pause, Modified, KeyPress, - VRButtonPress, + DeviceButtonPress, MouseEvent, PreUpdate, PostUpdate, Configure - //?? add one for collision? }; /// diff --git a/Source/Devices/imstkDeviceClient.h b/Source/Devices/imstkDeviceClient.h index 53b2989375de05bfa284e3f5e469116447edaa20..a4abc218b631f9a1828bb1c71f4f5164ca2e1361 100644 --- a/Source/Devices/imstkDeviceClient.h +++ b/Source/Devices/imstkDeviceClient.h @@ -28,6 +28,35 @@ namespace imstk { +using DeviceType = int; +#define UNKNOWN_DEVICE 0 +#define OPENHAPTICS_DEVICE 1 +#define OPENVR_LEFT_CONTROLLER 3 +#define OPENVR_RIGHT_CONTROLLER 4 +#define OPENVR_HMD 5 + +using ButtonStateType = int; +#define BUTTON_RELEASED 0 +#define BUTTON_TOUCHED 1 +#define BUTTON_UNTOUCHED 2 +#define BUTTON_PRESSED 3 + +class ButtonEvent : public Event +{ +public: + ButtonEvent(const int button, const ButtonStateType keyPressType) : Event(EventType::DeviceButtonPress, 1), + m_buttonState(keyPressType), + m_button(button) + { + } + + virtual ~ButtonEvent() override = default; + +public: + ButtonStateType m_buttonState; + const int m_button = -1; +}; + /// /// \class DeviceClient /// diff --git a/Source/Devices/imstkHapticDeviceClient.cpp b/Source/Devices/imstkHapticDeviceClient.cpp index 2f1ffa456135c26566883237c15a4c2db762b69d..aaee2ebccd18dd5af3d0314acb6aca8e9c476217 100644 --- a/Source/Devices/imstkHapticDeviceClient.cpp +++ b/Source/Devices/imstkHapticDeviceClient.cpp @@ -110,10 +110,22 @@ HapticDeviceClient::hapticCallback(void* pData) client->m_position << state.pos[0], state.pos[1], state.pos[2]; client->m_velocity << state.vel[0], state.vel[1], state.vel[2]; client->m_orientation = (Eigen::Affine3d(Eigen::Matrix4d(state.trans))).rotation(); - client->m_buttons[0] = state.buttons & HD_DEVICE_BUTTON_1; - client->m_buttons[1] = state.buttons & HD_DEVICE_BUTTON_2; - client->m_buttons[2] = state.buttons & HD_DEVICE_BUTTON_3; - client->m_buttons[3] = state.buttons & HD_DEVICE_BUTTON_4; + + for (int i = 0; i < 4; i++) + { + // If button down and not previously down + if ((state.buttons & (1 << i)) && !client->m_buttons[i]) + { + client->m_buttons[i] = true; + client->postEvent(ButtonEvent(i, BUTTON_PRESSED)); + } + // If button not down, and previously down + else if (!(state.buttons & (1 << i)) && client->m_buttons[i]) + { + client->m_buttons[i] = false; + client->postEvent(ButtonEvent(i, BUTTON_RELEASED)); + } + } client->m_trackingEnabled = true; diff --git a/Source/Devices/imstkOpenVRDeviceClient.cpp b/Source/Devices/imstkOpenVRDeviceClient.cpp index 6605e0a3d642b1822ac9ac07662b0f69f361c9ea..7e9da67a07c44f3349eccec937fb047926465690 100644 --- a/Source/Devices/imstkOpenVRDeviceClient.cpp +++ b/Source/Devices/imstkOpenVRDeviceClient.cpp @@ -24,7 +24,7 @@ namespace imstk { std::shared_ptr<OpenVRDeviceClient> -OpenVRDeviceClient::New(VRDeviceType deviceType) +OpenVRDeviceClient::New(DeviceType deviceType) { return std::shared_ptr<OpenVRDeviceClient>(new OpenVRDeviceClient(deviceType)); } @@ -36,7 +36,7 @@ OpenVRDeviceClient::emitButtonTouched(const int buttonId) m_buttons[buttonId] = BUTTON_TOUCHED; if (prevButtonState != BUTTON_TOUCHED) { - this->postEvent(VRButtonEvent(buttonId, BUTTON_TOUCHED)); + this->postEvent(ButtonEvent(buttonId, BUTTON_TOUCHED)); } } @@ -47,7 +47,7 @@ OpenVRDeviceClient::emitButtonUntouched(const int buttonId) m_buttons[buttonId] = BUTTON_UNTOUCHED; if (prevButtonState != BUTTON_UNTOUCHED) { - this->postEvent(VRButtonEvent(buttonId, BUTTON_UNTOUCHED)); + this->postEvent(ButtonEvent(buttonId, BUTTON_UNTOUCHED)); } } @@ -58,7 +58,7 @@ OpenVRDeviceClient::emitButtonPress(const int buttonId) m_buttons[buttonId] = BUTTON_PRESSED; if (prevButtonState != BUTTON_PRESSED) { - this->postEvent(VRButtonEvent(buttonId, BUTTON_PRESSED)); + this->postEvent(ButtonEvent(buttonId, BUTTON_PRESSED)); } } @@ -69,7 +69,7 @@ OpenVRDeviceClient::emitButtonRelease(const int buttonId) m_buttons[buttonId] = BUTTON_RELEASED; if (prevButtonState != BUTTON_RELEASED) { - this->postEvent(VRButtonEvent(buttonId, BUTTON_RELEASED)); + this->postEvent(ButtonEvent(buttonId, BUTTON_RELEASED)); } } } \ No newline at end of file diff --git a/Source/Devices/imstkOpenVRDeviceClient.h b/Source/Devices/imstkOpenVRDeviceClient.h index 42aa17b13bc98771e690de7af136edae8f423bc4..9a00831a4d800fcc6128dc526ffbb73602109871 100644 --- a/Source/Devices/imstkOpenVRDeviceClient.h +++ b/Source/Devices/imstkOpenVRDeviceClient.h @@ -28,33 +28,6 @@ class vtkInteractorStyleVR; namespace imstk { -using VRDeviceType = int; -#define OPENVR_LEFT_CONTROLLER 0 -#define OPENVR_RIGHT_CONTROLLER 1 -#define OPENVR_HMD 2 - -using VRButtonStateType = int; -#define BUTTON_RELEASED 0 -#define BUTTON_TOUCHED 1 -#define BUTTON_UNTOUCHED 2 -#define BUTTON_PRESSED 3 - -class VRButtonEvent : public Event -{ -public: - VRButtonEvent(const int button, const VRButtonStateType keyPressType) : Event(EventType::VRButtonPress, 1), - m_buttonState(keyPressType), - m_button(button) - { - } - - virtual ~VRButtonEvent() override = default; - -public: - VRButtonStateType m_buttonState; - const int m_button = -1; -}; - /// /// \class OpenVRDeviceClient /// @@ -68,13 +41,13 @@ protected: /// /// This object is only creatable through its New method /// - OpenVRDeviceClient(VRDeviceType deviceType) : DeviceClient("OpenVRDevice", ""), + OpenVRDeviceClient(DeviceType deviceType) : DeviceClient("OpenVRDevice", ""), m_deviceType(deviceType) { } /// /// This object is only creatable through this method /// - static std::shared_ptr<OpenVRDeviceClient> New(VRDeviceType deviceType); + static std::shared_ptr<OpenVRDeviceClient> New(DeviceType deviceType); public: virtual ~OpenVRDeviceClient() override = default; @@ -83,7 +56,7 @@ public: friend class ::vtkInteractorStyleVR; public: - VRDeviceType getDeviceType() const { return m_deviceType; } + DeviceType getDeviceType() const { return m_deviceType; } /// /// \brief Set the current position and orientation @@ -105,6 +78,6 @@ protected: void emitButtonRelease(const int buttonId); private: - VRDeviceType m_deviceType; + DeviceType m_deviceType; }; } \ No newline at end of file