-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapRenderer.cs
288 lines (254 loc) · 10 KB
/
MapRenderer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace GPSTracker
{
public class Map
{
private List<MapPoint> MapPoints { get; set; }
private Bitmap _staticMap;
private float Width { get; set; }
private float Height { get; set; }
public Map(List<MapPoint> points, float width = 255f, float height = 255f)
{
MapPoints = points;
Width = width;
Height = height;
}
public void InitStaticMap(int mapWidth, int mapHeight, int stepGrid = 15, float scale = 0.5f)
{
_staticMap = new Bitmap(width : mapWidth, height : mapHeight);
using ( Graphics g = Graphics.FromImage( image : _staticMap ) )
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(color : Color.Black);
float coef = mapWidth / Width;
for (float i = mapWidth; i >= 0; i -= coef * stepGrid)
{
g.DrawLine(pen : new Pen(brush : new SolidBrush(color : Color.DarkGreen)), x1 : 0, y1 : i, x2 : mapWidth, y2 : i);
g.DrawString(s : (Width + 1 - (i / coef)).ToString(), font : new Font(familyName : "Arial", emSize : 5), brush : new SolidBrush(color : Color.White), x : 0, y : i);
g.DrawLine(pen : new Pen(brush : new SolidBrush(color : Color.DarkGreen)), x1 : i, y1 : 0, x2 : i, y2 : mapWidth);
g.DrawString(s : (i / coef).ToString(), font : new Font(familyName : "Arial", emSize : 5), brush : new SolidBrush(color : Color.White), x : i, y : 0);
}
float coefX = mapWidth / Width;
float coefY = mapHeight / Height;
foreach (MapPoint point in MapPoints)
{
using (var brush = new SolidBrush(color : ColorTranslator.FromHtml(htmlColor : point.ColorHex)))
{
float x = point.GetScaledX(scale: coefX);
float y = point.GetScaledY(scale: coefY, Height);
g.FillRectangle(brush : brush, x : x, y : y, width : coefX * scale, height : coefY * scale);
}
}
}
}
public void DrawPoint(
int mapWidth,
int mapHeight,
Graphics g,
MapPoint point,
Color entityColor,
Brush textBrush)
{
float coefX = mapWidth / Width;
float coefY = mapHeight / Height;
// Рисуем маркер
using (var brush = new SolidBrush(color : entityColor))
{
g.FillRectangle(brush : brush, x : point.GetScaledX(coefX), y : point.GetScaledY(coefY, Height), width : coefX * 1, height : coefY * 1);
}
// Рисуем текст
SizeF textSize = g.MeasureString(text : point.Tag, font : SystemFonts.CaptionFont);
g.DrawString(
s : point.Tag,
font : SystemFonts.CaptionFont,
brush : textBrush,
x : point.GetScaledX(coefX) - textSize.Width / 2,
y : point.GetScaledY(coefY, Height) - textSize.Height
);
}
public Bitmap GetStaticMap()
{
return _staticMap;
}
public List<MapPoint> GetPoints()
{
return MapPoints;
}
}
public class MapPoint
{
public int X { get; set; }
public int Y { get; set; }
public int Z { get; set; }
public string ColorHex { get; set; }
public string Tag { get; set; }
public float GetScaledX(
float scale )
{
return X * scale;
}
public float GetScaledY(
float scale, float height = 255f )
{
return (height - Y) * scale;
}
}
public class VisionObject
{
public string Name { get; set; }
public string Color { get; set; }
public int Priority { get; set; }
}
public class MapRenderer
{
private List<VisionObject> _visionObjects = new List<VisionObject>();
private Dictionary<string, VisionObject> _tagsObjects = new Dictionary<string, VisionObject>();
public List<MapPoint> ParseMap(string mapName)
{
var points = new List<MapPoint>();
// Получаем Z-уровень карты
string zLevel = GetZLevel(mapName);
if (string.IsNullOrEmpty(zLevel)) return points;
// Загружаем объекты и их цвета
LoadVisionObjects();
// Парсим теги и их цвета
ParseMapTags(mapName);
// Читаем координаты
ParseCoordinates(mapName, zLevel, points);
return points;
}
private string GetZLevel(string mapName)
{
try
{
using (var reader = new StreamReader("maps.txt"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
var parts = line.Split('=');
if (parts.Length == 2 && parts[0].Trim() == mapName)
{
return parts[1].Trim();
}
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка чтения maps.txt: {ex.Message}");
}
return "";
}
private void LoadVisionObjects()
{
try
{
using (var reader = new StreamReader("objects_vision.txt"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
var parts = line.Split('=');
var obj = new VisionObject
{
Name = parts[0].Trim(),
Color = parts.Length > 1 ? parts[1].Trim() : "#f6b26b",
Priority = parts.Length > 2 ? int.Parse(parts[2].Trim()) : 0
};
_visionObjects.Add(obj);
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка чтения objects_vision.txt: {ex.Message}");
}
}
private void ParseMapTags(string mapPath)
{
try
{
using (var reader = new StreamReader(mapPath))
{
string line;
string currentTag = null;
while ((line = reader.ReadLine()) != null)
{
if (line.Contains(" = ("))
{
currentTag = line.Split('=')[0].Trim().Trim('"');
}
else if (currentTag != null)
{
if (line.Contains(")"))
{
currentTag = null;
continue;
}
foreach (var visionObj in _visionObjects)
{
if (line.Contains(visionObj.Name))
{
if (!_tagsObjects.ContainsKey(currentTag) ||
visionObj.Priority > (_tagsObjects[currentTag]?.Priority ?? 0))
{
_tagsObjects[currentTag] = visionObj;
}
}
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка чтения карты: {ex.Message}");
}
}
private void ParseCoordinates(string mapPath, string zLevel, List<MapPoint> points)
{
try
{
using (var reader = new StreamReader(mapPath))
{
string line;
int currentX = 0;
while ((line = reader.ReadLine()) != null)
{
if (Regex.IsMatch(line, @"\(\d+,1,1\)"))
{
currentX = int.Parse(Regex.Match(line, @"\((\d+),").Groups[1].Value);
int currentY = 1;
for (int i = 0; i < 255; i++)
{
line = reader.ReadLine()?.Trim();
if (line == null) break;
if (_tagsObjects.TryGetValue(line, out var visionObj) && visionObj != null)
{
points.Add(new MapPoint
{
X = currentX,
Y = 257 - currentY,
Z = int.Parse(zLevel),
ColorHex = visionObj.Color
});
}
currentY++;
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка парсинга координат: {ex.Message}");
}
}
}
}