Mercurial > projects > doodle
annotate dia/standard_tools.d @ 27:f3d91579bb28
Checkpoint
author | David Bryant <daveb@acres.com.au> |
---|---|
date | Wed, 29 Jul 2009 14:11:35 +0930 |
parents | 06c30d250c0a |
children |
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; |
27 | 97 viewport.set_cursor(Cursor.HAND); |
17 | 98 return true; |
99 } | |
100 else { | |
101 return false; | |
102 } | |
103 } | |
104 | |
26 | 105 override bool handle_button_release(scope Viewport viewport, in ButtonEvent event) { |
17 | 106 if (event.button_name == ButtonName.LEFT && mActive) { |
107 mActive = false; | |
19 | 108 viewport.damage_pixel(Rectangle(mAnchorPoint, mCurrentPoint).feathered(LINE_WIDTH / 2.0)); |
27 | 109 viewport.set_cursor(Cursor.DEFAULT); |
17 | 110 return true; |
111 } | |
112 else { | |
113 return false; | |
114 } | |
115 } | |
116 | |
26 | 117 override bool handle_motion(scope Viewport viewport, in MotionEvent event) { |
17 | 118 if (mActive) { |
19 | 119 viewport.damage_pixel(Rectangle(mAnchorPoint, mCurrentPoint).feathered(LINE_WIDTH / 2.0)); |
18 | 120 mCurrentPoint = event.pixel_point; |
19 | 121 viewport.damage_pixel(Rectangle(mAnchorPoint, mCurrentPoint).feathered(LINE_WIDTH / 2.0)); |
17 | 122 } |
123 | |
124 return false; | |
125 } | |
126 | |
26 | 127 override void draw(in Viewport viewport, |
19 | 128 in Rectangle pixel_damage, scope Context pixel_cr, |
129 in Rectangle model_damage, scope Context model_cr) const { | |
18 | 130 if (mActive) { |
131 pixel_cr.save; { | |
26 | 132 pixel_cr.setSourceRgba(0.0, 0.0, 0.8, 0.3); |
133 rectangle(pixel_cr, Rectangle(mCurrentPoint, mAnchorPoint)); | |
134 pixel_cr.fill(); | |
135 } pixel_cr.restore(); | |
136 | |
137 pixel_cr.save(); { | |
138 //double[] dashes = [ 4.0, 4.0 ]; | |
139 //pixel_cr.setDash(dashes, 0.0); | |
19 | 140 pixel_cr.setSourceRgba(0.0, 0.0, 0.5, 1.0); |
141 pixel_cr.setLineWidth(LINE_WIDTH); | |
18 | 142 //writefln("Drawing rectangle: %s", Rectangle(mCurrentPoint, mAnchorPoint)); |
143 rectangle(pixel_cr, Rectangle(mCurrentPoint, mAnchorPoint)); | |
144 pixel_cr.stroke; | |
145 } pixel_cr.restore; | |
146 } | |
17 | 147 } |
148 | |
149 private { | |
150 bool mActive; | |
18 | 151 Point mCurrentPoint; |
17 | 152 Point mAnchorPoint; // Pixel |
19 | 153 static invariant double LINE_WIDTH = 1.0; |
17 | 154 } |
155 } |