Mercurial > projects > doodle
annotate doodle/dia/grid_layer.d @ 74:c03ed75c0f8e
Icky
author | "David Bryant <bagnose@gmail.com>" |
---|---|
date | Sun, 15 Aug 2010 01:11:58 +0930 |
parents | 6f2525e170f2 |
children | b759414d2b72 |
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 { | |
73 | 8 import doodle.tk.cairo; |
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 | |
57 | 20 class GridLayer : Layer, IGrid { |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
21 immutable double MIN_SPACING = 5.0; // pixels |
35 | 22 |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
23 this(in string name = "Grid") { |
24 | 24 super(name); |
48 | 25 _zoomValid = 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 | |
74 | 35 override void draw(in Rectangle pixelDamage, scope Context pixelCr, |
48 | 36 in Rectangle modelDamage, scope Context modelCr) const { |
37 assert(_zoomValid); | |
35 | 38 |
36 | 39 double xx = 1.0, yy = 1.0; |
48 | 40 modelCr.userToDeviceDistance(xx, yy); |
36 | 41 |
48 | 42 modelCr.save(); { |
43 modelCr.setSourceRgba(0.0, 0.0, 0.0, 0.3); | |
44 modelCr.setLineWidth(0.5); | |
35 | 45 |
46 { | |
47 // vertical grid lines | |
70 | 48 double x = start(modelDamage.corner0.x, _spacing); |
35 | 49 |
50 for (;;) { | |
70 | 51 vline(modelCr, x, modelDamage.corner0.y, modelDamage.corner1.y); |
36 | 52 |
53 // Ensure 1 pixel wide FIXME is this naughty? We are sneaking | |
54 // through cairo to mix model and pixel coordinates... | |
48 | 55 modelCr.save(); { |
56 modelCr.scale(1.0 / xx, 1.0 / yy); | |
57 modelCr.stroke(); | |
58 } modelCr.restore(); | |
35 | 59 |
70 | 60 if (x > modelDamage.corner1.x) { |
35 | 61 break; |
62 } | |
63 | |
48 | 64 x += _spacing; |
35 | 65 } |
66 } | |
67 | |
68 { | |
69 // horizontal grid lines | |
70 | 70 double y = start(modelDamage.corner0.y, _spacing); |
35 | 71 |
72 for (;;) { | |
70 | 73 hline(modelCr, y, modelDamage.corner0.x, modelDamage.corner1.x); |
36 | 74 |
41 | 75 // FIXME? |
48 | 76 modelCr.save(); { |
77 modelCr.scale(1.0 / xx, 1.0 / yy); | |
78 modelCr.stroke(); | |
79 } modelCr.restore(); | |
35 | 80 |
70 | 81 if (y > modelDamage.corner1.y) { |
35 | 82 break; |
83 } | |
84 | |
48 | 85 y += _spacing; |
35 | 86 } |
87 } | |
48 | 88 } modelCr.restore(); |
11 | 89 } |
90 | |
57 | 91 // IGrid overrides: |
35 | 92 |
48 | 93 override void zoomChanged(double zoom) { |
94 _zoom = zoom; | |
95 _zoomValid = true; | |
36 | 96 |
97 // FIXME compute spacing properly | |
48 | 98 _spacing = 20.0 / _zoom; // mm |
35 | 99 } |
100 | |
36 | 101 // FIXME use inout parameter? |
102 override bool snap(in Point a, out Point b) const { | |
35 | 103 b = a; |
104 return false; | |
105 } | |
106 | |
11 | 107 private { |
48 | 108 bool _zoomValid; |
109 double _zoom; // pixels per millimetre | |
35 | 110 |
48 | 111 double _spacing; // model spacing |
11 | 112 } |
113 } |