zturtleman / mm3d

Maverick Model 3D is a 3D model editor and animator for games.
https://clover.moe/mm3d
GNU General Public License v2.0
114 stars 22 forks source link

Zoom relative to cursor (with mouse wheel) #142

Open m-7761 opened 4 years ago

m-7761 commented 4 years ago

Here's some example code from (https://github.com/mick-p1982/mm3d/commit/d97120b8c305e5fcc3a5c82fdc656f14ff233d9e) that I developed yesterday to change the mouse wheel zoom behavior to be relative to the cursor. My code is probably a little different but you can probably adapt it in the meantime.

void TextureWidget::wheelEvent(int wh, int bs, int x, int y)
{
    //if(m_interactive) if(wh>0) zoomIn(); else zoomOut();
    if(m_interactive) //Is navigation interaction?
    zoom(wh>0,getWindowXCoord(x-m_x),getWindowYCoord(y-m_y));
}
void ModelViewport::wheelEvent(int wh, int bs, int x, int y)
{
    //bool rotate = (e->modifiers()&Qt::ControlModifier)!=0;
    if(bs&Tool::BS_Ctrl&&~bs&Tool::BS_Alt)
    return wh>0?rotateClockwise():rotateCounterClockwise();

    double xDiff,yDiff;
    getRawParentXYValue(x-m_viewportX,y-m_viewportY,xDiff,yDiff);
    zoom(wh>0,xDiff+m_scroll[0],yDiff+m_scroll[1]);
}
void ScrollWidget::zoom(bool in, double x, double y)
{
    double z = m_zoom;  
    if(in) z*=texwidgit_zoom; else z/=texwidgit_zoom;       
    z = std::min(std::max(z,zoom_min),zoom_max);
    if(m_zoom!=z) 
    {
        double zDiff = z/m_zoom;

        x-=m_scroll[0]; m_scroll[0]-=x*zDiff-x; 
        y-=m_scroll[1]; m_scroll[1]-=y*zDiff-y;

        m_zoom = z; updateViewport('z');
    }
}