Zoom in and out with the mouse wheel.
authorDavid Gow <[email protected]>
Wed, 9 Apr 2014 08:01:54 +0000 (16:01 +0800)
committerDavid Gow <[email protected]>
Wed, 9 Apr 2014 08:01:54 +0000 (16:01 +0800)
Note that zoom-out is totally broken with half-precision,
so single-precision is the default. You can still see some artefacts
in single precision mode if you zoom in enough.

Zoom into the corner of a rectangle and then click on the corner an try to drag it.
Eventually you'll notice the reduced precision, and the rectangle will jump around.

src/main.h
src/real.h
src/screen.cpp
src/screen.h
src/view.cpp
src/view.h

index c7d5eca..2346119 100644 (file)
@@ -35,6 +35,11 @@ inline void MainLoop(Document & doc)
                }
                oldx = x;
                oldy = y;
+               
+               if (wheel)
+               {
+                       view.ScaleAroundPoint(Real(x)/Real(scr.ViewportWidth()),Real(y)/Real(scr.ViewportHeight()), expf(-wheel/20.f));
+               }
        }
        );
        while (scr.PumpEvents())
index a7f56be..5703671 100644 (file)
@@ -6,9 +6,9 @@
 namespace IPDF
 {
 
-//#define REAL_FLOAT
+#define REAL_SINGLE
 //#define REAL_DOUBLE
-#define REAL_HALF
+//#define REAL_HALF
 
 #ifdef REAL_SINGLE
        typedef float Real;
index d924c06..9ee15b4 100644 (file)
@@ -58,6 +58,8 @@ bool Screen::PumpEvents()
                        }
                        break;
                case SDL_MOUSEMOTION:
+                       m_last_mouse_x = evt.motion.x;
+                       m_last_mouse_y = evt.motion.y;
                        if (m_mouse_handler)
                        {
                                m_mouse_handler(evt.motion.x, evt.motion.y,evt.motion.state, 0);
@@ -65,11 +67,19 @@ bool Screen::PumpEvents()
                        break;
                case SDL_MOUSEBUTTONDOWN:
                case SDL_MOUSEBUTTONUP:
+                       m_last_mouse_x = evt.button.x;
+                       m_last_mouse_y = evt.button.y;
                        if (m_mouse_handler)
                        {
                                m_mouse_handler(evt.button.x, evt.button.y, evt.button.state, 0);
                        }
                        break;
+               case SDL_MOUSEWHEEL:
+                       if (m_mouse_handler)
+                       {
+                               m_mouse_handler(m_last_mouse_x, m_last_mouse_y, 0, evt.wheel.y);
+                       }
+                       break;
                default:
                        break;
                }
index 8452efc..4d92cb3 100644 (file)
@@ -47,6 +47,8 @@ namespace IPDF
                void ResizeViewport(int width, int height);
                
                MouseHandler m_mouse_handler;
+               int m_last_mouse_x;
+               int m_last_mouse_y;
 
                int m_viewport_width;
                int m_viewport_height;
index f78ec40..80f4986 100644 (file)
@@ -7,10 +7,34 @@ using namespace std;
 
 void View::Translate(Real x, Real y)
 {
+       x *= m_bounds.w;
+       y *= m_bounds.h;
        m_bounds.x += x;
        m_bounds.y += y;
 }
 
+void View::ScaleAroundPoint(Real x, Real y, Real scaleAmt)
+{
+       // Convert to local coords.
+       x *= m_bounds.w;
+       y *= m_bounds.h;
+       x += m_bounds.x;
+       y += m_bounds.y;
+       
+       Debug("Mouse wheel event %f %f %f\n", Float(x), Float(y), Float(scaleAmt));
+       
+       Real top = y - m_bounds.y;
+       Real left = x - m_bounds.x;
+       
+       top *= scaleAmt;
+       left *= scaleAmt;
+       
+       m_bounds.x = x - left;
+       m_bounds.y = y - top;
+       m_bounds.w *= scaleAmt;
+       m_bounds.h *= scaleAmt;
+}
+
 void View::Render()
 {
        static bool debug_output_done = false;
index cc09a89..6b63799 100644 (file)
@@ -15,6 +15,7 @@ namespace IPDF
                        void Render();
                        
                        void Translate(Real x, Real y);
+                       void ScaleAroundPoint(Real x, Real y, Real scaleAmt);
                
                private:
                        Document & m_document;

UCC git Repository :: git.ucc.asn.au