annotate doodle/dia/grid_layer.d @ 92:a98116479793

Removed GL support. Improved grid.
author daveb
date Fri, 20 Aug 2010 18:34:32 +0930
parents 42766e14534e
children bc5baa585b32
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
a24c13bb9c98 Builds again.
"David Bryant <bagnose@gmail.com>"
parents: 22
diff changeset
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
a24c13bb9c98 Builds again.
"David Bryant <bagnose@gmail.com>"
parents: 22
diff changeset
5 }
a24c13bb9c98 Builds again.
"David Bryant <bagnose@gmail.com>"
parents: 22
diff changeset
6
a24c13bb9c98 Builds again.
"David Bryant <bagnose@gmail.com>"
parents: 22
diff changeset
7 private {
a24c13bb9c98 Builds again.
"David Bryant <bagnose@gmail.com>"
parents: 22
diff changeset
8 import std.math;
89
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
9 import std.stdio;
92
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
10 import std.array;
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
11
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
12 import doodle.core.logging;
24
a24c13bb9c98 Builds again.
"David Bryant <bagnose@gmail.com>"
parents: 22
diff changeset
13 }
11
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
14
92
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
15 // Draw a grid.
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
16 // zoom -> pixels / millimetre
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
17 // Grid lines must have a maximum density
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
18 //
11
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
19
57
9960c4fbd0dd I is for Interface
"David Bryant <bagnose@gmail.com>"
parents: 48
diff changeset
20 class GridLayer : Layer, IGrid {
58
c63719604adb Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents: 57
diff changeset
21 this(in string name = "Grid") {
24
a24c13bb9c98 Builds again.
"David Bryant <bagnose@gmail.com>"
parents: 22
diff changeset
22 super(name);
92
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
23 _spacingValid = false;
24
a24c13bb9c98 Builds again.
"David Bryant <bagnose@gmail.com>"
parents: 22
diff changeset
24 }
a24c13bb9c98 Builds again.
"David Bryant <bagnose@gmail.com>"
parents: 22
diff changeset
25
35
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
26 // Layer overrides:
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
27
11
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
28 override Rectangle bounds() const {
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
29 // We don't require any geometry
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
30 return Rectangle();
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
31 }
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
32
84
cdd4fc728d94 Renamed Drawable to Renderer
daveb
parents: 81
diff changeset
33 override void draw(in Rectangle screenDamage, scope Renderer screenRenderer,
89
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
34 in Rectangle modelDamage, scope Renderer modelRenderer,
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
35 in ScreenModel screenModel) const {
92
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
36 assert(_spacingValid);
35
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
37
92
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
38 const z = screenModel.zoom;
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
39 const lineWidthModel = LINE_WIDTH_SCREEN / z;
89
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
40
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
41 modelRenderer.pushState; {
92
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
42 modelRenderer.setColor(doodle.tk.color.Color(0.0, 0.0, 0.7, 1.0));
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
43 modelRenderer.setLineWidth(lineWidthModel);
90
885914257e0e Ok, the cairo resource leak (and cost of reqaquisition) in GtkD is the cause
David Bryant <bagnose@gmail.com>
parents: 89
diff changeset
44
92
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
45 auto x = roundDownSpacing(modelDamage.corner0.x);
36
188397ef9a12 Late night tinkering
David Bryant <bagnose@gmail.com>
parents: 35
diff changeset
46
89
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
47 for (;;) {
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
48 modelRenderer.drawVLine(x, modelDamage.corner0.y, modelDamage.corner1.y);
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
49 x += _spacing;
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
50 if (x > modelDamage.corner1.x) break;
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
51 }
90
885914257e0e Ok, the cairo resource leak (and cost of reqaquisition) in GtkD is the cause
David Bryant <bagnose@gmail.com>
parents: 89
diff changeset
52
92
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
53 auto y = roundDownSpacing(modelDamage.corner0.y);
90
885914257e0e Ok, the cairo resource leak (and cost of reqaquisition) in GtkD is the cause
David Bryant <bagnose@gmail.com>
parents: 89
diff changeset
54
885914257e0e Ok, the cairo resource leak (and cost of reqaquisition) in GtkD is the cause
David Bryant <bagnose@gmail.com>
parents: 89
diff changeset
55 for (;;) {
885914257e0e Ok, the cairo resource leak (and cost of reqaquisition) in GtkD is the cause
David Bryant <bagnose@gmail.com>
parents: 89
diff changeset
56 modelRenderer.drawHLine(y, modelDamage.corner0.x, modelDamage.corner1.x);
885914257e0e Ok, the cairo resource leak (and cost of reqaquisition) in GtkD is the cause
David Bryant <bagnose@gmail.com>
parents: 89
diff changeset
57 y += _spacing;
885914257e0e Ok, the cairo resource leak (and cost of reqaquisition) in GtkD is the cause
David Bryant <bagnose@gmail.com>
parents: 89
diff changeset
58 if (y > modelDamage.corner1.y) break;
885914257e0e Ok, the cairo resource leak (and cost of reqaquisition) in GtkD is the cause
David Bryant <bagnose@gmail.com>
parents: 89
diff changeset
59 }
89
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
60 } modelRenderer.popState;
11
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
61 }
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
62
57
9960c4fbd0dd I is for Interface
"David Bryant <bagnose@gmail.com>"
parents: 48
diff changeset
63 // IGrid overrides:
35
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
64
92
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
65 override void zoomChanged(in double zoom) {
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
66 foreach (s; SPACINGS) {
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
67 _spacing = s;
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
68 double pixels = zoom * _spacing;
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
69 if (pixels > MIN_SPACING) { break; }
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
70 }
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
71 _spacingValid = true;
35
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
72 }
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
73
11
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
74 private {
92
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
75 double roundDownSpacing(in double value) const {
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
76 return _spacing * floor(value / _spacing);
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
77 }
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
78
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
79 bool _spacingValid;
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
80 double _spacing; // model spacing
35
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
81
92
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
82 immutable double LINE_WIDTH_SCREEN = 0.25;
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
83 immutable double MIN_SPACING = 40.0; // pixels
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
84 immutable double[] SPACINGS = // millimetres
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
85 [
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
86 5.0,
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
87 10.0,
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
88 20.0,
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
89 50.0
a98116479793 Removed GL support. Improved grid.
daveb
parents: 91
diff changeset
90 ];
11
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
91 }
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
92 }