Browse Source

Add love.mouse.getGlobalPosition. Useful for custom window dragging.

Sasha Szpakowski 1 year ago
parent
commit
50b18d1ac7

+ 1 - 4
src/modules/mouse/Mouse.h

@@ -53,12 +53,9 @@ public:
 
 	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;

+ 26 - 20
src/modules/mouse/sdl/Mouse.cpp

@@ -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;
@@ -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)

+ 1 - 4
src/modules/mouse/sdl/Mouse.h

@@ -55,12 +55,9 @@ public:
 
 	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;

+ 26 - 6
src/modules/mouse/wrap_Mouse.cpp

@@ -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;
 }
 
@@ -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;
 }
 
@@ -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);
@@ -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 },