Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1240 was 1240, checked in by mstoeger, 15 years ago

Transformations on shapes are possible outside of the Draw method. (#424)

File size: 3.0 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 IShape parent;
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
22    private AnchorPositionX anchorPositionX = AnchorPositionX.Left;
23    private AnchorPositionY anchorPositionY = AnchorPositionY.Top;
24
25    public TextShape(double x, double y, string text) : this(x, y, text, 8) {}
26
27    public TextShape(double x, double y, string text, int fontSize) {
28      this.x = x;
29      this.y = y;
30      this.text = text;
31      font = new Font("Arial", fontSize);
32
33      Color = Color.Blue;
34    }
35
36    public string Text {
37      get { return text; }
38      set { text = value; }
39    }
40
41    public double X {
42      get { return x; }
43      set { x = value; }
44    }
45
46    public double Y {
47      get { return y; }
48      set { y = value; }
49    }
50
51    public Color Color {
52      get { return color; }
53      set {
54        color = value;
55        brush = new SolidBrush(color);
56      }
57    }
58
59    public AnchorPositionX AnchorPositionX {
60      get { return anchorPositionX; }
61      set { anchorPositionX = value; }
62    }
63
64    public AnchorPositionY AnchorPositionY {
65      get { return anchorPositionY; }
66      set { anchorPositionY = value; }
67    }
68
69    #region IShape Members
70
71    public void Draw(Graphics graphics) {
72      int screenX = Transform.ToScreenX(x, Parent.Viewport, Parent.ClippingArea);
73      int screenY = Transform.ToScreenY(y, Parent.Viewport, Parent.ClippingArea);
74
75      SizeF size = graphics.MeasureString(text, font);
76
77      switch (AnchorPositionX) {
78        case AnchorPositionX.Left:
79          break;
80        case AnchorPositionX.Middle:
81          screenX -= (int)(size.Width/2);
82          break;
83        case AnchorPositionX.Right:
84          screenX -= (int)size.Width;
85          break;
86        default:
87          throw new NotSupportedException("Unknown anchor position: " + AnchorPositionX);
88      }
89
90      switch (AnchorPositionY) {
91        case AnchorPositionY.Top:
92          break;
93        case AnchorPositionY.Middle:
94          screenY -= (int)(size.Height/2);
95          break;
96        case AnchorPositionY.Bottom:
97          screenY -= (int)size.Height;
98          break;
99        default:
100          throw new NotSupportedException("Unknown anchor position: " + AnchorPositionX);
101      }
102
103      graphics.DrawString(text, font, brush, screenX, screenY);
104    }
105
106    public RectangleD BoundingBox {
107      get { return RectangleD.Empty; }
108    }
109
110    public RectangleD ClippingArea {
111      get { return Parent.ClippingArea; }
112    }
113
114    public Rectangle Viewport {
115      get { return Parent.Viewport; }
116    }
117
118    public IShape Parent {
119      get { return parent; }
120      set { parent = value; }
121    }
122
123    #endregion
124  }
125}
Note: See TracBrowser for help on using the repository browser.