diff doodle/dia/grid_layer.d @ 35:3f6bb0bb22dc

Beginnings of grid
author David Bryant <bagnose@gmail.com>
date Sun, 30 Aug 2009 22:14:01 +0930
parents c2f11e1d7470
children 188397ef9a12
line wrap: on
line diff
--- a/doodle/dia/grid_layer.d	Sun Aug 30 15:32:12 2009 +0930
+++ b/doodle/dia/grid_layer.d	Sun Aug 30 22:14:01 2009 +0930
@@ -7,17 +7,25 @@
 private {
     import doodle.cairo.routines;
     import std.math;
+    import std.stdio;
 }
 
-interface Grid {
-    //void zoom_changed();
+private {
+    double start(in double value, in double spacing) {
+        real r = floor(value / spacing);
+        return r * spacing;
+    }
 }
 
 class GridLayer : Layer, Grid {
+    static const double MIN_SPACING = 5.0;      // pixels
+
     this(in string name) {
         super(name);
     }
 
+    // Layer overrides:
+
     override Rectangle bounds() const {
         // We don't require any geometry
         return Rectangle();
@@ -26,11 +34,69 @@
     override void draw(in Viewport viewport,
                        in Rectangle pixel_damage, scope Context pixel_cr,
                        in Rectangle model_damage, scope Context model_cr) const {
-        //double zoom = viewport.zoom;
+        assert(mZoomValid);
+
+        model_cr.save(); {
+            model_cr.setLineWidth(1.0);
+
+            {
+                // vertical grid lines
+                double x = start(model_damage.min_corner.x, mSpacing);
+                double y0 = model_damage.min_corner.y;
+                double y1 = model_damage.max_corner.y;
+
+                for (;;) {
+                    line(model_cr, x, y0, x, y1);
+                    model_cr.stroke();
+
+                    if (x > model_damage.max_corner.x) {
+                        break;
+                    }
+
+                    x += mSpacing;
+                }
+            }
+
+            {
+                // horizontal grid lines
+                double y = start(model_damage.min_corner.y, mSpacing);
+                double x0 = model_damage.min_corner.x;
+                double x1 = model_damage.max_corner.x;
+
+                for (;;) {
+                    line(model_cr, x0, y, x1, y);
+                    model_cr.stroke();
+
+                    if (y > model_damage.max_corner.y) {
+                        break;
+                    }
+
+                    y += mSpacing;
+                }
+            }
+        } model_cr.restore();
+
 
         //double start_x = modf(damage.min_corner.x, zoom);
     }
 
+    // Grid overrides:
+
+    void zoom_changed(double zoom) {
+        mZoom = zoom;
+        mZoomValid = true;
+        mSpacing = 20.0;        // mm
+    }
+
+    bool snap(in Point a, out Point b) const {
+        b = a;
+        return false;
+    }
+
     private {
+        bool mZoomValid;
+        double mZoom;           // pixels per millimetre
+
+        double mSpacing;        // model spacing
     }
 }