1
0
mirror of https://github.com/blupi-games/planetblupi synced 2024-12-30 10:15:36 +01:00

Fix mouse position when changing zoom and fullscreen

This commit is contained in:
Mathieu Schroeter 2018-06-25 22:17:39 +02:00
parent 3834861c1e
commit 375388b22f
5 changed files with 44 additions and 27 deletions

View File

@ -410,7 +410,10 @@ HandleEvent (const SDL_Event & event)
case EV_WARPMOUSE: case EV_WARPMOUSE:
{ {
const SDL_Point * coord = static_cast<SDL_Point *> (event.user.data1); const SDL_Point * coord = static_cast<SDL_Point *> (event.user.data1);
SDL_WarpMouseInWindow (g_window, coord->x, coord->y);
Sint32 x = coord->x, y = coord->y;
g_pPixmap->FromGameToDisplay (x, y);
SDL_WarpMouseInWindow (g_window, x, y);
delete coord; delete coord;
break; break;
} }

View File

@ -1723,6 +1723,10 @@ CEvent::GetMousePos ()
void void
CEvent::SetFullScreen (bool bFullScreen) CEvent::SetFullScreen (bool bFullScreen)
{ {
int x, y;
SDL_GetMouseState (&x, &y);
this->m_pPixmap->FromDisplayToGame (x, y);
g_bFullScreen = bFullScreen; g_bFullScreen = bFullScreen;
int displayIndex = 0; int displayIndex = 0;
@ -1762,12 +1766,15 @@ CEvent::SetFullScreen (bool bFullScreen)
m_pPixmap->LoadCursors (); m_pPixmap->LoadCursors ();
if (g_bFullScreen) /* Force this update before otherwise the coordinates retrieved with
{ * the Warp SDL function are corresponding to the previous size.
Sint32 w, h; */
SDL_GetWindowSize (g_window, &w, &h); CEvent::PushUserEvent (EV_UPDATE);
SDL_WarpMouseGlobal (w / 2, h / 2);
} auto coord = new SDL_Point; // Released by the event handler.
coord->x = x;
coord->y = y;
CEvent::PushUserEvent (EV_WARPMOUSE, coord);
} }
/** /**
@ -1803,10 +1810,11 @@ CEvent::SetWindowSize (Uint8 newScale)
* \param[in] newScale - The new scale. * \param[in] newScale - The new scale.
*/ */
void void
CEvent::SetWindowSize (Uint8 prevScale, Uint8 newScale) CEvent::SetWindowSize (double prevScale, double newScale)
{ {
int x, y; int x, y;
SDL_GetMouseState (&x, &y); SDL_GetMouseState (&x, &y);
this->m_pPixmap->FromDisplayToGame (x, y, prevScale);
if (g_bFullScreen && newScale == 2) if (g_bFullScreen && newScale == 2)
newScale = 1; newScale = 1;
@ -1829,8 +1837,8 @@ CEvent::SetWindowSize (Uint8 prevScale, Uint8 newScale)
CEvent::PushUserEvent (EV_UPDATE); CEvent::PushUserEvent (EV_UPDATE);
auto coord = new SDL_Point; // Released by the event handler. auto coord = new SDL_Point; // Released by the event handler.
coord->x = newScale < prevScale ? x / prevScale : x * newScale; coord->x = x;
coord->y = newScale < prevScale ? y / prevScale : x * newScale; coord->y = y;
CEvent::PushUserEvent (EV_WARPMOUSE, coord); CEvent::PushUserEvent (EV_WARPMOUSE, coord);
} }
@ -4245,16 +4253,21 @@ CEvent::ChangeButtons (Sint32 message)
break; break;
} }
case EV_BUTTON4: case EV_BUTTON4:
{
Sint32 w1;
SDL_GetWindowSize (g_window, &w1, nullptr);
SetFullScreen (false); SetFullScreen (false);
SetWindowSize (g_zoom, g_zoom); SetWindowSize (g_zoom * static_cast<double> (w1) / LXIMAGE, g_zoom);
break; break;
}
case EV_BUTTON5: case EV_BUTTON5:
{ {
auto scale = g_zoom; auto scale = g_zoom;
if (g_zoom > 1) if (g_zoom > 1)
--g_zoom; --g_zoom;
SetWindowSize (scale, g_zoom); SetWindowSize (scale, g_zoom);
SetFullScreen (g_bFullScreen); if (g_bFullScreen)
SetFullScreen (g_bFullScreen);
break; break;
} }
case EV_BUTTON6: case EV_BUTTON6:
@ -4263,7 +4276,8 @@ CEvent::ChangeButtons (Sint32 message)
if (g_zoom < 2) if (g_zoom < 2)
++g_zoom; ++g_zoom;
SetWindowSize (scale, g_zoom); SetWindowSize (scale, g_zoom);
SetFullScreen (g_bFullScreen); if (g_bFullScreen)
SetFullScreen (g_bFullScreen);
break; break;
} }
case EV_BUTTON7: case EV_BUTTON7:

View File

@ -167,7 +167,7 @@ protected:
Language GetStartLanguage (); Language GetStartLanguage ();
Language GetLanguage (); Language GetLanguage ();
void SetLanguage (Language lang = Language::undef); void SetLanguage (Language lang = Language::undef);
void SetWindowSize (Uint8 prevScale, Uint8 newScale); void SetWindowSize (double prevScale, double newScale);
void ChangeButtons (Sint32 message); void ChangeButtons (Sint32 message);

View File

@ -979,24 +979,24 @@ CPixmap::GetDisplayScale ()
} }
void void
CPixmap::FromDisplayToGame (Sint32 & x, Sint32 & y) CPixmap::FromDisplayToGame (Sint32 & x, Sint32 & y, double prevScale)
{ {
if (this->event->IsDemoPlaying ()) if (this->event->IsDemoPlaying ())
return; return;
SDL_DisplayMode displayMode; Sint32 w, h;
SDL_GetWindowDisplayMode (g_window, &displayMode); SDL_GetWindowSize (g_window, &w, &h);
if ( double factor = 1;
static_cast<double> (displayMode.w) / displayMode.h ==
static_cast<double> (SCRNUM) / SCRDEN) if (!g_bFullScreen)
factor = prevScale;
x /= factor;
y /= factor;
if (!g_bFullScreen)
return; return;
double w = displayMode.w, h = displayMode.h;
double ratio = w * SCRDEN / SCRNUM;
x = (x - (w - ratio) / 2) / (ratio / LXIMAGE);
y = y / (h / LYIMAGE);
} }
void void

View File

@ -95,7 +95,7 @@ public:
public: public:
double GetDisplayScale (); double GetDisplayScale ();
void FromDisplayToGame (Sint32 & x, Sint32 & y); void FromDisplayToGame (Sint32 & x, Sint32 & y, double prevScale = 1);
void FromGameToDisplay (Sint32 & x, Sint32 & y); void FromGameToDisplay (Sint32 & x, Sint32 & y);
protected: protected: