[1240] | 1 | using System;
|
---|
[1038] | 2 | using System.Diagnostics;
|
---|
[635] | 3 | using System.Drawing;
|
---|
| 4 | using System.Drawing.Drawing2D;
|
---|
| 5 |
|
---|
| 6 | namespace HeuristicLab.Visualization {
|
---|
[1240] | 7 | public class Canvas : IShape {
|
---|
| 8 | private readonly WorldShape worldShape;
|
---|
[635] | 9 |
|
---|
[1240] | 10 | private Rectangle viewport;
|
---|
| 11 |
|
---|
| 12 | public Canvas() {
|
---|
| 13 | worldShape = new WorldShape();
|
---|
| 14 | worldShape.Parent = this;
|
---|
[635] | 15 | }
|
---|
| 16 |
|
---|
[1240] | 17 | public void AddShape(IShape shape) {
|
---|
| 18 | worldShape.AddShape(shape);
|
---|
| 19 | }
|
---|
[1038] | 20 |
|
---|
[1240] | 21 | public void RemoveShape(IShape shape) {
|
---|
| 22 | worldShape.RemoveShape(shape);
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | public void ClearShapes() {
|
---|
| 26 | worldShape.ClearShapes();
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | public Rectangle Viewport {
|
---|
| 30 | get { return viewport; }
|
---|
| 31 | set {
|
---|
| 32 | viewport = value;
|
---|
| 33 | worldShape.ClippingArea = new RectangleD(0, 0, viewport.Width, viewport.Height);
|
---|
| 34 | worldShape.BoundingBox = worldShape.ClippingArea;
|
---|
| 35 | }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | public RectangleD ClippingArea {
|
---|
| 39 | get { return worldShape.ClippingArea; }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | public RectangleD BoundingBox {
|
---|
| 43 | get { throw new InvalidOperationException(); }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public IShape Parent {
|
---|
| 47 | get { throw new InvalidOperationException(); }
|
---|
| 48 | set { throw new InvalidOperationException(); }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public void Draw(Graphics graphics) {
|
---|
[1606] | 52 | try {
|
---|
| 53 | Stopwatch sw = new Stopwatch();
|
---|
| 54 | sw.Start();
|
---|
[1038] | 55 |
|
---|
[1606] | 56 | graphics.SmoothingMode = SmoothingMode.AntiAlias;
|
---|
| 57 | graphics.FillRectangle(Brushes.White, Viewport);
|
---|
[635] | 58 |
|
---|
[1606] | 59 | worldShape.Draw(graphics);
|
---|
[635] | 60 |
|
---|
[1606] | 61 | graphics.DrawRectangle(Pens.Black, 0, 0, Viewport.Width - 1, Viewport.Height - 1);
|
---|
[635] | 62 |
|
---|
[1606] | 63 | sw.Stop();
|
---|
| 64 | Trace.WriteLine(string.Format("Drawing time: {0:0.0}ms", sw.Elapsed.TotalMilliseconds));
|
---|
| 65 | } catch (OverflowException e) {
|
---|
| 66 | Trace.WriteLine(e);
|
---|
| 67 |
|
---|
| 68 | graphics.FillRectangle(Brushes.White, graphics.ClipBounds);
|
---|
| 69 |
|
---|
| 70 | using (Font font = new Font("Arial", 14)) {
|
---|
| 71 | const string message = "Zoom level is too high!";
|
---|
| 72 | graphics.DrawString(message, font, Brushes.Red, graphics.ClipBounds.X + 10, graphics.ClipBounds.Y + 10);
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
[635] | 75 | }
|
---|
| 76 | }
|
---|
| 77 | } |
---|