Mercurial > projects > doodle
annotate canvas.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 |
---|---|
0 | 1 module canvas; |
2 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
3 public { |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
4 import icanvas; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
5 import tk.geometry; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
6 import tk.events; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
7 } |
0 | 8 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
9 private { |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
10 import tk.gtk_support; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
11 import tk.misc; |
0 | 12 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
13 import cairo.Surface; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
14 import cairo_support; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
15 |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
16 import std.math; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
17 import std.stdio; |
0 | 18 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
19 import gtk.Widget; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
20 import gtk.Toolbar; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
21 import gtk.Table; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
22 import gtk.HRuler; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
23 import gtk.VRuler; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
24 import gtk.Range; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
25 import gtk.HScrollbar; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
26 import gtk.VScrollbar; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
27 import gtk.DrawingArea; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
28 import gtk.Adjustment; |
0 | 29 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
30 import gdk.Drawable; |
0 | 31 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
32 import gtkc.gtk; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
33 } |
0 | 34 |
35 // x and y run right and up respectively | |
36 | |
10 | 37 class Canvas : Table, Viewport { |
11 | 38 this(in Layer[] layers, EventHandler event_handler, in double ppi) { |
0 | 39 super(3, 3, 0); |
40 | |
17 | 41 mDamage = Rectangle.DEFAULT; |
42 | |
11 | 43 mLayers = layers.dup; |
0 | 44 mEventHandler = event_handler; |
13 | 45 mPPI = ppi; |
0 | 46 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
47 /* |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
48 writefln("Layer bounds: %s", layer_bounds); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
49 writefln("Canvas bounds: %s", mCanvasBounds); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
50 writefln("View centre: %s", mViewCentre); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
51 */ |
0 | 52 |
13 | 53 // Create our child widgets and register callbacks |
54 | |
55 mHRuler = new HRuler; | |
0 | 56 attach(mHRuler, |
57 1, 2, | |
58 0, 1, | |
59 AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.SHRINK, | |
60 0, 0); | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
61 mHRuler.setMetric(GtkMetricType.PIXELS); |
0 | 62 |
13 | 63 mVRuler = new VRuler; |
0 | 64 attach(mVRuler, |
65 0, 1, | |
66 1, 2, | |
67 AttachOptions.SHRINK, AttachOptions.FILL | AttachOptions.EXPAND, | |
68 0, 0); | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
69 mVRuler.setMetric(GtkMetricType.PIXELS); |
0 | 70 |
13 | 71 mDrawingArea = new DrawingArea; |
0 | 72 mDrawingArea.addOnRealize(&onRealize); |
73 mDrawingArea.addOnConfigure(&onConfigure); | |
74 mDrawingArea.addOnExpose(&onExpose); | |
5 | 75 mDrawingArea.addOnButtonPress(&onButtonPress); |
76 mDrawingArea.addOnButtonRelease(&onButtonRelease); | |
0 | 77 mDrawingArea.addOnKeyPress(&onKeyEvent); |
78 mDrawingArea.addOnKeyRelease(&onKeyEvent); | |
79 mDrawingArea.addOnMotionNotify(&onMotionNotify); | |
80 mDrawingArea.addOnScroll(&onScroll); | |
81 attach(mDrawingArea, | |
82 1, 2, | |
83 1, 2, | |
84 AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL | AttachOptions.EXPAND, | |
85 0, 0); | |
86 | |
87 // value, lower, upper, step-inc, page-inc, page-size | |
88 // Give the adjustments dummy values until we receive a configure | |
12 | 89 mHAdjustment = new Adjustment(0.0, 0.0, 1.0, 0.2, 0.5, 0.5); |
0 | 90 mHAdjustment.addOnValueChanged(&onValueChanged); |
91 mHScrollbar = new HScrollbar(mHAdjustment); | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
92 mHScrollbar.setInverted(false); |
0 | 93 attach(mHScrollbar, |
94 1, 2, | |
95 2, 3, | |
96 AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.SHRINK, | |
97 0, 0); | |
98 | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
99 mVAdjustment = new Adjustment(0.0, 0.0, 1.0, 0.2, 0.5, 0.5); |
0 | 100 mVAdjustment.addOnValueChanged(&onValueChanged); |
101 mVScrollbar = new VScrollbar(mVAdjustment); | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
102 mVScrollbar.setInverted(true); |
0 | 103 attach(mVScrollbar, |
104 2, 3, | |
105 1, 2, | |
106 AttachOptions.SHRINK, | |
107 AttachOptions.FILL | AttachOptions.EXPAND, | |
108 0, 0); | |
109 } | |
110 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
111 override void rel_zoom(Point pixel_datum, double factor) { |
0 | 112 // Work out pixel distance from current centre to datum, |
113 // Do the zoom, then work out the new centre that keeps the | |
114 // pixel distance the same | |
115 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
116 Point old_model_datum = pixel_to_model(pixel_datum); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
117 Vector pixel_distance = model_to_pixel(old_model_datum - mViewCentre); |
0 | 118 mZoom = clamp_zoom(factor * mZoom); |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
119 mViewCentre = old_model_datum - pixel_to_model(pixel_distance); |
0 | 120 |
13 | 121 update_adjustments; |
122 update_rulers; | |
123 queueDraw; | |
0 | 124 } |
125 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
126 override void rel_pan(Vector pixel_displacement) { |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
127 mViewCentre = mViewCentre + pixel_to_model(pixel_displacement); |
0 | 128 |
13 | 129 update_adjustments; |
130 update_rulers; | |
131 queueDraw; | |
132 } | |
133 | |
17 | 134 override void damage_model(Rectangle area) { |
135 mDamage = mDamage | model_to_pixel(area); | |
136 } | |
137 | |
18 | 138 override void damage_pixel(Rectangle area) { |
139 mDamage = mDamage | area; | |
140 } | |
141 | |
13 | 142 override double zoom() const { |
143 return mZoom; | |
144 } | |
145 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
146 override Point model_to_pixel(Point model) const { |
13 | 147 return Point.DEFAULT + mViewSize / 2.0 + mZoom * (model - mViewCentre); |
148 } | |
149 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
150 override Point pixel_to_model(Point pixel) const { |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
151 return mViewCentre + (pixel - mViewSize / 2.0 - Point.DEFAULT) / mZoom; |
0 | 152 } |
153 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
154 override Vector model_to_pixel(Vector model) const { |
13 | 155 return mZoom * model; |
156 } | |
157 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
158 override Vector pixel_to_model(Vector pixel) const { |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
159 return pixel / mZoom; |
13 | 160 } |
161 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
162 override double model_to_pixel(double model) const { |
13 | 163 return mZoom * model; |
164 } | |
165 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
166 override double pixel_to_model(double pixel) const { |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
167 return pixel / mZoom; |
13 | 168 } |
169 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
170 override Rectangle model_to_pixel(Rectangle model) const { |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
171 return Rectangle(model_to_pixel(model.position), model_to_pixel(model.size)); |
13 | 172 } |
173 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
174 override Rectangle pixel_to_model(Rectangle model) const { |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
175 return Rectangle(pixel_to_model(model.position), pixel_to_model(model.size)); |
13 | 176 } |
10 | 177 |
0 | 178 private { |
179 | |
180 void onRealize(Widget widget) { | |
181 assert(widget is mDrawingArea); | |
182 //writefln("Got realize\n"); | |
183 } | |
184 | |
185 bool onConfigure(GdkEventConfigure * event, Widget widget) { | |
186 assert(widget is mDrawingArea); | |
187 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
188 if (!mHadConfigure) { |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
189 const double MM_PER_INCH = 25.4; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
190 mZoom = 0.25 * mPPI / MM_PER_INCH; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
191 |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
192 // Take the union of the bounds of each layer to |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
193 // determine the canvas size |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
194 |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
195 Rectangle layer_bounds = Rectangle.DEFAULT; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
196 |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
197 foreach (ref layer; mLayers) { |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
198 layer_bounds = layer_bounds | layer.bounds; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
199 } |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
200 |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
201 assert(layer_bounds.valid); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
202 |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
203 mCanvasBounds = layer_bounds.moved(-layer_bounds.size).expanded(2.0 * layer_bounds.size); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
204 mViewCentre = mCanvasBounds.centre; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
205 |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
206 mHadConfigure = true; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
207 } |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
208 |
3
7d57cae10805
Renamed geometry2 to geometry
David Bryant <daveb@acres.com.au>
parents:
2
diff
changeset
|
209 mViewSize = Vector(cast(double)event.width, cast(double)event.height); |
13 | 210 update_adjustments; |
211 update_rulers; | |
0 | 212 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
213 //writefln("Canvas bounds: %s", mCanvasBounds); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
214 //writefln("View centre: %s", mViewCentre); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
215 |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
216 return true; |
0 | 217 } |
218 | |
219 bool onExpose(GdkEventExpose * event, Widget widget) { | |
220 assert(widget is mDrawingArea); | |
221 | |
13 | 222 Drawable dr = mDrawingArea.getWindow; |
0 | 223 |
224 int width, height; | |
225 dr.getSize(width, height); | |
226 //writefln("Got expose %dx%d\n", width, height); | |
227 | |
12 | 228 scope model_cr = new Context(dr); |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
229 scope pixel_cr = new Context(dr); |
12 | 230 |
11 | 231 Rectangle damage = |
232 event is null ? | |
233 Rectangle(Point(0.0, 0.0), Vector(cast(double)width, cast(double)height)) : | |
234 Rectangle(Point(cast(double)event.area.x, cast(double)event.area.y), | |
235 Vector(cast(double)event.area.width, cast(double)event.area.height)); | |
0 | 236 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
237 model_cr.save; pixel_cr.save; { |
18 | 238 // FIXME restore clipping |
239 | |
12 | 240 // Setup model context |
241 | |
18 | 242 //rectangle(model_cr, damage); |
243 //model_cr.clip; | |
12 | 244 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
245 GtkAdjustment * h_gtkAdjustment = mHAdjustment.getAdjustmentStruct; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
246 GtkAdjustment * v_gtkAdjustment = mVAdjustment.getAdjustmentStruct; |
12 | 247 |
13 | 248 model_cr.scale(mZoom, -mZoom); |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
249 model_cr.translate(-gtk_adjustment_get_value(h_gtkAdjustment), |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
250 -gtk_adjustment_get_value(v_gtkAdjustment) - gtk_adjustment_get_page_size(v_gtkAdjustment)); |
0 | 251 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
252 // Setup pixel context |
12 | 253 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
254 pixel_cr.translate(0.0, mViewSize.y); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
255 pixel_cr.scale(1.0, -1.0); |
18 | 256 //rectangle(pixel_cr, damage); |
257 //pixel_cr.clip; | |
12 | 258 |
259 // Fill the background | |
260 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
261 pixel_cr.save; { |
11 | 262 // Make the window light grey |
18 | 263 pixel_cr.setSourceRgba(0.6, 0.6, 0.6, 1.0); |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
264 rectangle(pixel_cr, damage); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
265 pixel_cr.fill; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
266 } pixel_cr.restore; |
12 | 267 |
268 // Draw each layer | |
0 | 269 |
11 | 270 foreach(ref layer; mLayers) { |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
271 model_cr.save; pixel_cr.save; { |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
272 layer.draw(this, damage, model_cr, pixel_cr); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
273 } pixel_cr.restore; model_cr.restore; |
11 | 274 } |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
275 } pixel_cr.restore; model_cr.restore; |
0 | 276 |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
277 return true; |
0 | 278 } |
279 | |
5 | 280 bool onButtonPress(GdkEventButton * event, Widget widget) { |
0 | 281 assert(widget is mDrawingArea); |
282 //writefln("Got button event\n"); | |
283 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
284 Point pixel_point = Point(event.x + 0.5, mViewSize.y - (event.y + 0.5)); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
285 Point model_point = pixel_to_model(pixel_point); |
0 | 286 |
7 | 287 auto button_event = new ButtonEvent(gtk2tk_button_action(event.type), |
288 gtk2tk_button_name(event.button), | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
289 pixel_point, |
0 | 290 model_point, |
291 gtk2tk_mask(event.state)); | |
292 | |
5 | 293 mEventHandler.handle_button_press(this, button_event); |
294 | |
17 | 295 process_damage; |
296 | |
5 | 297 return true; |
298 } | |
299 | |
300 bool onButtonRelease(GdkEventButton * event, Widget widget) { | |
301 assert(widget is mDrawingArea); | |
302 //writefln("Got button event\n"); | |
303 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
304 Point pixel_point = Point(event.x + 0.5, mViewSize.y - (event.y + 0.5)); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
305 Point model_point = pixel_to_model(pixel_point); |
5 | 306 |
7 | 307 auto button_event = new ButtonEvent(gtk2tk_button_action(event.type), |
308 gtk2tk_button_name(event.button), | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
309 pixel_point, |
5 | 310 model_point, |
311 gtk2tk_mask(event.state)); | |
312 | |
313 mEventHandler.handle_button_release(this, button_event); | |
0 | 314 |
17 | 315 process_damage; |
316 | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
317 return true; |
0 | 318 } |
319 | |
320 bool onKeyEvent(GdkEventKey * event, Widget widget) { | |
321 assert(widget is mDrawingArea); | |
322 //writefln("Got key event\n"); | |
323 | |
324 //auto key_event = new KeyEvent("", | |
325 // mEventHandle.handle_key(key_event); | |
326 | |
17 | 327 process_damage; |
328 | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
329 return true; |
0 | 330 } |
331 | |
332 bool onMotionNotify(GdkEventMotion * event, Widget widget) { | |
333 assert(widget is mDrawingArea); | |
334 //writefln("Got motion notify\n"); | |
335 gtk_widget_event(mHRuler.getWidgetStruct(), cast(GdkEvent *)event); | |
336 gtk_widget_event(mVRuler.getWidgetStruct(), cast(GdkEvent *)event); | |
337 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
338 Point pixel_point = Point(event.x + 0.5, mViewSize.y - (event.y + 0.5)); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
339 Point model_point = pixel_to_model(pixel_point); |
0 | 340 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
341 auto motion_event = new MotionEvent(pixel_point, |
0 | 342 model_point, |
343 gtk2tk_mask(event.state)); | |
344 | |
345 mEventHandler.handle_motion(this, motion_event); | |
346 | |
17 | 347 process_damage; |
348 | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
349 return true; |
0 | 350 } |
351 | |
352 bool onScroll(GdkEventScroll * event, Widget widget) { | |
353 assert(widget is mDrawingArea); | |
354 //writefln("Got scroll\n"); | |
355 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
356 Point pixel_point = Point(event.x + 0.5, mViewSize.y - (event.y + 0.5)); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
357 Point model_point = pixel_to_model(pixel_point); |
0 | 358 |
359 auto scroll_event = new ScrollEvent(gtk2tk_direction(event.direction), | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
360 pixel_point, |
0 | 361 model_point, |
362 gtk2tk_mask(event.state)); | |
363 | |
364 mEventHandler.handle_scroll(this, scroll_event); | |
365 | |
17 | 366 process_damage; |
367 | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
368 return true; |
0 | 369 } |
370 | |
371 void onValueChanged(Adjustment adjustment) { | |
13 | 372 GtkAdjustment * h_gtkAdjustment = mHAdjustment.getAdjustmentStruct; |
373 GtkAdjustment * v_gtkAdjustment = mVAdjustment.getAdjustmentStruct; | |
0 | 374 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
375 Point view_left_top = Point(gtk_adjustment_get_value(h_gtkAdjustment), |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
376 gtk_adjustment_get_value(v_gtkAdjustment)); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
377 |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
378 Vector model_size = pixel_to_model(mViewSize); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
379 |
9 | 380 //writefln("%s", view_left_bottom); |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
381 mViewCentre = view_left_top + model_size / 2.0; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
382 writefln("onValueChanged mViewCentre: %s", mViewCentre); |
0 | 383 |
13 | 384 update_rulers; |
0 | 385 |
13 | 386 queueDraw; |
0 | 387 } |
388 | |
389 void update_rulers() { | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
390 Vector model_size = pixel_to_model(mViewSize); |
0 | 391 |
3
7d57cae10805
Renamed geometry2 to geometry
David Bryant <daveb@acres.com.au>
parents:
2
diff
changeset
|
392 Point view_left_bottom = mViewCentre - model_size / 2.0; |
7d57cae10805
Renamed geometry2 to geometry
David Bryant <daveb@acres.com.au>
parents:
2
diff
changeset
|
393 Point view_right_top = mViewCentre + model_size / 2.0; |
0 | 394 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
395 // Define these just to obtain the position |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
396 // below and we can preserve it |
12 | 397 double lower, upper, position, max_size; |
398 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
399 |
12 | 400 mHRuler.getRange(lower, upper, position, max_size); |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
401 writefln("HRuler: %f %f %f", view_left_bottom.x, view_right_top.x, position); |
0 | 402 mHRuler.setRange(view_left_bottom.x, |
403 view_right_top.x, | |
12 | 404 position, |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
405 mZoom * 50.0); |
12 | 406 |
407 mVRuler.getRange(lower, upper, position, max_size); | |
0 | 408 mVRuler.setRange(view_right_top.y, |
409 view_left_bottom.y, | |
14 | 410 position, |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
411 mZoom * 50.0); |
0 | 412 } |
413 | |
414 void update_adjustments() { | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
415 Vector model_size = pixel_to_model(mViewSize); |
0 | 416 |
3
7d57cae10805
Renamed geometry2 to geometry
David Bryant <daveb@acres.com.au>
parents:
2
diff
changeset
|
417 Point view_left_bottom = mViewCentre - model_size / 2.0; |
7d57cae10805
Renamed geometry2 to geometry
David Bryant <daveb@acres.com.au>
parents:
2
diff
changeset
|
418 Point view_right_top = mViewCentre + model_size / 2.0; |
0 | 419 |
420 // Adjust the canvas size if necessary | |
15 | 421 mCanvasBounds = Rectangle(min_extents(mCanvasBounds.min_corner, view_left_bottom), |
422 max_extents(mCanvasBounds.max_corner, view_right_top)); | |
0 | 423 |
424 // Update the adjustments | |
425 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
426 GtkAdjustment * h_gtkAdjustment = mHAdjustment.getAdjustmentStruct; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
427 GtkAdjustment * v_gtkAdjustment = mVAdjustment.getAdjustmentStruct; |
0 | 428 |
15 | 429 gtk_adjustment_set_lower(h_gtkAdjustment, mCanvasBounds.min_corner.x); |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
430 gtk_adjustment_set_upper(h_gtkAdjustment, mCanvasBounds.max_corner.x); |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
431 gtk_adjustment_set_value(h_gtkAdjustment, view_left_bottom.x); |
15 | 432 gtk_adjustment_set_step_increment(h_gtkAdjustment, mCanvasBounds.size.x / 16.0); |
433 gtk_adjustment_set_page_increment(h_gtkAdjustment, mCanvasBounds.size.x / 4.0); | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
434 gtk_adjustment_set_page_size(h_gtkAdjustment, model_size.x); |
0 | 435 |
15 | 436 gtk_adjustment_set_lower(v_gtkAdjustment, mCanvasBounds.min_corner.y); |
437 gtk_adjustment_set_upper(v_gtkAdjustment, mCanvasBounds.max_corner.y); | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
438 gtk_adjustment_set_value(v_gtkAdjustment, view_left_bottom.y); |
15 | 439 gtk_adjustment_set_step_increment(v_gtkAdjustment, mCanvasBounds.size.y / 16.0); |
440 gtk_adjustment_set_page_increment(v_gtkAdjustment, mCanvasBounds.size.y / 4.0); | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
441 gtk_adjustment_set_page_size(v_gtkAdjustment, model_size.y); |
0 | 442 |
13 | 443 mHAdjustment.changed; |
444 mHAdjustment.valueChanged; | |
445 mVAdjustment.changed; | |
446 mVAdjustment.valueChanged; | |
0 | 447 } |
448 | |
17 | 449 void process_damage() { |
450 if (mDamage.valid) { | |
18 | 451 //writefln("Damage: %s", mDamage); |
17 | 452 int x, y, w, h; |
453 mDamage.get_quantised(x, y, w, h); | |
18 | 454 //writefln("Quantised damage: %d %d %d %d", x, y, w, h); |
455 y = cast(int)mViewSize.y - (y + h); | |
456 //writefln("Flipped Quantised damage: %d %d %d %d", x, y, w, h); | |
457 // FIXME only redraw the damaged area | |
458 //mDrawingArea.queueDrawArea(x, y, w, h); | |
459 mDrawingArea.queueDraw(); | |
17 | 460 mDamage = Rectangle.DEFAULT; |
461 } | |
462 else { | |
18 | 463 //writefln("No damage"); |
17 | 464 } |
465 } | |
466 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
467 double clamp_zoom(double zoom) { return clamp(zoom, 0.2, 10.0); } |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
468 |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
469 bool mHadConfigure; |
17 | 470 Rectangle mDamage; // pixels |
0 | 471 |
472 // Model units are in millimetres | |
473 // Screen units are in pixels | |
11 | 474 double mZoom; // pixels-per-model-unit (mm) |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
475 Vector mViewSize; // pixel: size of view window in pixels |
3
7d57cae10805
Renamed geometry2 to geometry
David Bryant <daveb@acres.com.au>
parents:
2
diff
changeset
|
476 Point mViewCentre; // model: where in the model is the centre of our view |
15 | 477 Rectangle mCanvasBounds; // model: |
0 | 478 |
10 | 479 // Child widgets: |
0 | 480 HRuler mHRuler; |
481 VRuler mVRuler; | |
482 DrawingArea mDrawingArea; | |
483 Adjustment mHAdjustment; | |
484 HScrollbar mHScrollbar; | |
485 Adjustment mVAdjustment; | |
486 VScrollbar mVScrollbar; | |
11 | 487 |
488 // Layers: | |
489 Layer[] mLayers; | |
13 | 490 EventHandler mEventHandler; |
491 double mPPI; | |
0 | 492 } |
493 } |