Mercurial > projects > doodle
annotate doodle/dia/grid_layer.d @ 81:d92b9f04b1e8
Bed time
author | "David Bryant <bagnose@gmail.com>" |
---|---|
date | Mon, 16 Aug 2010 00:04:27 +0930 |
parents | b759414d2b72 |
children | cdd4fc728d94 |
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 { | |
8 import std.math; | |
9 } | |
11 | 10 |
35 | 11 private { |
12 double start(in double value, in double spacing) { | |
13 real r = floor(value / spacing); | |
14 return r * spacing; | |
15 } | |
11 | 16 } |
17 | |
57 | 18 class GridLayer : Layer, IGrid { |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
19 immutable double MIN_SPACING = 5.0; // pixels |
35 | 20 |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
21 this(in string name = "Grid") { |
24 | 22 super(name); |
48 | 23 _zoomValid = false; |
24 | 24 } |
25 | |
35 | 26 // Layer overrides: |
27 | |
11 | 28 override Rectangle bounds() const { |
29 // We don't require any geometry | |
30 return Rectangle(); | |
31 } | |
32 | |
80
b759414d2b72
Switched from cairo to Drawable abstraction
"David Bryant <bagnose@gmail.com>"
parents:
74
diff
changeset
|
33 override void draw(in Rectangle screenDamage, scope Drawable screenDrawable, |
b759414d2b72
Switched from cairo to Drawable abstraction
"David Bryant <bagnose@gmail.com>"
parents:
74
diff
changeset
|
34 in Rectangle modelDamage, scope Drawable modelDrawable) const { |
b759414d2b72
Switched from cairo to Drawable abstraction
"David Bryant <bagnose@gmail.com>"
parents:
74
diff
changeset
|
35 /+ |
48 | 36 assert(_zoomValid); |
35 | 37 |
36 | 38 double xx = 1.0, yy = 1.0; |
48 | 39 modelCr.userToDeviceDistance(xx, yy); |
36 | 40 |
48 | 41 modelCr.save(); { |
42 modelCr.setSourceRgba(0.0, 0.0, 0.0, 0.3); | |
43 modelCr.setLineWidth(0.5); | |
35 | 44 |
45 { | |
46 // vertical grid lines | |
70 | 47 double x = start(modelDamage.corner0.x, _spacing); |
35 | 48 |
49 for (;;) { | |
70 | 50 vline(modelCr, x, modelDamage.corner0.y, modelDamage.corner1.y); |
36 | 51 |
52 // Ensure 1 pixel wide FIXME is this naughty? We are sneaking | |
53 // through cairo to mix model and pixel coordinates... | |
48 | 54 modelCr.save(); { |
55 modelCr.scale(1.0 / xx, 1.0 / yy); | |
56 modelCr.stroke(); | |
57 } modelCr.restore(); | |
35 | 58 |
70 | 59 if (x > modelDamage.corner1.x) { |
35 | 60 break; |
61 } | |
62 | |
48 | 63 x += _spacing; |
35 | 64 } |
65 } | |
66 | |
67 { | |
68 // horizontal grid lines | |
70 | 69 double y = start(modelDamage.corner0.y, _spacing); |
35 | 70 |
71 for (;;) { | |
70 | 72 hline(modelCr, y, modelDamage.corner0.x, modelDamage.corner1.x); |
36 | 73 |
41 | 74 // FIXME? |
48 | 75 modelCr.save(); { |
76 modelCr.scale(1.0 / xx, 1.0 / yy); | |
77 modelCr.stroke(); | |
78 } modelCr.restore(); | |
35 | 79 |
70 | 80 if (y > modelDamage.corner1.y) { |
35 | 81 break; |
82 } | |
83 | |
48 | 84 y += _spacing; |
35 | 85 } |
86 } | |
48 | 87 } modelCr.restore(); |
80
b759414d2b72
Switched from cairo to Drawable abstraction
"David Bryant <bagnose@gmail.com>"
parents:
74
diff
changeset
|
88 +/ |
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 } |