Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/3.2/TextShape.cs @ 1530

Last change on this file since 1530 was 1530, checked in by gkronber, 15 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

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