Mercurial > projects > doodle
annotate canvas.d @ 19:22abbf4cde96
Checkpoint
author | "David Bryant <bagnose@gmail.com>" |
---|---|
date | Mon, 13 Jul 2009 22:08:18 +0930 |
parents | df8d81d9f499 |
children | d6e7a5a6f008 |
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 |
19 | 231 Rectangle pixel_damage = |
11 | 232 event is null ? |
19 | 233 Rectangle(Point(0.0, 0.0), mViewSize) : |
234 Rectangle(Point(cast(double)event.area.x, mViewSize.y - cast(double)(event.area.y + event.area.height)), | |
11 | 235 Vector(cast(double)event.area.width, cast(double)event.area.height)); |
0 | 236 |
19 | 237 Rectangle model_damage = pixel_to_model(pixel_damage); |
238 | |
239 //writefln("Pixel damage: %s, model damage: %s", pixel_damage, model_damage); | |
240 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
241 model_cr.save; pixel_cr.save; { |
19 | 242 // Setup model context and clip |
12 | 243 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
244 GtkAdjustment * h_gtkAdjustment = mHAdjustment.getAdjustmentStruct; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
245 GtkAdjustment * v_gtkAdjustment = mVAdjustment.getAdjustmentStruct; |
12 | 246 |
13 | 247 model_cr.scale(mZoom, -mZoom); |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
248 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
|
249 -gtk_adjustment_get_value(v_gtkAdjustment) - gtk_adjustment_get_page_size(v_gtkAdjustment)); |
0 | 250 |
19 | 251 rectangle(model_cr, model_damage); |
252 model_cr.clip; | |
253 | |
254 // Setup pixel context and clip | |
12 | 255 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
256 pixel_cr.translate(0.0, mViewSize.y); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
257 pixel_cr.scale(1.0, -1.0); |
19 | 258 |
259 rectangle(pixel_cr, pixel_damage); | |
260 pixel_cr.clip; | |
12 | 261 |
262 // Fill the background | |
263 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
264 pixel_cr.save; { |
11 | 265 // Make the window light grey |
18 | 266 pixel_cr.setSourceRgba(0.6, 0.6, 0.6, 1.0); |
19 | 267 rectangle(pixel_cr, pixel_damage); |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
268 pixel_cr.fill; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
269 } pixel_cr.restore; |
12 | 270 |
271 // Draw each layer | |
0 | 272 |
11 | 273 foreach(ref layer; mLayers) { |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
274 model_cr.save; pixel_cr.save; { |
19 | 275 layer.draw(this, pixel_damage, pixel_cr, model_damage, model_cr); |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
276 } pixel_cr.restore; model_cr.restore; |
11 | 277 } |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
278 } pixel_cr.restore; model_cr.restore; |
0 | 279 |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
280 return true; |
0 | 281 } |
282 | |
5 | 283 bool onButtonPress(GdkEventButton * event, Widget widget) { |
0 | 284 assert(widget is mDrawingArea); |
285 //writefln("Got button event\n"); | |
286 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
287 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
|
288 Point model_point = pixel_to_model(pixel_point); |
0 | 289 |
7 | 290 auto button_event = new ButtonEvent(gtk2tk_button_action(event.type), |
291 gtk2tk_button_name(event.button), | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
292 pixel_point, |
0 | 293 model_point, |
294 gtk2tk_mask(event.state)); | |
295 | |
5 | 296 mEventHandler.handle_button_press(this, button_event); |
297 | |
17 | 298 process_damage; |
299 | |
5 | 300 return true; |
301 } | |
302 | |
303 bool onButtonRelease(GdkEventButton * event, Widget widget) { | |
304 assert(widget is mDrawingArea); | |
305 //writefln("Got button event\n"); | |
306 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
307 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
|
308 Point model_point = pixel_to_model(pixel_point); |
5 | 309 |
7 | 310 auto button_event = new ButtonEvent(gtk2tk_button_action(event.type), |
311 gtk2tk_button_name(event.button), | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
312 pixel_point, |
5 | 313 model_point, |
314 gtk2tk_mask(event.state)); | |
315 | |
316 mEventHandler.handle_button_release(this, button_event); | |
0 | 317 |
17 | 318 process_damage; |
319 | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
320 return true; |
0 | 321 } |
322 | |
323 bool onKeyEvent(GdkEventKey * event, Widget widget) { | |
324 assert(widget is mDrawingArea); | |
325 //writefln("Got key event\n"); | |
326 | |
327 //auto key_event = new KeyEvent("", | |
328 // mEventHandle.handle_key(key_event); | |
329 | |
17 | 330 process_damage; |
331 | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
332 return true; |
0 | 333 } |
334 | |
335 bool onMotionNotify(GdkEventMotion * event, Widget widget) { | |
336 assert(widget is mDrawingArea); | |
337 //writefln("Got motion notify\n"); | |
338 gtk_widget_event(mHRuler.getWidgetStruct(), cast(GdkEvent *)event); | |
339 gtk_widget_event(mVRuler.getWidgetStruct(), cast(GdkEvent *)event); | |
340 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
341 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
|
342 Point model_point = pixel_to_model(pixel_point); |
0 | 343 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
344 auto motion_event = new MotionEvent(pixel_point, |
0 | 345 model_point, |
346 gtk2tk_mask(event.state)); | |
347 | |
348 mEventHandler.handle_motion(this, motion_event); | |
349 | |
17 | 350 process_damage; |
351 | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
352 return true; |
0 | 353 } |
354 | |
355 bool onScroll(GdkEventScroll * event, Widget widget) { | |
356 assert(widget is mDrawingArea); | |
357 //writefln("Got scroll\n"); | |
358 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
359 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
|
360 Point model_point = pixel_to_model(pixel_point); |
0 | 361 |
362 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
|
363 pixel_point, |
0 | 364 model_point, |
365 gtk2tk_mask(event.state)); | |
366 | |
367 mEventHandler.handle_scroll(this, scroll_event); | |
368 | |
17 | 369 process_damage; |
370 | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
0
diff
changeset
|
371 return true; |
0 | 372 } |
373 | |
374 void onValueChanged(Adjustment adjustment) { | |
13 | 375 GtkAdjustment * h_gtkAdjustment = mHAdjustment.getAdjustmentStruct; |
376 GtkAdjustment * v_gtkAdjustment = mVAdjustment.getAdjustmentStruct; | |
0 | 377 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
378 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
|
379 gtk_adjustment_get_value(v_gtkAdjustment)); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
380 |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
381 Vector model_size = pixel_to_model(mViewSize); |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
382 |
9 | 383 //writefln("%s", view_left_bottom); |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
384 mViewCentre = view_left_top + model_size / 2.0; |
19 | 385 //writefln("onValueChanged mViewCentre: %s", mViewCentre); |
0 | 386 |
13 | 387 update_rulers; |
0 | 388 |
13 | 389 queueDraw; |
0 | 390 } |
391 | |
392 void update_rulers() { | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
393 Vector model_size = pixel_to_model(mViewSize); |
0 | 394 |
3
7d57cae10805
Renamed geometry2 to geometry
David Bryant <daveb@acres.com.au>
parents:
2
diff
changeset
|
395 Point view_left_bottom = mViewCentre - model_size / 2.0; |
7d57cae10805
Renamed geometry2 to geometry
David Bryant <daveb@acres.com.au>
parents:
2
diff
changeset
|
396 Point view_right_top = mViewCentre + model_size / 2.0; |
0 | 397 |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
398 // Define these just to obtain the position |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
399 // below and we can preserve it |
12 | 400 double lower, upper, position, max_size; |
401 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
402 |
12 | 403 mHRuler.getRange(lower, upper, position, max_size); |
0 | 404 mHRuler.setRange(view_left_bottom.x, |
405 view_right_top.x, | |
12 | 406 position, |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
407 mZoom * 50.0); |
12 | 408 |
409 mVRuler.getRange(lower, upper, position, max_size); | |
0 | 410 mVRuler.setRange(view_right_top.y, |
411 view_left_bottom.y, | |
14 | 412 position, |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
413 mZoom * 50.0); |
0 | 414 } |
415 | |
416 void update_adjustments() { | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
417 Vector model_size = pixel_to_model(mViewSize); |
0 | 418 |
3
7d57cae10805
Renamed geometry2 to geometry
David Bryant <daveb@acres.com.au>
parents:
2
diff
changeset
|
419 Point view_left_bottom = mViewCentre - model_size / 2.0; |
7d57cae10805
Renamed geometry2 to geometry
David Bryant <daveb@acres.com.au>
parents:
2
diff
changeset
|
420 Point view_right_top = mViewCentre + model_size / 2.0; |
0 | 421 |
422 // Adjust the canvas size if necessary | |
15 | 423 mCanvasBounds = Rectangle(min_extents(mCanvasBounds.min_corner, view_left_bottom), |
424 max_extents(mCanvasBounds.max_corner, view_right_top)); | |
0 | 425 |
426 // Update the adjustments | |
427 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
428 GtkAdjustment * h_gtkAdjustment = mHAdjustment.getAdjustmentStruct; |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
429 GtkAdjustment * v_gtkAdjustment = mVAdjustment.getAdjustmentStruct; |
0 | 430 |
15 | 431 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
|
432 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
|
433 gtk_adjustment_set_value(h_gtkAdjustment, view_left_bottom.x); |
15 | 434 gtk_adjustment_set_step_increment(h_gtkAdjustment, mCanvasBounds.size.x / 16.0); |
435 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
|
436 gtk_adjustment_set_page_size(h_gtkAdjustment, model_size.x); |
0 | 437 |
15 | 438 gtk_adjustment_set_lower(v_gtkAdjustment, mCanvasBounds.min_corner.y); |
439 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
|
440 gtk_adjustment_set_value(v_gtkAdjustment, view_left_bottom.y); |
15 | 441 gtk_adjustment_set_step_increment(v_gtkAdjustment, mCanvasBounds.size.y / 16.0); |
442 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
|
443 gtk_adjustment_set_page_size(v_gtkAdjustment, model_size.y); |
0 | 444 |
13 | 445 mHAdjustment.changed; |
446 mHAdjustment.valueChanged; | |
447 mVAdjustment.changed; | |
448 mVAdjustment.valueChanged; | |
0 | 449 } |
450 | |
17 | 451 void process_damage() { |
452 if (mDamage.valid) { | |
18 | 453 //writefln("Damage: %s", mDamage); |
17 | 454 int x, y, w, h; |
455 mDamage.get_quantised(x, y, w, h); | |
18 | 456 //writefln("Quantised damage: %d %d %d %d", x, y, w, h); |
457 y = cast(int)mViewSize.y - (y + h); | |
458 //writefln("Flipped Quantised damage: %d %d %d %d", x, y, w, h); | |
19 | 459 mDrawingArea.queueDrawArea(x, y, w, h); |
460 //mDrawingArea.queueDraw(); | |
17 | 461 mDamage = Rectangle.DEFAULT; |
462 } | |
463 else { | |
18 | 464 //writefln("No damage"); |
17 | 465 } |
466 } | |
467 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
468 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
|
469 |
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
470 bool mHadConfigure; |
17 | 471 Rectangle mDamage; // pixels |
0 | 472 |
473 // Model units are in millimetres | |
474 // Screen units are in pixels | |
11 | 475 double mZoom; // pixels-per-model-unit (mm) |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
15
diff
changeset
|
476 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
|
477 Point mViewCentre; // model: where in the model is the centre of our view |
15 | 478 Rectangle mCanvasBounds; // model: |
0 | 479 |
10 | 480 // Child widgets: |
0 | 481 HRuler mHRuler; |
482 VRuler mVRuler; | |
483 DrawingArea mDrawingArea; | |
484 Adjustment mHAdjustment; | |
485 HScrollbar mHScrollbar; | |
486 Adjustment mVAdjustment; | |
487 VScrollbar mVScrollbar; | |
11 | 488 |
489 // Layers: | |
490 Layer[] mLayers; | |
13 | 491 EventHandler mEventHandler; |
492 double mPPI; | |
0 | 493 } |
494 } |