Mercurial > projects > doodle
annotate dia/standard_tools.d @ 26:06c30d250c0a
Cleanup
author | "David Bryant <bagnose@gmail.com>" |
---|---|
date | Thu, 16 Jul 2009 00:12:02 +0930 |
parents | a24c13bb9c98 |
children | f3d91579bb28 |
rev | line source |
---|---|
22 | 1 module 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 { |
24 | 4 import 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 { |
24 | 8 import 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 { |
26 | 14 override bool handle_button_press(scope Viewport viewport, in ButtonEvent event) { |
7 | 15 if (event.button_name == ButtonName.MIDDLE) { |
17 | 16 mLastPosition = event.pixel_point; |
7 | 17 return true; |
18 } | |
19 else { | |
20 return false; | |
21 } | |
22 } | |
23 | |
26 | 24 override bool handle_motion(scope Viewport viewport, in MotionEvent event) { |
13 | 25 if (event.mask.is_set(Modifier.MIDDLE_BUTTON)) { |
20 | 26 viewport.pan_relative(mLastPosition - event.pixel_point); |
17 | 27 mLastPosition = event.pixel_point; |
8 | 28 |
29 return true; | |
30 } | |
31 else { | |
32 return false; | |
33 } | |
7 | 34 } |
35 | |
26 | 36 override bool handle_scroll(scope Viewport viewport, in ScrollEvent event) { |
13 | 37 if (event.mask.is_unset(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 |
8 | 40 switch (event.scroll_direction) { |
41 case ScrollDirection.UP: | |
14 | 42 delta = event.mask.is_set(Modifier.SHIFT) ? Vector(AMOUNT, 0.0) : Vector(0.0, AMOUNT); |
8 | 43 break; |
44 case ScrollDirection.DOWN: | |
14 | 45 delta = event.mask.is_set(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; |
53 } | |
54 | |
20 | 55 viewport.pan_relative(delta); |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
56 } |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
57 |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
58 return true; |
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 |
8 | 61 private { |
62 Point mLastPosition; | |
9 | 63 static invariant double AMOUNT = 60.0; |
8 | 64 } |
2
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 |
7 | 67 final class ZoomTool : Tool { |
26 | 68 override bool handle_scroll(scope Viewport viewport, in ScrollEvent event) { |
7 | 69 if (event.mask.is_set(Modifier.CONTROL)) { |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
70 if (event.scroll_direction == ScrollDirection.DOWN) { |
20 | 71 viewport.zoom_relative(event.pixel_point, 1.0 / ZOOM); |
9 | 72 return true; |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
73 } |
9 | 74 else if (event.scroll_direction == ScrollDirection.UP) { |
20 | 75 viewport.zoom_relative(event.pixel_point, ZOOM); |
9 | 76 return true; |
77 } | |
78 else { | |
79 return false; | |
80 } | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
81 } |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
82 else { |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
83 return false; |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
84 } |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
85 } |
7 | 86 |
9 | 87 private { |
88 static invariant double ZOOM = sqrt(2.0); | |
89 } | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
90 } |
17 | 91 |
92 final class LassoTool : Tool { | |
26 | 93 override bool handle_button_press(scope Viewport viewport, in ButtonEvent event) { |
17 | 94 if (event.button_name == ButtonName.LEFT) { |
95 mActive = true; | |
18 | 96 mAnchorPoint = mCurrentPoint = event.pixel_point; |
17 | 97 return true; |
98 } | |
99 else { | |
100 return false; | |
101 } | |
102 } | |
103 | |
26 | 104 override bool handle_button_release(scope Viewport viewport, in ButtonEvent event) { |
17 | 105 if (event.button_name == ButtonName.LEFT && mActive) { |
106 mActive = false; | |
19 | 107 viewport.damage_pixel(Rectangle(mAnchorPoint, mCurrentPoint).feathered(LINE_WIDTH / 2.0)); |
17 | 108 return true; |
109 } | |
110 else { | |
111 return false; | |
112 } | |
113 } | |
114 | |
26 | 115 override bool handle_motion(scope Viewport viewport, in MotionEvent event) { |
17 | 116 if (mActive) { |
19 | 117 viewport.damage_pixel(Rectangle(mAnchorPoint, mCurrentPoint).feathered(LINE_WIDTH / 2.0)); |
18 | 118 mCurrentPoint = event.pixel_point; |
19 | 119 viewport.damage_pixel(Rectangle(mAnchorPoint, mCurrentPoint).feathered(LINE_WIDTH / 2.0)); |
17 | 120 } |
121 | |
122 return false; | |
123 } | |
124 | |
26 | 125 override void draw(in Viewport viewport, |
19 | 126 in Rectangle pixel_damage, scope Context pixel_cr, |
127 in Rectangle model_damage, scope Context model_cr) const { | |
18 | 128 if (mActive) { |
129 pixel_cr.save; { | |
26 | 130 pixel_cr.setSourceRgba(0.0, 0.0, 0.8, 0.3); |
131 rectangle(pixel_cr, Rectangle(mCurrentPoint, mAnchorPoint)); | |
132 pixel_cr.fill(); | |
133 } pixel_cr.restore(); | |
134 | |
135 pixel_cr.save(); { | |
136 //double[] dashes = [ 4.0, 4.0 ]; | |
137 //pixel_cr.setDash(dashes, 0.0); | |
19 | 138 pixel_cr.setSourceRgba(0.0, 0.0, 0.5, 1.0); |
139 pixel_cr.setLineWidth(LINE_WIDTH); | |
18 | 140 //writefln("Drawing rectangle: %s", Rectangle(mCurrentPoint, mAnchorPoint)); |
141 rectangle(pixel_cr, Rectangle(mCurrentPoint, mAnchorPoint)); | |
142 pixel_cr.stroke; | |
143 } pixel_cr.restore; | |
144 } | |
17 | 145 } |
146 | |
147 private { | |
148 bool mActive; | |
18 | 149 Point mCurrentPoint; |
17 | 150 Point mAnchorPoint; // Pixel |
19 | 151 static invariant double LINE_WIDTH = 1.0; |
17 | 152 } |
153 } |