Skip to content
Snippets Groups Projects
Commit ac111cdb authored by Andrew Wilson's avatar Andrew Wilson :elephant:
Browse files

ENH: Haptic devices now emit button events

parent b2b8ba4c
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -40,12 +40,11 @@ enum class EventType
Pause,
Modified,
KeyPress,
VRButtonPress,
DeviceButtonPress,
MouseEvent,
PreUpdate,
PostUpdate,
Configure
//?? add one for collision?
};
///
......
......@@ -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
///
......
......@@ -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;
......
......@@ -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
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment