Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit d77b7ed

Browse files
committed
Update program.cs
1 parent 1af53fd commit d77b7ed

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

src/DrawBot/program.cs

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Diagnostics;
66
using System.Windows.Forms;
77
using System.Runtime.InteropServices;
8-
using static System.Windows.Forms.Keys;
98

109
namespace DrawBot
1110
{
@@ -34,15 +33,13 @@ public partial class program : Form
3433
private static extern short GetAsyncKeyState(int vKey);
3534
// 0x01 = leftmouse
3635
// 0x1B = esc
37-
private bool keyDetect(int key)
36+
private bool keyDetect(string key)
3837
{
39-
/*Keys keyCode = (Keys)Enum.Parse(typeof(Keys), "Escape");
40-
KeysConverter converter = new KeysConverter();
41-
Keys key = (Keys)converter.ConvertFrom(keyCode.ToString());
42-
string hexValue = ((int)key).ToString("X2");
43-
MessageBox.Show("Key: " + keyCode.ToString() + " | Keycode: 0x" + hexValue);*/
38+
int keyInt = 0x00;
39+
if (key == "LeftMouse") keyInt = 0x01;
40+
if (key == "Escape") keyInt = 0x1B;
4441

45-
short keyState = GetAsyncKeyState(key);
42+
short keyState = GetAsyncKeyState(keyInt);
4643
bool keyPressed = ((keyState >> 15) & 0x0001) == 0x0001;
4744

4845
if (keyPressed)
@@ -64,7 +61,7 @@ public struct colorInfo
6461
int x_region_end = 0;
6562
int y_region_end = 0;
6663

67-
// color correction
64+
// luminance correction
6865
const double r_luma_fix = 0.299;
6966
const double g_luma_fix = 0.587;
7067
const double b_luma_fix = 0.114;
@@ -89,19 +86,19 @@ public struct colorInfo
8986
public program()
9087
{
9188
InitializeComponent();
89+
}
9290

91+
private void program_Load(object sender, EventArgs e)
92+
{
9393
// default labels
9494
titlebarLabel.Text = "DrawBot " + version;
9595
infoLabel.Text = "DrawBot " + version + " by o7q";
9696
imageLabel.Text = "";
9797
colorsLabel.Text = "";
98-
98+
9999
// select default algorithm
100100
algorithmComboBox.SelectedIndex = 1;
101-
}
102101

103-
private void program_Load(object sender, EventArgs e)
104-
{
105102
Directory.CreateDirectory("DrawBot\\presets");
106103
refreshColorsList();
107104
}
@@ -200,12 +197,12 @@ private int drawUsingStrips(int x_bound, int y_bound, int quality_step, double s
200197
for (int i = 0; i < area; i++)
201198
{
202199
// abort draw
203-
if (keyDetect(0x1B) == true)
200+
if (keyDetect("Escape") == true)
204201
{
205202
int x_pause_temp = Cursor.Position.X;
206203
int y_pause_temp = Cursor.Position.Y;
207204

208-
DialogResult prompt = MessageBox.Show("Drawing paused.\n\nPress OK to continue\nPress CANCEL to abort", "", MessageBoxButtons.OKCancel);
205+
DialogResult prompt = MessageBox.Show("Drawing paused.\n\nPress OK to resume\nPress CANCEL to stop", "", MessageBoxButtons.OKCancel);
209206
if (prompt == DialogResult.Cancel) return 1;
210207

211208
moveCursor(x_pause_temp, y_pause_temp);
@@ -344,8 +341,10 @@ private int drawUsingBlobs(int x_bound, int y_bound, int quality_step, double sp
344341
int colorIndex = 0;
345342
int[,] pixelIndex = new int[x_bound, y_bound];
346343

344+
// for every color scan the entire area
347345
for (int j = 0; j < paletteAmount; j++)
348346
{
347+
// select color at index with position (x, y)
349348
moveCursor(colorPalette[colorIndex].x, colorPalette[colorIndex].y);
350349
clickCursor(speed);
351350

@@ -355,21 +354,22 @@ private int drawUsingBlobs(int x_bound, int y_bound, int quality_step, double sp
355354
int mouseX_pos = x_region_start;
356355
int mouseY_pos = y_region_start;
357356

357+
// draw with selected color
358358
for (int i = 0; i < area; i++)
359359
{
360360
// abort draw
361-
if (keyDetect(0x1B) == true)
361+
if (keyDetect("Escape") == true)
362362
{
363363
int x_pause_temp = Cursor.Position.X;
364364
int y_pause_temp = Cursor.Position.Y;
365365

366-
DialogResult prompt = MessageBox.Show("Drawing paused.\n\nPress OK to continue\nPress CANCEL to abort", "", MessageBoxButtons.OKCancel);
366+
DialogResult prompt = MessageBox.Show("Drawing paused.\n\nPress OK to resume\nPress CANCEL to stop", "", MessageBoxButtons.OKCancel);
367367
if (prompt == DialogResult.Cancel) return 1;
368368

369369
moveCursor(x_pause_temp, y_pause_temp);
370370
}
371371

372-
// calculate new line
372+
// calculate new line (like a typewriter resetting)
373373
if (x_index == x_bound)
374374
{
375375
mouseX_pos = mouseX_pos - x_bound * quality_step;
@@ -379,22 +379,27 @@ private int drawUsingBlobs(int x_bound, int y_bound, int quality_step, double sp
379379
y_index++;
380380
}
381381

382+
// getting the color of the pixel at (x, y)
382383
Color color = image.GetPixel(x_index, y_index);
383384
int dist = getDist(color.R, colorPalette[colorIndex].r, color.G, colorPalette[colorIndex].g, color.B, colorPalette[colorIndex].b);
384385

385-
// place blob pixel
386+
// place blob pixel if not already
386387
if (dist == quantizedImage[x_index, y_index] && pixelIndex[x_index, y_index] == 0)
387388
{
389+
// placing pixel
388390
moveCursor(mouseX_pos, mouseY_pos);
389391
clickCursor(speed);
390392

393+
// set pixel to already placed
391394
pixelIndex[x_index, y_index] = 1;
392395
}
393396

397+
// prepare for next pixel
394398
mouseX_pos += quality_step;
395399
x_index++;
396400
}
397401

402+
// cycling through palette colors
398403
colorIndex++;
399404
}
400405

@@ -432,7 +437,7 @@ private void closeButton_Click(object sender, EventArgs e)
432437

433438
private void loadImageButton_Click(object sender, EventArgs e)
434439
{
435-
openImageDialog.Filter = "PNG|*.png|JPG|*.jpg|All files (*.*)|*.*";
440+
openImageDialog.Filter = "All files (*.*)|*.*|PNG|*.png|JPG|*.jpg";
436441
if (openImageDialog.ShowDialog() == DialogResult.OK)
437442
{
438443
imagePath = openImageDialog.FileName;
@@ -455,7 +460,7 @@ private void imageLabel_TextChanged(object sender, EventArgs e)
455460
private void regionStartButton_Click(object sender, EventArgs e)
456461
{
457462
TopMost = true;
458-
while (keyDetect(0x01) == false) { }
463+
while (keyDetect("LeftMouse") == false) { }
459464

460465
x_region_start = Cursor.Position.X;
461466
y_region_start = Cursor.Position.Y;
@@ -466,7 +471,7 @@ private void regionStartButton_Click(object sender, EventArgs e)
466471
private void regionEndButton_Click(object sender, EventArgs e)
467472
{
468473
TopMost = true;
469-
while (keyDetect(0x01) == false) { }
474+
while (keyDetect("LeftMouse") == false) { }
470475

471476
x_region_end = Cursor.Position.X;
472477
y_region_end = Cursor.Position.Y;
@@ -488,7 +493,7 @@ private void registerColorButton_Click(object sender, EventArgs e)
488493
}
489494

490495
TopMost = true;
491-
while (keyDetect(0x01) == false) { }
496+
while (keyDetect("LeftMouse") == false) { }
492497

493498
int x = Cursor.Position.X;
494499
int y = Cursor.Position.Y;
@@ -538,6 +543,8 @@ private void loadColorsButton_Click(object sender, EventArgs e)
538543
string[] RGBpixel = paletteFile.Split('|');
539544
paletteAmount = RGBpixel.Length;
540545
colorPalette = new colorInfo[paletteFile.Length];
546+
547+
// load xyrgb data into palette
541548
for (int i = 0; i < RGBpixel.Length; i++)
542549
{
543550
string[] value = RGBpixel[i].Split(',');

0 commit comments

Comments
 (0)