/
Editor.cs
276 lines (248 loc) · 8.7 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace OpenXP
{
class Editor
{
public static string PreloadProject;
public static OpenXP Form { get; set; }
public static Project Project { get; set; } = null;
public static LayerType ActiveLayer { get; set; }
public static DrawToolType ActiveDrawTool { get; set; }
public static ZoomType ActiveZoomType { get; set; }
public static EditorIni Ini;
public static bool DimOtherLayers = true;
public static bool ViewAllLayers = true;
public static bool Exit()
{
bool shouldClose = CloseProject(); //prompts for save
if (!shouldClose) return false;
if(Ini != null) Ini.Save();
Application.Exit();
return true;
}
public static void UpdateCaption()
{
string caption = "";
if (Project != null) {
caption += Project.Ini.Title;
if (Project.Dirty) caption += "*";
caption += " - ";
}
caption += "OpenXP Editor";
Form.Text = caption;
}
public static void Touch()
{
Project.Touch();
UpdateCaption();
}
public static void OnStartup()
{
Ini = new EditorIni();
if(PreloadProject != null)
{
LoadProject(PreloadProject);
PreloadProject = null;
}
}
public static void LoadProject(string projectFile)
{
if (projectFile != null)
{
if (File.Exists(projectFile))
{
Project = new Project();
Project.Path = new FileInfo(projectFile).FullName;
Project.Directory = new FileInfo(projectFile).Directory.FullName;
if (Directory.Exists(Project.Directory))
{
string msg = Project.Setup();
if (string.IsNullOrWhiteSpace(msg))
{
//all good
Ini.LastProjectDirectory = Project.Directory;
EnableControls();
Editor.SelectMap(Editor.Project.Database.System.edit_map_id);
UpdateCaption();
return;
}
else
{
//there was an error during project setup
MessageBox.Show(msg);
Project = null;
UpdateCaption();
return;
}
}
}
}
//If we are still here, something has gone wrong.
Project = null;
MessageBox.Show("Error: Could not read locate project data.");
UpdateCaption();
}
public static void OpenProject()
{
string projectFile = null;
//browse for a project
OpenFileDialog openDialog = new OpenFileDialog();
openDialog.InitialDirectory = Ini.LastProjectDirectory;
openDialog.Filter = "RPGXP Project (*.rxproj)|*.rxproj|All files (*.*)|*.*";
openDialog.FilterIndex = 1;
openDialog.RestoreDirectory = true;
openDialog.Multiselect = false;
if (openDialog.ShowDialog() == DialogResult.OK)
{
bool shouldContinue = CloseProject(); //out with the old
if (shouldContinue)
{
try
{
if (!string.IsNullOrWhiteSpace(openDialog.FileName))
{
projectFile = openDialog.FileName;
LoadProject(projectFile);
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
UpdateCaption();
return;
}
}
}
}
public static void NewProject()
{
//UpdateCaption(form);
//EnableControls(form);
//do this when they hit ok to create one:
//CloseProject(); //out with the old
}
public static void SaveProject()
{
if (Project != null) Project.Save();
UpdateCaption();
}
//can also be used to refresh the editor
//returns true if no project is active afterwards
public static bool CloseProject()
{
if (Project != null)
{
if (Project.Dirty)
{
DialogResult dr = System.Windows.Forms.MessageBox.Show(Form, "This project has unsaved changes. Would you like to save them now?", "Save Project?", MessageBoxButtons.YesNoCancel);
if (dr == DialogResult.Yes)
{
Project.Save();
} else if(dr == DialogResult.Cancel)
{
return false;
}
//no falls through to cleanup without saving
}
Form.Cleanup();
Project = null;
ChangeLayer(LayerType.EVENTS);
DisableControls();
UpdateCaption();
return true;
}
else return true;
}
public static void ChangeDrawTool(DrawToolType newTool)
{
if(newTool != ActiveDrawTool)
{
ActiveDrawTool = newTool;
//update editor selection display
Form.updateSelectedDrawTool(ActiveDrawTool);
}
}
public static void ToggleDimOtherLayers()
{
DimOtherLayers = !DimOtherLayers;
//toggle the checked state of the menu item
Form.SetDimOtherLayersChecked(DimOtherLayers);
}
public static void SetViewAllLayers(bool state)
{
ViewAllLayers = state;
}
public static void ChangeZoom(ZoomType newZoom)
{
if(newZoom != ActiveZoomType)
{
ActiveZoomType = newZoom;
Form.RepaintMap();
}
}
public static void ChangeLayer(LayerType newLayer)
{
if(newLayer != ActiveLayer)
{
ActiveLayer = newLayer;
if(ActiveLayer == LayerType.EVENTS)
{
Form.disableDrawTools();
} else
{
Form.enableDrawTools();
}
//update editor selection display
Form.updateSelectedLayer(ActiveLayer);
//force a repaint on the map
Form.RepaintMap();
}
}
public static void Playtest()
{
if (Project.Dirty)
{
DialogResult dr = System.Windows.Forms.MessageBox.Show(Form, "This project has unsaved changes. Would you like to save them now?", "Save Project?", MessageBoxButtons.YesNoCancel);
if (dr == DialogResult.Yes)
{
Project.Save();
UpdateCaption();
}
else if (dr == DialogResult.Cancel)
{
return;
}
}
string command = Path.Combine(Project.Directory, @"Game.exe");
string args = "debug";
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = command;
process.StartInfo.Arguments = args;
process.Start();
}
//call when a project is loaded/enabled to enable toolbars/menus
public static void EnableControls()
{
Form.enableControls();
}
//call when a project is unloaded/disabled to disable toolbars/menus
public static void DisableControls()
{
Form.disableControls();
}
public static void SelectMap(int id)
{
Form.SelectMap(id);
}
public static int GetSelectedMapId()
{
return Form.GetMapId();
}
}
}