Fix mouse relative calculation to ensure pointer goes from minX to maxX (and Y) included.

Signed-off-by: Andrea Odetti <mariofutire@gmail.com>
This commit is contained in:
Andrea Odetti 2021-06-05 14:25:32 +01:00
parent db71bb1a4c
commit d68b503d0d
5 changed files with 23 additions and 9 deletions

View file

@ -152,8 +152,11 @@ namespace sa2
int width, height; int width, height;
SDL_GetWindowSize(myWindow.get(), &width, &height); SDL_GetWindowSize(myWindow.get(), &width, &height);
x = double(motion.x) / double(width); const int posY = std::max(motion.y - myDeadTopZone, 0);
y = double(std::max(motion.y, myDeadTopZone) - myDeadTopZone) / double(std::max(height - myDeadTopZone, 1)); height = std::max(height - myDeadTopZone, 1); // a real window has a minimum size of 1
x = GetRelativePosition(motion.x, width);
y = GetRelativePosition(posY, height);
} }
void SDLImGuiFrame::RenderPresent() void SDLImGuiFrame::RenderPresent()

View file

@ -39,7 +39,7 @@ namespace sa2
size_t myBorderlessWidth; size_t myBorderlessWidth;
size_t myBorderlessHeight; size_t myBorderlessHeight;
Sint32 myDeadTopZone; // for mouse position int myDeadTopZone; // for mouse position
SDL_GLContext myGLContext; SDL_GLContext myGLContext;
ImTextureID myTexture; ImTextureID myTexture;

View file

@ -65,8 +65,8 @@ namespace sa2
int width, height; int width, height;
SDL_GetWindowSize(myWindow.get(), &width, &height); SDL_GetWindowSize(myWindow.get(), &width, &height);
x = double(motion.x) / double(width); x = GetRelativePosition(motion.x, width);
y = double(motion.y) / double(height); y = GetRelativePosition(motion.y, height);
} }

View file

@ -296,6 +296,13 @@ namespace sa2
} }
} }
double SDLFrame::GetRelativePosition(const int value, const int size)
{
// the minimum size of a window is 1
const double result = double(value) / double(std::max(1, size - 1));
return result;
}
void SDLFrame::ProcessMouseMotion(const SDL_MouseMotionEvent & motion) void SDLFrame::ProcessMouseMotion(const SDL_MouseMotionEvent & motion)
{ {
CardManager & cardManager = GetCardMgr(); CardManager & cardManager = GetCardMgr();
@ -339,14 +346,16 @@ namespace sa2
} }
else else
{ {
const int sizeX = iMaxX - iMinX + 1; const int sizeX = iMaxX - iMinX;
const int sizeY = iMaxY - iMinY + 1; const int sizeY = iMaxY - iMinY;
double x, y; double x, y;
GetRelativeMousePosition(motion, x, y); GetRelativeMousePosition(motion, x, y);
const int newX = lround(x * sizeX + iMinX); const int newX = lround(x * sizeX) + iMinX;
const int newY = lround(y * sizeY + iMinY); const int newY = lround(y * sizeY) + iMinY;
dx = newX - iX; dx = newX - iX;
dy = newY - iY; dy = newY - iY;
} }

View file

@ -56,6 +56,8 @@ namespace sa2
void ExecuteInRunningMode(const size_t msNextFrame); void ExecuteInRunningMode(const size_t msNextFrame);
void ExecuteInDebugMode(const size_t msNextFrame); void ExecuteInDebugMode(const size_t msNextFrame);
static double GetRelativePosition(const int value, const int width);
std::shared_ptr<SDL_Window> myWindow; std::shared_ptr<SDL_Window> myWindow;
bool myForceCapsLock; bool myForceCapsLock;