Mercurial > projects > doodle
annotate standard_tools.d @ 18:df8d81d9f499
Lasso fiddling
author | David Bryant <daveb@acres.com.au> |
---|---|
date | Mon, 13 Jul 2009 17:08:32 +0930 |
parents | c643c04e3f5e |
children | 22abbf4cde96 |
rev | line source |
---|---|
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
1 module standard_tools; |
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 { |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
14
diff
changeset
|
4 import tool; |
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 { |
18 | 8 import cairo_support; |
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 { |
17 | 14 override bool handle_button_press(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 | |
17 | 24 override bool handle_motion(Viewport viewport, in MotionEvent event) { |
13 | 25 if (event.mask.is_set(Modifier.MIDDLE_BUTTON)) { |
17 | 26 viewport.rel_pan(mLastPosition - event.pixel_point); |
27 mLastPosition = event.pixel_point; | |
8 | 28 |
29 return true; | |
30 } | |
31 else { | |
32 return false; | |
33 } | |
7 | 34 } |
35 | |
17 | 36 override bool handle_scroll(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 | |
10 | 55 viewport.rel_pan(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 { |
17 | 68 override bool handle_scroll(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) { |
17 | 71 viewport.rel_zoom(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) { |
17 | 75 viewport.rel_zoom(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 { | |
93 override bool handle_button_press(Viewport viewport, in ButtonEvent event) { | |
94 if (event.button_name == ButtonName.LEFT) { | |
95 writefln("Start lasso"); | |
96 mActive = true; | |
18 | 97 mAnchorPoint = mCurrentPoint = event.pixel_point; |
17 | 98 return true; |
99 } | |
100 else { | |
101 return false; | |
102 } | |
103 } | |
104 | |
105 override bool handle_button_release(Viewport viewport, in ButtonEvent event) { | |
106 if (event.button_name == ButtonName.LEFT && mActive) { | |
107 writefln("End lasso"); | |
108 mActive = false; | |
18 | 109 viewport.damage_pixel(Rectangle(mAnchorPoint, mCurrentPoint)); |
17 | 110 return true; |
111 } | |
112 else { | |
113 return false; | |
114 } | |
115 } | |
116 | |
18 | 117 // FIXME account for the line width... |
118 | |
17 | 119 override bool handle_motion(Viewport viewport, in MotionEvent event) { |
120 if (mActive) { | |
18 | 121 viewport.damage_pixel(Rectangle(mAnchorPoint, mCurrentPoint)); |
122 mCurrentPoint = event.pixel_point; | |
123 viewport.damage_pixel(Rectangle(mAnchorPoint, mCurrentPoint)); | |
17 | 124 } |
125 | |
126 return false; | |
127 } | |
128 | |
18 | 129 override void draw(const Viewport viewport, in Rectangle damage, Context model_cr, Context pixel_cr) const { |
130 if (mActive) { | |
131 pixel_cr.save; { | |
132 pixel_cr.setSourceRgba(1.0, 0.0, 0.0, 1.0); | |
133 //writefln("Drawing rectangle: %s", Rectangle(mCurrentPoint, mAnchorPoint)); | |
134 rectangle(pixel_cr, Rectangle(mCurrentPoint, mAnchorPoint)); | |
135 pixel_cr.stroke; | |
136 } pixel_cr.restore; | |
137 } | |
17 | 138 } |
139 | |
140 private { | |
141 bool mActive; | |
18 | 142 Point mCurrentPoint; |
17 | 143 Point mAnchorPoint; // Pixel |
144 } | |
145 } |