lovr.system.wasMousePressed/Released;

This commit is contained in:
bjorn 2023-11-02 18:55:02 -07:00
parent 038db88cb7
commit c98db5b670
3 changed files with 30 additions and 0 deletions

View File

@ -290,6 +290,20 @@ static int l_lovrSystemIsMouseDown(lua_State* L) {
return 1;
}
static int l_lovrSystemWasMousePressed(lua_State* L) {
int button = luaL_checkint(L, 1) - 1;
bool pressed = lovrSystemWasMousePressed(button);
lua_pushboolean(L, pressed);
return 1;
}
static int l_lovrSystemWasMouseReleased(lua_State* L) {
int button = luaL_checkint(L, 1) - 1;
bool released = lovrSystemWasMouseReleased(button);
lua_pushboolean(L, released);
return 1;
}
static const luaL_Reg lovrSystem[] = {
{ "getOS", l_lovrSystemGetOS },
{ "getCoreCount", l_lovrSystemGetCoreCount },
@ -310,6 +324,8 @@ static const luaL_Reg lovrSystem[] = {
{ "getMouseY", l_lovrSystemGetMouseY },
{ "getMousePosition", l_lovrSystemGetMousePosition },
{ "isMouseDown", l_lovrSystemIsMouseDown },
{ "wasMousePressed", l_lovrSystemWasMousePressed },
{ "wasMouseReleased", l_lovrSystemWasMouseReleased },
{ NULL, NULL }
};

View File

@ -9,6 +9,7 @@ static struct {
bool keyRepeat;
bool prevKeyState[OS_KEY_COUNT];
bool keyState[OS_KEY_COUNT];
bool prevMouseState[8];
bool mouseState[8];
double mouseX;
double mouseY;
@ -134,6 +135,7 @@ float lovrSystemGetWindowDensity(void) {
void lovrSystemPollEvents(void) {
memcpy(state.prevKeyState, state.keyState, sizeof(state.keyState));
memcpy(state.prevMouseState, state.mouseState, sizeof(state.mouseState));
state.scrollDelta = 0.;
os_poll_events();
}
@ -168,6 +170,16 @@ bool lovrSystemIsMouseDown(int button) {
return state.mouseState[button];
}
bool lovrSystemWasMousePressed(int button) {
if ((size_t) button > COUNTOF(state.mouseState)) return false;
return !state.prevMouseState[button] && state.mouseState[button];
}
bool lovrSystemWasMouseReleased(int button) {
if ((size_t) button > COUNTOF(state.mouseState)) return false;
return state.prevMouseState[button] && !state.mouseState[button];
}
// This is kind of a hacky thing for the simulator, since we're kinda bad at event dispatch
float lovrSystemGetScrollDelta(void) {
return state.scrollDelta;

View File

@ -26,4 +26,6 @@ bool lovrSystemHasKeyRepeat(void);
void lovrSystemSetKeyRepeat(bool repeat);
void lovrSystemGetMousePosition(double* x, double* y);
bool lovrSystemIsMouseDown(int button);
bool lovrSystemWasMousePressed(int button);
bool lovrSystemWasMouseReleased(int button);
float lovrSystemGetScrollDelta(void);