Mercurial > projects > doodle
annotate doodle/dia/grid_layer.d @ 36:188397ef9a12
Late night tinkering
author | David Bryant <bagnose@gmail.com> |
---|---|
date | Sun, 30 Aug 2009 23:36:31 +0930 |
parents | 3f6bb0bb22dc |
children | 452915ecd1f4 |
rev | line source |
---|---|
28
1754cb773d41
Part-way through getting to compile with configure/builder.
Graham St Jack <graham.stjack@internode.on.net>
parents:
27
diff
changeset
|
1 module doodle.dia.grid_layer; |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
12
diff
changeset
|
2 |
24 | 3 public { |
28
1754cb773d41
Part-way through getting to compile with configure/builder.
Graham St Jack <graham.stjack@internode.on.net>
parents:
27
diff
changeset
|
4 import doodle.dia.icanvas; |
24 | 5 } |
6 | |
7 private { | |
28
1754cb773d41
Part-way through getting to compile with configure/builder.
Graham St Jack <graham.stjack@internode.on.net>
parents:
27
diff
changeset
|
8 import doodle.cairo.routines; |
24 | 9 import std.math; |
35 | 10 import std.stdio; |
24 | 11 } |
11 | 12 |
35 | 13 private { |
14 double start(in double value, in double spacing) { | |
15 real r = floor(value / spacing); | |
16 return r * spacing; | |
17 } | |
11 | 18 } |
19 | |
20 class GridLayer : Layer, Grid { | |
35 | 21 static const double MIN_SPACING = 5.0; // pixels |
22 | |
24 | 23 this(in string name) { |
24 super(name); | |
36 | 25 mZoomValid = false; |
24 | 26 } |
27 | |
35 | 28 // Layer overrides: |
29 | |
11 | 30 override Rectangle bounds() const { |
31 // We don't require any geometry | |
32 return Rectangle(); | |
33 } | |
34 | |
26 | 35 override void draw(in Viewport viewport, |
24 | 36 in Rectangle pixel_damage, scope Context pixel_cr, |
37 in Rectangle model_damage, scope Context model_cr) const { | |
35 | 38 assert(mZoomValid); |
39 | |
36 | 40 double xx = 1.0, yy = 1.0; |
41 model_cr.userToDeviceDistance(xx, yy); | |
42 | |
35 | 43 model_cr.save(); { |
36 | 44 model_cr.setSourceRgba(0.0, 0.0, 0.0, 0.3); |
45 model_cr.setLineWidth(0.5); | |
35 | 46 |
47 { | |
48 // vertical grid lines | |
49 double x = start(model_damage.min_corner.x, mSpacing); | |
50 double y0 = model_damage.min_corner.y; | |
51 double y1 = model_damage.max_corner.y; | |
52 | |
53 for (;;) { | |
54 line(model_cr, x, y0, x, y1); | |
36 | 55 |
56 // Ensure 1 pixel wide FIXME is this naughty? We are sneaking | |
57 // through cairo to mix model and pixel coordinates... | |
58 model_cr.save(); { | |
59 model_cr.scale(1.0 / xx, 1.0 / yy); | |
60 model_cr.stroke(); | |
61 } model_cr.restore(); | |
35 | 62 |
63 if (x > model_damage.max_corner.x) { | |
64 break; | |
65 } | |
66 | |
67 x += mSpacing; | |
68 } | |
69 } | |
70 | |
71 { | |
72 // horizontal grid lines | |
73 double y = start(model_damage.min_corner.y, mSpacing); | |
74 double x0 = model_damage.min_corner.x; | |
75 double x1 = model_damage.max_corner.x; | |
76 | |
77 for (;;) { | |
78 line(model_cr, x0, y, x1, y); | |
36 | 79 |
80 model_cr.save(); { | |
81 model_cr.scale(1.0 / xx, 1.0 / yy); | |
82 model_cr.stroke(); | |
83 } model_cr.restore(); | |
35 | 84 |
85 if (y > model_damage.max_corner.y) { | |
86 break; | |
87 } | |
88 | |
89 y += mSpacing; | |
90 } | |
91 } | |
92 } model_cr.restore(); | |
11 | 93 } |
94 | |
35 | 95 // Grid overrides: |
96 | |
36 | 97 override void zoom_changed(double zoom) { |
35 | 98 mZoom = zoom; |
99 mZoomValid = true; | |
36 | 100 |
101 // FIXME compute spacing properly | |
102 mSpacing = 10.0 / mZoom; // mm | |
35 | 103 } |
104 | |
36 | 105 // FIXME use inout parameter? |
106 override bool snap(in Point a, out Point b) const { | |
35 | 107 b = a; |
108 return false; | |
109 } | |
110 | |
11 | 111 private { |
35 | 112 bool mZoomValid; |
113 double mZoom; // pixels per millimetre | |
114 | |
115 double mSpacing; // model spacing | |
11 | 116 } |
117 } |