-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormActFile.cs
393 lines (390 loc) · 16 KB
/
FormActFile.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
using GS_ActEdit.Format;
using GS_ActEdit.Format.Serialization;
using GS_ActEdit.UI;
using GS_ActEdit.UI.TreeNodes;
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 GS_ActEdit
{
partial class FormActFile : Form
{
public ActObject file;
public string filename;
public FileNode root;
private Act2DMapLayoutObject current_layout;
public FormActFile()
{
InitializeComponent();
}
public void SetFileNameToCaption(string value)
{
this.filename = value;
this.Text = "Act File Editor - " + Path.GetFileName(value);
}
public void SetNodeSelected(TreeNode tn)
{
this.TreeViewMain.SelectedNode = tn;
tn.EnsureVisible();
}
private void UpdateObjectList(Act2DMapLayoutObject layout)
{
this.current_layout = layout;
this.ListViewLayout.Items.Clear();
if (layout == null)
{
return;
}
foreach (Act2DMapLayoutObject.Element current in layout.elements)
{
this.ListViewLayout.Items.Add(new ListViewItemElement(current, file));
}
}
public void AddElementToList()
{
if (this.current_layout != null)
{
Act2DMapLayoutObject.Element element = new Act2DMapLayoutObject.Element();
this.current_layout.elements.Add(element);
this.ListViewLayout.Items.Add(new ListViewItemElement(element, file));
}
}
private void UpdateTreeViewMain()
{
this.TreeViewMain.Nodes.Clear();
this.root = new FileNode(this.file, this);
this.TreeViewMain.Nodes.Add(this.root);
this.TreeViewMain.SelectedNode = this.root;
}
private void UpdateTreeViewSelect(TreeNode tn)
{
if (tn != null && tn is AbstractNode)
{
AbstractNode abstractNode = (AbstractNode)tn;
this.PropertyGridMain.SelectedObject = abstractNode.GetPropertyObject();
this.PropertyGridMain.Tag = abstractNode;
}
else
{
this.PropertyGridMain.SelectedObject = null;
this.PropertyGridMain.Tag = null;
}
if (tn != null && tn is MapLayoutNode)
{
this.UpdateObjectList(((MapLayoutNode)tn).GetLayout());
return;
}
this.UpdateObjectList(null);
}
private void TreeViewMain_AfterSelect(object sender, TreeViewEventArgs e)
{
this.UpdateTreeViewSelect(e.Node);
}
private void PropertyGridMain_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
if (this.PropertyGridMain.Tag != null && this.PropertyGridMain.Tag is AbstractNode)
{
((AbstractNode)this.PropertyGridMain.Tag).UpdateObject();
}
}
private void TreeViewMain_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
this.TreeViewMain.SelectedNode = e.Node;
}
private T GetCurrentNodeAs<T>() where T : TreeNode
{
if (this.TreeViewMain.SelectedNode != null && this.TreeViewMain.SelectedNode is T)
{
return (T)((object)this.TreeViewMain.SelectedNode);
}
return default(T);
}
private void Menu_File_Save_Click(object sender, EventArgs e)
{
if (!this.file.SaveActToFile(this.filename))
{
MessageBox.Show("Can not save to act file: " + this.SaveDialogFile.FileName);
}
}
private void Menu_File_SaveAs_Click(object sender, EventArgs e)
{
if (this.SaveDialogFile.ShowDialog() == DialogResult.OK)
{
if (!this.file.SaveActToFile(this.SaveDialogFile.FileName))
{
MessageBox.Show("Can not save to act file: " + this.SaveDialogFile.FileName);
return;
}
this.SetFileNameToCaption(this.SaveDialogFile.FileName);
}
}
private void Menu_File_Refresh_Click(object sender, EventArgs e)
{
if (this.GetCurrentNodeAs<FileNode>() != null)
{
this.GetCurrentNodeAs<FileNode>().UpdateObject();
this.UpdateResList();
}
}
private void Menu_File_Layer_NewLayer_Click(object sender, EventArgs e)
{
if (this.root != null)
{
this.root.AddLayer();
}
}
private void Menu_File_Resources_LinkToMcdFile_Click(object sender, EventArgs e)
{
if (this.FolderBrowserRoot.ShowDialog() != DialogResult.OK)
{
return;
}
string selectedPath = this.FolderBrowserRoot.SelectedPath;
this.file.LinkRootFolder(selectedPath);
this.UpdateTreeViewMain();
this.UpdateResList();
}
private void Menu_Code_EditCode_Click(object sender, EventArgs e)
{
if (this.GetCurrentNodeAs<CodeNode>() != null)
{
this.GetCurrentNodeAs<CodeNode>().EditCode();
}
}
private void FormActFile_Shown(object sender, EventArgs e)
{
string text;
if (Environment.GetCommandLineArgs().Length == 2)
{
text = Environment.GetCommandLineArgs()[1];
}
else
{
if (this.OpenDialogFile.ShowDialog() != DialogResult.OK)
{
base.Close();
return;
}
text = this.OpenDialogFile.FileName;
}
ActObject actObject = ActFile.ReadActFromFile(text);
if (actObject == null)
{
MessageBox.Show("Can not open act file: " + text);
base.Close();
return;
}
this.SetFileNameToCaption(text);
this.file = actObject;
this.UpdateTreeViewMain();
}
private void ListViewLayout_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.ListViewLayout.SelectedItems.Count == 0)
{
return;
}
ListViewItemElement listViewItemElement = this.ListViewLayout.SelectedItems.OfType<ListViewItemElement>().First<ListViewItemElement>();
this.PropertyGridMain.SelectedObject = listViewItemElement.GetPropertyObject();
this.PropertyGridMain.Tag = null;
this.PictureResourcePreview.Image = this.file.CreateBitmapForResource(listViewItemElement.GetElement().resourceID);
}
private void UpdateResList()
{
this.ListViewResources.Items.Clear();
if (this.file.mcd == null)
{
return;
}
foreach (ChipElement current in this.file.mcd.chips)
{
this.ListViewResources.Items.Add(new ListViewItemResource(current.chipID, file));
}
}
private void ListViewResources_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && this.ListViewResources.FocusedItem.Bounds.Contains(e.Location))
{
this.ContextMenuResource.Show(Cursor.Position);
}
}
private void Menu_Resource_AddToLayout_Click(object sender, EventArgs e)
{
ListViewItemResource listViewItemResource = (ListViewItemResource)this.ListViewResources.FocusedItem;
if (listViewItemResource == null)
{
return;
}
Act2DMapLayoutObject.Element element = Act2DMapLayoutObject.Element.CreateDefault(listViewItemResource.id);
this.current_layout.elements.Add(element);
this.ListViewLayout.Items.Add(new ListViewItemElement(element, file));
}
private void ListViewResources_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.ListViewResources.SelectedItems.Count == 0)
{
return;
}
ListViewItemResource listViewItemResource = this.ListViewResources.SelectedItems.OfType<ListViewItemResource>().First<ListViewItemResource>();
this.PictureResourcePreview.Image = this.file.CreateBitmapForResource(listViewItemResource.id);
}
private void PictureResourcePreview_DoubleClick(object sender, EventArgs e)
{
FormShowImage formShowImage = new FormShowImage(this.PictureResourcePreview.Image);
formShowImage.Show(null);
}
private void Menu_Layer_ShowPreview_Click(object sender, EventArgs e)
{
LayerNode currentNodeAs = this.GetCurrentNodeAs<LayerNode>();
if (currentNodeAs != null)
{
FormPreview formPreview = new FormPreview(currentNodeAs.layer.GetLayout(), this.file);
formPreview.Show();
}
}
private object GetDataFromElement(Act2DMapLayoutObject.Element e)
{
object result;
using (MemoryStream memoryStream = new MemoryStream())
{
using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
{
BinaryOutputStream s = new BinaryOutputStream(binaryWriter);
e.Write(s);
result = memoryStream.ToArray();
}
}
return result;
}
private Act2DMapLayoutObject.Element GetElementFromData(object obj)
{
if (obj is byte[])
{
byte[] buffer = (byte[])obj;
using (MemoryStream memoryStream = new MemoryStream(buffer))
{
using (BinaryReader binaryReader = new BinaryReader(memoryStream))
{
BinaryInputStream s = new BinaryInputStream(binaryReader);
Act2DMapLayoutObject.Element element = new Act2DMapLayoutObject.Element();
element.Read(s);
return element;
}
}
}
return null;
}
private void Menu_Element_Cut_Click(object sender, EventArgs e)
{
if (this.ListViewLayout.SelectedItems.Count == 1)
{
ListViewItemElement listViewItemElement = this.ListViewLayout.SelectedItems.OfType<ListViewItemElement>().First<ListViewItemElement>();
Act2DMapLayoutObject.Element element = listViewItemElement.GetElement();
Clipboard.SetData("ActMapElement", this.GetDataFromElement(element));
this.current_layout.elements.RemoveAt(listViewItemElement.Index);
listViewItemElement.Remove();
}
}
private void ContextMenuElementList_Opening(object sender, CancelEventArgs e)
{
this.cutToolStripMenuItem.Enabled = (this.ListViewLayout.SelectedItems.Count == 1);
this.copyToolStripMenuItem.Enabled = (this.ListViewLayout.SelectedItems.Count == 1);
this.pasteBeforeToolStripMenuItem.Enabled = (Clipboard.ContainsData("ActMapElement") && this.ListViewLayout.SelectedItems.Count == 1);
this.pasteAfterToolStripMenuItem.Enabled = (Clipboard.ContainsData("ActMapElement") && this.ListViewLayout.SelectedItems.Count == 1);
this.deleteToolStripMenuItem.Enabled = (this.ListViewLayout.SelectedItems.Count == 1);
this.clearAllToolStripMenuItem.Enabled = (this.ListViewLayout.Items.Count > 0);
}
private void ListViewLayout_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
this.ContextMenuElementList.Show(this.ListViewLayout, e.Location);
}
}
private void Menu_Element_Copy_Click(object sender, EventArgs e)
{
if (this.ListViewLayout.SelectedItems.Count == 1)
{
ListViewItemElement listViewItemElement = this.ListViewLayout.SelectedItems.OfType<ListViewItemElement>().First<ListViewItemElement>();
Act2DMapLayoutObject.Element element = listViewItemElement.GetElement();
Clipboard.SetData("ActMapElement", this.GetDataFromElement(element));
}
}
private void Menu_Element_PasteBefore_Click(object sender, EventArgs e)
{
if (Clipboard.ContainsData("ActMapElement") && this.ListViewLayout.SelectedItems.Count == 1)
{
Act2DMapLayoutObject.Element elementFromData = this.GetElementFromData(Clipboard.GetData("ActMapElement"));
if (elementFromData != null)
{
int index = this.ListViewLayout.SelectedItems.OfType<ListViewItemElement>().First<ListViewItemElement>().Index;
this.current_layout.elements.Insert(index, elementFromData);
this.ListViewLayout.Items.Insert(index, new ListViewItemElement(elementFromData, file));
}
}
}
private void Menu_Element_PasteAfter_Click(object sender, EventArgs e)
{
if (Clipboard.ContainsData("ActMapElement") && this.ListViewLayout.SelectedItems.Count == 1)
{
Act2DMapLayoutObject.Element elementFromData = this.GetElementFromData(Clipboard.GetData("ActMapElement"));
if (elementFromData != null)
{
int index = this.ListViewLayout.SelectedItems.OfType<ListViewItemElement>().First<ListViewItemElement>().Index + 1;
this.current_layout.elements.Insert(index, elementFromData);
this.ListViewLayout.Items.Insert(index, new ListViewItemElement(elementFromData, file));
}
}
}
private void Menu_Element_Delete_Click(object sender, EventArgs e)
{
if (this.ListViewLayout.SelectedItems.Count == 1)
{
ListViewItemElement listViewItemElement = this.ListViewLayout.SelectedItems.OfType<ListViewItemElement>().First<ListViewItemElement>();
this.current_layout.elements.RemoveAt(listViewItemElement.Index);
listViewItemElement.Remove();
}
}
private void Menu_Layer_Delete_Click(object sender, EventArgs e)
{
LayerNode currentNodeAs = this.GetCurrentNodeAs<LayerNode>();
if (currentNodeAs != null)
{
this.file.layers.Remove(currentNodeAs.layer);
currentNodeAs.Remove();
}
}
private void Menu_File_PreviewAllLayers_Click(object sender, EventArgs e)
{
List<LayerRenderOption> layers = file.renderLayers;
new FormMultiPreview(this.file, layers).Show();
}
private void Menu_Element_ClearAll_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Clear all elements?", "Clear all", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
this.current_layout.elements.Clear();
this.ListViewLayout.Items.Clear();
}
}
private void Menu_Layer_CalculateAABB_Click(object sender, EventArgs e)
{
LayerNode currentNodeAs = this.GetCurrentNodeAs<LayerNode>();
if (currentNodeAs != null)
{
currentNodeAs.layer.CalculateAABB(this.file);
}
}
private void setPreviewParametersToolStripMenuItem_Click(object sender, EventArgs e)
{
new FormLayerRenderOptions(file).ShowDialog();
}
}
}