This repository was archived by the owner on Mar 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainMenu.cs
More file actions
679 lines (561 loc) · 22.9 KB
/
Copy pathMainMenu.cs
File metadata and controls
679 lines (561 loc) · 22.9 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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
using System;
using System.IO;
using System.Net;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace DrawBot
{
public partial class MainMenu : Form
{
// configure mouse window events
public const int HT_CAPTION = 0x2;
public const int WM_NCLBUTTONDOWN = 0xA1;
// grab dlls for mousedown
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
// grab dlls for mouse controls
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
// mouse inputs
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
// key detection script
[DllImport("user32.dll")]
private static extern short GetAsyncKeyState(int vKey);
// 0x01 = leftmouse
// 0x1B = esc
private bool DetectKey(string key)
{
int keyInt = 0x00;
if (key == "LeftMouse") keyInt = 0x01;
if (key == "Escape") keyInt = 0x1B;
short keyState = GetAsyncKeyState(keyInt);
bool keyPressed = ((keyState >> 15) & 0x0001) == 0x0001;
if (keyPressed)
return true;
return false;
}
const string version = "v2.1.0";
public struct colorInfo
{
public int x, y, r, g, b;
}
// region
int x_region_start = 0;
int y_region_start = 0;
int x_region_end = 0;
int y_region_end = 0;
// luminance correction
const double r_luma_fix = 0.299;
const double g_luma_fix = 0.587;
const double b_luma_fix = 0.114;
double r_luma = r_luma_fix;
double g_luma = g_luma_fix;
double b_luma = b_luma_fix;
// preset creator
string colorsTemp = "";
int colorsIndex = 0;
bool hasSavedPalette = false;
// palette storage
public colorInfo[] colorPalette { get; set; }
int paletteAmount;
bool paletteLoaded = false;
// image
string imagePath = "";
Bitmap image;
public MainMenu()
{
InitializeComponent();
}
private void MainMenu_Load(object sender, EventArgs e)
{
// default labels
TitlebarLabel.Text = "DrawBot " + version;
InfoLabel.Text = "DrawBot " + version + " by o7q";
ImageLabel.Text = "";
PaletteLabel.Text = "";
// select default algorithm
DrawMethodComboBox.SelectedIndex = 1;
Directory.CreateDirectory("DrawBot\\presets");
RefreshColorsList();
}
private void StartButton_Click(object sender, EventArgs e)
{
if (ImageLabel.Text == "")
{
MessageBox.Show("Please select an image");
return;
}
if (x_region_start * x_region_end == 0 || y_region_start * y_region_end == 0)
{
MessageBox.Show("Please define your canvas bounds");
return;
}
if (paletteLoaded == false)
{
MessageBox.Show("Please select a color preset");
return;
}
if (DrawMethodComboBox.SelectedIndex == -1)
{
MessageBox.Show("Please select a drawing method");
return;
}
TopMost = true;
int quality_step = 200 / int.Parse(QualityTextBox.Text);
double speed = 0.1 / double.Parse(SpeedTextBox.Text);
int x_bound = (x_region_end - x_region_start) / quality_step;
int y_bound = (y_region_end - y_region_start) / quality_step;
if (LoadURLImageBox.Text == "")
{
// load local image
try
{
image = new Bitmap(Image.FromFile(imagePath), new Size(x_bound, y_bound));
}
catch (Exception ex)
{
MessageBox.Show("Error loading local image\n\n" + ex);
return;
}
}
else
{
// load web image
try
{
var request = WebRequest.Create(LoadURLImageBox.Text);
using (var response = request.GetResponse())
using (var stream = response.GetResponseStream())
image = new Bitmap(Bitmap.FromStream(stream), new Size(x_bound, y_bound));
}
catch (Exception ex)
{
MessageBox.Show("Error loading web image\n\n" + ex);
return;
}
}
int completeState = 0;
switch (DrawMethodComboBox.SelectedIndex)
{
case 0: completeState = DrawUsingStrips(x_bound, y_bound, quality_step, speed); break;
case 1: completeState = DrawUsingBlobs(x_bound, y_bound, quality_step, speed); break;
}
image.Dispose();
TopMost = false;
if (completeState == 1) return;
MoveCursor(x_region_start, y_region_start);
}
private int DrawUsingStrips(int x_bound, int y_bound, int quality_step, double speed)
{
int area = x_bound * y_bound;
int x_track = 0;
int y_track = 0;
bool selectNew = true;
bool initialSelect = true;
MoveCursor(x_region_start, y_region_start);
for (int i = 0; i < area; i++)
{
// abort draw
if (DetectKey("Escape") == true)
{
int x_pause_temp = Cursor.Position.X;
int y_pause_temp = Cursor.Position.Y;
DialogResult prompt = MessageBox.Show("Drawing paused.\n\nPress OK to resume\nPress CANCEL to stop", "", MessageBoxButtons.OKCancel);
if (prompt == DialogResult.Cancel) return 1;
MoveCursor(x_pause_temp, y_pause_temp);
}
// calculate future color
Color colorNext = new Color();
if (x_track + 1 < x_bound)
colorNext = image.GetPixel(x_track + 1, y_track);
// calculate next line
if (x_track == x_bound)
{
Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = new Point(Cursor.Position.X - x_bound * quality_step, Cursor.Position.Y + quality_step);
x_track = 0;
y_track++;
colorNext = image.GetPixel(x_track + 1, y_track);
}
Color color = image.GetPixel(x_track, y_track);
// color matching algorithm (thank you emmett!)
int nearestIndex = -1;
int nearestIndexNext = -1;
int nearestDist = int.MaxValue;
int nearestDistNext = int.MaxValue;
for (int j = 0; j < paletteAmount; j++)
{
// calculate distance to color
int dist = Get3DDistance(color.R, colorPalette[j].r, color.G, colorPalette[j].g, color.B, colorPalette[j].b);
int distNext = Get3DDistance(colorNext.R, colorPalette[j].r, colorNext.G, colorPalette[j].g, colorNext.B, colorPalette[j].b);
// update nearest color
if (dist < nearestDist)
{
nearestDist = dist;
nearestIndex = j;
}
// update future nearest color
if (distNext < nearestDistNext)
{
nearestDistNext = distNext;
nearestIndexNext = j;
}
}
int x_track_temp = Cursor.Position.X;
int y_track_temp = Cursor.Position.Y;
// select first needed color
if (initialSelect == true)
{
MoveCursor(colorPalette[nearestIndex].x, colorPalette[nearestIndex].y);
ClickCursor(speed);
MoveCursor(x_track_temp, y_track_temp);
initialSelect = false;
}
if (nearestIndex == nearestIndexNext)
{
// place pixel using same color
if (selectNew == true)
{
MoveCursor(colorPalette[nearestIndex].x, colorPalette[nearestIndex].y);
ClickCursor(speed);
MoveCursor(x_track_temp, y_track_temp);
}
selectNew = false;
ClickCursor(speed);
}
else
{
// place pixel using new color
MoveCursor(colorPalette[nearestIndex].x, colorPalette[nearestIndex].y);
ClickCursor(speed);
MoveCursor(x_track_temp, y_track_temp);
ClickCursor(speed);
selectNew = true;
}
MoveCursor(Cursor.Position.X + quality_step, Cursor.Position.Y);
x_track++;
}
return 0;
}
private int DrawUsingBlobs(int x_bound, int y_bound, int quality_step, double speed)
{
int area = x_bound * y_bound;
int x_pixel = 0;
int y_pixel = 0;
// quantize image using 3d rgb distance
int[,] quantizedImage = new int[x_bound, y_bound];
for (int i = 0; i < area; i++)
{
if (x_pixel == x_bound)
{
x_pixel = 0;
y_pixel++;
}
Color color = image.GetPixel(x_pixel, y_pixel);
// color matching algorithm (thank you emmett!)
int nearestDist = int.MaxValue;
for (int j = 0; j < paletteAmount; j++)
{
// calculate distance to color
int dist = Get3DDistance(color.R, colorPalette[j].r, color.G, colorPalette[j].g, color.B, colorPalette[j].b);
// update closest color
if (dist < nearestDist)
nearestDist = dist;
}
quantizedImage[x_pixel, y_pixel] = nearestDist;
x_pixel++;
}
MoveCursor(x_region_start, y_region_start);
int colorIndex = 0;
int[,] pixelIndex = new int[x_bound, y_bound];
// for every color scan the entire area
for (int j = 0; j < paletteAmount; j++)
{
// select color at index with position (x, y)
MoveCursor(colorPalette[colorIndex].x, colorPalette[colorIndex].y);
ClickCursor(speed);
int x_index = 0;
int y_index = 0;
int mouseX_pos = x_region_start;
int mouseY_pos = y_region_start;
// draw with selected color
for (int i = 0; i < area; i++)
{
// abort draw
if (DetectKey("Escape") == true)
{
int x_pause_temp = Cursor.Position.X;
int y_pause_temp = Cursor.Position.Y;
DialogResult prompt = MessageBox.Show("Drawing paused.\n\nPress OK to resume\nPress CANCEL to stop", "", MessageBoxButtons.OKCancel);
if (prompt == DialogResult.Cancel) return 1;
MoveCursor(x_pause_temp, y_pause_temp);
}
// calculate new line (like a typewriter resetting)
if (x_index == x_bound)
{
mouseX_pos = mouseX_pos - x_bound * quality_step;
mouseY_pos = mouseY_pos + quality_step;
x_index = 0;
y_index++;
}
// getting the color of the pixel at (x, y)
Color color = image.GetPixel(x_index, y_index);
int dist = Get3DDistance(color.R, colorPalette[colorIndex].r, color.G, colorPalette[colorIndex].g, color.B, colorPalette[colorIndex].b);
// place blob pixel if not already
if (dist == quantizedImage[x_index, y_index] && pixelIndex[x_index, y_index] == 0)
{
// placing pixel
MoveCursor(mouseX_pos, mouseY_pos);
ClickCursor(speed);
// set pixel to already placed
pixelIndex[x_index, y_index] = 1;
}
// prepare for next pixel
mouseX_pos += quality_step;
x_index++;
}
// cycling through palette colors
colorIndex++;
}
return 0;
}
private int Get3DDistance(int r1, int r2, int g1, int g2, int b1, int b2)
{
return (int)Math.Sqrt
(
r_luma * Math.Pow(r1 - r2, 2) +
g_luma * Math.Pow(g1 - g2, 2) +
b_luma * Math.Pow(b1 - b2, 2)
);
}
private void MoveCursor(int x, int y)
{
Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = new Point(x, y);
}
private void ClickCursor(double wait)
{
uint X = (uint)Cursor.Position.X;
uint Y = (uint)Cursor.Position.Y;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
var sw = Stopwatch.StartNew();
while (sw.ElapsedTicks < Math.Round(wait * Stopwatch.Frequency)) { }
}
private void CloseButton_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void LoadImageButton_Click(object sender, EventArgs e)
{
openImageDialog.Filter = "All files (*.*)|*.*|PNG|*.png|JPG|*.jpg";
if (openImageDialog.ShowDialog() == DialogResult.OK)
{
imagePath = openImageDialog.FileName;
ImageLabel.Text = Path.GetFileName(imagePath);
}
}
private void LoadURLImageBox_TextChanged(object sender, EventArgs e)
{
ImageLabel.Text = LoadURLImageBox.Text;
if (ImageLabel.Text.Length > 11)
ImageLabel.Text = ImageLabel.Text.Substring(ImageLabel.Text.Length - 11);
if (LoadURLImageBox.Text == "")
ImageLabel.Text = Path.GetFileName(imagePath);
}
private void RegionStartButton_Click(object sender, EventArgs e)
{
TopMost = true;
while (DetectKey("LeftMouse") == false) { }
x_region_start = Cursor.Position.X;
y_region_start = Cursor.Position.Y;
Pos1Label.Text = (x_region_start, y_region_start).ToString();
BoundsLabel.Text = "(" + (x_region_end - x_region_start).ToString() + ", " + (y_region_end - y_region_start).ToString() + ")";
}
private void RegionEndButton_Click(object sender, EventArgs e)
{
TopMost = true;
while (DetectKey("LeftMouse") == false) { }
x_region_end = Cursor.Position.X;
y_region_end = Cursor.Position.Y;
Pos2Label.Text = (x_region_end, y_region_end).ToString();
BoundsLabel.Text = "(" + (x_region_end - x_region_start).ToString() + ", " + (y_region_end - y_region_start).ToString() + ")";
}
private void RegisterColorButton_Click(object sender, EventArgs e)
{
if (hasSavedPalette == true)
{
colorsTemp = "";
colorsIndex = 0;
ColorIndexLabel.Text = "Color 0"; ColorIndexLabel.Refresh();
ColorRGBLabel.Text = "(0, 0, 0)"; ColorRGBLabel.Refresh();
ColorSquare.BackColor = Color.FromArgb(255, 0, 0, 0);
hasSavedPalette = false;
}
TopMost = true;
while (DetectKey("LeftMouse") == false) { }
int x = Cursor.Position.X;
int y = Cursor.Position.Y;
var rgb = GetColorAt(x, y);
colorsTemp += "|" + x + "," + y + "," + rgb.Item1 + "," + rgb.Item2 + "," + rgb.Item3;
colorsIndex++;
ColorIndexLabel.Text = "Color " + colorsIndex.ToString();
ColorRGBLabel.Text = rgb.ToString();
ColorSquare.BackColor = Color.FromArgb(255, rgb.Item1, rgb.Item2, rgb.Item3);
}
private void UndoColorButton_Click(object sender, EventArgs e)
{
if (colorsTemp == "") return;
colorsIndex--;
if (colorsIndex == 0)
colorsTemp = "";
else
colorsTemp = colorsTemp.Remove(colorsTemp.LastIndexOf('|'));
ColorIndexLabel.Text = "Color " + colorsIndex;
ColorRGBLabel.Text = "(0, 0, 0)";
ColorSquare.BackColor = Color.FromArgb(255, 0, 0, 0);
}
private void SavePaletteButton_Click(object sender, EventArgs e)
{
if (colorsTemp == "") return;
colorsTemp = colorsTemp.Substring(1);
string name = PaletteNameTextBox.Text == "" ? "preset" + DateTime.Now.ToString("Mdy-hms") : PaletteNameTextBox.Text;
File.WriteAllText("DrawBot\\presets\\" + name + ".palette", colorsTemp);
RefreshColorsList();
PaletteNameTextBox.Text = "";
hasSavedPalette = true;
}
private void LoadPaletteButton_Click(object sender, EventArgs e)
{
if (Directory.GetFiles("DrawBot\\presets", "*.*", SearchOption.TopDirectoryOnly).Length == 0) return;
string paletteFilePath = "DrawBot\\presets\\" + PaletteListBox.SelectedItem + ".palette";
string paletteFile = File.ReadAllText(paletteFilePath);
string[] RGBpixel = paletteFile.Split('|');
paletteAmount = RGBpixel.Length;
colorPalette = new colorInfo[paletteFile.Length];
// load xyrgb data into palette
for (int i = 0; i < RGBpixel.Length; i++)
{
string[] value = RGBpixel[i].Split(',');
int[] valueInt = new int[5];
for (int j = 0; j < 5; j++)
valueInt[j] = int.Parse(value[j]);
colorPalette[i].x = valueInt[0];
colorPalette[i].y = valueInt[1];
colorPalette[i].r = valueInt[2];
colorPalette[i].g = valueInt[3];
colorPalette[i].b = valueInt[4];
}
PaletteLabel.Text = Path.GetFileNameWithoutExtension(paletteFilePath);
paletteLoaded = true;
}
private void DeletePaletteButton_Click(object sender, EventArgs e)
{
if (Directory.GetFiles("DrawBot\\presets", "*.*", SearchOption.TopDirectoryOnly).Length == 0) return;
File.Delete("DrawBot\\presets\\" + PaletteListBox.SelectedItem + ".palette");
RefreshColorsList();
if (PaletteListBox.Items.Count > 0) PaletteListBox.SelectedIndex = 0;
PaletteLabel.Text = "";
paletteLoaded = false;
}
private void QualityTextBox_TextChanged(object sender, EventArgs e)
{
int quality;
try
{
quality = int.Parse(QualityTextBox.Text);
}
catch
{
quality = 50;
}
if (quality > 100) quality = 100;
if (quality < 1) quality = 1;
QualityTextBox.Text = quality.ToString();
QualityBar.Value = quality;
}
private void QualityBar_Scroll(object sender, EventArgs e)
{
QualityTextBox.Text = QualityBar.Value.ToString();
}
private void SpeedTextBox_TextChanged(object sender, EventArgs e)
{
int speed;
try
{
speed = int.Parse(SpeedTextBox.Text);
}
catch
{
speed = 50;
}
if (speed > 100) speed = 100;
if (speed < 1) speed = 1;
SpeedTextBox.Text = speed.ToString();
SpeedBar.Value = speed;
}
private void SpeedBar_Scroll(object sender, EventArgs e)
{
SpeedTextBox.Text = SpeedBar.Value.ToString();
}
private void LumaFixCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (LumaFixCheckBox.Checked == true)
{
r_luma = r_luma_fix;
g_luma = g_luma_fix;
b_luma = b_luma_fix;
}
else
{
r_luma = 1.0;
g_luma = 1.0;
b_luma = 1.0;
}
}
private void MainMenu_MouseEnter(object sender, EventArgs e)
{
TopMost = false;
}
private void TitlebarLabel_MouseDown(object sender, MouseEventArgs e)
{
MoveForm(e);
}
private void TitlebarPanel_MouseDown(object sender, MouseEventArgs e)
{
MoveForm(e);
}
private void SidebarPanel_MouseDown(object sender, MouseEventArgs e)
{
MoveForm(e);
}
private void RefreshColorsList()
{
PaletteListBox.Items.Clear();
string[] paletteFiles = Directory.GetFiles("DrawBot\\presets", "*.palette");
for (int i = 0; i < paletteFiles.Length; i++)
PaletteListBox.Items.Add(Path.GetFileNameWithoutExtension(paletteFiles[i]));
}
Tuple<int, int, int> GetColorAt(int x, int y)
{
Bitmap bmp = new Bitmap(1, 1);
Rectangle bounds = new Rectangle(x, y, 1, 1);
using (Graphics g = Graphics.FromImage(bmp))
g.CopyFromScreen(bounds.Location, Point.Empty, bounds.Size);
Color col = bmp.GetPixel(0, 0);
return Tuple.Create(Convert.ToInt32(col.R), Convert.ToInt32(col.G), Convert.ToInt32(col.B));
}
private void MoveForm(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
}
}