-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAddItemDialog.cs
170 lines (150 loc) · 6.42 KB
/
AddItemDialog.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
namespace XNALara
{
public partial class AddItemDialog : Form
{
private Game game;
private List<ItemDesc> selectedItems = new List<ItemDesc>();
public AddItemDialog(Game game, List<Item> existingItems) {
this.game = game;
InitializeComponent(game.EnableAddModelMultiSelect);
ScanSubDirectories("data");
if (treeView.Nodes.Count > 0) {
treeView.Nodes[0].Expand();
}
}
public ItemDesc[] SelectedItems {
get { return selectedItems.ToArray(); }
}
public bool InFrontOfCameraChecked {
get { return checkBoxInFrontOfCamera.Checked; }
set { checkBoxInFrontOfCamera.Checked = value; }
}
private void ScanSubDirectories(string directoryName) {
ScanSubDirectories(directoryName, null, null);
}
private void ScanSubDirectories(string directoryName, string rootDirectory, TreeNode treeRoot) {
string directoryRelativePath =
(rootDirectory != null ? rootDirectory + "\\" + directoryName : directoryName);
if (directoryRelativePath == "data\\common" ||
directoryRelativePath.StartsWith("data\\skydome_")) {
return;
}
DirectoryInfo[] directories = new DirectoryInfo(directoryRelativePath).GetDirectories();
if (directories.Length > 0) {
SortDirectories(directories);
TreeNode treeNode = AddNodeToTree(directoryName, treeRoot);
foreach (DirectoryInfo directory in directories) {
ScanSubDirectories(directory.Name, directoryRelativePath, treeNode);
}
}
else {
if (rootDirectory != null) {
ItemType itemType = CheckDirectory(directoryName, rootDirectory);
if (itemType != ItemType.None) {
AddNodeToTree(directoryName, treeRoot, itemType, directoryRelativePath);
}
}
}
}
private void SortDirectories(DirectoryInfo[] directories) {
Array.Sort<DirectoryInfo>(directories,
delegate(DirectoryInfo d1, DirectoryInfo d2) {
return d1.Name.CompareTo(d2.Name);
}
);
}
private TreeNode AddNodeToTree(string name, TreeNode parentNode) {
TreeNode childNode = new TreeNode();
childNode.Text = name;
if (parentNode != null) {
parentNode.Nodes.Add(childNode);
}
else {
treeView.Nodes.Add(childNode);
}
return childNode;
}
private TreeNode AddNodeToTree(string name, TreeNode parentNode, ItemType itemType, string dirPath) {
TreeNode childNode = new TreeNode();
childNode.Text = name;
childNode.Tag = new ItemDesc(name, itemType, dirPath);
if (parentNode != null) {
parentNode.Nodes.Add(childNode);
}
else {
treeView.Nodes.Add(childNode);
}
return childNode;
}
private ItemType CheckDirectory(string directoryName, string rootDirectory) {
string directoryRelativePath = rootDirectory + "\\" + directoryName;
FileInfo[] filesMesh = new DirectoryInfo(directoryRelativePath).GetFiles("*.mesh");
FileInfo[] filesObj = new DirectoryInfo(directoryRelativePath).GetFiles("*.obj");
if (filesMesh.Length > 0 && filesObj.Length > 0 ||
filesMesh.Length > 1 || filesObj.Length > 1) {
MessageBox.Show("Data folder \"" + directoryRelativePath + "\" contains multiple mesh files (ambiguous).\nSkipping ...",
"Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return ItemType.None;
}
ItemType itemType = ItemType.None;
try {
itemType = (ItemType)Enum.Parse(typeof(ItemType), directoryName, true);
}
catch (Exception) {
if (filesMesh.Length == 0 && filesObj.Length == 0) {
MessageBox.Show("Data folder \"" + directoryRelativePath + "\" contains no mesh file.\nSkipping ...",
"Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return ItemType.None;
}
if (filesMesh.Length == 0) {
itemType = ItemType.ExternObj;
}
else {
string name = null;
try {
name = filesMesh[0].Name;
name = name.Substring(0, name.Length - 5);
itemType = (ItemType)Enum.Parse(typeof(ItemType), name, true);
}
catch (Exception) {
MessageBox.Show("Data folder \"" + directoryRelativePath + "\" contains unknown mesh type \"" + name + "\".\nSkipping ...",
"Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return ItemType.None;
}
}
}
return itemType;
}
private void ButtonOKClick(object sender, EventArgs e) {
TreeNode[] selectedNodes = null;
if (treeView is TreeViewMultiSelect) {
selectedNodes = ((TreeViewMultiSelect)treeView).SelectedNodes;
}
else {
if (treeView.SelectedNode != null) {
selectedNodes = new TreeNode[] { treeView.SelectedNode };
}
}
selectedItems.Clear();
if (selectedNodes != null) {
foreach (TreeNode node in selectedNodes) {
ItemDesc itemDesc = (ItemDesc)node.Tag;
if (itemDesc != null) {
selectedItems.Add(itemDesc);
}
}
}
this.Close();
}
protected override void OnActivated(EventArgs e) {
game.HasFocus = false;
}
protected override void OnDeactivate(EventArgs e) {
game.HasFocus = true;
}
}
}