[1182] | 1 | using System;
|
---|
| 2 | using System.Drawing;
|
---|
[1046] | 3 |
|
---|
| 4 | namespace HeuristicLab.Visualization {
|
---|
[1182] | 5 | public enum AnchorPositionX {
|
---|
| 6 | Left, Middle, Right
|
---|
| 7 | }
|
---|
| 8 |
|
---|
| 9 | public enum AnchorPositionY {
|
---|
| 10 | Bottom, Middle, Top
|
---|
| 11 | }
|
---|
| 12 |
|
---|
[1046] | 13 | public class TextShape : IShape {
|
---|
[1240] | 14 | private IShape parent;
|
---|
[1046] | 15 | private Font font;
|
---|
| 16 | private Brush brush;
|
---|
| 17 | private Color color;
|
---|
| 18 | private string text;
|
---|
| 19 | private double x;
|
---|
| 20 | private double y;
|
---|
| 21 |
|
---|
[1182] | 22 | private AnchorPositionX anchorPositionX = AnchorPositionX.Left;
|
---|
| 23 | private AnchorPositionY anchorPositionY = AnchorPositionY.Top;
|
---|
| 24 |
|
---|
[1285] | 25 | public TextShape(string text) : this(0, 0, text, 14) {}
|
---|
| 26 |
|
---|
[1046] | 27 | public TextShape(double x, double y, string text) : this(x, y, text, 8) {}
|
---|
| 28 |
|
---|
| 29 | public TextShape(double x, double y, string text, int fontSize) {
|
---|
| 30 | this.x = x;
|
---|
| 31 | this.y = y;
|
---|
| 32 | this.text = text;
|
---|
| 33 | font = new Font("Arial", fontSize);
|
---|
| 34 |
|
---|
| 35 | Color = Color.Blue;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | public string Text {
|
---|
| 39 | get { return text; }
|
---|
| 40 | set { text = value; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | public double X {
|
---|
| 44 | get { return x; }
|
---|
| 45 | set { x = value; }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public double Y {
|
---|
| 49 | get { return y; }
|
---|
| 50 | set { y = value; }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | public Color Color {
|
---|
| 54 | get { return color; }
|
---|
| 55 | set {
|
---|
| 56 | color = value;
|
---|
| 57 | brush = new SolidBrush(color);
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[1182] | 61 | public AnchorPositionX AnchorPositionX {
|
---|
| 62 | get { return anchorPositionX; }
|
---|
| 63 | set { anchorPositionX = value; }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | public AnchorPositionY AnchorPositionY {
|
---|
| 67 | get { return anchorPositionY; }
|
---|
| 68 | set { anchorPositionY = value; }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[1046] | 71 | #region IShape Members
|
---|
| 72 |
|
---|
[1240] | 73 | public void Draw(Graphics graphics) {
|
---|
| 74 | int screenX = Transform.ToScreenX(x, Parent.Viewport, Parent.ClippingArea);
|
---|
| 75 | int screenY = Transform.ToScreenY(y, Parent.Viewport, Parent.ClippingArea);
|
---|
[1046] | 76 |
|
---|
[1182] | 77 | SizeF size = graphics.MeasureString(text, font);
|
---|
| 78 |
|
---|
| 79 | switch (AnchorPositionX) {
|
---|
| 80 | case AnchorPositionX.Left:
|
---|
| 81 | break;
|
---|
| 82 | case AnchorPositionX.Middle:
|
---|
| 83 | screenX -= (int)(size.Width/2);
|
---|
| 84 | break;
|
---|
| 85 | case AnchorPositionX.Right:
|
---|
| 86 | screenX -= (int)size.Width;
|
---|
| 87 | break;
|
---|
| 88 | default:
|
---|
| 89 | throw new NotSupportedException("Unknown anchor position: " + AnchorPositionX);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | switch (AnchorPositionY) {
|
---|
| 93 | case AnchorPositionY.Top:
|
---|
| 94 | break;
|
---|
| 95 | case AnchorPositionY.Middle:
|
---|
| 96 | screenY -= (int)(size.Height/2);
|
---|
| 97 | break;
|
---|
| 98 | case AnchorPositionY.Bottom:
|
---|
| 99 | screenY -= (int)size.Height;
|
---|
| 100 | break;
|
---|
| 101 | default:
|
---|
| 102 | throw new NotSupportedException("Unknown anchor position: " + AnchorPositionX);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[1046] | 105 | graphics.DrawString(text, font, brush, screenX, screenY);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | public RectangleD BoundingBox {
|
---|
| 109 | get { return RectangleD.Empty; }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[1240] | 112 | public RectangleD ClippingArea {
|
---|
| 113 | get { return Parent.ClippingArea; }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | public Rectangle Viewport {
|
---|
| 117 | get { return Parent.Viewport; }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | public IShape Parent {
|
---|
| 121 | get { return parent; }
|
---|
| 122 | set { parent = value; }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[1046] | 125 | #endregion
|
---|
| 126 | }
|
---|
| 127 | } |
---|