Skip to content

Commit

Permalink
#5753: Merge pull request #21 from duzenko/5753_xy_zoom_cursor
Browse files Browse the repository at this point in the history
XY View: use the mouse cursor position as base for zoom in/out instea…
  • Loading branch information
codereader committed Sep 17, 2021
2 parents 8e1dafb + 606b6f5 commit 7bbd03b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
45 changes: 26 additions & 19 deletions radiant/xyview/XYWnd.cpp
Expand Up @@ -1546,32 +1546,36 @@ void XYWnd::mouseToPoint(int x, int y, Vector3& point)
// NOTE: the zoom out factor is 4/5, we could think about customizing it
// we don't go below a zoom factor corresponding to 10% of the max world size
// (this has to be computed against the window size)
float XYWnd::getZoomedScale( int steps ) {
const float min_scale = std::min( getWidth(), getHeight() ) / ( 1.1f * ( _maxWorldCoord - _minWorldCoord ) );
const float max_scale = GlobalXYWnd().maxZoomFactor();
const float fZoom = pow( 5.0f / 4.0f, steps );
const float scale = getScale() * fZoom;
if ( scale > max_scale ) {
return max_scale;
}
if ( scale < min_scale ) {
return min_scale;
}
return scale;
}

void XYWnd::zoomOut()
{
float min_scale = std::min(getWidth(),getHeight()) / ( 1.1f * (_maxWorldCoord - _minWorldCoord));
float scale = getScale() * 4.0f / 5.0f;
if (scale < min_scale) {
if (getScale() != min_scale) {
setScale(min_scale);
}
} else {
setScale(scale);
}
setScale( getZoomedScale( -1 ) );
}

void XYWnd::zoomIn()
{
float max_scale = GlobalXYWnd().maxZoomFactor();
float scale = getScale() * 5.0f / 4.0f;
setScale( getZoomedScale( 1 ) );
}

if (scale > max_scale) {
if (getScale() != max_scale) {
setScale(max_scale);
}
}
else {
setScale(scale);
}
void XYWnd::zoomInOn( wxPoint cursor, int zoom ) {
const float oldScale = _scale;
const float newScale = getZoomedScale( zoom );
scroll( cursor.x - _width/2, _height / 2 - cursor.y );
setScale( newScale );
scroll( ( _width / 2 - cursor.x ) * oldScale / newScale, ( cursor.y - _height / 2 ) * oldScale / newScale );
}

// ================ CALLBACKS ======================================
Expand Down Expand Up @@ -1621,6 +1625,9 @@ bool XYWnd::onRender()

void XYWnd::onGLWindowScroll(wxMouseEvent& ev)
{
if ( !ev.ShiftDown() ) {
zoomInOn( ev.GetPosition(), ev.GetWheelRotation() / ev.GetWheelDelta() );
}
if (ev.GetWheelRotation() > 0)
{
zoomIn();
Expand Down
2 changes: 2 additions & 0 deletions radiant/xyview/XYWnd.h
Expand Up @@ -133,8 +133,10 @@ class XYWnd :

void mouseToPoint(int x, int y, Vector3& point);

float getZoomedScale( int steps );
void zoomIn() override;
void zoomOut() override;
void zoomInOn( wxPoint cursor, int zoom );

void setActive(bool b);
bool isActive() const;
Expand Down

0 comments on commit 7bbd03b

Please sign in to comment.