Mercurial > projects > doodle
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 |
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 { |
48 | 14 override bool handleButtonPress(scope Viewport viewport, in ButtonEvent event) { |
7 | 15 if (event.button_name == ButtonName.MIDDLE) { |
48 | 16 mLastPosition = event.pixelPoint; |
7 | 17 return true; |
18 } | |
19 else { | |
20 return false; | |
21 } | |
22 } | |
23 | |
48 | 24 override bool handleMotion(scope Viewport viewport, in MotionEvent event) { |
25 if (event.mask.isSet(Modifier.MIDDLE_BUTTON)) { | |
26 viewport.panRelative(mLastPosition - event.pixelPoint); | |
27 mLastPosition = event.pixelPoint; | |
8 | 28 |
29 return true; | |
30 } | |
31 else { | |
32 return false; | |
33 } | |
7 | 34 } |
35 | |
48 | 36 override bool handleScroll(scope Viewport viewport, in ScrollEvent event) { |
37 if (event.mask.isUnset(Modifier.MIDDLE_BUTTON)) { | |
9 | 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 | 40 switch (event.scrollDirection) { |
8 | 41 case ScrollDirection.UP: |
48 | 42 delta = event.mask.isSet(Modifier.SHIFT) ? Vector(-AMOUNT, 0.0) : Vector(0.0, AMOUNT); |
8 | 43 break; |
44 case ScrollDirection.DOWN: | |
48 | 45 delta = event.mask.isSet(Modifier.SHIFT) ? Vector(AMOUNT, 0.0) : Vector(0.0, -AMOUNT); |
8 | 46 break; |
47 case ScrollDirection.LEFT: | |
9 | 48 delta = Vector(-AMOUNT, 0.0); |
8 | 49 break; |
50 case ScrollDirection.RIGHT: | |
9 | 51 delta = Vector(AMOUNT, 0.0); |
8 | 52 break; |
40 | 53 default: |
54 assert(0); | |
8 | 55 } |
56 | |
48 | 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 | 63 private { |
64 Point mLastPosition; | |
9 | 65 static invariant double AMOUNT = 60.0; |
8 | 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 | 69 final class ZoomTool : Tool { |
48 | 70 override bool handleScroll(scope Viewport viewport, in ScrollEvent event) { |
71 if (event.mask.isSet(Modifier.CONTROL)) { | |
72 if (event.scrollDirection == ScrollDirection.DOWN) { | |
73 viewport.zoomRelative(event.pixelPoint, 1.0 / ZOOM); | |
9 | 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 | 76 else if (event.scrollDirection == ScrollDirection.UP) { |
77 viewport.zoomRelative(event.pixelPoint, ZOOM); | |
9 | 78 return true; |
79 } | |
80 else { | |
81 return false; | |
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 | 88 |
9 | 89 private { |
90 static invariant double ZOOM = sqrt(2.0); | |
91 } | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
92 } |
17 | 93 |
36 | 94 final class LassoTool : Tool { // TODO convert to SelectTool |
48 | 95 override bool handleButtonPress(scope Viewport viewport, in ButtonEvent event) { |
17 | 96 if (event.button_name == ButtonName.LEFT) { |
48 | 97 _active = true; |
98 _anchorPoint = _currentPoint = event.pixelPoint; | |
99 viewport.setCursor(Cursor.HAND); | |
17 | 100 return true; |
101 } | |
102 else { | |
103 return false; | |
104 } | |
105 } | |
106 | |
48 | 107 override bool handleButtonRelease(scope Viewport viewport, in ButtonEvent event) { |
108 if (event.button_name == ButtonName.LEFT && _active) { | |
109 _active = false; | |
110 viewport.damagePixel(feather(Rectangle(_anchorPoint, _currentPoint), LINE_WIDTH / 2.0)); | |
111 viewport.setCursor(Cursor.DEFAULT); | |
17 | 112 return true; |
113 } | |
114 else { | |
115 return false; | |
116 } | |
117 } | |
118 | |
48 | 119 override bool handleMotion(scope Viewport viewport, in MotionEvent event) { |
120 if (_active) { | |
121 viewport.damagePixel(feather(Rectangle(_anchorPoint, _currentPoint), LINE_WIDTH / 2.0)); | |
122 _currentPoint = event.pixelPoint; | |
123 viewport.damagePixel(feather(Rectangle(_anchorPoint, _currentPoint), LINE_WIDTH / 2.0)); | |
17 | 124 } |
125 | |
126 return false; | |
127 } | |
128 | |
26 | 129 override void draw(in Viewport viewport, |
48 | 130 in Rectangle pixelDamage, scope Context pixelCr, |
131 in Rectangle modelDamage, scope Context modelCr) const { | |
132 if (_active) { | |
133 pixelCr.save; { | |
134 pixelCr.setSourceRgba(0.0, 0.0, 0.8, 0.3); | |
135 rectangle(pixelCr, Rectangle(_currentPoint, _anchorPoint)); | |
136 pixelCr.fill(); | |
137 } pixelCr.restore(); | |
26 | 138 |
48 | 139 pixelCr.save(); { |
26 | 140 //double[] dashes = [ 4.0, 4.0 ]; |
48 | 141 //pixelCr.setDash(dashes, 0.0); |
142 pixelCr.setSourceRgba(0.0, 0.0, 0.5, 1.0); | |
143 pixelCr.setLineWidth(LINE_WIDTH); | |
144 //writefln("Drawing rectangle: %s", Rectangle(_currentPoint, _anchorPoint)); | |
145 rectangle(pixelCr, Rectangle(_currentPoint, _anchorPoint)); | |
146 pixelCr.stroke; | |
147 } pixelCr.restore; | |
18 | 148 } |
17 | 149 } |
150 | |
151 private { | |
48 | 152 bool _active; |
153 Point _currentPoint; | |
154 Point _anchorPoint; // Pixel | |
19 | 155 static invariant double LINE_WIDTH = 1.0; |
17 | 156 } |
157 } |