Mercurial > projects > doodle
annotate doodle/dia/standard_tools.d @ 58:c63719604adb
Beginnings of creating a rectangle...
author | "David Bryant <bagnose@gmail.com>" |
---|---|
date | Mon, 09 Aug 2010 21:43:24 +0930 |
parents | 9960c4fbd0dd |
children | 20d6327c4a75 |
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 | 10 import std.stdio; |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
14
diff
changeset
|
11 } |
7 | 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 { |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
14 this() { |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
15 super("Pan"); |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
16 } |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
17 |
57 | 18 override bool handleButtonPress(scope IViewport viewport, in ButtonEvent event) { |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
19 if (event.buttonName == ButtonName.MIDDLE) { |
48 | 20 mLastPosition = event.pixelPoint; |
7 | 21 return true; |
22 } | |
23 else { | |
24 return false; | |
25 } | |
26 } | |
27 | |
57 | 28 override bool handleMotion(scope IViewport viewport, in MotionEvent event) { |
48 | 29 if (event.mask.isSet(Modifier.MIDDLE_BUTTON)) { |
30 viewport.panRelative(mLastPosition - event.pixelPoint); | |
31 mLastPosition = event.pixelPoint; | |
8 | 32 |
33 return true; | |
34 } | |
35 else { | |
36 return false; | |
37 } | |
7 | 38 } |
39 | |
57 | 40 override bool handleScroll(scope IViewport viewport, in ScrollEvent event) { |
48 | 41 if (event.mask.isUnset(Modifier.MIDDLE_BUTTON)) { |
9 | 42 Vector delta; |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
43 |
48 | 44 switch (event.scrollDirection) { |
8 | 45 case ScrollDirection.UP: |
48 | 46 delta = event.mask.isSet(Modifier.SHIFT) ? Vector(-AMOUNT, 0.0) : Vector(0.0, AMOUNT); |
8 | 47 break; |
48 case ScrollDirection.DOWN: | |
48 | 49 delta = event.mask.isSet(Modifier.SHIFT) ? Vector(AMOUNT, 0.0) : Vector(0.0, -AMOUNT); |
8 | 50 break; |
51 case ScrollDirection.LEFT: | |
9 | 52 delta = Vector(-AMOUNT, 0.0); |
8 | 53 break; |
54 case ScrollDirection.RIGHT: | |
9 | 55 delta = Vector(AMOUNT, 0.0); |
8 | 56 break; |
40 | 57 default: |
58 assert(0); | |
8 | 59 } |
60 | |
48 | 61 viewport.panRelative(delta); |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
62 } |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
63 |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
64 return true; |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
65 } |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
66 |
8 | 67 private { |
68 Point mLastPosition; | |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
69 static immutable double AMOUNT = 60.0; |
8 | 70 } |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
71 } |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
72 |
7 | 73 final class ZoomTool : Tool { |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
74 this() { |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
75 super("Zoom"); |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
76 } |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
77 |
57 | 78 override bool handleScroll(scope IViewport viewport, in ScrollEvent event) { |
48 | 79 if (event.mask.isSet(Modifier.CONTROL)) { |
80 if (event.scrollDirection == ScrollDirection.DOWN) { | |
81 viewport.zoomRelative(event.pixelPoint, 1.0 / ZOOM); | |
9 | 82 return true; |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
83 } |
48 | 84 else if (event.scrollDirection == ScrollDirection.UP) { |
85 viewport.zoomRelative(event.pixelPoint, ZOOM); | |
9 | 86 return true; |
87 } | |
88 else { | |
89 return false; | |
90 } | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
91 } |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
92 else { |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
93 return false; |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
94 } |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
95 } |
7 | 96 |
9 | 97 private { |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
98 static immutable double ZOOM = sqrt(2.0); |
9 | 99 } |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
100 } |
17 | 101 |
56 | 102 final class SelectTool : Tool { |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
103 this() { |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
104 super("Select"); |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
105 } |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
106 |
57 | 107 override bool handleButtonPress(scope IViewport viewport, in ButtonEvent event) { |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
108 if (event.buttonName == ButtonName.LEFT) { |
48 | 109 _active = true; |
110 _anchorPoint = _currentPoint = event.pixelPoint; | |
111 viewport.setCursor(Cursor.HAND); | |
17 | 112 return true; |
113 } | |
114 else { | |
115 return false; | |
116 } | |
117 } | |
118 | |
57 | 119 override bool handleButtonRelease(scope IViewport viewport, in ButtonEvent event) { |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
120 if (event.buttonName == ButtonName.LEFT && _active) { |
48 | 121 _active = false; |
122 viewport.damagePixel(feather(Rectangle(_anchorPoint, _currentPoint), LINE_WIDTH / 2.0)); | |
123 viewport.setCursor(Cursor.DEFAULT); | |
17 | 124 return true; |
125 } | |
126 else { | |
127 return false; | |
128 } | |
129 } | |
130 | |
57 | 131 override bool handleMotion(scope IViewport viewport, in MotionEvent event) { |
48 | 132 if (_active) { |
133 viewport.damagePixel(feather(Rectangle(_anchorPoint, _currentPoint), LINE_WIDTH / 2.0)); | |
134 _currentPoint = event.pixelPoint; | |
135 viewport.damagePixel(feather(Rectangle(_anchorPoint, _currentPoint), LINE_WIDTH / 2.0)); | |
17 | 136 } |
137 | |
138 return false; | |
139 } | |
140 | |
57 | 141 override void draw(in IViewport viewport, |
56 | 142 in Rectangle pixelDamage, scope Context pixelCr) const { |
48 | 143 if (_active) { |
56 | 144 /* |
48 | 145 pixelCr.save; { |
146 pixelCr.setSourceRgba(0.0, 0.0, 0.8, 0.3); | |
147 rectangle(pixelCr, Rectangle(_currentPoint, _anchorPoint)); | |
148 pixelCr.fill(); | |
149 } pixelCr.restore(); | |
56 | 150 */ |
26 | 151 |
48 | 152 pixelCr.save(); { |
56 | 153 double[] dashes = [ 4.0, 4.0 ]; |
154 pixelCr.setDash(dashes, 0.0); | |
48 | 155 pixelCr.setSourceRgba(0.0, 0.0, 0.5, 1.0); |
156 pixelCr.setLineWidth(LINE_WIDTH); | |
157 rectangle(pixelCr, Rectangle(_currentPoint, _anchorPoint)); | |
158 pixelCr.stroke; | |
159 } pixelCr.restore; | |
18 | 160 } |
17 | 161 } |
162 | |
163 private { | |
48 | 164 bool _active; |
165 Point _currentPoint; | |
166 Point _anchorPoint; // Pixel | |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
167 static immutable double LINE_WIDTH = 1.0; |
17 | 168 } |
169 } |