-
Notifications
You must be signed in to change notification settings - Fork 2
/
Form1.cs
320 lines (273 loc) · 9.38 KB
/
Form1.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace VisualVoronoi
{
public partial class Form1 : Form
{
#region Drawing Variables
Graphics g;
bool drawBiomes = true;
bool drawRivers = true;
bool drawSites = false;
bool drawCorners = false;
bool drawDelaunay = false;
bool isRandom = true;
bool overlay = true;
bool lighting = true;
bool noisyEdges = true;
bool smoothBlending = true;
#endregion Drawing Variables
#region Voronoi Variables
static bool moreRandom = false;
int numSites = 6000;
int numLloyd = 2;
int bounds = 850;
int seed = 0;
VoronoiGraph curr;
Random seedGen;
Bitmap img;
Bitmap img2;
Bitmap fImg;
#endregion Voronoi Variables
public Form1()
{
InitializeComponent();
g = this.MapPanel.CreateGraphics();
seedGen = new Random();
}
public static VoronoiGraph CreateVoronoiGraph(int bounds, int numSites, int numLloyd, int seed)
{
Random r = new Random(seed);
Voronoi v = new Voronoi(numSites, bounds, bounds, r);
GraphImplementation graph = new GraphImplementation(v, numLloyd, r, moreRandom);
return graph;
}
#region CreateButton
private void CreateButton_Click(object sender, EventArgs e)
{
if (nSitesBox.Text != "")
{
try
{
numSites = int.Parse(nSitesBox.Text);
}
catch (Exception)
{
numSites = 6000;
}
}
else
{
numSites = 6000;
}
if (nLloydBox.Text != "")
{
try
{
numLloyd = int.Parse(nLloydBox.Text);
}
catch (Exception)
{
numLloyd = 2;
}
}
else
{
numLloyd = 2;
}
if (BoundsBox.Text != "")
{
try
{
bounds = int.Parse(BoundsBox.Text);
}
catch (Exception)
{
bounds = 850;
}
}
else
{
bounds = 850;
}
if (isRandom)
{
seed = seedGen.Next();
seedBox.Text = seed + "";
}
else if (seedBox.Text != "")
{
try
{
seed = int.Parse(seedBox.Text);
}
catch (Exception)
{
seed = seedGen.Next();
seedBox.Text = seed + "";
}
}
else
{
seed = seedGen.Next();
seedBox.Text = seed + "";
}
curr = CreateVoronoiGraph(bounds, numSites, numLloyd, seed);
img = curr.CreateMap(g, drawBiomes, drawRivers, drawSites, drawCorners, drawDelaunay, noisyEdges, smoothBlending, lighting);
fImg = new Bitmap(bounds, bounds);
Graphics tG = Graphics.FromImage(fImg);
tG.DrawImage(img, new PointF(0, 0));
// EXPERIMENTAL Noise Application (Gives map "texture")
if (overlay)
{
if (!File.Exists(@"gscale.png"))
{
SimplexNoise s = new SimplexNoise(16, 0.25, 555);
img2 = s.GenerateGreyScale(bounds, bounds);
img2.Save("gscale.png", System.Drawing.Imaging.ImageFormat.Png);
}
// Read noise in from file, much faster, looks worse at higher res
img2 = new Bitmap(@"gscale.png");
img2 = new Bitmap(img2, bounds, bounds);
tG.DrawImage(img2, new PointF(0, 0));
}
MapImageBox.Image = fImg;
MapPanel.Refresh();
}
#endregion CreateButton
#region RefreshButton
private void RefreshButton_Click(object sender, EventArgs e)
{
curr.Paint(System.Drawing.Graphics.FromImage(img), drawBiomes, drawRivers, drawSites, drawCorners, drawDelaunay, false, noisyEdges, smoothBlending, lighting);
fImg = new Bitmap(bounds, bounds);
Graphics tG = Graphics.FromImage(fImg);
tG.DrawImage(img, new PointF(0, 0));
// EXPERIMENTAL Noise Application (Gives map "texture")
if (overlay)
{
if (img2 == null)
{
if (!File.Exists(@"gscale.png"))
{
SimplexNoise s = new SimplexNoise(16, 0.25, 555);
img2 = s.GenerateGreyScale(bounds, bounds);
img2.Save("gscale.png", System.Drawing.Imaging.ImageFormat.Png);
}
// Read noise in from file instead, much faster, looks worse at higher res.
img2 = new Bitmap("gscale.png");
img2 = new Bitmap(img2, bounds, bounds);
}
tG.DrawImage(img2, new PointF(0, 0));
}
MapImageBox.Image = fImg;
MapPanel.Refresh();
this.MapImageBox.Refresh();
}
#endregion RefreshButton
#region Map Dragging
private System.Drawing.Point StartPoint = new System.Drawing.Point();
private bool isDragged = false;
private void MapImageBox_MouseMove(object sender, MouseEventArgs e)
{
if (isDragged)
{
System.Drawing.Point cPoint = new System.Drawing.Point(e.Location.X - StartPoint.X, e.Location.Y - StartPoint.Y);
MapPanel.AutoScrollPosition = new System.Drawing.Point(-MapPanel.AutoScrollPosition.X - cPoint.X, -MapPanel.AutoScrollPosition.Y - cPoint.Y);
}
}
private void MapImageBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
StartPoint = e.Location;
isDragged = true;
}
private void MapImageBox_MouseUp(object sender, MouseEventArgs e)
{
isDragged = false;
}
#endregion Map Dragging
#region Map Zoom
private void Zoom(object sender, MouseEventArgs e)
{
if (e.Delta < 0)
{
MapImageBox.Width = Convert.ToInt32(MapImageBox.Width / 1.2);
MapImageBox.Height = Convert.ToInt32(MapImageBox.Height / 1.2);
MapImageBox.SizeMode = PictureBoxSizeMode.StretchImage;
}
else
{
MapImageBox.Width = Convert.ToInt32(MapImageBox.Width * 1.2);
MapImageBox.Height = Convert.ToInt32(MapImageBox.Height * 1.2);
MapImageBox.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
private void MapPanel_MouseHover(object sender, EventArgs e)
{
MapImageBox.Focus();
}
private void MapImageBox_MouseHover(object sender, EventArgs e)
{
MapImageBox.Focus();
}
#endregion Map Zoom
#region CheckBoxes
private void BiomeCheck_CheckedChanged(object sender, EventArgs e)
{
drawBiomes = BiomeCheck.Checked;
}
private void RiverCheck_CheckedChanged(object sender, EventArgs e)
{
drawRivers = RiverCheck.Checked;
}
private void SitesCheck_CheckedChanged(object sender, EventArgs e)
{
drawSites = SitesCheck.Checked;
}
private void CornerCheck_CheckedChanged(object sender, EventArgs e)
{
drawCorners = CornerCheck.Checked;
}
private void DelaunayCheck_CheckedChanged(object sender, EventArgs e)
{
drawDelaunay = DelaunayCheck.Checked;
}
private void RandomCheck_CheckedChanged(object sender, EventArgs e)
{
isRandom = RandomCheck.Checked;
}
private void RHeightCheck_CheckedChanged(object sender, EventArgs e)
{
moreRandom = RHeightCheck.Checked;
}
private void NoisyCheck_CheckedChanged(object sender, EventArgs e)
{
noisyEdges = NoisyCheck.Checked;
}
private void OverlayCheck_CheckedChanged(object sender, EventArgs e)
{
overlay = OverlayCheck.Checked;
}
private void BlendCheck_CheckedChanged(object sender, EventArgs e)
{
smoothBlending = BlendCheck.Checked;
}
private void LightingCheck_CheckedChanged(object sender, EventArgs e)
{
lighting = LightingCheck.Checked;
}
#endregion CheckBoxes
private void SaveButton_Click(object sender, EventArgs e)
{
fImg.Save(seed + ".png", System.Drawing.Imaging.ImageFormat.Png);
}
}
}