2014-01-05 09:59:17 +00:00
|
|
|
#ifndef RUBY_INPUT_MOUSE_RAWINPUT
|
|
|
|
#define RUBY_INPUT_MOUSE_RAWINPUT
|
|
|
|
|
|
|
|
namespace ruby {
|
|
|
|
|
|
|
|
struct InputMouseRawInput {
|
|
|
|
uintptr_t handle = 0;
|
|
|
|
bool mouseAcquired = false;
|
|
|
|
|
|
|
|
struct Mouse {
|
2015-06-12 13:14:38 +00:00
|
|
|
shared_pointer<HID::Mouse> hid{new HID::Mouse};
|
2014-01-05 09:59:17 +00:00
|
|
|
|
|
|
|
signed relativeX = 0;
|
|
|
|
signed relativeY = 0;
|
|
|
|
signed relativeZ = 0;
|
|
|
|
bool buttons[5] = {0};
|
|
|
|
} ms;
|
|
|
|
|
2015-06-12 13:14:38 +00:00
|
|
|
auto acquire() -> bool {
|
2014-01-05 09:59:17 +00:00
|
|
|
if(mouseAcquired == false) {
|
|
|
|
mouseAcquired = true;
|
|
|
|
ShowCursor(false);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-12 13:14:38 +00:00
|
|
|
auto unacquire() -> bool {
|
2014-01-05 09:59:17 +00:00
|
|
|
if(mouseAcquired == true) {
|
|
|
|
mouseAcquired = false;
|
|
|
|
ReleaseCapture();
|
2015-06-12 13:14:38 +00:00
|
|
|
ClipCursor(nullptr);
|
2014-01-05 09:59:17 +00:00
|
|
|
ShowCursor(true);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-12 13:14:38 +00:00
|
|
|
auto acquired() -> bool {
|
2014-01-05 09:59:17 +00:00
|
|
|
if(mouseAcquired == true) {
|
|
|
|
SetFocus((HWND)handle);
|
|
|
|
SetCapture((HWND)handle);
|
|
|
|
RECT rc;
|
|
|
|
GetWindowRect((HWND)handle, &rc);
|
|
|
|
ClipCursor(&rc);
|
|
|
|
}
|
|
|
|
return GetCapture() == (HWND)handle;
|
|
|
|
}
|
|
|
|
|
2015-06-12 13:14:38 +00:00
|
|
|
auto update(RAWINPUT* input) -> void {
|
2014-01-05 09:59:17 +00:00
|
|
|
if((input->data.mouse.usFlags & 1) == MOUSE_MOVE_RELATIVE) {
|
|
|
|
ms.relativeX += input->data.mouse.lLastX;
|
|
|
|
ms.relativeY += input->data.mouse.lLastY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(input->data.mouse.usButtonFlags & RI_MOUSE_WHEEL) {
|
|
|
|
ms.relativeZ += (int16_t)input->data.mouse.usButtonData;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(input->data.mouse.usButtonFlags & RI_MOUSE_BUTTON_1_DOWN) ms.buttons[0] = 1;
|
|
|
|
if(input->data.mouse.usButtonFlags & RI_MOUSE_BUTTON_1_UP ) ms.buttons[0] = 0;
|
|
|
|
if(input->data.mouse.usButtonFlags & RI_MOUSE_BUTTON_2_DOWN) ms.buttons[1] = 1;
|
|
|
|
if(input->data.mouse.usButtonFlags & RI_MOUSE_BUTTON_2_UP ) ms.buttons[1] = 0;
|
|
|
|
if(input->data.mouse.usButtonFlags & RI_MOUSE_BUTTON_3_DOWN) ms.buttons[2] = 1;
|
|
|
|
if(input->data.mouse.usButtonFlags & RI_MOUSE_BUTTON_3_UP ) ms.buttons[2] = 0;
|
|
|
|
if(input->data.mouse.usButtonFlags & RI_MOUSE_BUTTON_4_DOWN) ms.buttons[3] = 1;
|
|
|
|
if(input->data.mouse.usButtonFlags & RI_MOUSE_BUTTON_4_UP ) ms.buttons[3] = 0;
|
|
|
|
if(input->data.mouse.usButtonFlags & RI_MOUSE_BUTTON_5_DOWN) ms.buttons[4] = 1;
|
|
|
|
if(input->data.mouse.usButtonFlags & RI_MOUSE_BUTTON_5_UP ) ms.buttons[4] = 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 13:14:38 +00:00
|
|
|
auto assign(unsigned groupID, unsigned inputID, int16_t value) -> void {
|
|
|
|
auto& group = ms.hid->group(groupID);
|
|
|
|
if(group.input(inputID).value() == value) return;
|
|
|
|
if(input.onChange) input.onChange(ms.hid, groupID, inputID, group.input(inputID).value(), value);
|
|
|
|
group.input(inputID).setValue(value);
|
2014-01-05 09:59:17 +00:00
|
|
|
}
|
|
|
|
|
2015-06-12 13:14:38 +00:00
|
|
|
auto poll(vector<shared_pointer<HID::Device>>& devices) -> void {
|
2014-01-05 09:59:17 +00:00
|
|
|
assign(HID::Mouse::GroupID::Axis, 0, ms.relativeX);
|
|
|
|
assign(HID::Mouse::GroupID::Axis, 1, ms.relativeY);
|
|
|
|
assign(HID::Mouse::GroupID::Axis, 2, ms.relativeZ);
|
|
|
|
|
|
|
|
//keys are intentionally reordered below:
|
|
|
|
//in ruby, button order is {left, middle, right, up, down}
|
|
|
|
assign(HID::Mouse::GroupID::Button, 0, ms.buttons[0]);
|
|
|
|
assign(HID::Mouse::GroupID::Button, 2, ms.buttons[1]);
|
|
|
|
assign(HID::Mouse::GroupID::Button, 1, ms.buttons[2]);
|
|
|
|
assign(HID::Mouse::GroupID::Button, 4, ms.buttons[3]);
|
|
|
|
assign(HID::Mouse::GroupID::Button, 3, ms.buttons[4]);
|
|
|
|
|
|
|
|
ms.relativeX = 0;
|
|
|
|
ms.relativeY = 0;
|
|
|
|
ms.relativeZ = 0;
|
|
|
|
|
2015-06-12 13:14:38 +00:00
|
|
|
devices.append(ms.hid);
|
2014-01-05 09:59:17 +00:00
|
|
|
}
|
|
|
|
|
2015-06-12 13:14:38 +00:00
|
|
|
auto init(uintptr_t handle) -> bool {
|
2014-01-05 09:59:17 +00:00
|
|
|
this->handle = handle;
|
|
|
|
|
2015-06-12 13:14:38 +00:00
|
|
|
ms.hid->setID(2);
|
2014-01-05 09:59:17 +00:00
|
|
|
|
2015-06-12 13:14:38 +00:00
|
|
|
ms.hid->axes().append("X");
|
|
|
|
ms.hid->axes().append("Y");
|
|
|
|
ms.hid->axes().append("Z");
|
2014-01-05 09:59:17 +00:00
|
|
|
|
2015-06-12 13:14:38 +00:00
|
|
|
ms.hid->buttons().append("Left");
|
|
|
|
ms.hid->buttons().append("Middle");
|
|
|
|
ms.hid->buttons().append("Right");
|
|
|
|
ms.hid->buttons().append("Up");
|
|
|
|
ms.hid->buttons().append("Down");
|
2014-01-05 09:59:17 +00:00
|
|
|
|
|
|
|
rawinput.updateMouse = {&InputMouseRawInput::update, this};
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-12 13:14:38 +00:00
|
|
|
auto term() -> void {
|
2014-01-05 09:59:17 +00:00
|
|
|
unacquire();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|