[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 |
|
---|
[1337] | 38 | public TextShape(double x, double y, string text, Font font, Color color) {
|
---|
| 39 | this.x = x;
|
---|
| 40 | this.y = y;
|
---|
| 41 | this.text = text;
|
---|
| 42 | Font = font;
|
---|
| 43 | Color = color;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[1046] | 46 | public string Text {
|
---|
| 47 | get { return text; }
|
---|
| 48 | set { text = value; }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public double X {
|
---|
| 52 | get { return x; }
|
---|
| 53 | set { x = value; }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | public double Y {
|
---|
| 57 | get { return y; }
|
---|
| 58 | set { y = value; }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | public Color Color {
|
---|
| 62 | get { return color; }
|
---|
| 63 | set {
|
---|
| 64 | color = value;
|
---|
| 65 | brush = new SolidBrush(color);
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[1328] | 69 | public Font Font {
|
---|
| 70 | get { return font; }
|
---|
| 71 | set { font = value; }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[1182] | 74 | public AnchorPositionX AnchorPositionX {
|
---|
| 75 | get { return anchorPositionX; }
|
---|
| 76 | set { anchorPositionX = value; }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | public AnchorPositionY AnchorPositionY {
|
---|
| 80 | get { return anchorPositionY; }
|
---|
| 81 | set { anchorPositionY = value; }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[1046] | 84 | #region IShape Members
|
---|
| 85 |
|
---|
[1240] | 86 | public void Draw(Graphics graphics) {
|
---|
| 87 | int screenX = Transform.ToScreenX(x, Parent.Viewport, Parent.ClippingArea);
|
---|
| 88 | int screenY = Transform.ToScreenY(y, Parent.Viewport, Parent.ClippingArea);
|
---|
[1046] | 89 |
|
---|
[1182] | 90 | SizeF size = graphics.MeasureString(text, font);
|
---|
| 91 |
|
---|
| 92 | switch (AnchorPositionX) {
|
---|
| 93 | case AnchorPositionX.Left:
|
---|
| 94 | break;
|
---|
| 95 | case AnchorPositionX.Middle:
|
---|
| 96 | screenX -= (int)(size.Width/2);
|
---|
| 97 | break;
|
---|
| 98 | case AnchorPositionX.Right:
|
---|
| 99 | screenX -= (int)size.Width;
|
---|
| 100 | break;
|
---|
| 101 | default:
|
---|
| 102 | throw new NotSupportedException("Unknown anchor position: " + AnchorPositionX);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | switch (AnchorPositionY) {
|
---|
| 106 | case AnchorPositionY.Top:
|
---|
| 107 | break;
|
---|
| 108 | case AnchorPositionY.Middle:
|
---|
| 109 | screenY -= (int)(size.Height/2);
|
---|
| 110 | break;
|
---|
| 111 | case AnchorPositionY.Bottom:
|
---|
| 112 | screenY -= (int)size.Height;
|
---|
| 113 | break;
|
---|
| 114 | default:
|
---|
| 115 | throw new NotSupportedException("Unknown anchor position: " + AnchorPositionX);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[1046] | 118 | graphics.DrawString(text, font, brush, screenX, screenY);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | public RectangleD BoundingBox {
|
---|
| 122 | get { return RectangleD.Empty; }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[1240] | 125 | public RectangleD ClippingArea {
|
---|
| 126 | get { return Parent.ClippingArea; }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | public Rectangle Viewport {
|
---|
| 130 | get { return Parent.Viewport; }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | public IShape Parent {
|
---|
| 134 | get { return parent; }
|
---|
| 135 | set { parent = value; }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[1046] | 138 | #endregion
|
---|
| 139 | }
|
---|
| 140 | } |
---|