5 public class Transform
{
7 public double Scale
= 1.0 / 32.0;
8 public short XOffset
= 0;
9 public short YOffset
= 0;
11 public double ToScreenX(short x
) {
12 return (x
- XOffset
) * Scale
;
15 public double ToScreenY(short y
) {
16 return (y
- YOffset
) * Scale
;
19 public short ToMapX(double X
) {
20 return (short) ((double) (X
/ Scale
) + XOffset
);
23 public short ToMapY(double Y
) {
24 return (short) ((double) (Y
/ Scale
) + YOffset
);
27 public PointD
ToScreenPointD(Point p
) {
28 return new PointD(ToScreenX(p
.X
) + 0.5, ToScreenY(p
.Y
) + 0.5);
32 public class MapDrawingArea
: Gtk
.DrawingArea
{
34 public Transform Transform
= new Transform();
36 public short GridResolution
= 1024;
38 public MapDrawingArea() { }
40 Color backgroundColor
= new Color(0.25, 0.25, 0.25);
41 Color pointColor
= new Color(1, 0, 0);
42 Color solidLineColor
= new Color(0, 0, 0);
43 Color transparentLineColor
= new Color(0, 0.75, 0.75);
44 Color polygonColor
= new Color(0.75, 0.75, 0.75);
45 Color gridLineColor
= new Color(0.5, 0.5, 0.5);
46 Color gridPointColor
= new Color(0, 0.75, 0.75);
48 protected override bool OnExposeEvent(Gdk
.EventExpose args
) {
49 Context context
= Gdk
.CairoHelper
.Create(GdkWindow
);
51 context
.Color
= backgroundColor
;
58 foreach (Polygon polygon
in Level
.Polygons
) {
59 DrawPolygon(context
, polygon
);
62 foreach (Line line
in Level
.Lines
) {
63 DrawLine(context
, line
);
66 foreach (Point point
in Level
.Endpoints
) {
67 DrawPoint(context
, point
);
71 ((IDisposable
) context
.Target
).Dispose();
72 ((IDisposable
) context
).Dispose();
77 public void Center(short X
, short Y
) {
78 Transform
.XOffset
= (short) (X
- Allocation
.Width
/ 2 / Transform
.Scale
);
79 Transform
.YOffset
= (short) (Y
- Allocation
.Height
/ 2 / Transform
.Scale
);
82 void DrawPoint(Context context
, Point point
) {
83 context
.MoveTo(Transform
.ToScreenPointD(point
));
85 context
.LineCap
= LineCap
.Round
;
86 context
.Color
= pointColor
;
87 context
.LineWidth
= 2.5;
91 void DrawGrid(Context context
) {
92 Point p1
= new Point();
93 Point p2
= new Point();
95 for (int i
= 0; i
< short.MaxValue
; i
+= GridResolution
) {
96 p1
.X
= short.MinValue
;
98 p2
.X
= short.MaxValue
;
101 context
.MoveTo(Transform
.ToScreenPointD(p1
));
102 context
.LineTo(Transform
.ToScreenPointD(p2
));
107 context
.MoveTo(Transform
.ToScreenPointD(p1
));
108 context
.LineTo(Transform
.ToScreenPointD(p2
));
111 p1
.Y
= short.MinValue
;
113 p2
.Y
= short.MaxValue
;
115 context
.MoveTo(Transform
.ToScreenPointD(p1
));
116 context
.LineTo(Transform
.ToScreenPointD(p2
));
121 context
.MoveTo(Transform
.ToScreenPointD(p1
));
122 context
.LineTo(Transform
.ToScreenPointD(p2
));
125 context
.Color
= gridLineColor
;
126 context
.LineWidth
= 1.0;
129 for (int i
= 0; i
< short.MaxValue
; i
+= 1024) {
130 for (int j
= 0; j
< short.MaxValue
; j
+= 1024) {
134 context
.MoveTo(Transform
.ToScreenPointD(p1
));
139 context
.MoveTo(Transform
.ToScreenPointD(p1
));
144 context
.MoveTo(Transform
.ToScreenPointD(p1
));
149 context
.MoveTo(Transform
.ToScreenPointD(p1
));
154 context
.LineCap
= LineCap
.Round
;
155 context
.Color
= gridPointColor
;
156 context
.LineWidth
= 2.0;
161 void DrawLine(Context context
, Line line
) {
162 Point p1
= Level
.Endpoints
[line
.EndpointIndexes
[0]];
163 Point p2
= Level
.Endpoints
[line
.EndpointIndexes
[1]];
165 context
.MoveTo(Transform
.ToScreenPointD(p1
));
166 context
.LineTo(Transform
.ToScreenPointD(p2
));
167 if (line
.ClockwisePolygonOwner
!= -1 && line
.CounterclockwisePolygonOwner
!= -1) {
168 context
.Color
= transparentLineColor
;
170 context
.Color
= solidLineColor
;
173 context
.LineWidth
= 1.0;
177 void DrawPolygon(Context context
, Polygon polygon
) {
178 Point p
= Level
.Endpoints
[polygon
.EndpointIndexes
[0]];
179 context
.MoveTo(Transform
.ToScreenPointD(p
));
180 for (int i
= 1; i
< polygon
.VertexCount
; ++i
) {
181 context
.LineTo(Transform
.ToScreenPointD(Level
.Endpoints
[polygon
.EndpointIndexes
[i
]]));
184 context
.Color
= polygonColor
;