annotate doodle/tk/screen_model.d @ 81:d92b9f04b1e8

Bed time
author "David Bryant <bagnose@gmail.com>"
date Mon, 16 Aug 2010 00:04:27 +0930
parents doodle/tk/pixel_model.d@535bae7a7305
children 100dd23c7bdf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
77
15ca7d5cd1ed The rulers are broken
"David Bryant <bagnose@gmail.com>"
parents: 72
diff changeset
1 module doodle.tk.screen_model;
70
0e61702c6ea6 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
2
0e61702c6ea6 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
3 public {
0e61702c6ea6 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
4 import doodle.tk.geometry;
0e61702c6ea6 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
5 }
0e61702c6ea6 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
6
71
0f7cf6c6f206 Reimplemented gtk.canvas in terms of tk.pixel_model but
"David Bryant <bagnose@gmail.com>"
parents: 70
diff changeset
7 private {
0f7cf6c6f206 Reimplemented gtk.canvas in terms of tk.pixel_model but
"David Bryant <bagnose@gmail.com>"
parents: 70
diff changeset
8 import doodle.core.misc;
0f7cf6c6f206 Reimplemented gtk.canvas in terms of tk.pixel_model but
"David Bryant <bagnose@gmail.com>"
parents: 70
diff changeset
9 }
0f7cf6c6f206 Reimplemented gtk.canvas in terms of tk.pixel_model but
"David Bryant <bagnose@gmail.com>"
parents: 70
diff changeset
10
77
15ca7d5cd1ed The rulers are broken
"David Bryant <bagnose@gmail.com>"
parents: 72
diff changeset
11 // This class manages the relationship between screen space and model space.
78
"David Bryant <bagnose@gmail.com>"
parents: 77
diff changeset
12 // Screen is defined as the current window/viewport into the model
71
0f7cf6c6f206 Reimplemented gtk.canvas in terms of tk.pixel_model but
"David Bryant <bagnose@gmail.com>"
parents: 70
diff changeset
13 // It provides convenient high-level operations.
0f7cf6c6f206 Reimplemented gtk.canvas in terms of tk.pixel_model but
"David Bryant <bagnose@gmail.com>"
parents: 70
diff changeset
14 //
77
15ca7d5cd1ed The rulers are broken
"David Bryant <bagnose@gmail.com>"
parents: 72
diff changeset
15 // x and y run right and up respectively for screen and model space
71
0f7cf6c6f206 Reimplemented gtk.canvas in terms of tk.pixel_model but
"David Bryant <bagnose@gmail.com>"
parents: 70
diff changeset
16
77
15ca7d5cd1ed The rulers are broken
"David Bryant <bagnose@gmail.com>"
parents: 72
diff changeset
17 class ScreenModel {
15ca7d5cd1ed The rulers are broken
"David Bryant <bagnose@gmail.com>"
parents: 72
diff changeset
18 this(in double zoom, in Rectangle canvasBoundsModel, in Rectangle viewBoundsScreen) {
70
0e61702c6ea6 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
19 _zoom = zoom;
77
15ca7d5cd1ed The rulers are broken
"David Bryant <bagnose@gmail.com>"
parents: 72
diff changeset
20 _viewBoundsScreen = viewBoundsScreen;
15ca7d5cd1ed The rulers are broken
"David Bryant <bagnose@gmail.com>"
parents: 72
diff changeset
21 _canvasBoundsModel = canvasBoundsModel;
70
0e61702c6ea6 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
22
71
0f7cf6c6f206 Reimplemented gtk.canvas in terms of tk.pixel_model but
"David Bryant <bagnose@gmail.com>"
parents: 70
diff changeset
23 // Choose the centre of the canvas as the centre of the view
72
5cc2de64f6d0 Cautious Saturday night commits
"David Bryant <bagnose@gmail.com>"
parents: 71
diff changeset
24 _viewCentreModel = _canvasBoundsModel.centre;
70
0e61702c6ea6 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
25 }
0e61702c6ea6 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
26
79
535bae7a7305 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 78
diff changeset
27 void consolidateCanvasBounds(in Rectangle requiredCanvasBounds) { _canvasBoundsModel = screenToModel(_viewBoundsScreen) | requiredCanvasBounds; }
535bae7a7305 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 78
diff changeset
28 void canvasAccommodate(in Rectangle bounds) { _canvasBoundsModel = _canvasBoundsModel | bounds; }
71
0f7cf6c6f206 Reimplemented gtk.canvas in terms of tk.pixel_model but
"David Bryant <bagnose@gmail.com>"
parents: 70
diff changeset
29
77
15ca7d5cd1ed The rulers are broken
"David Bryant <bagnose@gmail.com>"
parents: 72
diff changeset
30 void zoomRelative(in double factor, in Point screenDatum) {
15ca7d5cd1ed The rulers are broken
"David Bryant <bagnose@gmail.com>"
parents: 72
diff changeset
31 // Work out screen distance from current centre to datum,
71
0f7cf6c6f206 Reimplemented gtk.canvas in terms of tk.pixel_model but
"David Bryant <bagnose@gmail.com>"
parents: 70
diff changeset
32 // Do the zoom, then work out the new centre that keeps the
77
15ca7d5cd1ed The rulers are broken
"David Bryant <bagnose@gmail.com>"
parents: 72
diff changeset
33 // screen distance the same
71
0f7cf6c6f206 Reimplemented gtk.canvas in terms of tk.pixel_model but
"David Bryant <bagnose@gmail.com>"
parents: 70
diff changeset
34
77
15ca7d5cd1ed The rulers are broken
"David Bryant <bagnose@gmail.com>"
parents: 72
diff changeset
35 Point oldModelDatum = screenToModel(screenDatum);
15ca7d5cd1ed The rulers are broken
"David Bryant <bagnose@gmail.com>"
parents: 72
diff changeset
36 Vector screenDistance = modelToScreen(oldModelDatum - _viewCentreModel);
71
0f7cf6c6f206 Reimplemented gtk.canvas in terms of tk.pixel_model but
"David Bryant <bagnose@gmail.com>"
parents: 70
diff changeset
37 _zoom = clampZoom(zoom * factor);
77
15ca7d5cd1ed The rulers are broken
"David Bryant <bagnose@gmail.com>"
parents: 72
diff changeset
38 _viewCentreModel = oldModelDatum - screenToModel(screenDistance);
71
0f7cf6c6f206 Reimplemented gtk.canvas in terms of tk.pixel_model but
"David Bryant <bagnose@gmail.com>"
parents: 70
diff changeset
39 }
0f7cf6c6f206 Reimplemented gtk.canvas in terms of tk.pixel_model but
"David Bryant <bagnose@gmail.com>"
parents: 70
diff changeset
40
77
15ca7d5cd1ed The rulers are broken
"David Bryant <bagnose@gmail.com>"
parents: 72
diff changeset
41 void panRelativeScreen(in Vector screenDisplacement) { _viewCentreModel = _viewCentreModel + screenToModel(screenDisplacement); }
72
5cc2de64f6d0 Cautious Saturday night commits
"David Bryant <bagnose@gmail.com>"
parents: 71
diff changeset
42 void panRelativeModel(in Vector modelDisplacement) { _viewCentreModel = _viewCentreModel + modelDisplacement; }
71
0f7cf6c6f206 Reimplemented gtk.canvas in terms of tk.pixel_model but
"David Bryant <bagnose@gmail.com>"
parents: 70
diff changeset
43
79
535bae7a7305 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 78
diff changeset
44 // For userZoom 1.0 -> 100% means the presentation on the screen is one-to-one with real-life
535bae7a7305 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 78
diff changeset
45 double userZoom(in double pixelsPerMillimetre) const { return _zoom / pixelsPerMillimetre; }
71
0f7cf6c6f206 Reimplemented gtk.canvas in terms of tk.pixel_model but
"David Bryant <bagnose@gmail.com>"
parents: 70
diff changeset
46 double zoom() const { return _zoom; }
77
15ca7d5cd1ed The rulers are broken
"David Bryant <bagnose@gmail.com>"
parents: 72
diff changeset
47 Rectangle viewBoundsScreen() const { return _viewBoundsScreen; }
79
535bae7a7305 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 78
diff changeset
48 Rectangle viewBoundsModel() const { return screenToModel(_viewBoundsScreen); }
77
15ca7d5cd1ed The rulers are broken
"David Bryant <bagnose@gmail.com>"
parents: 72
diff changeset
49 Rectangle canvasBoundsModel() const { return _canvasBoundsModel; }
79
535bae7a7305 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 78
diff changeset
50 Rectangle canvasBoundsScreen() const { return modelToScreen(_canvasBoundsModel); }
70
0e61702c6ea6 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
51
77
15ca7d5cd1ed The rulers are broken
"David Bryant <bagnose@gmail.com>"
parents: 72
diff changeset
52 Point modelToScreen(in Point model) const { return _viewBoundsScreen.centre + _zoom * (model - _viewCentreModel); }
15ca7d5cd1ed The rulers are broken
"David Bryant <bagnose@gmail.com>"
parents: 72
diff changeset
53 Point screenToModel(in Point screen) const { return _viewCentreModel + (screen - _viewBoundsScreen.centre) / _zoom; }
15ca7d5cd1ed The rulers are broken
"David Bryant <bagnose@gmail.com>"
parents: 72
diff changeset
54 Vector modelToScreen(in Vector model) const { return _zoom * model; }
15ca7d5cd1ed The rulers are broken
"David Bryant <bagnose@gmail.com>"
parents: 72
diff changeset
55 Vector screenToModel(in Vector screen) const { return screen / _zoom; }
15ca7d5cd1ed The rulers are broken
"David Bryant <bagnose@gmail.com>"
parents: 72
diff changeset
56 Rectangle modelToScreen(in Rectangle model) const { return Rectangle(modelToScreen(model.position), modelToScreen(model.size)); }
15ca7d5cd1ed The rulers are broken
"David Bryant <bagnose@gmail.com>"
parents: 72
diff changeset
57 Rectangle screenToModel(in Rectangle model) const { return Rectangle(screenToModel(model.position), screenToModel(model.size)); }
70
0e61702c6ea6 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
58
0e61702c6ea6 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
59 private {
79
535bae7a7305 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 78
diff changeset
60 static double clampZoom(in double zoom) { return clamp(zoom, 1e-1, 1e2); }
71
0f7cf6c6f206 Reimplemented gtk.canvas in terms of tk.pixel_model but
"David Bryant <bagnose@gmail.com>"
parents: 70
diff changeset
61
78
"David Bryant <bagnose@gmail.com>"
parents: 77
diff changeset
62 // Screen units are pixels
70
0e61702c6ea6 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
63 // Model units are millimetres
78
"David Bryant <bagnose@gmail.com>"
parents: 77
diff changeset
64 double _zoom; // pixels-per-millimetre
"David Bryant <bagnose@gmail.com>"
parents: 77
diff changeset
65 Rectangle _viewBoundsScreen; // bounds of the viewport in screen space
"David Bryant <bagnose@gmail.com>"
parents: 77
diff changeset
66 Point _viewCentreModel; // where in the model is the centre of our screen
"David Bryant <bagnose@gmail.com>"
parents: 77
diff changeset
67 Rectangle _canvasBoundsModel; // the bounds of the canvas in model space
70
0e61702c6ea6 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
68 }
0e61702c6ea6 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
69 }