-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
521 lines (449 loc) · 19.3 KB
/
MainForm.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MapEditor
{
enum MapBrush {pencil, rectangle, ellipse, fill, select };
enum ViewMode {all, below };
public partial class MainForm : Form
{
private const int init_map_size_x = 16;
private const int init_map_size_y = 16;
static Map map = null;
static MapBrush mapBrush = MapBrush.pencil;
static int layer;
static private string projectPath;
static BindingList<Map> mapList = new BindingList<Map>();
static string[] tileset_name = { "001-Grassland01", "002-Woods01", "003-Forest01", "004-Mountain01", "005-Beach01", "006-Desert01", "007-Swamp01", "008-Snowfield01", "009-CastleTown01", "010-CastleTown02", "011-PortTown01", "012-PortTown02", "013-PostTown01", "014-PostTown02", "015-ForestTown01", "016-ForestTown02", "017-MineTown01", "018-MineTown02", "019-DesertTown01", "020-DesertTown02", "021-SnowTown01", "022-SnowTown02", "023-FarmVillage01", "024-FarmVillage02", "025-Castle01", "026-Castle02", "027-Castle03", "028-Church01", "029-Church02", "030-Ship01", "031-Ship02", "032-Heaven01", "033-Heaven02", "034-Bridge01", "035-Ruins01", "036-Shop01", "037-Fort01", "038-Fort02", "039-Tower01", "040-Tower02", "041-EvilCastle01", "042-EvilCastle02", "043-Cave01", "044-Cave02", "045-Cave03", "046-Cave04", "047-Mine01", "048-Sewer01", "049-InnerBody01", "050-DarkSpace01" };
private bool isMouseDown = false;
private string _selectedMenuItem;
private readonly ContextMenuStrip mapsContextMenuStrip;
public MainForm()
{
InitializeComponent();
panel1.ClientSize = new Size(panel1.Width, tableLayoutPanel1.Height - 23);//TODO: fix this fuckin thing
layer = 1;
listBox_maps.DataSource = mapList;
pictureBox_tile.MouseMove += new MouseEventHandler(pictureBox_tile_MouseMove);
pictureBox_tile.MouseDown += new MouseEventHandler(pictureBox_tile_MouseDown);
pictureBox_tile.MouseUp += new MouseEventHandler(pictureBox_tile_MouseUp);
pictureBox_map.MouseMove += new MouseEventHandler(pictureBox_map_MouseMove);
pictureBox_map.MouseDown += new MouseEventHandler(pictureBox_map_MouseDown);
pictureBox_map.MouseUp += new MouseEventHandler(pictureBox_map_MouseUp);
pictureBox_map.MouseLeave += new EventHandler(pictureBox_map_MouseLeave);
var toolStripMenuItem_map_properties = new ToolStripMenuItem { Text = "Map Properties" };
var toolStripMenuItem_new_map = new ToolStripMenuItem { Text = "New Map" };
var toolStripMenuItem_delete_map = new ToolStripMenuItem { Text = "Delete Map" };
var toolStripMenuItem_duplicate_map = new ToolStripMenuItem { Text = "Duplicate Map" };
toolStripMenuItem_new_map.Click += toolStripMenuItem_new_map_click;
toolStripMenuItem_delete_map.Click += toolStripMenuItem_delete_map_click;
toolStripMenuItem_map_properties.Click += toolStripMenuItem_map_properties_click;
toolStripMenuItem_duplicate_map.Click += toolStripMenuItem_duplicate_map_click;
//toolStripMenuItem2.Click += toolStripMenuItem2_Click;
mapsContextMenuStrip = new ContextMenuStrip();
mapsContextMenuStrip.Items.AddRange(new ToolStripItem[] { toolStripMenuItem_map_properties, new ToolStripSeparator(), toolStripMenuItem_new_map, new ToolStripSeparator(), toolStripMenuItem_delete_map, toolStripMenuItem_duplicate_map });
listBox_maps.MouseDown += listBox_maps_MouseDown;
}
private void toolStripMenuItem_duplicate_map_click(object sender, EventArgs e)
{
int newID = -1;
foreach (var map in mapList)
{
if (map.ID >= newID)
newID = map.ID + 1;
}
Map newMap = new Map(map, string.Format("Map {0:000}", newID), newID);
mapList.Add(newMap);
map = newMap;
listBox_maps.SelectedItem = map;
map.UpdateGraphics(layer);
}
private void toolStripMenuItem_map_properties_click(object sender, EventArgs e)
{
MapPropertiesForm form = new MapPropertiesForm(tileset_name, OnPropertyChange, map.Name, map.Tileset_id, map.Map_tile_count_x, map.Map_tile_count_y);
form.Show();
}
public void OnPropertyChange(string name, int tile_id, int map_tile_count_x, int map_tile_count_y)
{
map.UpdateProperties(name, tile_id, map_tile_count_x, map_tile_count_y);
map.UpdateGraphics(layer);
listBox_maps.DataSource = null;
listBox_maps.DataSource = mapList;
//UpdateToString(listBox_maps);
}
private void toolStripMenuItem_delete_map_click(object sender, EventArgs e)
{
File.SetAttributes(projectPath + "\\" + map.filename + ".mapinfo", FileAttributes.Normal);
if (mapList.Count == 0) return;
else if(mapList.Count == 1)
{
File.Delete(projectPath + "\\" + map.filename + ".mapinfo");
mapList.Remove(map);
map = null;
}
else
{
File.Delete(projectPath + "\\" + map.filename + ".mapinfo");
mapList.Remove(map);
map = mapList[0];
listBox_maps.SelectedIndex = 0;
}
map.UpdateGraphics(layer);
}
private void toolStripMenuItem_new_map_click(object sender, EventArgs e)
{
int newID = -1;
foreach(var map in mapList)
{
if (map.ID >= newID)
newID = map.ID + 1;
}
Map newMap = new MapEditor.Map(string.Format("Map {0:000}", newID), newID, init_map_size_x, init_map_size_y, 0, projectPath, string.Format("Map {0:000}", newID) + newID, ref pictureBox_tile, ref pictureBox_map);
mapList.Add(newMap);
map = newMap;
listBox_maps.SelectedItem = map;
//Map newMap = new Map()
}
private void listBox_maps_MouseDown(object sender, MouseEventArgs e)
{
if (listBox_maps.IndexFromPoint(e.Location) >= 0)
{
listBox_maps.SelectedIndex = listBox_maps.IndexFromPoint(e.Location);
map = (Map)listBox_maps.SelectedItem;
map.UpdateGraphics(layer);
}
if (e.Button != MouseButtons.Right) return;
var index = listBox_maps.IndexFromPoint(e.Location);
mapsContextMenuStrip.Items[0].Enabled = true;
if (index != ListBox.NoMatches)
{
_selectedMenuItem = listBox_maps.Items[index].ToString();
mapsContextMenuStrip.Show(Cursor.Position);
mapsContextMenuStrip.Visible = true;
}
else
{
if (listBox_maps.SelectedItem != null)
{
_selectedMenuItem = listBox_maps.SelectedItem.ToString();
}
else
{
mapsContextMenuStrip.Items[0].Enabled = false;
}
mapsContextMenuStrip.Show(Cursor.Position);
mapsContextMenuStrip.Visible = true;
}
}
public void OnNewProject(string path)
{
projectPath = path;
if(!Directory.Exists(path+ "\\tiles"))
{
Directory.CreateDirectory(path + "\\tiles");
}
this.Text = "Map Editor " + projectPath;
mapList.Add(new Map("Map 000", 0, 16, 16, 1, projectPath, "Map 0000", ref pictureBox_tile, ref pictureBox_map));
listBox_maps.SelectedItem = mapList[0];
map = mapList[0];
}
private void pictureBox_map_MouseLeave(object sender, EventArgs e)
{
if (map != null)
map.ClearMapOverlay();
}
private void pictureBox_map_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
if(map!=null)
if (mapBrush == MapBrush.pencil) map.DrawTiles(e.Location, mapBrush);
}
private void pictureBox_map_MouseDown(object sender, MouseEventArgs e)
{
isMouseDown = true;
if (map != null)
{
if (mapBrush == MapBrush.pencil) map.DrawTiles(e.Location, mapBrush);
else if (mapBrush == MapBrush.rectangle) map.DrawMapOverlayPencil(e.Location, mapBrush);
}
}
private void pictureBox_map_MouseMove(object sender, MouseEventArgs e)
{
if (map != null)
{
if(mapBrush == MapBrush.pencil) map.DrawMapOverlayPencil(e.Location, mapBrush);
if (isMouseDown)
{
if (mapBrush == MapBrush.pencil) map.DrawTiles(e.Location, mapBrush);
else if (mapBrush == MapBrush.rectangle) map.DrawMapOverlayPencil(e.Location, mapBrush);
}
}
}
private void pictureBox_tile_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (isMouseDown)
{
if (map != null)
map.SelectTilesMiddle(e.Location);
}
}
private void pictureBox_tile_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
isMouseDown = true;
if (map != null)
map.SelectTilesStart(e.Location);
}
private void pictureBox_tile_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
isMouseDown = false;
if (map != null)
map.SelectTilesEnd(e.Location);
}
private void toolStripDropDownButton1_Click(object sender, EventArgs e)
{
}
private void toolStripContainer1_ContentPanel_Load(object sender, EventArgs e)
{
}
private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
{
}
private void elipseToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox aboutbox = new AboutBox();
aboutbox.Show();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void toolStripButton_layer1_Click(object sender, EventArgs e)
{
toolStripButton_layer1.Checked = true;
toolStripButton_layer2.Checked = false;
toolStripButton_layer3.Checked = false;
toolStripButton_all.Checked = false;
layer = 1;
if (map != null)
map.SetLayer(1);
}
private void toolStripButton_layer2_Click(object sender, EventArgs e)
{
toolStripButton_layer1.Checked = false;
toolStripButton_layer2.Checked = true;
toolStripButton_layer3.Checked = false;
toolStripButton_all.Checked = false;
layer = 2;
if (map != null)
map.SetLayer(2);
}
private void toolStripButton_layer3_Click(object sender, EventArgs e)
{
toolStripButton_layer1.Checked = false;
toolStripButton_layer2.Checked = false;
toolStripButton_layer3.Checked = true;
toolStripButton_all.Checked = false;
layer = 3;
if (map != null)
map.SetLayer(3);
}
private void toolStripButton_all_Click(object sender, EventArgs e)
{
toolStripButton_layer1.Checked = false;
toolStripButton_layer2.Checked = false;
toolStripButton_layer3.Checked = false;
toolStripButton_all.Checked = true;
layer = 0;
if (map != null)
map.SetLayer(0);
}
private void toolStripButton_pencil_Click(object sender, EventArgs e)
{
toolStripButton_pencil.Checked = true;
toolStripButton_fill.Checked = false;
toolStripButton_select.Checked = false;
toolStripButton_rectangle.Checked = false;
toolStripButton_ellipse.Checked = false;
mapBrush = MapBrush.pencil;
}
private void toolStripButton_fill_Click(object sender, EventArgs e)
{
toolStripButton_pencil.Checked = false;
toolStripButton_fill.Checked = true;
toolStripButton_select.Checked = false;
toolStripButton_rectangle.Checked = false;
toolStripButton_ellipse.Checked = false;
mapBrush = MapBrush.fill;
}
private void toolStripButton_select_Click(object sender, EventArgs e)
{
toolStripButton_pencil.Checked = false;
toolStripButton_fill.Checked = false;
toolStripButton_select.Checked = true;
toolStripButton_rectangle.Checked = false;
toolStripButton_ellipse.Checked = false;
mapBrush = MapBrush.select;
}
private void toolStripButton_rectangle_Click(object sender, EventArgs e)
{
toolStripButton_pencil.Checked = false;
toolStripButton_fill.Checked = false;
toolStripButton_select.Checked = false;
toolStripButton_rectangle.Checked = true;
toolStripButton_ellipse.Checked = false;
mapBrush = MapBrush.rectangle;
}
private void toolStripButton_ellipse_Click(object sender, EventArgs e)
{
toolStripButton_pencil.Checked = false;
toolStripButton_fill.Checked = false;
toolStripButton_select.Checked = false;
toolStripButton_rectangle.Checked = false;
toolStripButton_ellipse.Checked = true;
mapBrush = MapBrush.ellipse;
}
private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
private void pictureBox_map_Click(object sender, EventArgs e)
{
if (mapBrush == MapBrush.fill && map != null)
{
map.FillEverything();
}
}
private void Form_Main_Resize(object sender, EventArgs e)
{
panel1.ClientSize = new Size(panel1.Width, tableLayoutPanel1.Height - 23);
}
private void toolStripButton_new_Click(object sender, EventArgs e)
{
NewProjectForm npf = new NewProjectForm(new PathPassDelegate(OnNewProject));
npf.Show();
}
private void toolStripButton_save_Click(object sender, EventArgs e)
{
if (map != null)
map.SaveToFile();
}
private void toolStripButton_open_Click(object sender, EventArgs e)
{
using (var fbd = new FolderBrowserDialog())
{
if(mapList.Count >0 )
{
DialogResult dialogResult = MessageBox.Show("Save Project?", "Message", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
map.SaveToFile();
}
}
mapList.Clear();
fbd.Description = "Select Project Folder to Open.";
DialogResult result = fbd.ShowDialog();
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
{
try
{
if (!Directory.Exists(fbd.SelectedPath + "\\tiles"))
{
MessageBox.Show("No Project Detected.");
return;
}
projectPath = fbd.SelectedPath;
this.Text = "Map Editor " + projectPath;
string[] maps = Directory.GetFiles(projectPath, "*.mapinfo");
foreach(string filepath in maps)
{
mapList.Add(new Map(projectPath, Path.GetFileNameWithoutExtension(filepath), ref pictureBox_tile, ref pictureBox_map));
}
map = mapList[mapList.Count - 1];
listBox_maps.SelectedItem = map;
map.SetLayer(layer);
}
catch (IOException)
{
}
}
}
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripButton_new_Click(sender, e);
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripButton_open_Click(sender, e);
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripButton_save_Click(sender, e);
}
private void layer1ToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripButton_layer1_Click(sender, e);
}
private void layer2ToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripButton_layer2_Click(sender, e);
}
private void layer3ToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripButton_layer3_Click(sender, e);
}
private void pencilToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripButton_pencil_Click(sender, e);
}
private void allLayersToolStripMenuItem_Click(object sender, EventArgs e)
{
currentLayerAndBelowToolStripMenuItem.Checked = false;
allLayersToolStripMenuItem.Checked = true;
if(map!=null)
{
map.ViewModeSetting = ViewMode.all;
}
}
private void currentLayerAndBelowToolStripMenuItem_Click(object sender, EventArgs e)
{
currentLayerAndBelowToolStripMenuItem.Checked = true;
allLayersToolStripMenuItem.Checked = false;
if (map != null)
{
map.ViewModeSetting = ViewMode.below;
}
}
private void dimOtherLayersToolStripMenuItem_Click(object sender, EventArgs e)
{
dimOtherLayersToolStripMenuItem.Checked = !dimOtherLayersToolStripMenuItem.Checked;
if (map != null)
{
map.Dim = dimOtherLayersToolStripMenuItem.Checked;
}
}
private void floodFillToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripButton_fill_Click(sender, e);
}
private void UpdateToString(ListBox listBox)
{
int count = listBox.Items.Count;
listBox.SuspendLayout();
for (int i = 0; i < count; i++)
{
listBox.Items[i] = listBox.Items[i];
}
listBox.ResumeLayout();
}
}
}