Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 3.8 KB
RevLine 
[1182]1using System;
2using System.Drawing;
[1462]3using System.Drawing.Drawing2D;
[1046]4
5namespace HeuristicLab.Visualization {
[1182]6  public enum AnchorPositionX {
7    Left, Middle, Right
8  }
9
10  public enum AnchorPositionY {
11    Bottom, Middle, Top
12  }
13
[1046]14  public class TextShape : IShape {
[1240]15    private IShape parent;
[1046]16    private Font font;
17    private Brush brush;
18    private Color color;
19    private string text;
20    private double x;
21    private double y;
[1462]22    private float rotation = 0;
[1046]23
[1182]24    private AnchorPositionX anchorPositionX = AnchorPositionX.Left;
25    private AnchorPositionY anchorPositionY = AnchorPositionY.Top;
26
[1285]27    public TextShape(string text) : this(0, 0, text, 14) {}
28
[1046]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
[1337]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
[1046]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
[1462]63    public float Rotation {
64      get { return rotation; }
65      set { rotation = value; }
66    }
67
[1046]68    public Color Color {
69      get { return color; }
70      set {
71        color = value;
72        brush = new SolidBrush(color);
73      }
74    }
75
[1328]76    public Font Font {
77      get { return font; }
78      set { font = value; }
79    }
80
[1182]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
[1046]91    #region IShape Members
92
[1240]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);
[1462]96      int offsetX, offsetY;
[1046]97
[1182]98      SizeF size = graphics.MeasureString(text, font);
99
100      switch (AnchorPositionX) {
101        case AnchorPositionX.Left:
[1462]102          offsetX = 0;
[1182]103          break;
104        case AnchorPositionX.Middle:
[1462]105          offsetX = -(int)(size.Width/2);
[1182]106          break;
107        case AnchorPositionX.Right:
[1462]108          offsetX = -(int)size.Width;
[1182]109          break;
110        default:
111          throw new NotSupportedException("Unknown anchor position: " + AnchorPositionX);
112      }
113
114      switch (AnchorPositionY) {
115        case AnchorPositionY.Top:
[1462]116          offsetY = 0;
[1182]117          break;
118        case AnchorPositionY.Middle:
[1462]119          offsetY = -(int)(size.Height/2);
[1182]120          break;
121        case AnchorPositionY.Bottom:
[1462]122          offsetY = -(int)size.Height;
[1182]123          break;
124        default:
125          throw new NotSupportedException("Unknown anchor position: " + AnchorPositionX);
126      }
127
[1462]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);
[1046]133    }
134
135    public RectangleD BoundingBox {
136      get { return RectangleD.Empty; }
137    }
138
[1240]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
[1046]152    #endregion
153  }
154}
Note: See TracBrowser for help on using the repository browser.