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