-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
656 lines (556 loc) · 16.2 KB
/
MainForm.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
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
using System;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static MainLibrary.MainLib;
namespace AutoPixelArt
{
using strings = Resources.Strings;
public partial class MainForm : Form
{
private static readonly Properties.Settings settings = Properties.Settings.Default;
private static ManualForm manualForm;
private static SettingsForm settingsForm;
public static byte Cd { get; set; }
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
#if DEBUG
Debugging = true;
#endif
string[] args = Environment.GetCommandLineArgs();
if (args.Length > 1 && args[1] == "/debug")
Debugging = true;
if (Debugging)
Text = $"AutoPixelArt ({strings.debugMode})";
HMain = Handle;
hideControlsPanel.Location = new Point(0, 24);
hideControlsPanel.BringToFront();
RestoreCheckboxes();
if (settings.vanishMinecraft)
{
autoMinCheckBox.Enabled = false;
MouseClick += MainForm_MouseClick;
}
bool settingsIsReset = false;
try
{
verBox.SelectedIndex = settings.paletteVersion;
}
catch
{
verBox.SelectedIndex = settings.paletteVersion = -1;
settingsIsReset = true;
}
try
{
using (SettingsForm settForm = new())
{
settForm.delayBar.Value = settings.delay;
}
}
catch (ArgumentOutOfRangeException)
{
settings.delay = 10;
settingsIsReset = true;
}
if (settingsIsReset)
{
settings.Save();
Warning(strings.someSettingsDamaged, false);
}
PrintPaletteLabel();
russianLangItem.Checked = Lang == "ru";
englishLangItem.Checked = Lang == "en";
SetMenuItemsIcons();
CountHiddenWindows();
}
public void DisplayProcessingLabel(bool displayPanel, bool displayLabel)
{
hideControlsPanel.Enabled = displayPanel;
hideControlsPanel.Visible = displayPanel;
processingLabel.Visible = displayLabel;
}
public void IncProgressBar()
{
progressBar.Value++;
}
public async void UpdateStatLabel(int estimated)
{
await Task.Delay(400);
double percents = (double)progressBar.Value / progressBar.Maximum;
if (percents < 1.0 && Math.Ceiling(percents * 100) == 100)
percents = 0.99;
progressStatLabel.Text = $"{progressBar.Value}/{progressBar.Maximum}";
progressPercentsLabel.Text = $"({Math.Round(percents * 100)} %)";
currentLayerLabel.Text = $"{strings.currentLayer}{Math.Ceiling(percents * OrigImgWH[1])}";
if (progressTimeLabel.Visible)
progressTimeLabel.Text = $"{strings.minutesRemaining}{Math.Ceiling(estimated - (estimated * percents))}";
}
public void OnRobotFinish(string mode = "")
{
if (mode == "closing")
{
MinimizeMinecraft(false);
return;
}
if (settings.showProgress)
{
HideProgress();
ClearProgress();
}
if (mode == "cancelled")
OnRobotCancel();
else
Activate();
notifyIcon.Visible = false;
progressBar.Visible = false;
progressLabel.Visible = false;
progressStatLabel.Visible = false;
progressPercentsLabel.Visible = false;
currentLayerLabel.Visible = false;
progressBar.Value = OrigImgWH[0] * (OrigImgWH[1] - GetImageWH(1));
progressStatLabel.Text = "0/0";
progressPercentsLabel.Text = "(0 %)";
progressTimeLabel.Text = $"{strings.minutesRemaining}0";
currentLayerLabel.Text = $"{strings.currentLayer}1";
if (progressTimeLabel.Visible)
progressTimeLabel.Visible = false;
else
currentLayerLabel.Location = new Point(progressTimeLabel.Location.X, progressTimeLabel.Location.Y + progressTimeLabel.Size.Height);
verBox.Enabled = true;
showPicAndPal.Enabled = true;
blockCheckBox.Enabled = true;
autoMinCheckBox.Enabled = !settings.vanishMinecraft;
showProgressCheckBox.Enabled = true;
continueCheckBox.Enabled = true;
continueButton.Enabled = continueCheckBox.Checked;
continueTextBox.Enabled = continueCheckBox.Checked;
startStopButton.Text = strings.start;
startStopButton.Enabled = true;
if (settings.vanishMinecraft)
MouseClick += MainForm_MouseClick;
if (mode != "cancelled")
{
double spentMinutes = Math.Round(GetSpentTime());
Info($"{strings.doneBuilding}{(spentMinutes == 0 ? strings.lessThanOne : spentMinutes)}.");
}
}
public void RestoreCheckboxes()
{
blockCheckBox.CheckedChanged -= blockCheckBox_CheckedChanged;
autoMinCheckBox.CheckedChanged -= autoMinCheckBox_CheckedChanged;
showProgressCheckBox.CheckedChanged -= showProgressCheckBox_CheckedChanged;
blockCheckBox.Checked = settings.blockInput;
autoMinCheckBox.Checked = settings.vanishMinecraft || settings.hideMinecraft;
showProgressCheckBox.Checked = settings.showProgress;
blockCheckBox.CheckedChanged += blockCheckBox_CheckedChanged;
autoMinCheckBox.CheckedChanged += autoMinCheckBox_CheckedChanged;
showProgressCheckBox.CheckedChanged += showProgressCheckBox_CheckedChanged;
}
public void PrintPaletteLabel()
{
Color[] labelColors = new Color[] { Color.Red, Color.Orange, Color.Gold, Color.Green, Color.LightBlue, Color.Blue, Color.Magenta };
verText.ResetText();
verText.SelectionStart = 0;
verText.SelectionLength = 0;
if (Lang == "ru")
{
verText.SelectionColor = Color.Black;
verText.AppendText(strings.paletteVer0);
}
for (int i = 0; i < 7; i++)
{
verText.SelectionColor = labelColors[i];
verText.AppendText(strings.paletteVer1[i].ToString());
}
if (Lang == "en")
{
verText.SelectionColor = Color.Black;
verText.AppendText(strings.paletteVer0);
}
}
public void SetMenuItemsIcons()
{
exitItem.Image = GetIcon("exitIcon");
openSettingsItem.Image = GetIcon("settingsIcon");
resetSettingsItem.Image = GetIcon("resetSettingsIcon");
openManualItem.Image = GetIcon("manualIcon");
aboutItem.Image = GetIcon("aboutIcon");
authorItem.Image = GetIcon("emailIcon");
}
private void showPicAndPal_Click(object sender, EventArgs e)
{
ShowPreview();
ShowPalette(verBox.SelectedIndex != 0);
}
private void OnFileOpened(string fileName)
{
PictureBox pictureBox = new();
try
{
pictureBox.Load(fileName);
}
catch
{
Error(strings.imgDamaged, false);
return;
}
finally
{
pictureBox.Dispose();
}
DisplayProcessingLabel(true, true);
Task.Run(() =>
{
int[] imgCWHE = ProcessImage(fileName);
if (imgCWHE[0] != 0)
{
BeginInvoke((Action)delegate
{
DisplayProcessingLabel(false, false);
widthLabel.Text = $"{strings.width}{imgCWHE[1]}";
heightLabel.Text = $"{strings.height}{imgCWHE[2]}";
colorsLabel.Text = $"{strings.colorCount}{imgCWHE[0]}";
estimatedLabel.Text = $"{strings.takeMinutes}{((imgCWHE[3] == 0) ? strings.lessThanOne : imgCWHE[3])}";
});
}
else
BeginInvoke((Action)delegate
{
DisplayProcessingLabel(true, false);
});
});
previewForm?.Close();
}
private void MainForm_DragDrop(object sender, DragEventArgs e)
{
OnFileOpened(((string[])e.Data.GetData(DataFormats.FileDrop))[0]);
}
private void MainForm_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void verBox_TextUpdate(object sender, EventArgs e)
{
verBox.Text = strings.chooseVer;
}
private void verBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (!showPicAndPal.Enabled)
showPicAndPal.Enabled = true;
if (verBox.SelectedIndex == -1)
{
verBox.Text = strings.chooseVer;
showPicAndPal.Enabled = false;
}
if (verBox.SelectedIndex == 0)
{
if (verBox.SelectedIndex != settings.paletteVersion)
Warning(strings.noPalette, false);
}
else
GeneratePalette(verBox.SelectedIndex);
if (verBox.SelectedIndex != settings.paletteVersion)
{
settings.paletteVersion = (short)verBox.SelectedIndex;
settings.Save();
RecalculateImage();
}
}
private async void EnableStartStopButton()
{
await Task.Delay(1000);
startStopButton.Enabled = true;
}
private void startStopButton_Click(object sender, EventArgs e)
{
if (startStopButton.Text == strings.start)
{
if (StartMain())
{
HidePreview();
HidePalette();
MouseClick -= MainForm_MouseClick;
verBox.Enabled = false;
showPicAndPal.Enabled = false;
blockCheckBox.Enabled = false;
autoMinCheckBox.Enabled = false;
showProgressCheckBox.Enabled = false;
continueCheckBox.Enabled = false;
continueButton.Enabled = false;
continueTextBox.Enabled = false;
startStopButton.Text = strings.cancel;
startStopButton.Enabled = false;
countdownTimer.Enabled = true;
notifyIcon.BalloonTipTitle = strings.countdown;
EnableStartStopButton();
}
}
else
{
if (countdownTimer.Enabled)
{
countdownTimer.Enabled = false;
notifyIcon.Visible = false;
if (settings.vanishMinecraft)
{
autoMinCheckBox.Enabled = false;
MouseClick += MainForm_MouseClick;
}
else
autoMinCheckBox.Enabled = true;
verBox.Enabled = true;
showPicAndPal.Enabled = true;
blockCheckBox.Enabled = true;
showProgressCheckBox.Enabled = true;
continueCheckBox.Enabled = true;
if (continueCheckBox.Checked)
{
continueButton.Enabled = true;
continueTextBox.Enabled = true;
}
}
else
{
RobotRunning = false;
while (!RobotFinished)
Thread.Sleep(100);
OnRobotFinish("cancelled");
KeyUpAll();
}
startStopButton.Text = strings.start;
}
}
private void countdownTimer_Tick(object sender, EventArgs e)
{
if (!notifyIcon.Visible)
{
Cd = settings.delay;
notifyIcon.Icon = Icon;
}
notifyIcon.Visible = true;
if (Cd == 0)
{
notifyIcon.BalloonTipTitle = "AutoPixelArt";
notifyIcon.BalloonTipText = strings.startSuccess;
notifyIcon.ShowBalloonTip(1);
countdownTimer.Enabled = false;
if (settings.showProgress)
{
FillProgress();
ShowProgress();
}
progressBar.Visible = true;
progressLabel.Visible = true;
progressStatLabel.Visible = true;
progressPercentsLabel.Visible = true;
currentLayerLabel.Visible = true;
if (!estimatedLabel.Text.Contains(strings.lessThanOne) && estimatedLabel.Text.Split(" - ")[^1] != "1")
{
progressTimeLabel.Text = $"{strings.minutesRemaining}{estimatedLabel.Text.Split(" - ")[^1]}";
progressTimeLabel.Visible = true;
}
else
currentLayerLabel.Location = new Point(currentLayerLabel.Location.X, progressTimeLabel.Location.Y);
Task.Run(RobotThread);
}
else
{
notifyIcon.BalloonTipText = Cd.ToString();
notifyIcon.ShowBalloonTip(1);
}
Cd--;
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (RobotRunning)
{
e.Cancel = true;
RobotRunning = false;
while (!RobotFinished)
Thread.Sleep(100);
OnRobotFinish("closing");
KeyUpAll();
e.Cancel = false;
}
Application.Exit();
}
private void blockCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (!settings.blockInput)
Warning(strings.toCancelBlock, false);
settings.blockInput = blockCheckBox.Checked;
settings.Save();
}
private void autoMinCheckBox_CheckedChanged(object sender, EventArgs e)
{
settings.hideMinecraft = autoMinCheckBox.Checked;
settings.Save();
}
private void showProgressCheckBox_CheckedChanged(object sender, EventArgs e)
{
settings.showProgress = showProgressCheckBox.Checked;
settings.Save();
}
private void openItem_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new()
{
Filter = $"{strings.pic}|*.png; *.jpg; *.jpeg; *.bmp; *.dip; *.tif; *.tiff; *.gif|{strings.allFiles}|*.*",
RestoreDirectory = true,
Title = strings.openPic
};
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
OnFileOpened(openFileDialog.FileName);
}
openFileDialog.Dispose();
}
private void exitItem_Click(object sender, EventArgs e)
{
Close();
}
private void aboutItem_Click(object sender, EventArgs e)
{
Info($"{strings.ver} - alpha {Application.ProductVersion}.\n{strings.author}\nE-mail - [email protected]");
}
private void authorItem_Click(object sender, EventArgs e)
{
Process.Start("explorer", "mailto:[email protected]");
}
private void resetSettingsItem_Click(object sender, EventArgs e)
{
if (Application.OpenForms["previewForm"] is not null)
{
HidePreview();
if (Application.OpenForms["paletteForm"] is not null)
HidePalette();
}
settings.Reset();
settings.Save();
blockCheckBox.CheckedChanged -= blockCheckBox_CheckedChanged;
autoMinCheckBox.CheckedChanged -= autoMinCheckBox_CheckedChanged;
showProgressCheckBox.CheckedChanged -= showProgressCheckBox_CheckedChanged;
blockCheckBox.Checked = settings.blockInput;
autoMinCheckBox.Checked = settings.hideMinecraft;
showProgressCheckBox.Checked = settings.showProgress;
verBox.SelectedIndex = settings.paletteVersion;
blockCheckBox.CheckedChanged += blockCheckBox_CheckedChanged;
autoMinCheckBox.CheckedChanged += autoMinCheckBox_CheckedChanged;
showProgressCheckBox.CheckedChanged += showProgressCheckBox_CheckedChanged;
Msg(strings.settingsReset);
}
private void openManualItem_Click(object sender, EventArgs e)
{
if (settings.firstOpened)
{
Warning(strings.readCarefully, false);
settings.firstOpened = false;
settings.Save();
}
if (manualForm is null || manualForm.IsDisposed)
{
manualForm = new ManualForm(Lang);
manualForm.richTextBox.SelectAll();
manualForm.richTextBox.SelectedRtf = Lang == "en" ? Properties.Resources.Manual_EN : Properties.Resources.Manual_RU;
manualForm.richTextBox.SelectionStart = 0;
}
else
{
if (manualForm.WindowState == FormWindowState.Minimized)
manualForm.WindowState = FormWindowState.Normal;
manualForm.Activate();
}
manualForm.Show();
}
private void openSettingsItem_Click(object sender, EventArgs e)
{
settingsForm = new SettingsForm();
settingsForm.Show();
}
private void continueCheckBox_CheckedChanged(object sender, EventArgs e)
{
ContinueEnabled = continueButton.Enabled = continueTextBox.Enabled = continueCheckBox.Checked;
if (!ContinueEnabled && progressBar.Value != 0)
{
RecalculateImage();
progressBar.Value = OrigImgWH[0] * (OrigImgWH[1] - GetImageWH(1));
}
}
private void continueButton_Click(object sender, EventArgs e)
{
int enteredLayer = 0;
try
{
enteredLayer = Convert.ToInt32(continueTextBox.Text) - 1;
}
catch
{
Error(strings.invalidNum, false);
return;
}
if (enteredLayer < 1 || enteredLayer >= OrigImgWH[1])
{
Error($"{strings.validPicLayer}{OrigImgWH[1]}.", false);
return;
}
Task.Run(() =>
{
Invoke((Action)delegate { CropImage(enteredLayer); });
}
).ContinueWith(delegate
{
if (Application.OpenForms["previewForm"] is not null)
Invoke((Action)delegate { showPicAndPal.PerformClick(); });
}
);
}
private void continueTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
continueButton.PerformClick();
e.Handled = e.SuppressKeyPress = true;
}
}
public void MainForm_MouseClick(object sender, MouseEventArgs e)
{
if (!autoMinCheckBox.Enabled && autoMinCheckBox.Bounds.Contains(e.Location))
Warning(strings.hideEnabled, false);
}
private void russianLangItem_Click(object sender, EventArgs e)
{
if (Lang != "ru")
{
russianLangItem.Checked = true;
englishLangItem.Checked = false;
settings.lang = Lang = "ru";
settings.Save();
string warnStr = strings.ResourceManager.GetObject("langChangeReset", new CultureInfo("ru")) as string;
Warning(warnStr, false);
}
}
private void englishLangItem_Click(object sender, EventArgs e)
{
if (Lang != "en")
{
englishLangItem.Checked = true;
russianLangItem.Checked = false;
settings.lang = Lang = "en";
settings.Save();
string warnStr = strings.ResourceManager.GetObject("langChangeReset", new CultureInfo("en-US")) as string;
Warning(warnStr, false);
}
}
}
}