-
Notifications
You must be signed in to change notification settings - Fork 0
/
NormalMapGeneratorForm.cs
272 lines (257 loc) · 11.2 KB
/
NormalMapGeneratorForm.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PixelArtTileNormalMapGenerator
{
/// <summary>
/// Class specifying the behaviour of the Normal Map Generator Form.
/// </summary>
public partial class NormalMapGeneratorForm : Form
{
// Bitmaps
public static Bitmap OriginalImageBitmap;
public static Bitmap NormalMapImageBitmap;
// Dimensions
public static int ImageWidth;
public static int ImageHeight;
// Colors
public static Color DefaultNormalMapBGColor;
public static Color BackgroundColor;
public static Color SeparatorColor;
public static Color IndividualColor;
// Difference checks
public static int BackgroundColorMaxDifference;
public static int SeparatorColorMaxDifference;
public static int IndividualColorMaxDifference;
// Lists
public static Dictionary<Vector2Int, Tile> Tiles = new Dictionary<Vector2Int, Tile>();
// Flags
public static bool CreatingNormalMap = false;
// Cancellation tokens
public static CancellationTokenSource cTokenSource = new CancellationTokenSource();
public static CancellationToken cToken = new CancellationToken();
/// <summary>
/// Default constructor.
/// </summary>
public NormalMapGeneratorForm()
{
this.InitializeComponent();
}
/// <summary>
/// Loads the normal map generator form.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void NormalMapGeneratorForm_Load(object sender, EventArgs args)
{
this.InitializeValues();
}
/// <summary>
/// Initialize colors and bitmap images to default values.
/// </summary>
private void InitializeValues()
{
OriginalImageBitmap = null;
this.OriginalImagePreviewBox.Image = OriginalImageBitmap;
NormalMapImageBitmap = null;
this.NormalMapImagePreviewBox.Image = NormalMapImageBitmap;
DefaultNormalMapBGColor = Color.FromArgb(255, 123, 123, 255);
BackgroundColor = Color.Black;
this.BackgroundColorBox.BackColor = BackgroundColor;
SeparatorColor = Color.Black;
this.SeparatorColorBox.BackColor = SeparatorColor;
IndividualColor = Color.Black;
this.IndividualColorBox.BackColor = IndividualColor;
BackgroundColorMaxDifference = 10;
this.BackgroundColorMaxDifferencePicker.Value = BackgroundColorMaxDifference;
SeparatorColorMaxDifference = 10;
this.SeparatorColorMaxDifferencePicker.Value = SeparatorColorMaxDifference;
IndividualColorMaxDifference = 10;
this.IndividualColorMaxDifferencePicker.Value = IndividualColorMaxDifference;
this.NormalMapGenerationProgressLabel.Text = "Idle";
this.NormalMapGenerationProgressBar.Value = 0;
this.NormalMapGenerationProgressDetailLabel.Text = @"0% --- 0 / 0";
Tiles.Clear();
}
/// <summary>
/// Opens a file select dialog, selected image is read to bitmap and shown in original image preview box.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void LoadImageButton_Click(object sender, EventArgs args)
{
DialogResult result = this.OpenFileDialog.ShowDialog();
if(result == DialogResult.OK)
{
try
{
OriginalImageBitmap = new Bitmap(this.OpenFileDialog.FileName);
this.OriginalImagePreviewBox.Image = OriginalImageBitmap;
this.OriginalImagePreviewBox.SizeMode = PictureBoxSizeMode.StretchImage;
ImageWidth = OriginalImageBitmap.Width;
ImageHeight = OriginalImageBitmap.Height;
}
catch(Exception)
{
MessageBox.Show($@"Unable to read image!{Environment.NewLine}Please select a valid image format.{Environment.NewLine}For best results use PNG format.");
}
}
}
// TODO: Tidy up the form and make it look nicer, make sure elements are aligned etc.
// TODO: Add a better way to select colors, ideas: color dropper select color from image preview box, a dropdown of all the colors in the image to select from etc.
/// <summary>
/// Opens a color select dialog box for background color.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void SelectBackgroundColorButton_Click(object sender, EventArgs args)
{
DialogResult result = this.BackgroundColorDialog.ShowDialog();
if(result == DialogResult.OK)
{
BackgroundColor = this.BackgroundColorDialog.Color;
this.BackgroundColorBox.BackColor = BackgroundColor;
}
}
/// <summary>
/// Opens a color select dialog box for separator color.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void SelectSeparatorColorButton_Click(object sender, EventArgs args)
{
DialogResult result = this.SeparatorColorDialog.ShowDialog();
if(result == DialogResult.OK)
{
SeparatorColor = this.SeparatorColorDialog.Color;
this.SeparatorColorBox.BackColor = SeparatorColor;
}
}
/// <summary>
/// Opens a color select dialog box for individual color.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void SelectIndividualColorButton_Click(object sender, EventArgs args)
{
DialogResult result = this.IndividualColorDialog.ShowDialog();
if(result == DialogResult.OK)
{
IndividualColor = this.IndividualColorDialog.Color;
this.IndividualColorBox.BackColor = IndividualColor;
}
}
/// <summary>
/// Starts the normal map generation process and displays created normal map to normal map preview box.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void GenerateNormalMapButton_Click(object sender, EventArgs args)
{
BackgroundColorMaxDifference = (int)this.BackgroundColorMaxDifferencePicker.Value;
SeparatorColorMaxDifference = (int)this.SeparatorColorMaxDifferencePicker.Value;
IndividualColorMaxDifference = (int)this.IndividualColorMaxDifferencePicker.Value;
if(CreatingNormalMap == false)
{
if(OriginalImageBitmap != null)
{
NormalMapImageBitmap = new Bitmap(ImageWidth, ImageHeight);
this.CreateNormalMap();
}
else
{
MessageBox.Show($@"No image loaded!{Environment.NewLine}Please load an image first.");
}
}
else
{
MessageBox.Show($@"Operation already in progress!{Environment.NewLine}Please wait for operation to finish before starting another.");
}
}
/// <summary>
/// Runs initialize values method.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void ResetButton_Click(object sender, EventArgs args)
{
this.InitializeValues();
}
/// <summary>
/// Cancels the ongoing normal map generation.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CancelGeneratorButton_Click(object sender, EventArgs e)
{
if(CreatingNormalMap == true)
{
cTokenSource.Cancel();
}
else
{
MessageBox.Show("No operation currently running to cancel.");
}
}
/// <summary>
/// Saves the current normal map to disk.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SaveButton_Click(object sender, EventArgs e)
{
if(NormalMapImageBitmap != null)
{
if(CreatingNormalMap == false)
{
this.SaveFileDialog.Filter = "PNG Image (*.PNG, *.png)|";
this.SaveFileDialog.Title = "Save Normal Map...";
this.SaveFileDialog.FileName = Path.GetFileNameWithoutExtension(this.OpenFileDialog.FileName) + " Normal.PNG";
DialogResult result = this.SaveFileDialog.ShowDialog();
if(result == DialogResult.OK)
{
NormalMapImageBitmap.Save(this.SaveFileDialog.FileName, ImageFormat.Png);
}
}
else
{
MessageBox.Show($@"The normal map is currently being generated!{Environment.NewLine}Please wait until it finishes to save.");
}
}
else
{
MessageBox.Show($@"There is no normal map to save!{Environment.NewLine}Please generate a normal map first.");
}
}
/// <summary>
/// Creates a normal map from an original image using the selected colors to traverse.
/// </summary>
private async void CreateNormalMap()
{
Tiles.Clear();
CreatingNormalMap = true;
cToken = cTokenSource.Token;
this.NormalMapGenerationProgressBar.Minimum = 0;
this.NormalMapGenerationProgressBar.Maximum = (ImageWidth * ImageHeight) + 1;
this.NormalMapGenerationProgressBar.Step = this.NormalMapGenerationProgressBar.Maximum / 100;
Progress<string> nmgProgressLabelText = new Progress<string>(s => this.NormalMapGenerationProgressLabel.Text = s);
Progress<string> nmgProgressLabelDetailText = new Progress<string>(s => this.NormalMapGenerationProgressDetailLabel.Text = s);
Progress<int> nmgProgressBarValue = new Progress<int>(i => this.NormalMapGenerationProgressBar.Value = i);
await Task.Factory.StartNew(() => GeneratorWorker.CreateNormalMap(nmgProgressLabelText, nmgProgressBarValue, nmgProgressLabelDetailText), TaskCreationOptions.RunContinuationsAsynchronously);
this.UpdateNormalMapPreviewBox();
Tiles.Clear();
}
/// <summary>
/// Updates the normal map image preview box with the current normal map bitmap image.
/// </summary>
public void UpdateNormalMapPreviewBox()
{
this.NormalMapImagePreviewBox.Image = NormalMapImageBitmap;
}
}
}