annotate doodle/dia/standard_tools.d @ 48:1b4c9ba58673

Stylistic overhaul.
author daveb
date Tue, 03 Aug 2010 17:37:21 +0930
parents 1f97022e5c6d
children b190a9d9352e
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.standard_tools;
2
d6f44347373d * Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff changeset
2
16
9e63308b749c * Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents: 14
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.tool;
16
9e63308b749c * Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents: 14
diff changeset
5 }
9e63308b749c * Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents: 14
diff changeset
6
9e63308b749c * Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents: 14
diff changeset
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;
16
9e63308b749c * Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents: 14
diff changeset
9 import std.math;
17
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
10 import std.stdio;
16
9e63308b749c * Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents: 14
diff changeset
11 }
7
936feb16eed4 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 3
diff changeset
12
2
d6f44347373d * Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff changeset
13 final class PanTool : Tool {
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
14 override bool handleButtonPress(scope Viewport viewport, in ButtonEvent event) {
7
936feb16eed4 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 3
diff changeset
15 if (event.button_name == ButtonName.MIDDLE) {
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
16 mLastPosition = event.pixelPoint;
7
936feb16eed4 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 3
diff changeset
17 return true;
936feb16eed4 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 3
diff changeset
18 }
936feb16eed4 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 3
diff changeset
19 else {
936feb16eed4 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 3
diff changeset
20 return false;
936feb16eed4 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 3
diff changeset
21 }
936feb16eed4 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 3
diff changeset
22 }
936feb16eed4 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 3
diff changeset
23
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
24 override bool handleMotion(scope Viewport viewport, in MotionEvent event) {
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
25 if (event.mask.isSet(Modifier.MIDDLE_BUTTON)) {
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
26 viewport.panRelative(mLastPosition - event.pixelPoint);
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
27 mLastPosition = event.pixelPoint;
8
bf7903435f58 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 7
diff changeset
28
bf7903435f58 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 7
diff changeset
29 return true;
bf7903435f58 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 7
diff changeset
30 }
bf7903435f58 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 7
diff changeset
31 else {
bf7903435f58 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 7
diff changeset
32 return false;
bf7903435f58 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 7
diff changeset
33 }
7
936feb16eed4 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 3
diff changeset
34 }
936feb16eed4 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 3
diff changeset
35
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
36 override bool handleScroll(scope Viewport viewport, in ScrollEvent event) {
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
37 if (event.mask.isUnset(Modifier.MIDDLE_BUTTON)) {
9
66b47e122b31 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 8
diff changeset
38 Vector delta;
2
d6f44347373d * Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff changeset
39
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
40 switch (event.scrollDirection) {
8
bf7903435f58 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 7
diff changeset
41 case ScrollDirection.UP:
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
42 delta = event.mask.isSet(Modifier.SHIFT) ? Vector(-AMOUNT, 0.0) : Vector(0.0, AMOUNT);
8
bf7903435f58 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 7
diff changeset
43 break;
bf7903435f58 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 7
diff changeset
44 case ScrollDirection.DOWN:
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
45 delta = event.mask.isSet(Modifier.SHIFT) ? Vector(AMOUNT, 0.0) : Vector(0.0, -AMOUNT);
8
bf7903435f58 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 7
diff changeset
46 break;
bf7903435f58 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 7
diff changeset
47 case ScrollDirection.LEFT:
9
66b47e122b31 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 8
diff changeset
48 delta = Vector(-AMOUNT, 0.0);
8
bf7903435f58 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 7
diff changeset
49 break;
bf7903435f58 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 7
diff changeset
50 case ScrollDirection.RIGHT:
9
66b47e122b31 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 8
diff changeset
51 delta = Vector(AMOUNT, 0.0);
8
bf7903435f58 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 7
diff changeset
52 break;
40
1f97022e5c6d Checkpoint. Development continues...
daveb
parents: 36
diff changeset
53 default:
1f97022e5c6d Checkpoint. Development continues...
daveb
parents: 36
diff changeset
54 assert(0);
8
bf7903435f58 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 7
diff changeset
55 }
bf7903435f58 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 7
diff changeset
56
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
57 viewport.panRelative(delta);
2
d6f44347373d * Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff changeset
58 }
d6f44347373d * Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff changeset
59
d6f44347373d * Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff changeset
60 return true;
d6f44347373d * Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff changeset
61 }
d6f44347373d * Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff changeset
62
8
bf7903435f58 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 7
diff changeset
63 private {
bf7903435f58 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 7
diff changeset
64 Point mLastPosition;
9
66b47e122b31 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 8
diff changeset
65 static invariant double AMOUNT = 60.0;
8
bf7903435f58 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 7
diff changeset
66 }
2
d6f44347373d * Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff changeset
67 }
d6f44347373d * Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff changeset
68
7
936feb16eed4 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 3
diff changeset
69 final class ZoomTool : Tool {
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
70 override bool handleScroll(scope Viewport viewport, in ScrollEvent event) {
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
71 if (event.mask.isSet(Modifier.CONTROL)) {
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
72 if (event.scrollDirection == ScrollDirection.DOWN) {
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
73 viewport.zoomRelative(event.pixelPoint, 1.0 / ZOOM);
9
66b47e122b31 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 8
diff changeset
74 return true;
2
d6f44347373d * Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff changeset
75 }
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
76 else if (event.scrollDirection == ScrollDirection.UP) {
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
77 viewport.zoomRelative(event.pixelPoint, ZOOM);
9
66b47e122b31 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 8
diff changeset
78 return true;
66b47e122b31 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 8
diff changeset
79 }
66b47e122b31 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 8
diff changeset
80 else {
66b47e122b31 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 8
diff changeset
81 return false;
66b47e122b31 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 8
diff changeset
82 }
2
d6f44347373d * Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff changeset
83 }
d6f44347373d * Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff changeset
84 else {
d6f44347373d * Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff changeset
85 return false;
d6f44347373d * Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff changeset
86 }
d6f44347373d * Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff changeset
87 }
7
936feb16eed4 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 3
diff changeset
88
9
66b47e122b31 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 8
diff changeset
89 private {
66b47e122b31 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 8
diff changeset
90 static invariant double ZOOM = sqrt(2.0);
66b47e122b31 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 8
diff changeset
91 }
2
d6f44347373d * Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff changeset
92 }
17
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
93
36
188397ef9a12 Late night tinkering
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
94 final class LassoTool : Tool { // TODO convert to SelectTool
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
95 override bool handleButtonPress(scope Viewport viewport, in ButtonEvent event) {
17
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
96 if (event.button_name == ButtonName.LEFT) {
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
97 _active = true;
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
98 _anchorPoint = _currentPoint = event.pixelPoint;
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
99 viewport.setCursor(Cursor.HAND);
17
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
100 return true;
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
101 }
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
102 else {
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
103 return false;
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
104 }
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
105 }
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
106
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
107 override bool handleButtonRelease(scope Viewport viewport, in ButtonEvent event) {
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
108 if (event.button_name == ButtonName.LEFT && _active) {
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
109 _active = false;
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
110 viewport.damagePixel(feather(Rectangle(_anchorPoint, _currentPoint), LINE_WIDTH / 2.0));
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
111 viewport.setCursor(Cursor.DEFAULT);
17
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
112 return true;
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
113 }
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
114 else {
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
115 return false;
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
116 }
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
117 }
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
118
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
119 override bool handleMotion(scope Viewport viewport, in MotionEvent event) {
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
120 if (_active) {
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
121 viewport.damagePixel(feather(Rectangle(_anchorPoint, _currentPoint), LINE_WIDTH / 2.0));
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
122 _currentPoint = event.pixelPoint;
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
123 viewport.damagePixel(feather(Rectangle(_anchorPoint, _currentPoint), LINE_WIDTH / 2.0));
17
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
124 }
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
125
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
126 return false;
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
127 }
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
128
26
06c30d250c0a Cleanup
"David Bryant <bagnose@gmail.com>"
parents: 24
diff changeset
129 override void draw(in Viewport viewport,
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
130 in Rectangle pixelDamage, scope Context pixelCr,
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
131 in Rectangle modelDamage, scope Context modelCr) const {
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
132 if (_active) {
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
133 pixelCr.save; {
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
134 pixelCr.setSourceRgba(0.0, 0.0, 0.8, 0.3);
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
135 rectangle(pixelCr, Rectangle(_currentPoint, _anchorPoint));
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
136 pixelCr.fill();
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
137 } pixelCr.restore();
26
06c30d250c0a Cleanup
"David Bryant <bagnose@gmail.com>"
parents: 24
diff changeset
138
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
139 pixelCr.save(); {
26
06c30d250c0a Cleanup
"David Bryant <bagnose@gmail.com>"
parents: 24
diff changeset
140 //double[] dashes = [ 4.0, 4.0 ];
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
141 //pixelCr.setDash(dashes, 0.0);
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
142 pixelCr.setSourceRgba(0.0, 0.0, 0.5, 1.0);
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
143 pixelCr.setLineWidth(LINE_WIDTH);
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
144 //writefln("Drawing rectangle: %s", Rectangle(_currentPoint, _anchorPoint));
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
145 rectangle(pixelCr, Rectangle(_currentPoint, _anchorPoint));
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
146 pixelCr.stroke;
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
147 } pixelCr.restore;
18
df8d81d9f499 Lasso fiddling
David Bryant <daveb@acres.com.au>
parents: 17
diff changeset
148 }
17
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
149 }
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
150
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
151 private {
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
152 bool _active;
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
153 Point _currentPoint;
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 40
diff changeset
154 Point _anchorPoint; // Pixel
19
22abbf4cde96 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 18
diff changeset
155 static invariant double LINE_WIDTH = 1.0;
17
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
156 }
c643c04e3f5e Checkpoint
David Bryant <daveb@acres.com.au>
parents: 16
diff changeset
157 }