-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTilesetFixer.csx
224 lines (198 loc) · 6.58 KB
/
TilesetFixer.csx
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
/*
This script is the messiest thing ive ever made.
But if it aint broke, dont fix it.
*/
using System.Text;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using System.Drawing;
using UndertaleModLib.Util;
using ImageMagick;
using System.Windows.Forms;
// Ensure data is loaded before accessing it
EnsureDataLoaded();
string root_folder = Path.GetDirectoryName(FilePath) + "\\";
string script_folder = $"{root_folder}Fixed_Tilesets\\";
Directory.CreateDirectory(script_folder);
TextureWorker worker = new TextureWorker();
IMagickImage<byte> final_result = null;
public class TileData
{
public TileData(UndertaleBackground bg)
{
name = bg.Name.Content;
width = (int)bg.GMS2TileWidth;
height = (int)bg.GMS2TileHeight;
xoff = (int)bg.GMS2OutputBorderX;
yoff = (int)bg.GMS2OutputBorderX;
hsep = (int)bg.GMS2OutputBorderX * 2;
vsep = (int)bg.GMS2OutputBorderY * 2;
out_tilehborder = (int)bg.GMS2OutputBorderX;
out_tilevborder = (int)bg.GMS2OutputBorderY;
columns = (int)bg.GMS2TileColumns;
tile_count = (int)bg.GMS2TileCount;
}
public string name { get; set; }
public int width { get; set; }
public int height { get; set; }
public int xoff { get; set; }
public int yoff { get; set; }
public int hsep { get; set; }
public int vsep { get; set; }
public int out_tilehborder { get; set; }
public int out_tilevborder { get; set; }
public int columns { get; set; }
public int tile_count { get; set; }
}
// create the directory
Directory.CreateDirectory($"{root_folder}Corrected_Tilesets");
public Bitmap ConvertToBitmap(IMagickImage<byte> img)
{
using (MemoryStream memoryStream = new MemoryStream())
{
img.Write(memoryStream, MagickFormat.Png);
memoryStream.Position = 0;
return new Bitmap(memoryStream);
}
}
public Bitmap? ProcessTileset(UndertaleBackground bg, int rows)
{
// create tiledata based on the background
TileData td = new TileData(bg);
// obtain the image for the background
using var image = worker.GetTextureFor(bg.Texture, bg.Name.Content);
// seperate the image into a list of tiles.
var tiledImage = image.CropToTiles(td.width + td.hsep, td.height + td.vsep).ToList();
max_rows = tiledImage.Count;
// set the geometry to the tileset dimensions
var geometry = new MagickGeometry(td.width, td.height);
//remove checkerboard
tiledImage[0] = new MagickImage(MagickColors.Transparent, td.width, td.height);
// iterate through each tile, fixing the padding by setting the tileset to the correct dimensions.
for (int i = 0; i <= tiledImage.Count - 1; i++)
{
tiledImage[i].Extent(geometry, Gravity.Center, MagickColors.Transparent);
}
using var exported_image = new MagickImageCollection();
// construct the image by each tile.
foreach (var tile in tiledImage)
{
exported_image.Add(tile);
}
MontageSettings ms = new MontageSettings()
{
Geometry = geometry,
TileGeometry = new MagickGeometry(rows, 0),
BackgroundColor = MagickColors.None,
Gravity = Gravity.Center
};
// save the image to a file when complete.
using (var result = exported_image.Montage(ms))
{
final_result = result.Clone();
var resized_result = result;
resized_result.Resize(400, 400);
return ConvertToBitmap(resized_result);
}
}
// variables to manage state
int current_index = 0;
int rows = 0;
int tileset_count = Data.Backgrounds.Count;
int max_rows = 0;
// create form
Form form = new Form();
PictureBox display = new PictureBox();
TrackBar row_slider = new TrackBar();
TextBox value_box = new TextBox();
TrackBar index_slider = new TrackBar();
TextBox index_box = new TextBox();
Button save_button = new Button();
void InitializeForm()
{
// settings
form.ClientSize = new Size(400, 500);
form.Text = "Tileset Fixer";
// display
display.Anchor = AnchorStyles.Top;
display.Location = new Point(0, 0);
display.Size = new Size(400, 400);
form.Controls.Add(display);
// value box
value_box.Location = new Point(240, 16 + 400);
value_box.Size = new Size(48, 20);
value_box.Text = "0";
value_box.TextChanged += (sender, e) => {
if (int.TryParse(value_box.Text, out int new_value))
{
row_slider.Value = new_value;
if (new_value > 0 && new_value < max_rows)
rows = new_value;
UpdateDisplay();
}
};
form.Controls.Add(value_box);
// row slider
row_slider.Location = new Point(8, 8 + 400);
row_slider.Size = new Size(224, 45);
row_slider.Maximum = 100;
row_slider.TickFrequency = 5;
row_slider.LargeChange = 3;
row_slider.SmallChange = 1;
row_slider.Scroll += (sender, e) => {
value_box.Text = row_slider.Value.ToString();
rows = row_slider.Value;
UpdateDisplay();
};
form.Controls.Add(row_slider);
// Initial display
UpdateDisplay();
// index slider
index_slider.Location = new Point(8, 58 + 400);
index_slider.Size = new Size(224, 45);
index_slider.Maximum = Data.Backgrounds.Count;
index_slider.TickFrequency = 5;
index_slider.LargeChange = 3;
index_slider.SmallChange = 1;
index_slider.Scroll += (sender, e) => {
index_box.Text = index_slider.Value.ToString();
current_index = index_slider.Value;
UpdateDisplay();
};
form.Controls.Add(index_slider);
// index box
index_box.Location = new Point(240, 66 + 400);
index_box.Size = new Size(48, 20);
index_box.Text = "0";
index_box.TextChanged += (sender, e) => {
if (int.TryParse(index_box.Text, out int new_value))
{
index_slider.Value = new_value;
if (new_value >= 0 && new_value < Data.Backgrounds.Count)
current_index = new_value;
UpdateDisplay();
}
};
form.Controls.Add(index_box);
save_button.Location = new Point(300, 410);
save_button.Text = "Save";
save_button.Click += (sender, e) => {
TextureWorker.SaveImageToFile(final_result, $"{script_folder}{Data.Backgrounds[current_index].Name.Content}.png");
current_index++;
index_box.Text = current_index.ToString();
};
form.Controls.Add(save_button);
}
// Update the display with current tileset
void UpdateDisplay()
{
display.Image = ProcessTileset(Data.Backgrounds[current_index], rows);
}
// Initialize and run the form
InitializeForm();
Application.Run(form);
// Cleanup
worker.Dispose();