Skip to content

Commit

Permalink
Zoom via mouse wheel: keep the position under the cursor in place (#3998
Browse files Browse the repository at this point in the history
)

* Zoom centered on the mouse position

Zooming via the scroll wheel now keeps the position under the curser in
place.

* QT <5.14 compatibility

* Use an exponential factor when zooming

This makes the zoom experience a lot smoother: the step size no longer
depends on how close you're zoomed in.
  • Loading branch information
smurfix committed Jul 31, 2020
1 parent 3cf49d2 commit 654aeae
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
47 changes: 38 additions & 9 deletions src/T2DMap.cpp
Expand Up @@ -1242,8 +1242,8 @@ void T2DMap::paintEvent(QPaintEvent* e)
return;
}

int ox;
int oy;
qreal ox;
qreal oy;
if (mRoomID != playerRoomId && mShiftMode) {
mShiftMode = false;
}
Expand Down Expand Up @@ -4608,16 +4608,45 @@ void T2DMap::wheelEvent(QWheelEvent* e)

// int delta = e->delta() / 8 / 15; // Deprecated in Qt 5.x ...!
int delta = e->angleDelta().y() / (8 * 15);
if (e->modifiers() & Qt::ControlModifier) { // Increase rate 10-fold if control key down - it makes scrolling through
if (e->modifiers() & Qt::ControlModifier) { // Increase rate if control key down - it makes scrolling through
// a large number of items in a listwidget's contents easier AND this make it
// easier to zoom in and out on LARGE area maps
delta *= 10;
delta *= 5;
}
if (delta != 0) {
mPick = false;
int oldZoom = xyzoom;
xyzoom = qMax(3, xyzoom + delta);
qreal oldZoom = xyzoom;
xyzoom = qMax(3.0, xyzoom * pow(1.07, delta));

if (oldZoom != xyzoom) {
const float widgetWidth = width();
const float widgetHeight = height();
float xs = 1.0;
float ys = 1.0;
if (widgetWidth > 10 && widgetHeight > 10) {
if (widgetWidth > widgetHeight)
xs = (widgetWidth / widgetHeight);
else
ys = (widgetHeight / widgetWidth);
}

// mouse pos within the widget
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
const QPointF pos = e->position();
#else
const QPoint pos = mapFromGlobal(e->globalPos());
#endif

// Position of the mouse within the map, scaled -1 .. +1
// i.e. if the mouse is in the center, nothing changes
const float dx = 2.0 * pos.x() / widgetWidth - 1.0;
const float dy = 2.0 * pos.y() / widgetHeight - 1.0;

// now shift the origin by that, scaled by the difference in
// zoom factors. Thus the point under the mouse stays in place.
mOx += dx * (oldZoom - xyzoom) / 2.0 * xs;
mOy += dy * (oldZoom - xyzoom) / 2.0 * ys;

flushSymbolPixmapCache();
update();
}
Expand All @@ -4628,10 +4657,10 @@ void T2DMap::wheelEvent(QWheelEvent* e)
return;
}

void T2DMap::setMapZoom(int zoom)
void T2DMap::setMapZoom(qreal zoom)
{
int oldZoom = xyzoom;
xyzoom = qMax(3, zoom);
qreal oldZoom = xyzoom;
xyzoom = qMax(3.0, zoom);
if (oldZoom != xyzoom) {
flushSymbolPixmapCache();
update();
Expand Down
8 changes: 4 additions & 4 deletions src/T2DMap.h
Expand Up @@ -56,7 +56,7 @@ class T2DMap : public QWidget
Q_DISABLE_COPY(T2DMap)
explicit T2DMap(QWidget* parent = nullptr);
void paintMap();
void setMapZoom(int zoom);
void setMapZoom(qreal zoom);
QColor getColor(int id);
void init();
void exportAreaImage(int);
Expand Down Expand Up @@ -86,7 +86,7 @@ class T2DMap : public QWidget

TMap* mpMap;
QPointer<Host> mpHost;
int xyzoom;
qreal xyzoom;
int mRX;
int mRY;
QPoint mPHighlight;
Expand Down Expand Up @@ -130,8 +130,8 @@ class T2DMap : public QWidget
double eSize;
int mRoomID;
int mAreaID;
int mOx;
int mOy;
qreal mOx;
qreal mOy;
int mOz;
bool mShiftMode;
bool mShowInfo;
Expand Down
4 changes: 2 additions & 2 deletions src/TLuaInterpreter.cpp
Expand Up @@ -9845,13 +9845,13 @@ int TLuaInterpreter::createMapLabel(lua_State* L)
// Documentation: https://wiki.mudlet.org/w/Manual:Lua_Functions#setMapZoom
int TLuaInterpreter::setMapZoom(lua_State* L)
{
int zoom = 3;
qreal zoom = 3.0;
if (!lua_isnumber(L, 1)) {
lua_pushstring(L, "setMapZoom: wrong argument type");
lua_error(L);
return 1;
} else {
zoom = lua_tointeger(L, 1);
zoom = lua_tonumber(L, 1);
}
Host& host = getHostFromLua(L);
if (host.mpMap) {
Expand Down

0 comments on commit 654aeae

Please sign in to comment.