add MapWindow.cs; don't show resources in levels menu
[weland.git] / MapDrawingArea.cs
blob954ee139eb5d1063e5ec5323ab2d6d45910e1cc5
1 using System;
2 using Cairo;
4 namespace Weland {
5 public class Transform {
6 public 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();
35 public Level Level;
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;
52 context.Paint();
54 DrawGrid(context);
56 if (Level != null) {
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();
74 return true;
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));
84 context.ClosePath();
85 context.LineCap = LineCap.Round;
86 context.Color = pointColor;
87 context.LineWidth = 2.5;
88 context.Stroke();
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;
97 p1.Y = (short) i;
98 p2.X = short.MaxValue;
99 p2.Y = (short) i;
101 context.MoveTo(Transform.ToScreenPointD(p1));
102 context.LineTo(Transform.ToScreenPointD(p2));
104 p1.Y = (short) -i;
105 p2.Y = (short) -i;
107 context.MoveTo(Transform.ToScreenPointD(p1));
108 context.LineTo(Transform.ToScreenPointD(p2));
110 p1.X = (short) i;
111 p1.Y = short.MinValue;
112 p2.X = (short) i;
113 p2.Y = short.MaxValue;
115 context.MoveTo(Transform.ToScreenPointD(p1));
116 context.LineTo(Transform.ToScreenPointD(p2));
118 p1.X = (short) -i;
119 p2.X = (short) -i;
121 context.MoveTo(Transform.ToScreenPointD(p1));
122 context.LineTo(Transform.ToScreenPointD(p2));
125 context.Color = gridLineColor;
126 context.LineWidth = 1.0;
127 context.Stroke();
129 for (int i = 0; i < short.MaxValue; i += 1024) {
130 for (int j = 0; j < short.MaxValue; j += 1024) {
131 p1.X = (short) i;
132 p1.Y = (short) j;
134 context.MoveTo(Transform.ToScreenPointD(p1));
135 context.ClosePath();
137 p1.X = (short) -i;
139 context.MoveTo(Transform.ToScreenPointD(p1));
140 context.ClosePath();
142 p1.Y = (short) -j;
144 context.MoveTo(Transform.ToScreenPointD(p1));
145 context.ClosePath();
147 p1.X = (short) i;
149 context.MoveTo(Transform.ToScreenPointD(p1));
150 context.ClosePath();
154 context.LineCap = LineCap.Round;
155 context.Color = gridPointColor;
156 context.LineWidth = 2.0;
157 context.Stroke();
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;
169 } else {
170 context.Color = solidLineColor;
173 context.LineWidth = 1.0;
174 context.Stroke();
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;
185 context.ClosePath();
186 context.Fill();