Free cookie consent management tool by TermsFeed Policy Generator

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

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

moved the canvas and the basic types of shapes to their own namespace. #498

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