forked from Violet-CLM/MLLE
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTileSetOrganizer.cs
More file actions
192 lines (181 loc) · 9.13 KB
/
Copy pathTileSetOrganizer.cs
File metadata and controls
192 lines (181 loc) · 9.13 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Ini;
namespace MLLE
{
public partial class TileSetOrganizer : Form
{
public TileSetOrganizer()
{
InitializeComponent();
}
List<ListViewItem> AllTilesets;
IniFile Settings;
Version VersionType;
string VersionString;
string TileDirectory;
internal void ShowForm(IniFile settings, Version versionType, string tileDirectory)
{
Settings = settings;
VersionType = versionType;
VersionString = VersionType.ToString();
TileDirectory = tileDirectory;
if (!Directory.Exists(tileDirectory))
Directory.CreateDirectory(tileDirectory);
listView1.Columns[listView1.Columns.Count - 1].Width = -2;
RefreshList();
ShowDialog();
}
private void RefreshList() {
AllTilesets = new List<ListViewItem>();
listView1.Items.Clear();
int iniTilesetIndex = 1;
while (true)
{
string iniText = Settings.IniReadValue("Tilesets", (iniTilesetIndex++).ToString());
if (string.IsNullOrWhiteSpace(iniText)) //run out of subsequently numbered ini lines
break;
var match = System.Text.RegularExpressions.Regex.Match(iniText, "\\s*\"([^\"]+)\",\\s*\"([^\"]+)\",\\s*\"([^\"]+)\",\\s*\"([^\"]+)\",\\s*(TSF|JJ2|BC|AGA|GorH)\\s*");
if (!match.Success) //something went wrong
continue; //oh well!
ListViewItem newRecord = new ListViewItem(match.Groups[2].Value);
newRecord.SubItems.Add(match.Groups[1].Value);
newRecord.SubItems.Add(match.Groups[4].Value);
newRecord.SubItems.Add(match.Groups[3].Value);
newRecord.Tag = match.Groups[5].Value;
ThinkAboutAdding(newRecord);
}
}
void SaveList()
{
int iniTilesetIndex = 0;
AllTilesets.Sort((x,y) => x.SubItems[1].Text.CompareTo(y.SubItems[1].Text));
while (true)
{
string keyName = (iniTilesetIndex + 1).ToString();
if (iniTilesetIndex < AllTilesets.Count)
{
ListViewItem record = AllTilesets[iniTilesetIndex];
Settings.IniWriteValue("Tilesets", keyName, "\"" + record.SubItems[1].Text + "\", \"" + record.Text + "\", \"" + record.SubItems[3].Text + "\", \"" + record.SubItems[2].Text + "\", " + (record.Tag as string));
}
else if (!string.IsNullOrWhiteSpace(Settings.IniReadValue("Tilesets", keyName))) //leftover line number that doesn't need to exist anymore after the list got shorter
Settings.IniWriteValue("Tilesets", keyName, null); //delete it
else
break;
++iniTilesetIndex;
}
}
private void OK_Click(object sender, EventArgs e)
{
Close();
}
private void Add_Click(object sender, EventArgs e)
{
var newRecord = new ListViewItem();
for (int i = 0; i < 3; ++i)
newRecord.SubItems.Add(new ListViewItem.ListViewSubItem());
newRecord.Tag = VersionString;
Select(newRecord);
}
private bool ThinkAboutAdding(ListViewItem newRecord)
{
if (!AllTilesets.Contains(newRecord))
{
AllTilesets.Add(newRecord); //regardless of version, because we'll be saving these later
if ((newRecord.Tag as string) == VersionString) //otherwise it's a tileset destined for a different game
listView1.Items.Add(newRecord);
return true;
}
return false;
}
private void Select(ListViewItem newRecord)
{
if (new SelectTileSet().ShowForm(newRecord, TileDirectory, VersionType != Version.AGA ? ".j2t" : ".til"))
{
ThinkAboutAdding(newRecord);
SaveList();
}
}
private void listView1_DoubleClick(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 1)
Select(listView1.SelectedItems[0]);
}
private void Remove_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 1 && MessageBox.Show("This will remove MLLE's record of which image files you used to make this tileset. The tileset file will still exist, but you will not be able to rebuild it. Do you wish to continue?", "Remove Tileset Listing", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
AllTilesets.Remove(listView1.SelectedItems[0]);
listView1.Items.Remove(listView1.SelectedItems[0]);
SaveList();
}
}
private void Build_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 1)
{
var record = listView1.SelectedItems[0];
var J2T = new J2TFile();
J2T.VersionType = VersionType;
Bitmap image = new Bitmap(1,1), mask = new Bitmap(1,1);
string sourceFilepath = Path.Combine(TileDirectory, record.SubItems[2].Text);
try
{
image = new Bitmap(sourceFilepath);
sourceFilepath = Path.Combine(TileDirectory, record.SubItems[3].Text);
mask = new Bitmap(sourceFilepath);
switch (J2T.Build(image, mask, record.Text))
{
case BuildResults.DifferentDimensions:
MessageBox.Show(String.Format("The image and the mask must be the same dimensions. Your image is {0} by {1}, and your mask is {2} by {3}.", image.Width, image.Height, mask.Width, mask.Height), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
case BuildResults.BadDimensions:
MessageBox.Show(String.Format("A tileset image must be 320 pixels wide and a multiple of 32 pixels high. Your image is {0} by {1}.", image.Width, image.Height), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
case BuildResults.ImageWrongFormat:
MessageBox.Show("Your image file is saved in an incorrect color mode. A tileset image must use 8-bit color with no transparency (color 0 is used for transparency instead).", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
case BuildResults.MaskWrongFormat:
MessageBox.Show("Your mask file is saved in an incorrect color mode. A tileset mask must use 8-bit color with no transparency (color 0 is used for transparency instead).", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
case BuildResults.TooBigForVersion:
MessageBox.Show(String.Format("Your tileset images are too big. The tile limit for a {0} tileset is {1} tiles, but your tileset contains {2}.", J2File.FullVersionNames[J2T.VersionType], J2T.MaxTiles, (image.Height / 32 * 10)), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
case BuildResults.Success:
string fullFilePath = Path.Combine(Directory.GetParent(TileDirectory).ToString(), record.SubItems[1].Text);
if (J2T.Save(fullFilePath) != SavingResults.Success)
MessageBox.Show("Something went wrong.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
MessageBox.Show(String.Format("OK: Your tileset '{0}' has been built at {1} with {2} tiles ({3} Kb).", record.Text, fullFilePath, (image.Height / 32 * 10), new System.IO.FileInfo(fullFilePath).Length / 1024), "Successful Build", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}
}
catch (FileNotFoundException)
{
MessageBox.Show(sourceFilepath + " not found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch
{
MessageBox.Show(sourceFilepath + " does not use an image format supported by this program. (Try PNG, GIF, TIFF, or BMP.)", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
image.Dispose();
mask.Dispose();
}
}
}
private void Refresh_Click(object sender, EventArgs e)
{
RefreshList();
}
}
}