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