refactor: use InputManager for screens, DPI-aware InputManager

This commit is contained in:
Tropical 2026-03-09 23:41:56 -05:00
parent f0aa04a7ee
commit c4ce9b5377
3 changed files with 40 additions and 41 deletions

View file

@ -61,13 +61,21 @@ static GLFWwindow *getWindow() {
// GLFW callbacks // GLFW callbacks
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
static void onCursorPos(GLFWwindow * /*w*/, double x, double y) { static void onCursorPos(GLFWwindow* w, double x, double y) {
float scaleX = 1;
float scaleY = 1;
glfwGetWindowContentScale(w, &scaleX, &scaleY);
x *= scaleX;
y *= scaleX;
if (s_cursorInitialized) { if (s_cursorInitialized) {
s_mouseAccumX += (float)(x - s_lastCursorX); s_mouseAccumX += (float)(x - s_lastCursorX);
s_mouseAccumY += (float)(y - s_lastCursorY); s_mouseAccumY += (float)(y - s_lastCursorY);
} else { } else {
s_cursorInitialized = true; s_cursorInitialized = true;
} }
s_lastCursorX = x; s_lastCursorX = x;
s_lastCursorY = y; s_lastCursorY = y;
} }
@ -208,24 +216,25 @@ void C_4JInput::Tick(void) {
s_mouseAccumX = s_mouseAccumY = 0.0f; s_mouseAccumX = s_mouseAccumY = 0.0f;
// 6. Mouse buttons (only meaningful when locked in-game) // 6. Mouse buttons (only meaningful when locked in-game)
if (s_mouseLocked) { s_mouseLeftCurrent = (glfwGetMouseButton(w, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS);
s_mouseLeftCurrent = (glfwGetMouseButton(w, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS); s_mouseRightCurrent = (glfwGetMouseButton(w, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS);
s_mouseRightCurrent = (glfwGetMouseButton(w, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS); // if (s_mouseLocked) {
} else { // printf("locked\n");
// Not locked. Allow a left-click to re-lock (if not in a menu) // } else {
bool lclick = (glfwGetMouseButton(w, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS); // // Not locked. Allow a left-click to re-lock (if not in a menu)
if (!menuNow && lclick) { // bool lclick = (glfwGetMouseButton(w, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS);
s_mouseLocked = true; // if (!menuNow && lclick) {
glfwSetInputMode(w, GLFW_CURSOR, GLFW_CURSOR_DISABLED); // s_mouseLocked = true;
if (glfwRawMouseMotionSupported()) // glfwSetInputMode(w, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetInputMode(w, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE); // if (glfwRawMouseMotionSupported())
s_mouseAccumX = s_mouseAccumY = 0.0f; // glfwSetInputMode(w, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
s_cursorInitialized = false; // s_mouseAccumX = s_mouseAccumY = 0.0f;
} // s_cursorInitialized = false;
s_mouseLeftCurrent = false; // }
s_mouseRightCurrent = false; // s_mouseLeftCurrent = false;
s_frameRelX = s_frameRelY = 0.0f; // s_mouseRightCurrent = false;
} // s_frameRelX = s_frameRelY = 0.0f;
// }
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------

View file

@ -44,7 +44,6 @@ void Button::render(Minecraft *minecraft, int xm, int ym)
glBindTexture(GL_TEXTURE_2D, minecraft->textures->loadTexture(TN_GUI_GUI)); // 4J was L"/gui/gui.png" glBindTexture(GL_TEXTURE_2D, minecraft->textures->loadTexture(TN_GUI_GUI)); // 4J was L"/gui/gui.png"
glColor4f(1, 1, 1, 1); glColor4f(1, 1, 1, 1);
bool hovered = xm >= x && ym >= y && xm < x + w && ym < y + h; bool hovered = xm >= x && ym >= y && xm < x + w && ym < y + h;
int yImage = getYImage(hovered); int yImage = getYImage(hovered);

View file

@ -106,31 +106,22 @@ void Screen::updateEvents()
{ {
// TODO: update for SDL if we ever get around to that // TODO: update for SDL if we ever get around to that
#if (defined (ENABLE_JAVA_GUIS)) #if (defined (ENABLE_JAVA_GUIS))
int fbw, fbh; int fbw, fbh;
RenderManager.GetFramebufferSize(fbw, fbh); RenderManager.GetFramebufferSize(fbw, fbh);
glViewport(0, 0, fbw, fbh); glViewport(0, 0, fbw, fbh);
ScreenSizeCalculator ssc(minecraft->options, minecraft->width, minecraft->height); ScreenSizeCalculator ssc(minecraft->options, minecraft->width, minecraft->height);
int screenWidth = ssc.getWidth(); int screenWidth = ssc.getWidth();
int screenHeight = ssc.getHeight(); int screenHeight = ssc.getHeight();
int xMouse = InputManager.GetMouseX() * screenWidth / fbw;
GLFWwindow* window = glfwGetCurrentContext(); int yMouse = InputManager.GetMouseY() * screenHeight / fbh - 1;
if (!window) return;
float windowScaleX = 1;
float windowScaleY = 1;
glfwGetWindowContentScale(window, &windowScaleX, &windowScaleY);
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
int xMouse = (int)(xpos * windowScaleX) * screenWidth / fbw;
int yMouse = (int)(ypos * windowScaleY) * screenHeight / fbh - 1;
static bool prevLeftState = false; static bool prevLeftState = false;
static bool prevRightState = false; static bool prevRightState = false;
bool leftState = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS; bool leftState = InputManager.ButtonDown(0, MINECRAFT_ACTION_ACTION);
bool rightState = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS; bool rightState = InputManager.ButtonDown(0, MINECRAFT_ACTION_USE);
printf("%d, %d\n", leftState, rightState);
if (leftState && !prevLeftState) { if (leftState && !prevLeftState) {
mouseClicked(xMouse, yMouse, 0); mouseClicked(xMouse, yMouse, 0);