Skip to content

Commit efe239f

Browse files
author
nihonium
committed
fix(nui): use absolute RGD mouse pos in SDK
tweak(input): allow usage of raw mouse input tweak(sdk): use raw mouse input instead of sendMousePos
1 parent b4f5d3f commit efe239f

File tree

14 files changed

+242
-106
lines changed

14 files changed

+242
-106
lines changed

code/client/citicore/ReverseGameData.h

+11-4
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,18 @@ struct ioPad
7878

7979
struct ReverseGameData
8080
{
81-
uint8_t keyboardState[256];
82-
int mouseX;
83-
int mouseY;
81+
uint8_t keyboardState[256];
82+
83+
int mouseAbsX;
84+
int mouseAbsY;
85+
86+
int mouseDeltaX;
87+
int mouseDeltaY;
88+
8489
int mouseWheel;
8590
int mouseButtons;
91+
92+
bool useRawMouseCapture;
8693

8794
HANDLE inputMutex;
8895
DWORD inputMutexPID;
@@ -114,7 +121,7 @@ struct ReverseGameData
114121
rage::ioPad gamepad;
115122

116123
ReverseGameData()
117-
: mouseX(0), mouseY(0), mouseWheel(0), mouseButtons(0), inputMutex(NULL), produceSema(NULL), consumeSema(NULL), surfaceLimit(0), produceIdx(0), consumeIdx(0), inited(false), isLauncher(false), editWidth(false), fpsLimit(0)
124+
: mouseDeltaX(0), mouseAbsX(0), mouseDeltaY(0), mouseAbsY(0), mouseWheel(0), mouseButtons(0), useRawMouseCapture(false), inputMutex(NULL), produceSema(NULL), consumeSema(NULL), surfaceLimit(0), produceIdx(0), consumeIdx(0), inited(false), isLauncher(false), editWidth(false), fpsLimit(0)
118125
{
119126
memset(keyboardState, 0, sizeof(keyboardState));
120127
memset(surfaces, 0, sizeof(surfaces));

code/components/citizen-game-main/src/GameWindow.Win32.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -809,8 +809,8 @@ class Win32GameWindow : public GameWindow
809809
}
810810
else if (input->header.dwType == RIM_TYPEMOUSE)
811811
{
812-
rgd->mouseX += input->data.mouse.lLastX;
813-
rgd->mouseY += input->data.mouse.lLastY;
812+
rgd->mouseDeltaX += input->data.mouse.lLastX;
813+
rgd->mouseDeltaY += input->data.mouse.lLastY;
814814

815815
if (input->data.mouse.usButtonFlags & RI_MOUSE_WHEEL)
816816
{

code/components/nui-core/src/NUIApp.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ void NUIApp::OnContextCreated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame>
149149
std::vector<std::string> fxdkHandlers{
150150
"resizeGame",
151151
"initRGDInput",
152-
"sendMousePos",
152+
"setRawMouseCapture",
153153
"sendMouseWheel",
154154
"setKeyState",
155155
"setMouseButtonState",

code/components/nui-core/src/NUICallbacks_SDK.cpp

-21
Original file line numberDiff line numberDiff line change
@@ -120,27 +120,6 @@ static InitFunction initFunction([]()
120120

121121
return CefV8Value::CreateUndefined();
122122
});
123-
124-
nuiApp->AddV8Handler("setInputChar", [](const CefV8ValueList& arguments, CefString& exception)
125-
{
126-
if (arguments.size() == 1)
127-
{
128-
// "x", "X", "y", "Y"
129-
if (arguments[0]->IsString())
130-
{
131-
auto charString = arguments[0]->GetStringValue();
132-
rgd->inputChar = charString.c_str()[0];
133-
}
134-
// big keys in JS are processed as "BackSpace", "Shift", "Alt", ... The charcode is sent instead in this case.
135-
else
136-
{
137-
auto charCode = arguments[0]->GetIntValue();
138-
rgd->inputChar = (wchar_t)charCode;
139-
}
140-
}
141-
142-
return CefV8Value::CreateUndefined();
143-
});
144123

145124
nuiApp->AddV8Handler("fxdkSendApiMessage", [](const CefV8ValueList& arguments, CefString& exception)
146125
{

code/components/nui-core/src/NUICallbacks_SDKInput.cpp

+52-16
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,19 @@ static void ExecOp(const QueueOp& opt, bool additive = false)
5959

6060
switch (op)
6161
{
62-
case INPUT_OP::MOUSE_POS:
62+
/*case INPUT_OP::MOUSE_POS:
6363
{
6464
auto& mousePos = std::get<InputMousePos>(payload);
6565
6666
if (additive)
6767
{
68-
rgd->mouseX += mousePos.x;
69-
rgd->mouseY += mousePos.y;
68+
rgd->mouseDeltaX += mousePos.x;
69+
rgd->mouseDeltaY += mousePos.y;
7070
}
7171
else
7272
{
73-
rgd->mouseX = mousePos.x;
74-
rgd->mouseY = mousePos.y;
73+
rgd->mouseDeltaX = mousePos.x;
74+
rgd->mouseDeltaY = mousePos.y;
7575
}
7676
break;
7777
}
@@ -100,7 +100,7 @@ static void ExecOp(const QueueOp& opt, bool additive = false)
100100
rgd->mouseButtons &= ~(1 << mouseButtonState.index);
101101
}
102102
break;
103-
}
103+
}*/
104104
case INPUT_OP::KEY_STATE:
105105
{
106106
auto& keyState = std::get<InputKeyState>(payload);
@@ -193,25 +193,40 @@ static InitFunction initFunction([]()
193193
return CefV8Value::CreateBool(true);
194194
});
195195

196-
nuiApp->AddV8Handler("sendMousePos", [](const CefV8ValueList& arguments, CefString& exception)
196+
nuiApp->AddV8Handler("setRawMouseCapture", [](const CefV8ValueList& arguments, CefString& exception)
197197
{
198-
if (arguments.size() != 2)
198+
if (arguments.size() != 1)
199199
{
200-
exception.FromString(fmt::sprintf("Expected 2 arguments, got %d", arguments.size()));
200+
exception.FromString(fmt::sprintf("Expected 1 argument, got %d", arguments.size()));
201201
return CefV8Value::CreateBool(false);
202202
}
203203

204-
int x = arguments[0]->GetIntValue();
205-
int y = arguments[1]->GetIntValue();
206-
207-
rgd->mouseX = x;
208-
rgd->mouseY = y;
209-
210-
//ExecOrQueueOp({ INPUT_OP::MOUSE_POS, InputMousePos{ x, y } });
204+
rgd->useRawMouseCapture = arguments[0]->GetBoolValue();
211205

212206
return CefV8Value::CreateBool(true);
213207
});
214208

209+
//nuiApp->AddV8Handler("sendMousePos", [](const CefV8ValueList& arguments, CefString& exception)
210+
//{
211+
// return CefV8Value::CreateBool(true);
212+
213+
// if (arguments.size() != 2)
214+
// {
215+
// exception.FromString(fmt::sprintf("Expected 2 arguments, got %d", arguments.size()));
216+
// return CefV8Value::CreateBool(false);
217+
// }
218+
219+
// int x = arguments[0]->GetIntValue();
220+
// int y = arguments[1]->GetIntValue();
221+
222+
// rgd->mouseDeltaX = x;
223+
// rgd->mouseDeltaY = y;
224+
225+
// //ExecOrQueueOp({ INPUT_OP::MOUSE_POS, InputMousePos{ x, y } });
226+
227+
// return CefV8Value::CreateBool(true);
228+
//});
229+
215230
nuiApp->AddV8Handler("setMouseButtonState", [](const CefV8ValueList& arguments, CefString& exception)
216231
{
217232
if (arguments.size() != 2)
@@ -269,6 +284,27 @@ static InitFunction initFunction([]()
269284

270285
return CefV8Value::CreateBool(true);
271286
});
287+
288+
nuiApp->AddV8Handler("setInputChar", [](const CefV8ValueList& arguments, CefString& exception)
289+
{
290+
if (arguments.size() == 1)
291+
{
292+
// "x", "X", "y", "Y"
293+
if (arguments[0]->IsString())
294+
{
295+
auto charString = arguments[0]->GetStringValue();
296+
rgd->inputChar = charString.c_str()[0];
297+
}
298+
// big keys in JS are processed as "BackSpace", "Shift", "Alt", ... The charcode is sent instead in this case.
299+
else
300+
{
301+
auto charCode = arguments[0]->GetIntValue();
302+
rgd->inputChar = (wchar_t)charCode;
303+
}
304+
}
305+
306+
return CefV8Value::CreateUndefined();
307+
});
272308

273309
nuiApp->AddContextReleaseHandler([](CefRefPtr<CefV8Context>)
274310
{

code/components/nui-core/src/NUIRenderCallbacks.cpp

+22-2
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,28 @@ static HookFunction initFunction([] ()
130130
{
131131
static HostSharedData<ReverseGameData> rgd("CfxReverseGameData");
132132

133-
cursorPos.x = rgd->mouseX;
134-
cursorPos.y = rgd->mouseY;
133+
cursorPos.x = rgd->mouseAbsX;
134+
cursorPos.y = rgd->mouseAbsY;
135+
136+
//static auto mp = POINT{ 0, 0 };
137+
138+
//static auto clamp = [](int n, int min, int max)
139+
//{
140+
// if (n < min) {
141+
// return min;
142+
// }
143+
144+
// if (n > max) {
145+
// return max;
146+
// }
147+
148+
// return n;
149+
//};
150+
151+
//mp.x = clamp(mp.x + rgd->mouseDeltaX, 0, rgd->twidth);
152+
//mp.y = clamp(mp.y + rgd->mouseDeltaY, 0, rgd->theight);
153+
154+
//cursorPos = mp;
135155
}
136156
else
137157
{

code/components/nui-resources/src/ResourceUIScripting.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,8 @@ static InitFunction initFunction([] ()
421421
{
422422
static HostSharedData<ReverseGameData> rgd("CfxReverseGameData");
423423

424-
*context.GetArgument<int*>(0) = rgd->mouseX;
425-
*context.GetArgument<int*>(1) = rgd->mouseY;
424+
*context.GetArgument<int*>(0) = rgd->mouseAbsX;
425+
*context.GetArgument<int*>(1) = rgd->mouseAbsY;
426426
});
427427
}
428428
else

code/components/rage-input-five/include/GlobalInput.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ struct GlobalInputHandler
44
{
55
virtual ~GlobalInputHandler() = default;
66

7-
fwEvent<DWORD, bool> OnKey;
7+
fwEvent<DWORD, bool> OnKey;
8+
fwEvent<RAWMOUSE> OnMouse;
89
};
910

1011
extern std::shared_ptr<GlobalInputHandler> CreateGlobalInputHandler();

code/components/rage-input-five/src/GlobalInput.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,19 @@ LRESULT GlobalInputHandlerLocal::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam
5050
{
5151
case WM_CREATE:
5252
{
53-
constexpr uint8_t nRid = 1;
53+
constexpr uint8_t nRid = 2;
5454
RAWINPUTDEVICE rid[nRid] = {};
5555

5656
rid[0].usUsagePage = HID_USAGE_PAGE_GENERIC;
5757
rid[0].usUsage = HID_USAGE_GENERIC_KEYBOARD;
5858
rid[0].dwFlags = RIDEV_INPUTSINK | RIDEV_DEVNOTIFY;
5959
rid[0].hwndTarget = hWnd;
6060

61+
rid[1].usUsagePage = HID_USAGE_PAGE_GENERIC;
62+
rid[1].usUsage = HID_USAGE_GENERIC_MOUSE;
63+
rid[1].dwFlags = RIDEV_INPUTSINK | RIDEV_DEVNOTIFY;
64+
rid[1].hwndTarget = hWnd;
65+
6166
if (!RegisterRawInputDevices(rid, nRid, sizeof(RAWINPUTDEVICE)))
6267
{
6368
trace("RegisterRawInputDevices() failed with error %u!\n", GetLastError());
@@ -86,8 +91,9 @@ LRESULT GlobalInputHandlerLocal::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam
8691
{
8792
case RIM_TYPEMOUSE:
8893
{
89-
const RAWMOUSE& mouse = input->data.mouse;
90-
// no-op
94+
const RAWMOUSE& mouse = input->data.mouse;
95+
96+
OnMouse(input->data.mouse);
9197
break;
9298
}
9399
case RIM_TYPEKEYBOARD:

0 commit comments

Comments
 (0)