/[MorrowindCustomClothingSlotCreator]/trunk/MorrowindCustomClothingSlotCreator/Form1.cs
ViewVC logotype

Contents of /trunk/MorrowindCustomClothingSlotCreator/Form1.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 43 - (show annotations) (download)
Sat Jul 30 15:11:12 2011 UTC (8 years, 5 months ago) by william
File size: 10781 byte(s)
** tidy up code

1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using libTES3FileFormat;
9
10 namespace MorrowindCustomClothingSlotCreator
11 {
12
13 public partial class Form1 : Form
14 {
15 public static MorrowindPathHelper MorrowindData = new MorrowindPathHelper();
16 public static string MorrowindPath = MorrowindData.MorrowindInstallPath;
17 public static string MorrowindDataFilesPath = MorrowindPath + "\\Data Files\\";
18
19 private TES3PluginLoader TES3PluginLoader;
20
21 public Form1()
22 {
23 InitializeComponent();
24 this.TES3PluginLoader = new TES3PluginLoader();
25
26 foreach (int val in Enum.GetValues(typeof(ClothingTypes))) { comboClothingType.Items.Add(Enum.GetName(typeof(ClothingTypes), val)); }
27 comboCustomClothingSlotIndex.Items.Add("Default");
28 for (int i = 1; i < 0x10; i++) { comboCustomClothingSlotIndex.Items.Add("0x" + i.ToString("X2")); }
29
30 }
31
32 public bool LoadPlugin(string pluginFile)
33 {
34 bool loaded = this.TES3PluginLoader.Load();
35
36 if (loaded)
37 {
38 foreach (KeyValuePair<string,ClothingEntry> entry in this.TES3PluginLoader.ClothingSection.ClothingEntries)
39 {
40 lstClothingEntries.Items.Add(entry.Key);
41 }
42 MessageBox.Show("Successfully Loaded Plugin: " + pluginFile, "Successfully Loaded File:", MessageBoxButtons.OK, MessageBoxIcon.Information);
43 }
44
45 return loaded;
46 }
47
48
49 private void mnuItemExit_Click(object sender, EventArgs e)
50 {
51 this.Close();
52 }
53 private void mnuItemSavePlugin_Click(object sender, EventArgs e)
54 {
55 if (!this.TES3PluginLoader.PluginLoaded) return;
56 if (!this.TES3PluginLoader.PluginSavingIsImplemented)
57 {
58 MessageBox.Show("Plugin Saving has not been implemetned", "Unimplemented Function:", MessageBoxButtons.OK, MessageBoxIcon.Error);
59 return;
60 }
61 if (!this.TES3PluginLoader.Save())
62 {
63 MessageBox.Show("Failed to Save Plugin: " + this.TES3PluginLoader.PluginFile + "\n\nReason: \n\n" + this.TES3PluginLoader.PluginLoadFailureReason, "Failed to Save File:", MessageBoxButtons.OK, MessageBoxIcon.Error);
64 }
65 else
66 {
67 MessageBox.Show("Successfully Saved Plugin: " + this.TES3PluginLoader.PluginFile, "Successfully Saved File:", MessageBoxButtons.OK, MessageBoxIcon.Information);
68 }
69 }
70 private void mnuItemSavePluginAs_Click(object sender, EventArgs e)
71 {
72 if (!this.TES3PluginLoader.PluginLoaded) return;
73
74 if (!this.TES3PluginLoader.PluginSavingIsImplemented)
75 {
76 MessageBox.Show("Plugin Saving has not been implemetned", "Unimplemented Function:", MessageBoxButtons.OK, MessageBoxIcon.Error);
77 return;
78 }
79
80 DialogResult result = PluginSaver.ShowDialog();
81 string PluginFile = PluginSaver.FileName;
82 if (result != DialogResult.OK) return;
83
84 if (!this.TES3PluginLoader.SaveAs(PluginFile))
85 {
86 MessageBox.Show("Failed to Save Plugin As: " + PluginFile + "\n\nReason: \n\n" + this.TES3PluginLoader.PluginLoadFailureReason, "Failed to Save File As:", MessageBoxButtons.OK, MessageBoxIcon.Error);
87 }
88 else
89 {
90 MessageBox.Show("Successfully Saved Plugin: " + PluginFile, "Successfully Saved File:", MessageBoxButtons.OK, MessageBoxIcon.Information);
91 }
92 }
93 private void mnuItemOpenPlugin_Click(object sender, EventArgs e)
94 {
95 this.log.Clear();
96 PluginLoader.InitialDirectory = MorrowindDataFilesPath;
97 DialogResult result = PluginLoader.ShowDialog();
98 string PluginFile = PluginLoader.FileName;
99 if (result != DialogResult.OK) return;
100 //MessageBox.Show("Load Plugin: " + PluginLoader.FileName);
101 this.TES3PluginLoader = new TES3PluginLoader(PluginFile, this.log);
102 if (!this.LoadPlugin(PluginFile))
103 {
104 MessageBox.Show("Failed to Load Plugin: " + PluginFile + "\n\nReason: \n\n" + this.TES3PluginLoader.PluginLoadFailureReason, "Failed to Load File:", MessageBoxButtons.OK, MessageBoxIcon.Error);
105 }
106 }
107
108 private void lstClothingEntries_SelectedIndexChanged(object sender, EventArgs e)
109 {
110 ClothingEntry chosen_entry = null;
111 editor_flow.Enabled = false;
112 btnCommitEntry.Tag = null;
113 foreach (KeyValuePair<string, ClothingEntry> entry in this.TES3PluginLoader.ClothingSection.ClothingEntries)
114 {
115 string key = lstClothingEntries.Items[lstClothingEntries.SelectedIndex].ToString();
116 if (key == entry.Key) { chosen_entry = entry.Value; break; }
117 }
118 if (chosen_entry == null) return;
119 this.PopulateEditorData(chosen_entry);
120 }
121
122 private void PopulateEditorData(ClothingEntry entry)
123 {
124 if (entry == null) return;
125 editor_flow.Enabled = true;
126
127 btnCommitEntry.Tag = entry;
128
129 txtClothingName.Text = entry.CLOT_NAME;
130
131 int actual_type = entry.CLOT_CTDT_TYPE;
132 //int actual_type = 0x00000F9;
133
134 byte type = (byte)(actual_type & 0x0000000F);
135 byte index = (byte)((actual_type & 0x000000F0) >> 4);
136
137 // Set the clothing type in the drop down
138 foreach (string item in comboClothingType.Items)
139 {
140 string type_name = Enum.GetName(typeof(ClothingTypes), type);
141 bool HaveUnknownType = false;
142 foreach (int val in Enum.GetValues(typeof(ClothingTypes)))
143 {
144 if (type == val) { HaveUnknownType = false; break; }
145 else { HaveUnknownType = true; }
146 }
147 if (HaveUnknownType) { comboClothingType.Text = "Unknown"; break; }
148 if (item == type_name)
149 {
150 comboClothingType.SelectedIndex = comboClothingType.Items.IndexOf(item);
151 comboClothingType.Text = item;
152 break;
153 }
154 }
155 foreach (string item_index in comboCustomClothingSlotIndex.Items)
156 {
157 if (index == 0) { comboCustomClothingSlotIndex.Text = "Default"; break; }
158 else if (item_index == string.Format("0x{0:X2}",index))
159 {
160 comboCustomClothingSlotIndex.SelectedIndex = comboCustomClothingSlotIndex.Items.IndexOf(item_index);
161 comboCustomClothingSlotIndex.Text = item_index.ToString();
162 break;
163 }
164 }
165 }
166
167 private void Form1_Shown(object sender, EventArgs e) { editor_flow.Enabled = false; }
168
169 private void btnCommitEntry_Click(object sender, EventArgs e)
170 {
171 ClothingEntry entry = (btnCommitEntry.Tag as ClothingEntry);
172 if (entry == null) return;
173 int unknown1 = 0;
174 byte type = 0;
175 byte index = 0;
176 unknown1 = entry.CLOT_CTDT_TYPE;
177 if (comboClothingType.Items[comboClothingType.SelectedIndex].ToString() == "Unknown") { type = (byte)(unknown1 & 0x0000000F); }
178 else
179 {
180 string type_name = comboClothingType.Items[comboClothingType.SelectedIndex].ToString();
181 byte type_value = (byte)((entry.CLOT_CTDT_TYPE & 0x000000FF) >> 0);
182 foreach (int val in Enum.GetValues(typeof(ClothingTypes))) { if (Enum.GetName(typeof(ClothingTypes), val) == type_name) { type_value = (byte)val; break; } } type = Convert.ToByte(type_value);
183 }
184 if (comboCustomClothingSlotIndex.Items[comboCustomClothingSlotIndex.SelectedIndex].ToString() == "Default") { index = 0; }
185 else { index = Convert.ToByte(comboCustomClothingSlotIndex.Items[comboCustomClothingSlotIndex.SelectedIndex].ToString(),16); }
186 int SlotIndex = (index << 4) | type | (int)(unknown1 & 0xFFFFFF00);
187 int custom_slot = (index << 4) | type;
188 if (CommitClothingEntry(entry, SlotIndex))
189 {
190 List<string> duplicate_entries = new List<string>();
191 if (CheckForDuplicateSlotEntries(custom_slot, (index == 0), out duplicate_entries)) { if (duplicate_entries.Count > 1) { MessageBox.Show(string.Format("Warning one or more entries are this custom slot: 0x{0:X2}\nClothing Entries sharing the index:\n{1}", custom_slot, duplicate_entries.ToStr()), "Duplicate Slot Index:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
192 MessageBox.Show("Successfully Commited: " + entry.CLOT_NAME, "Clothing Entry Commit:", MessageBoxButtons.OK, MessageBoxIcon.Information);
193 }
194 else { MessageBox.Show("Failed to Commit: " + entry.CLOT_NAME, "Clothing Entry Commit:", MessageBoxButtons.OK, MessageBoxIcon.Error); }
195 }
196 private bool CheckForDuplicateSlotEntries(int SlotIndex, bool isDefaultSlot, out List<string> entry_names)
197 {
198 entry_names = new List<string>();
199 List<ClothingEntry> entries = new List<ClothingEntry>();
200 foreach (KeyValuePair<string, ClothingEntry> pair in this.TES3PluginLoader.ClothingSection.ClothingEntries)
201 {
202 int actual_type = pair.Value.CLOT_CTDT_TYPE;
203 byte type = (byte)(actual_type & 0x0000000F);
204 byte index = (byte)((actual_type & 0x000000F0) >> 4);
205 if (index == 0) continue;
206 int check_slot = (index << 4) | type;
207 if (check_slot == SlotIndex) { entries.Add(pair.Value); }
208 }
209 if (entries.Count > 1) { foreach (ClothingEntry entry in entries) { entry_names.Add(entry.CLOT_NAME); } return true; }
210 return false;
211 }
212 private bool CommitClothingEntry(ClothingEntry entry, int SlotIndex)
213 {
214 bool ret = false;
215 entry.CLOT_CTDT_TYPE = SlotIndex;
216 ret = entry.Commit(this.TES3PluginLoader);
217 return ret;
218 }
219 }
220 }

  ViewVC Help
Powered by ViewVC 1.1.22