forked from se5a/HazeronMapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHazMap.cs
418 lines (355 loc) · 14.9 KB
/
HazMap.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace HazeronMapper
{
public partial class HazMap : Form
{
Galaxy galaxy = new Galaxy();
Canvasdata canvasdata;
List<Point> points = new List<Point>();
Dictionary<string, SystemObj> mappedsystems = new Dictionary<string, SystemObj>();
List<WormHoleObj> mappedwormholes = new List<WormHoleObj>();
List<SystemObj> systemPlaced = new List<SystemObj>();
List<UserControl_System> sys_usrCtrls = new List<UserControl_System>();
Point canvasloc;
UserControl_System syslink_usrctrl;
bool leftmousegrab = false;
Point mousedownOffset;
public HazMap()
{
InitializeComponent();
canvasloc = new Point((pictureBox1.Width / 2) * -1, pictureBox1.Height / 2);
canvasdata = new Canvasdata(1, pictureBox1.Width, pictureBox1.Height, canvasloc);
}
public void canvasupdate()
{
canvasdata = new Canvasdata(1, pictureBox1.Width, pictureBox1.Height, canvasloc);
}
public void refreshlines()
{
//pictureBox1.Invalidate();
if (mappedsystems.Count > 0)
{
pictureBox1.Paint += new PaintEventHandler(this.drawlines);
pictureBox1.Invalidate();
}
}
public void refresh()
{ pictureBox1.Invalidate(); }
public Galaxy thegalaxy
{
get { return this.galaxy; }
}
private void toolStripButton_drawmap_Click(object sender, EventArgs e)
{
//Point startmappoint = new Point(0,200);
canvasupdate();
if (galaxy.sectors_dictionary.Count > 0)
{
selectSystemPrompt promptwindow = new selectSystemPrompt();
selectsys_returnObj retobj = promptwindow.selectSystemPromptdlg(galaxy);
SystemObj startSys = retobj.system;
int depth = retobj.depth;
bool resetlocs = retobj.resetloc;
foreach (UserControl_System control in sys_usrCtrls)//clear usercontrols
{
pictureBox1.Controls.Remove(control);
control.Dispose();
}
if (resetlocs)
{
startSys.maploc = new Point(0,200);
}
sys_usrCtrls = new List<UserControl_System>(); //new usercontrol list
mappedsystems = new Dictionary<string, SystemObj>(); //new mappedsystems dictionary
mappedwormholes = new List<WormHoleObj>();
systemPlaced = new List<SystemObj>();
//startSys.maploc = startmappoint; //set first system location
systemPlaced.Add(startSys);
syslink_usrctrl = new UserControl_System(startSys, this, canvasdata); //first system usercontrol
sys_usrCtrls.Add(syslink_usrctrl); //addthe user control to the usercontrol list
mappedsystems.Add(startSys.location, startSys);//add the system to the mappedsystems dictionary
pictureBox1.Controls.Add(syslink_usrctrl); //add teh control to the picturebox.
Point loc = syslink_usrctrl.Location;
placesystems(startSys, 180, depth, resetlocs);
}
refreshlines();
}
#region Draw stars and lines
private void placesystems(SystemObj parentsystem, int angle, int depth, bool resetlocs)
{
if (depth > 0)
{
//Dictionary<string, SystemObj> systemslinked = new Dictionary<string, SystemObj>();
List<SystemObj> systemslinked = new List<SystemObj>();
int icondistance = 100;
// Point parentMaploc = parentsystem.maploc;
bool resetthisloc = false;
foreach (WormHoleObj wh in parentsystem.Wormholes) //foreach wormhole from this system.
{
SystemObj linkedsys = wh.getlink(parentsystem);
//if (!systemslinked.Keys.Contains(linkedsys.location))
if (!systemslinked.Contains(linkedsys))
{
//systemslinked.Add(linkedsys.location, linkedsys);
systemslinked.Add(linkedsys);
}
if (!mappedwormholes.Contains(wh))
{
mappedwormholes.Add(wh);
}
if (!mappedsystems.Keys.Contains(linkedsys.location))
{
syslink_usrctrl = new UserControl_System(linkedsys, this, canvasdata);
sys_usrCtrls.Add(syslink_usrctrl);
mappedsystems.Add(linkedsys.location, linkedsys);
pictureBox1.Controls.Add(syslink_usrctrl);
}
}
//foreach (KeyValuePair<string, SystemObj> kvp_system in systemslinked)
foreach (SystemObj linkedsys in systemslinked) //this list should not include the parent...
{
if (!systemPlaced.Contains(linkedsys))
{
//SystemObj linkedsys = kvp_system.Value;
Point thismaploc = new Point();
//Point dloc = mappedsystems[linkedsys.location].maploc;
//if (dloc.X < 1 || dloc.Y < 1) { resetthisloc = true; }
int linkcount = systemslinked.Count;
//if (linkcount > 1) { linkcount -= 1; }
if (resetthisloc || resetlocs) //reset.
{
thismaploc = parentsystem.maploc;
angle += (360 / linkcount);
thismaploc = sides_ab(icondistance, angle, false);
thismaploc.X += parentsystem.maploc.X;
thismaploc.Y += parentsystem.maploc.Y;
linkedsys.maploc = thismaploc;
//
}
//mappedsystems[linkedsys.location].maploc = thismaploc;
systemPlaced.Add(linkedsys);
}
//placesystems(mappedsystems[linkedsys.location], angle, depth - 1, resetlocs);
placesystems(linkedsys, angle, depth - 1, resetlocs);
}
}
}
public void drawlines(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
//if (mappedsystems.Count > 1)
foreach (UserControl_System sysctrl in sys_usrCtrls)
{
sysctrl.location(canvasdata);
}
{
foreach (WormHoleObj wh in mappedwormholes)
{
Pen linkline = new Pen(Color.Blue, 2);
List<SystemObj> systemslinked = new List<SystemObj>(wh.getlinks);
//mappedsystems[systemslinked[0].location].usrctrl.updateloc(canvasdata);
//mappedsystems[systemslinked[1].location].usrctrl.updateloc(canvasdata);
//Point pt1 = canvasdata.canvasLocation(mappedsystems[systemslinked[0].location].maploc);
Point pt1 = canvasdata.canvasLocation(systemslinked[0].maploc);
// Point pt2 = canvasdata.canvasLocation(mappedsystems[systemslinked[1].location].maploc);
Point pt2 = canvasdata.canvasLocation(systemslinked[1].maploc);
Point pt3 = canvasdata.zeroedpoint(pt1, pt2);
if (pt3.Y == 0)
{
pt3.Y = 1;
}
int offangle = (int)Math.Atan(pt3.X / pt3.Y);
if (wh.polarity)
{
offangle += 90;
int offset1 = 4;
pt3 = sides_ab(offset1, offangle);
linkline.Color = Color.Red;
pt1 = canvasdata.add(pt1, pt3);
pt2 = canvasdata.add(pt2, pt3);
}
else
{
offangle -= 90;
int offset1 = 4;
pt3 = sides_ab(offset1, offangle);
linkline.Color = Color.Blue;
pt1 = canvasdata.add(pt1, pt3);
pt2 = canvasdata.add(pt2, pt3);
}
g.DrawLine(linkline, pt1, pt2);
}
}
}
public Point sides_ab(int hypotinuse, int angle_A, bool radians = true )
{
double angle_a = angle_A;
if (!radians)
{ angle_a = angle_A * Math.PI / 180; }
double side_a = 0;
double side_b = 0;
side_b = Math.Sin(angle_a) * hypotinuse;
side_a = Math.Cos(angle_a) * hypotinuse;
Point sides = new Point((int)side_b, (int)side_a);
return sides;
}
#endregion
#region Save and Load
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
string filename = savefilediag(".hzm");
if (filename != null)
{
filehandling.SerializeObject(galaxy, filename);
}
}
private string savefilediag(string extension)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.AddExtension = true;
sfd.DefaultExt = extension;
string filename = null;
DialogResult sfd_result = sfd.ShowDialog();
if (sfd_result == DialogResult.OK)
{
filename = sfd.FileName;
}
return filename;
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
string filename = openfilediag(".hzm");
if (filename != null)
{
galaxy = (Galaxy)filehandling.DeserializeObject(filename);
}
}
private string openfilediag(string extension)
{
OpenFileDialog ofd = new OpenFileDialog();
//ofd.Filter = "*" + extension;
ofd.DefaultExt = extension;
string filename = null;
DialogResult ofd_result = ofd.ShowDialog();
if (ofd_result == DialogResult.OK)
{
filename = ofd.FileName;
}
return filename;
}
#endregion
#region Import events
private void clipboardToolStripMenuItem_Click(object sender, EventArgs e)
{
string hazscan = Clipboard.GetText();
// There has to be "System Survey of " in the clipboard, or else it isn't a scan report.
if (hazscan != null && hazscan.Contains("System Survey of "))
{
string pastemesage = Readscan.readscan(hazscan, galaxy);
if (pastemesage != null)
{
this.toolStripStatusLabel1.Text = pastemesage;
}
else
{
this.toolStripStatusLabel1.Text = "Paste failed";
}
}
else
{
this.toolStripStatusLabel1.Text = "Paste invalid";
}
}
private void textFileToolStripMenuItem_Click(object sender, EventArgs e)
{
string filename = openfilediag(".txt");
string scanfile = filename;
try
{
string scantext = File.ReadAllText(scanfile);
if (scantext.Contains("System Survey of "))
{
Readscan.readscan(scantext, galaxy);
}
else
{
this.toolStripStatusLabel1.Text = "Text file invalid";
}
}
catch (IOException)
{
this.toolStripStatusLabel1.Text = "Text file read error";
}
}
#endregion
#region Moving the stars and so
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
leftmousegrab = true;
mousedownOffset = e.Location;
//this.Cursor = Cursors. fuuuuu, no good cursors for this in default.
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
canvasupdate();
pictureBox1.Invalidate();
leftmousegrab = false;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <remarks>this could be cleaned up, instead of getting delta from mousedownoffset and new mousemove loc
/// get it from the canvaslocation itself and the mouse move loc.</remarks>
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (leftmousegrab)
{
canvasupdate();
pictureBox1.Invalidate();
Point mousemove = e.Location;
Point offset = new Point();
offset.X = mousedownOffset.X - mousemove.X;
offset.Y = mousedownOffset.Y - mousemove.Y;
mousedownOffset = mousemove;
canvasloc.X += offset.X;
canvasloc.Y -= offset.Y;
}
}
private void HazMap_SizeChanged(object sender, EventArgs e)
{
int offsetx = canvasdata.width / 2 - pictureBox1.Width / 2;
int offsety = canvasdata.height / 2 - pictureBox1.Height / 2;
canvasloc.X += offsetx;
canvasloc.Y -= offsety;
canvasupdate();
pictureBox1.Invalidate();
}
#endregion
}
public class selectsys_returnObj
{
public SystemObj system;
public int depth;
public bool resetloc;
public selectsys_returnObj(SystemObj system, int depth, bool resetloc)
{
this.system = system;
this.depth = depth;
this.resetloc = resetloc;
}
}
}