Skip to content

Commit

Permalink
Add love.mouse.getGlobalPosition. Useful for custom window dragging.
Browse files Browse the repository at this point in the history
  • Loading branch information
slime73 committed Jan 13, 2024
1 parent bf6a6f1 commit 50b18d1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 34 deletions.
5 changes: 1 addition & 4 deletions src/modules/mouse/Mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,9 @@ class Mouse : public Module

virtual bool isCursorSupported() const = 0;

virtual double getX() const = 0;
virtual double getY() const = 0;
virtual void getPosition(double &x, double &y) const = 0;
virtual void setX(double x) = 0;
virtual void setY(double y) = 0;
virtual void setPosition(double x, double y) = 0;
virtual void getGlobalPosition(double &x, double &y, int &displayindex) const = 0;
virtual void setVisible(bool visible) = 0;
virtual bool isDown(const std::vector<int> &buttons) const = 0;
virtual bool isVisible() const = 0;
Expand Down
46 changes: 26 additions & 20 deletions src/modules/mouse/sdl/Mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,6 @@ bool Mouse::isCursorSupported() const
return SDL_GetDefaultCursor() != nullptr;
}

double Mouse::getX() const
{
double x, y;
getPosition(x, y);
return x;
}

double Mouse::getY() const
{
double x, y;
getPosition(x, y);
return y;
}

void Mouse::getPosition(double &x, double &y) const
{
int mx, my;
Expand Down Expand Up @@ -173,14 +159,34 @@ void Mouse::setPosition(double x, double y)
SDL_PumpEvents();
}

void Mouse::setX(double x)
void Mouse::getGlobalPosition(double &x, double &y, int &displayindex) const
{
setPosition(x, getY());
}
int globalx, globaly;
SDL_GetGlobalMouseState(&globalx, &globaly);

void Mouse::setY(double y)
{
setPosition(getX(), y);
int mx = globalx;
int my = globaly;

int displaycount = SDL_GetNumVideoDisplays();

for (displayindex = 0; displayindex < displaycount; displayindex++)
{
SDL_Rect rect = {};
SDL_GetDisplayBounds(displayindex, &rect);

mx -= rect.x;
my -= rect.y;

SDL_Point p = { globalx, globaly };
if (SDL_PointInRect(&p, &rect))
break;
}

if (displayindex >= displaycount)
displayindex = 0;

x = (double)mx;
y = (double)my;
}

void Mouse::setVisible(bool visible)
Expand Down
5 changes: 1 addition & 4 deletions src/modules/mouse/sdl/Mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,9 @@ class Mouse : public love::mouse::Mouse

bool isCursorSupported() const override;

double getX() const override;
double getY() const override;
void getPosition(double &x, double &y) const override;
void setX(double x) override;
void setY(double y) override;
void setPosition(double x, double y) override;
void getGlobalPosition(double &x, double &y, int &displayindex) const override;
void setVisible(bool visible) override;
bool isDown(const std::vector<int> &buttons) const override;
bool isVisible() const override;
Expand Down
32 changes: 26 additions & 6 deletions src/modules/mouse/wrap_Mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,17 @@ int w_isCursorSupported(lua_State *L)

int w_getX(lua_State *L)
{
lua_pushnumber(L, instance()->getX());
double x, y;
instance()->getPosition(x, y);
lua_pushnumber(L, x);
return 1;
}

int w_getY(lua_State *L)
{
lua_pushnumber(L, instance()->getY());
double x, y;
instance()->getPosition(x, y);
lua_pushnumber(L, y);
return 1;
}

Expand All @@ -121,15 +125,19 @@ int w_getPosition(lua_State *L)

int w_setX(lua_State *L)
{
double x = luaL_checknumber(L, 1);
instance()->setX(x);
double x, y;
instance()->getPosition(x, y);
x = luaL_checknumber(L, 1);
instance()->setPosition(x, y);
return 0;
}

int w_setY(lua_State *L)
{
double y = luaL_checknumber(L, 1);
instance()->setY(y);
double x, y;
instance()->getPosition(x, y);
y = luaL_checknumber(L, 1);
instance()->setPosition(x, y);
return 0;
}

Expand All @@ -141,6 +149,17 @@ int w_setPosition(lua_State *L)
return 0;
}

int w_getGlobalPosition(lua_State *L)
{
double x, y;
int displayindex;
instance()->getGlobalPosition(x, y, displayindex);
lua_pushnumber(L, x);
lua_pushnumber(L, y);
lua_pushinteger(L, displayindex + 1);
return 3;
}

int w_isDown(lua_State *L)
{
bool istable = lua_istable(L, 1);
Expand Down Expand Up @@ -220,6 +239,7 @@ static const luaL_Reg functions[] =
{ "setX", w_setX },
{ "setY", w_setY },
{ "setPosition", w_setPosition },
{ "getGlobalPosition", w_getGlobalPosition },
{ "isDown", w_isDown },
{ "setVisible", w_setVisible },
{ "isVisible", w_isVisible },
Expand Down

0 comments on commit 50b18d1

Please sign in to comment.