<commit_before>﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;

// ReSharper disable once CheckNamespace
namespace RegulatedNoise
{
    public partial class OcrCalibratorTab : UserControl
    {
        public OcrCalibratorTab()
        {
            InitializeComponent();
        }

        private CalibrationPoint _selCalibrationPoint;
        private bool _isMouseDown;
        private bool _drawPoints;

        private void OcrCalibratorTab_Load(object sender, EventArgs e)
        {
            SetResolutionValues();

            FillRawData();

            tb_uicolor.Text = Form1.RegulatedNoiseSettings.UiColour;
        }

        private void SetResolutionValues()
        {
            if (Form1.GameSettings.Display == null)
            {
                tb_resolution.Text = "ERROR";
            }
            else
            {
                tb_resolution.Text = Form1.GameSettings.Display.Resolution.X + "x" + Form1.GameSettings.Display.Resolution.Y;
            }
        }
        private void DoCalibration()
        {
            if (Form1.GameSettings.Display != null && (Form1.OcrCalibrator.CalibrationBoxes == null || Form1.OcrCalibrator.CalibrationBoxes.Count < 1))
            {
                var calibrations = Form1.OcrCalibrator.GetCalculatedCalibrationPoints(Form1.GameSettings.Display.Resolution);
                DrawCalibrationPoints(calibrations);
                return;
            }

            if (Form1.OcrCalibrator.CalibrationBoxes != null || Form1.OcrCalibrator.CalibrationBoxes.Count >= 1)
                return;

            MessageBox.Show("Unable to calibrate automatically, please calibrate manually...");
        }
        private void ManualCalibrate()
        {
            Form1.OcrCalibrator.CalibrationBoxes = new List<CalibrationPoint>();
            var p = new Point(30, 30);
            tb_description.Text = "Point 1: " + new CalibrationPoint().CalibrationDescriptions[0];
            for (var i = 0; i < 12; i++)
            {
                var cb = new CalibrationPoint(i, new Point(p.X+(50*(i+1)), p.Y));
                Form1.OcrCalibrator.CalibrationBoxes.Add(cb);
            }
            FillRawData();
            _drawPoints = true;
            pb_calibratorBox.Refresh();
        }

        private void FillRawData()
        {
            if (Form1.OcrCalibrator.CalibrationBoxes == null || Form1.OcrCalibrator.CalibrationBoxes.Count < 1)
                return;
            tb_rawdata.Text = string.Join(Environment.NewLine, Form1.OcrCalibrator.CalibrationBoxes.Select(p => p.Position.X + ";" + p.Position.Y));
        }

        private Bitmap getReferenceScreenshot(bool necessarily)
        {
            var openFile = new OpenFileDialog
            {
                DefaultExt = "bmp",
                Multiselect = true,
                Filter = "BMP (*.bmp)|*.bmp",
                InitialDirectory =
                    Environment.GetFolderPath((Environment.SpecialFolder.MyPictures)) +
                    @"\Frontier Developments\Elite Dangerous",
                Title =
                    necessarily
                        ? "Open a screenshot for calibration verification? (not essential)"
                        : "Open a screenshot for calibration"
            };

            if (openFile.ShowDialog() == DialogResult.OK)
            {
                var bmp = new Bitmap(openFile.FileName);

                if (bmp.Height == Form1.GameSettings.Display.Resolution.Y &&
                    bmp.Width == Form1.GameSettings.Display.Resolution.X) return bmp;
                var wrongres = MessageBox.Show("The selected image has a different resolution from your current game settings. Do you want to pick another image?", "Ooops...", MessageBoxButtons.YesNo);
                return wrongres == DialogResult.Yes ? getReferenceScreenshot(necessarily) : bmp;
            }
            return getReferenceScreenshot(necessarily);
        }

        private Bitmap _refbmp;

        private void btn_calibrate_Click(object sender, EventArgs e)
        {
            _drawPoints = false;
            DoCalibration();

            if (_refbmp != null)
                _refbmp.Dispose();

            _refbmp = getReferenceScreenshot(true);
<commit_msg>Fix Manual calibration doesn't let you set the 12th point correctly, according to user feedback. #39<commit_after>﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;

// ReSharper disable once CheckNamespace
namespace RegulatedNoise
{
    public partial class OcrCalibratorTab : UserControl
    {
        public OcrCalibratorTab()
        {
            InitializeComponent();
        }

        private CalibrationPoint _selCalibrationPoint;
        private bool _isMouseDown;
        private bool _drawPoints;

        private void OcrCalibratorTab_Load(object sender, EventArgs e)
        {
            SetResolutionValues();

            FillRawData();

            tb_uicolor.Text = Form1.RegulatedNoiseSettings.UiColour;
        }

        private void SetResolutionValues()
        {
            if (Form1.GameSettings.Display == null)
            {
                tb_resolution.Text = "ERROR";
            }
            else
            {
                tb_resolution.Text = Form1.GameSettings.Display.Resolution.X + "x" + Form1.GameSettings.Display.Resolution.Y;
            }
        }
        private void DoCalibration()
        {
            if (Form1.GameSettings.Display != null && (Form1.OcrCalibrator.CalibrationBoxes == null || Form1.OcrCalibrator.CalibrationBoxes.Count < 1))
            {
                var calibrations = Form1.OcrCalibrator.GetCalculatedCalibrationPoints(Form1.GameSettings.Display.Resolution);
                DrawCalibrationPoints(calibrations);
                return;
            }

            if (Form1.OcrCalibrator.CalibrationBoxes != null || Form1.OcrCalibrator.CalibrationBoxes.Count >= 1)
                return;

            MessageBox.Show("Unable to calibrate automatically, please calibrate manually...");
        }
        private void ManualCalibrate()
        {
            Form1.OcrCalibrator.CalibrationBoxes = new List<CalibrationPoint>();
            var p = new Point(30, 30);
            tb_description.Text = "Point 1: " + new CalibrationPoint().CalibrationDescriptions[0];
            for (var i = 0; i < 12; i++)
            {
                var cb = new CalibrationPoint(i, new Point(p.X+(50*(i+1)), p.Y));
                Form1.OcrCalibrator.CalibrationBoxes.Add(cb);
            }
            FillRawData();
            _drawPoints = true;
            pb_calibratorBox.Refresh();
        }

        private void FillRawData()
        {
            if (Form1.OcrCalibrator.CalibrationBoxes == null || Form1.OcrCalibrator.CalibrationBoxes.Count < 1)
                return;
            tb_rawdata.Text = string.Join(Environment.NewLine, Form1.OcrCalibrator.CalibrationBoxes.Select(p => p.Position.X + ";" + p.Position.Y));
        }

        private Bitmap getReferenceScreenshot(bool necessarily)
        {
            var openFile = new OpenFileDialog
            {
                DefaultExt = "bmp",
                Multiselect = true,
                Filter = "BMP (*.bmp)|*.bmp",
                InitialDirectory =
                    Environment.GetFolderPath((Environment.SpecialFolder.MyPictures)) +
                    @"\Frontier Developments\Elite Dangerous",
                Title =
                    necessarily
                        ? "Open a screenshot for calibration verification? (not essential)"
                        : "Open a screenshot for calibration"
            };

            if (openFile.ShowDialog() == DialogResult.OK)
            {
                var bmp = new Bitmap(openFile.FileName);

                if (bmp.Height == Form1.GameSettings.Display.Resolution.Y &&
                    bmp.Width == Form1.GameSettings.Display.Resolution.X) return bmp;
                var wrongres = MessageBox.Show("The selected image has a different resolution from your current game settings. Do you want to pick another image?", "Ooops...", MessageBoxButtons.YesNo);
                if (wrongres == DialogResult.Yes)
                {
                    return getReferenceScreenshot(necessarily);
                }
                
                // Force resolution from input bmp
                Form1.GameSettings.Display.ScreenHeight = bmp.Height;
                Form1.GameSettings.Display.ScreenWidth = bmp.Width;
                SetResolutionValues();
                var calibrations = Form1.OcrCalibrator.GetCalculatedCalibrationPoints(Form1.GameSettings.Display.Resolution);
                DrawCalibrationPoints(calibrations);

                return bmp;
            }
            return getReferenceScreenshot(necessarily);
        }

        private Bitmap _refbmp;

        private void btn_calibrate_Click(object sender, EventArgs e)
        {
            _drawPoints = false;
            DoCalibration();

            if (_refbmp != null)
                _refbmp.Dispose();

            _refbmp = getReferenceScreenshot(true);

{'fraction_non_alphanumeric': 0.08817549491706796, 'fraction_numerical': 0.008132691278758694, 'mean_word_length': 1.830405814657783, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4036829', 'n_tokens_mistral': 2565, 'n_tokens_neox': 2400, 'n_words': 499}