Mercurial > projects > doodle
view doodle/dia/grid_layer.d @ 41:f2e4e1d29b98
Bah
author | daveb |
---|---|
date | Tue, 01 Jun 2010 17:21:01 +0930 |
parents | 452915ecd1f4 |
children | 1b4c9ba58673 |
line wrap: on
line source
module doodle.dia.grid_layer; public { import doodle.dia.icanvas; } private { import doodle.cairo.routines; import std.math; import std.stdio; } 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); mZoomValid = false; } // Layer overrides: override Rectangle bounds() const { // We don't require any geometry return Rectangle(); } override void draw(in Viewport viewport, in Rectangle pixel_damage, scope Context pixel_cr, in Rectangle model_damage, scope Context model_cr) const { assert(mZoomValid); double xx = 1.0, yy = 1.0; model_cr.userToDeviceDistance(xx, yy); model_cr.save(); { model_cr.setSourceRgba(0.0, 0.0, 0.0, 0.3); model_cr.setLineWidth(0.5); { // vertical grid lines double x = start(model_damage.min_corner.x, mSpacing); for (;;) { vline(model_cr, x, model_damage.min_corner.y, model_damage.max_corner.y); // Ensure 1 pixel wide FIXME is this naughty? We are sneaking // through cairo to mix model and pixel coordinates... model_cr.save(); { model_cr.scale(1.0 / xx, 1.0 / yy); model_cr.stroke(); } model_cr.restore(); if (x > model_damage.max_corner.x) { break; } x += mSpacing; } } { // horizontal grid lines double y = start(model_damage.min_corner.y, mSpacing); for (;;) { hline(model_cr, y, model_damage.min_corner.x, model_damage.max_corner.x); // FIXME? model_cr.save(); { model_cr.scale(1.0 / xx, 1.0 / yy); model_cr.stroke(); } model_cr.restore(); if (y > model_damage.max_corner.y) { break; } y += mSpacing; } } } model_cr.restore(); } // Grid overrides: override void zoom_changed(double zoom) { mZoom = zoom; mZoomValid = true; // FIXME compute spacing properly mSpacing = 20.0 / mZoom; // mm } // FIXME use inout parameter? override 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 } }