-
Notifications
You must be signed in to change notification settings - Fork 0
/
Editor.cs
1777 lines (1420 loc) · 60.6 KB
/
Editor.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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* TODO:
*
* Beta:
* Smooth rotation.
*
* Change background
* */
using System;
using System.IO;
using System.Text.RegularExpressions; // For string tokenizing
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Color = System.Drawing.Color;
using Bounce.Common;
using DrawPoint = System.Drawing.Point;
using XnaPoint = Microsoft.Xna.Framework.Point;
using DrawRect = System.Drawing.Rectangle;
using XnaRectangle = Microsoft.Xna.Framework.Rectangle;
namespace Bounce.LevelEditor
{
public partial class Editor : Form
{
//An enum representing the current state of the editor.
private enum State
{
NO_EDITS, //Used when we don't have anything in the level editor
EDITING_WORLD, //Used when we have a world loaded, as opposed to a room.
EDITING_ROOM //Used when we have a single room loaded.
}
private State currentState = State.NO_EDITS;
private string currentFileName = "";
private EditorTool currentTool;
private Image texture;
private string textureDir;
private GameWorld world;
private Room currentlySelectedRoom;
string currdir = CurrDirHack();
//The object that the user just selected. May be null!
private SpaceObject currentlySelectedObject;
//The box inside which the user must click in order to resize the object.
private RectangleF resizeObjectBox;
public Editor()
{
InitializeComponent();
Size pbLevelSize = new Size(Constants.WINDOW_WIDTH, Constants.WINDOW_HEIGHT - Constants.HUD_HEIGHT);
pb_Level.MaximumSize = (pb_Level.MinimumSize = (pb_Level.Size = pbLevelSize));
this.FormBorderStyle = FormBorderStyle.FixedSingle;
currentTool = EditorTool.Insertion;
ClearToolMenu();
tool_Insertion.Checked = true;
// Disable object properties by default
tb_Damage.Enabled = false;
tb_Rotation.Enabled = false;
b_ApplyProperties.Enabled = false;
b_Front.Enabled = false;
tb_SLevel.Enabled = false;
//Only enable this menu item if the currently selected object is a door. When we start,
// we don't have a currently selected item, so disable this menu item.
menu_door.Enabled = false;
menu_room.Enabled = false;
menu_debug.Enabled = false;
}
//------The following are callbacks for when the user selects a new type of object in the radio button
// list. They populate the list of textures.
private void rb_WallStatic_CheckedChanged(object sender, EventArgs e)
{
if (rb_WallStatic.Checked)
{
textureDir = "Art\\Static Walls\\";
PopulateTextureList(textureDir);
}
}
// Hardcoded to only show the player's icon
private void rb_Player_CheckedChanged(object sender, EventArgs e)
{
if (rb_Player.Checked)
{
textureDir = "Art\\Players\\";
//PopulateTextureList(textureDir);
this.lb_TextureList.Items.Clear();
if (File.Exists(currdir + "\\Art\\Players\\player.png"))
lb_TextureList.Items.Add("player.png");
if (lb_TextureList.Items.Count != 0)
{
lb_TextureList.SelectedIndex = 0;
}
}
}
private void rb_WallDynamic_CheckedChanged_1(object sender, EventArgs e)
{
if (rb_WallDynamic.Checked)
{
textureDir = "Art\\Dynamic Walls\\";
PopulateTextureList(textureDir);
}
}
private void rb_HazardStatic_CheckedChanged_1(object sender, EventArgs e)
{
if (rb_HazardStatic.Checked)
{
textureDir = "Art\\Static Hazards\\";
PopulateTextureList(textureDir);
}
}
private void rb_HazardDynamic_CheckedChanged_1(object sender, EventArgs e)
{
if (rb_HazardDynamic.Checked)
{
textureDir = "Art\\Dynamic Hazards\\";
PopulateTextureList(textureDir);
}
}
private void rb_Handlebars_CheckedChanged_1(object sender, EventArgs e)
{
if (rb_Handlebars.Checked)
{
textureDir = "Art\\Handlebars\\";
PopulateTextureList(textureDir);
}
}
private void rb_Parts_CheckedChanged_1(object sender, EventArgs e)
{
if (rb_Parts.Checked)
{
textureDir = "Art\\Parts\\";
PopulateTextureList(textureDir);
}
}
private void rb_Survivors_CheckedChanged(object sender, EventArgs e)
{
if (rb_Survivors.Checked)
{
textureDir = "Art\\Survivors\\";
PopulateTextureList(textureDir);
}
}
//This is slightly different than all the others. There is no "texture"
//for doors, you see, so we're going to just add one item in the
//list at the bottom, and make it selected.
private void rb_Doors_CheckedChanged(object sender, EventArgs e)
{
if (rb_Doors.Checked)
{
lb_TextureList.Items.Clear();
lb_TextureList.Items.Add("Door");
lb_TextureList.SelectedIndex = 0;
}
}
private void rb_VictoryTest_CheckedChanged(object sender, EventArgs e)
{
if (rb_VictoryTest.Checked)
{
textureDir = "Art\\Victory Tests\\";
PopulateTextureList(textureDir);
}
}
private void rb_SavePoint_CheckedChanged(object sender, EventArgs e)
{
if (rb_SavePoint.Checked)
{
textureDir = "Art\\Save Points\\";
PopulateTextureList(textureDir);
}
}
private void rb_Vanish_Walls_CheckedChanged(object sender, EventArgs e)
{
if (rb_Vanish_Walls.Checked)
{
textureDir = "Art\\Static Walls\\";
PopulateTextureList(textureDir);
}
}
private void PopulateTextureList(string dir)
{
this.lb_TextureList.Items.Clear();
DirectoryInfo di = new DirectoryInfo(currdir + "\\" + dir);
FileInfo[] fileList = di.GetFiles("*.png");
foreach (FileInfo file in fileList)
{
lb_TextureList.Items.Add(file.Name);
}
if (lb_TextureList.Items.Count != 0)
{
lb_TextureList.SelectedIndex = 0;
}
}
//------End of the texture callbacks
//----------------------------------------------------------------------------------------------------
//These methods and variables deal with clicking on the paint area. If we are in insertion mode, and
// the user left clicks, insert the object that is selected by the radio buttons to the left.
// If the user right clicks, then remove the object under the cursor if there is one.
// If we are in selection mode and the user clicks, select the object under the cursor.
// If the user clicks and drags, move the selected object.
private bool toDrag = false;
private bool toResize = false;
private int y_drag_transform = 0;
private int x_drag_transform = 0;
//Callback for when the user clicks on the painting area. We need to insert or select an object
private void pb_Level_MouseClick(object sender, MouseEventArgs e)
{
if (currentState != State.NO_EDITS)
{
if (currentTool == EditorTool.Insertion)
{
painting_clicked_insert(e);
}
else if (currentTool == EditorTool.Selection && !toResize)
{
if (Keyboard.GetState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.T)
&& (currentlySelectedObject != null)
&& (currentlySelectedObject is Door))
{
((Door)currentlySelectedObject).TargetPosition = new Vector2(e.X, e.Y);
}
painting_clicked_select(e);
}
pb_Level.Refresh();
}
}
//The callback for when the user presses down the mouse inside the painting area. This means the
//user wants to start dragging an object.
private void pb_Level_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && currentlySelectedObject != null)
{
bool isInResizeBox = resizeObjectBox.Contains(e.Location.X, e.Location.Y);
if (isInResizeBox)
{
toResize = true;
}
else
{
x_drag_transform = (int)(e.X - currentlySelectedObject.Position.X);
y_drag_transform = (int)(e.Y - currentlySelectedObject.Position.Y);
toDrag = true;
}
}
}
//The user wants to stop dragging.
private void pb_Level_MouseUp(object sender, MouseEventArgs e)
{
toDrag = false;
toResize = false;
}
//The user may have dragged the object
private void pb_Level_MouseMove(object sender, MouseEventArgs e)
{
if (currentlySelectedObject != null &&
currentTool == EditorTool.Selection)
{
if (toDrag)
{
//Update the object's position
int new_X = (e.Location.X - x_drag_transform);
int new_Y = (e.Location.Y - y_drag_transform);
currentlySelectedObject.Position =
new Vector2(new_X, new_Y);
if (currentlySelectedObject is Dynamic)
((Dynamic)currentlySelectedObject).OriginalPosition = new Vector2(new_X, new_Y);
}
else if (toResize)
{
//Update the object's horizontal and vertical scaling.
//SCALING WILL ONLY WORK BEFORE ROTATING. DO NOT SCALE AFTER ROTATING. You have been warned.
Vector2 pos = currentlySelectedObject.Position;
currentlySelectedObject.HorizontalScale =
Math.Abs(pos.X - e.Location.X) / currentlySelectedObject.getOriginalWidth();
currentlySelectedObject.VerticalScale =
Math.Abs(pos.Y - e.Location.Y) / currentlySelectedObject.getOriginalHeight();
}
refreshResizeObjectBox();
//Refresh the painting area so we can dynamic drawing
pb_Level.Refresh();
}
}
//End of the callbacks, beginning of the helper fuctions
//Used when the user clicks in the painting area while using the select tool.
// This will select the object who's center is closest to the mouse.
private void painting_clicked_select(MouseEventArgs e)
{
XnaPoint mousePosition = Conversion.PointToPoint(pb_Level.PointToClient(MousePosition));
currentlySelectedObject = null;
//Find all possible objects that we might be selecting.
List<SpaceObject> candidates = new List<SpaceObject>();
foreach (SpaceObject obj in currentlySelectedRoom.Objects)
{
if (obj.getBBRelativeToWorld().Contains(mousePosition))
{
candidates.Add(obj);
}
}
//If the room is the currently selected room, the player is not null, and the click is close enough
// to the player's location, add the player to the list of possible candidates.
if(currentState == State.EDITING_WORLD &&
currentlySelectedRoom == world.CurrentRoom &&
world.player != null &&
world.player.getBBRelativeToWorld().Contains(mousePosition))
{
candidates.Add(world.player);
}
//Find the object whose center is closest to the current mouse position. If there is no
// possible selection, then the currentlySelectedObject continues to be null.
foreach (SpaceObject cand in candidates)
{
if (currentlySelectedObject == null ||
dist(mousePosition, cand.getCenterPoint()) < dist(mousePosition, currentlySelectedObject.getCenterPoint()))
{
currentlySelectedObject = cand;
refreshResizeObjectBox();
}
}
//If the currently selected item is a door, enable the door menu item. Otherwise, disable it.
if (currentlySelectedObject != null &&
currentlySelectedObject is Door)
{
menu_door.Enabled = true;
}
else
{
menu_door.Enabled = false;
}
// Set fields in Object Properties panel
if (currentlySelectedObject != null)
{
tb_Damage.Enabled = false; // Only true if this is a hazard (below)
tb_Rotation.Enabled = true;
b_ApplyProperties.Enabled = true;
b_Front.Enabled = true;
tb_SLevel.Enabled = false;
tb_Rotation.Text = (currentlySelectedObject.Rotation * 180.0f / MathHelper.Pi).ToString();
if (currentlySelectedObject is HazardStatic)
{
tb_Damage.Text = ((HazardStatic)currentlySelectedObject).Damage.ToString();
tb_Damage.Enabled = true;
}
else if (currentlySelectedObject is HazardDynamic)
{
// Other
tb_Damage.Text = ((HazardDynamic)currentlySelectedObject).Damage.ToString();
tb_Damage.Enabled = true;
}
else if (currentlySelectedObject is Survivor || currentlySelectedObject is VanishWall)
{
tb_SLevel.Enabled = true;
}
SetScriptingFields();
}
else
{
// Clear all the properties fields
tb_Damage.Text = "";
tb_Rotation.Text = "";
tb_SLevel.Text = "";
b_ApplyProperties.Enabled = false;
b_Front.Enabled = false;
tb_Damage.Enabled = false;
tb_Rotation.Enabled = false;
tb_SLevel.Enabled = false;
tb_Script.Text = "";
tb_Script.Enabled = false;
cbox_Scripted.Checked = false;
cbox_Scripted.Enabled = false;
}
}
//The distance between two points.
private double dist(XnaPoint p, XnaPoint center)
{
return Math.Sqrt(Math.Pow(p.X - center.X, 2) + Math.Pow(p.Y - center.Y, 2));
}
//Used when the user clicks in the painting area while using the insert tool.
// This will create a new object when the user left-clicks, and delete the object
// it is currently on when it is right clicked.
private void painting_clicked_insert(MouseEventArgs e)
{
DrawPoint mouse = pb_Level.PointToClient(MousePosition);
// Remove the object under the cursor.
if (e.Button == MouseButtons.Right)
{
foreach (SpaceObject obj in currentlySelectedRoom.Objects.Reverse<SpaceObject>())
{
XnaRectangle bb = obj.getBBRelativeToWorld();
/*texture = Image.FromFile(currdir + "\\" + obj.TextureName + ".png");
if (mouse.X > obj.Position.X && mouse.X < (obj.Position.X + texture.Width) &&
mouse.Y > obj.Position.Y && mouse.Y < (obj.Position.Y + texture.Height))*/
if (bb.Contains(Conversion.PointToPoint(mouse)))
{
currentlySelectedRoom.Objects.Remove(obj);
if (obj == currentlySelectedObject)
{
currentlySelectedObject = null;
}
break;
}
}
}
// Add an object.
if (e.Button == MouseButtons.Left)
{
if (rb_Doors.Checked)
{
makeDoor(mouse);
}
else
{
object textureName = lb_TextureList.SelectedItem;
if (textureName != null)
{
texture = Image.FromFile(currdir + "\\" + textureDir + textureName.ToString());
MakeSpaceObject(textureDir + textureName.ToString(), texture, mouse);
}
}
}
}
// Create object in room upon user click
private void MakeSpaceObject(string texName, Image tex, DrawPoint mp)
{
texName = texName.Replace(".png", "");
int numberOfFrames = SpaceObject.GetNumberOfFrames(texName);
//Much repeated code to account for each possible object
if (rb_WallStatic.Checked)
{
mp.X -= (texture.Width / (numberOfFrames * 2));
mp.Y -= (texture.Height / 2);
WallStatic wall = new WallStatic(
Conversion.DrawPointToVector2(mp),
currdir + "\\" + texName + ".tri");
wall.TextureName = texName;
currentlySelectedRoom.AddObject(wall);
}
else if (rb_WallDynamic.Checked)
{
mp.X -= (texture.Width / (numberOfFrames * 2));
mp.Y -= (texture.Height / 2);
WallDynamic wall = new WallDynamic(
Conversion.DrawPointToVector2(mp),
new Vector2(0, 0),
currdir + "\\" + texName + ".tri");
wall.TextureName = texName;
currentlySelectedRoom.AddObject(wall);
}
else if (rb_HazardStatic.Checked)
{
mp.X -= (texture.Width / (numberOfFrames * 2));
mp.Y -= (texture.Height / 2);
HazardStatic hazard = new HazardStatic(
Conversion.DrawPointToVector2(mp),
currdir + "\\" + texName + ".tri");
hazard.TextureName = texName;
currentlySelectedRoom.AddObject(hazard);
}
else if (rb_HazardDynamic.Checked)
{
mp.X -= (texture.Width / (numberOfFrames * 2));
mp.Y -= (texture.Height / 2);
HazardDynamic hazard = new HazardDynamic(
Conversion.DrawPointToVector2(mp),
new Vector2(0, 0),
currdir + "\\" + texName + ".tri");
hazard.TextureName = texName;
currentlySelectedRoom.AddObject(hazard);
}
else if (rb_Parts.Checked)
{
mp.X -= (texture.Width / (numberOfFrames * 2));
mp.Y -= (texture.Height / 2);
Part part = new Part(
Conversion.DrawPointToVector2(mp),
currdir + "\\" + texName + ".tri");
part.TextureName = texName;
currentlySelectedRoom.AddObject(part);
}
else if (rb_Survivors.Checked)
{
mp.X -= (texture.Width / (numberOfFrames * 2));
mp.Y -= (texture.Height / 2);
Survivor survivor = new Survivor(
Conversion.DrawPointToVector2(mp),
currdir + "\\" + texName + ".tri");
survivor.TextureName = texName;
//sets up the survivor color as appropriate
switch (survivor.TextureName)
{
case "Art\\Survivors\\SurvivorBlue":
survivor.SColor = Survivor.survivorColor.BLUE;
break;
case "Art\\Survivors\\SurvivorRed":
survivor.SColor = Survivor.survivorColor.RED;
break;
case "Art\\Survivors\\SurvivorYellow":
survivor.SColor = Survivor.survivorColor.YELLOW;
break;
case "Art\\Survivors\\SurvivorPurple":
survivor.SColor = Survivor.survivorColor.PURPLE;
break;
case "Art\\Survivors\\SurvivorOrange":
survivor.SColor = Survivor.survivorColor.ORANGE;
break;
case "Art\\Survivors\\Survivor":
survivor.SColor = Survivor.survivorColor.GREEN;
break;
default:
break;
}
currentlySelectedRoom.AddObject(survivor);
}
else if (rb_Vanish_Walls.Checked)
{
mp.X -= (texture.Width / (numberOfFrames * 2));
mp.Y -= (texture.Height / 2);
VanishWall vanish = new VanishWall(
Conversion.DrawPointToVector2(mp),
currdir + "\\" + texName + ".tri");
vanish.TextureName = texName;
currentlySelectedRoom.AddObject(vanish);
}
else if (rb_Handlebars.Checked)
{
mp.X -= (texture.Width / 2);
mp.Y -= (texture.Height / 2);
Handlebar hdlbar = new Handlebar(
Conversion.DrawPointToVector2(mp),
new Vector2(0, 0),
currdir + "\\" + texName + ".tri");
hdlbar.TextureName = texName;
currentlySelectedRoom.AddObject(hdlbar);
}
else if (rb_VictoryTest.Checked)
{
mp.X -= (texture.Width / 2);
mp.Y -= (texture.Height / 2);
VictoryTest victest = new VictoryTest(
Conversion.DrawPointToVector2(mp),
currdir + "\\" + texName + ".tri");
victest.TextureName = texName;
currentlySelectedRoom.AddObject(victest);
}
else if (rb_SavePoint.Checked)
{
mp.X -= (texture.Width / (numberOfFrames * 2));
mp.Y -= (texture.Height / 2);
SavePoint savePoint = new SavePoint(
Conversion.DrawPointToVector2(mp),
currdir + "\\" + texName + ".tri");
savePoint.TextureName = texName;
currentlySelectedRoom.AddObject(savePoint);
}
else if (rb_Player.Checked)
{
mp.X -= (int)((texture.Width * Constants.PLAYER_SCALE) / 2);
mp.Y -= (int)((texture.Height * Constants.PLAYER_SCALE) / 2);
world.player = new Player(
Conversion.DrawPointToVector2(mp),
currdir + "\\" + texName + ".tri"
);
world.CurrentRoom = currentlySelectedRoom;
world.player.TextureName = texName;
}
}
//End of the clicking on paint area callbacks
//----------------------------------------------------------------------------------------------------
//-----------------Functions dealing with saving and loading levels-----------------------------------
// More specifically, these are the callbacks and functions related to the menu bar in the top.
//Saves the current file, using the currentFileName as the name of the file.
private void mi_Save_Click(object sender, EventArgs e)
{
if (currentState != State.NO_EDITS)
{
if (currentFileName == "")
{
mi_Save_As_Click(sender, e);
}
else if (currentState == State.EDITING_WORLD)
{
verifyInvariants(false);
Serializer.Serialize(world, currentFileName);
}
else if (currentState == State.EDITING_ROOM)
{
verifyInvariants(false);
Serializer.SerializeRoom(currentlySelectedRoom, currentFileName);
}
}
}
//Allows the user to choose the name of the current file to save.
private void mi_Save_As_Click(object sender, EventArgs e)
{
if (currentState != State.NO_EDITS)
{
verifyInvariants(false);
//Allow the user to choose a name and a location
SaveFileDialog dialog = new SaveFileDialog();
if (currentState == State.EDITING_WORLD)
{
dialog.Filter = "World Files | *.world";
}
else
{
dialog.Filter = "Room Files | *.room";
}
dialog.InitialDirectory = ".";
dialog.Title = "Choose the file to save.";
DialogResult result = dialog.ShowDialog();
//If the result was ok, load the resultant file, otherwise, just return.
if (result == DialogResult.OK)
{
if (currentState == State.EDITING_WORLD)
{
Serializer.Serialize(world, dialog.FileName);
}
else
{
Serializer.SerializeRoom(currentlySelectedRoom, dialog.FileName);
}
currentFileName = dialog.FileName;
}
}
}
//Loads the specified file.
private void mi_Load_World_Click(object sender, EventArgs e)
{
load_file(true, "World Files | *.world", "Select a world file.");
}
private void mi_Load_Room_Click(object sender, EventArgs e)
{
load_file(false, "Room Files | *.room", "Select a room file.");
}
private void load_file(bool loadWorld, string filter, string title)
{
//First, choose the file we want to load.
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = filter;
dialog.InitialDirectory = ".";
dialog.Title = title;
DialogResult result = dialog.ShowDialog();
//If the result was ok, load the resultant file, otherwise, just return.
if (result == DialogResult.OK)
{
if (loadWorld)
{
world = Serializer.DeSerialize(dialog.FileName);
switchRooms(world.CurrentRoom);
}
else
{
currentlySelectedRoom = Serializer.DeserializeRoom(dialog.FileName);
switchRooms(currentlySelectedRoom);
}
currentFileName = dialog.FileName;
enableEditing(loadWorld);
pb_Level.Refresh();
}
}
//Creates a new game world and allows editing
private void mi_New_World_Click(object sender, EventArgs e)
{
//Get the new room from the user.
StringPromptDialog dialog = new StringPromptDialog("Enter the name of the first room: ");
//Create the world.
if (dialog.ShowDialog() == DialogResult.OK)
{
world = new GameWorld(dialog.UserInput);
enableEditing(true);
switchRooms(world.CurrentRoom);
}
}
//Creates a new empty room and allows editing
private void mi_New_Room_Click(object sender, EventArgs e)
{
//Get the new room from the user.
StringPromptDialog dialog = new StringPromptDialog("Enter the name of the room: ");
//Create the world.
if (dialog.ShowDialog() == DialogResult.OK)
{
currentlySelectedRoom = new Room(dialog.UserInput);
enableEditing(false);
switchRooms(currentlySelectedRoom);
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
//Set various boolean values to their appropriate state
private void enableEditing(bool editingWorld)
{
if (editingWorld)
{
currentState = State.EDITING_WORLD;
menu_room.Enabled = true;
mi_goToRoom.Enabled = true;
mi_Insert_Room.Enabled = true;
mi_merge_room.Enabled = true;
mi_link_door.Enabled = true;
deleteRoomFromWorldToolStripMenuItem.Enabled = true;
rb_Player.Enabled = true;
}
else
{
currentState = State.EDITING_ROOM;
menu_room.Enabled = true;
mi_goToRoom.Enabled = false;
mi_Insert_Room.Enabled = false;
mi_merge_room.Enabled = false;
mi_link_door.Enabled = false;
deleteRoomFromWorldToolStripMenuItem.Enabled = false;
rb_Player.Enabled = false;
}
menu_debug.Enabled = true;
}
//------------End of saving/loading functions-------------------------------------------------------
//------------Functions for working with doors------------------------------------------------------
//If the currently selected object is a door, then pops up a prompt asking for the new name for the
// door, verifies that it is unique, and renames the door.
private void renameDoorToolStripMenuItem_Click(object sender, EventArgs e)
{
if (currentlySelectedObject != null &&
currentlySelectedObject is Door)
{
StringPromptDialog dialog = new StringPromptDialog("Enter the new name: ");
if (dialog.ShowDialog() == DialogResult.OK)
{
if (isDoorNameUnique(dialog.UserInput))
{
((Door)currentlySelectedObject).name = dialog.UserInput;
}
else
{
showMessage("Uniqueness Error", "A door with that name already exists in the room");
}
}
}
}
//Call back for linking the
private void linkDoorToolStripMenuItem_Click(object sender, EventArgs e)
{
if (currentState == State.EDITING_WORLD &&
currentlySelectedObject != null &&
currentlySelectedObject is Door)
{
Door A = ((Door)currentlySelectedObject);
//First, pop up the menu and get whichever door the user selects.
string title = "Currently selected door: " +
A.name + " in room " + currentlySelectedRoom.name;
RoomSelectDialog dialog = new RoomSelectDialog(world, false, title);
if (dialog.ShowDialog() == DialogResult.OK)
{
Door B = dialog.SelectedDoor;
A.Link = null;
B.Link = null;
A.Link = B;
B.Link = A;
}
}
}
/// <summary>
/// Returns true if the door is uniquly named in the room it is in.
/// </summary>
private bool isDoorNameUnique(string name)
{
foreach (Door door in currentlySelectedRoom.Doors)
{
if (door.name == name)
{
return false;
}
}
return true;
}
//Creates a door at the location of the user click.
private void makeDoor(DrawPoint pt)
{
//First, get name for door
StringPromptDialog dialog = new StringPromptDialog("Please enter the name of the door");
if (dialog.ShowDialog() == DialogResult.OK)
{
if (isDoorNameUnique(dialog.UserInput))
{
//Create the door
Vector2 postition = new Vector2(
pt.X - (EditorConstants.DEFAULT_DOOR_SIZE / 2),
pt.Y - (EditorConstants.DEFAULT_DOOR_SIZE / 2));
string boundsFile = currdir + "\\Art\\Doors\\Door.tri";
Door door = new Door(postition, boundsFile, dialog.UserInput, currentlySelectedRoom);