Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/TextShape.cs @ 1235

Last change on this file since 1235 was 1234, checked in by mstoeger, 16 years ago

Added Xml comments for IShape, WorldShape and Transforms. (#406)

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