feat: Implement pixel-perfect camera panning

Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
2026-02-17 11:59:24 -07:00
parent f3a1f73f45
commit ddf6f6fd85
3 changed files with 13 additions and 8 deletions

View File

@@ -21,7 +21,7 @@ void Camera::mousePressEvent(QMouseEvent* event)
m_lastPos = event->pos();
}
void Camera::mouseMoveEvent(QMouseEvent* event)
void Camera::mouseMoveEvent(QMouseEvent* event, int viewportHeight)
{
int dx = event->pos().x() - m_lastPos.x();
int dy = event->pos().y() - m_lastPos.y();
@@ -29,12 +29,17 @@ void Camera::mouseMoveEvent(QMouseEvent* event)
if (event->buttons() & Qt::MiddleButton) {
if (QApplication::keyboardModifiers() & Qt::ShiftModifier) {
// Pan
if (viewportHeight == 0) viewportHeight = 1;
// This logic is based on a 45-degree field of view.
float fov = 45.0f;
float dist = -m_zoom;
dist = qMin(dist, 200.0f); // Cap distance to avoid crazy fast pan.
// Match old speed at default zoom, and have a minimum speed.
float panFactor = dist * 0.000495f + 0.0001f;
setPanX(m_panX + dx * panFactor);
setPanY(m_panY - dy * panFactor);
float world_height = 2.0f * dist * tan(qDegreesToRadians(fov / 2.0f));
float pixels_to_world = world_height / viewportHeight;
setPanX(m_panX + dx * pixels_to_world);
setPanY(m_panY - dy * pixels_to_world);
} else {
// Rotate
setXRotation(m_xRot + 8 * dy);