using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace OperateCamera
{
    public partial class Form_Display : Form
    {
        private float X2;//The width of the current form
        private float Y2;//The height of the current form

        public Form_Display()
        {
            InitializeComponent();
        }

        private void Form_Display_Resize(object sender, EventArgs e)
        {
            float newx2 = (this.Width) / X2; //Scale scaling of form
            float newy2 = (this.Height) / Y2;//Height scaling of the form
            setControls2(newx2, newy2, this);//Change the size of the control with the form
        }

        private void Form_Display_Load(object sender, EventArgs e)
        {
            X2 = this.Width;//Get the width of the form
            Y2 = this.Height;//Get the height of the form
            setTag2(this);//Invocation method
        }

        private void setTag2(Control cons2)
        {
            foreach (Control con2 in cons2.Controls)
            {
                con2.Tag = con2.Width + ":" + con2.Height + ":" + con2.Left + ":" + con2.Top + ":" + con2.Font.Size;
                if (con2.Controls.Count > 0)
                    setTag2(con2);
            }
        }

        //Adjust the size of the control according to the size of the form
        private void setControls2(float newx2, float newy2, Control cons2)
        {
            //Traversing the controls in the form, resetting the value of the control
            foreach (Control con2 in cons2.Controls)
            {
                string[] mytag2 = con2.Tag.ToString().Split(new char[] { ':' });//Get the Tag attribute value of the control and store the array of strings after splitting
                float a2 = System.Convert.ToSingle(mytag2[0]) * newx2;//Determine the value of the control according to the scale scale of the form, the width of the control
                con2.Width = (int)a2;//width
                a2 = System.Convert.ToSingle(mytag2[1]) * newy2;//height
                con2.Height = (int)(a2);
                a2 = System.Convert.ToSingle(mytag2[2]) * newx2;//Left distance
                con2.Left = (int)(a2);
                a2 = System.Convert.ToSingle(mytag2[3]) * newy2;//Upper edge distance
                con2.Top = (int)(a2);
                Single currentSize2 = System.Convert.ToSingle(mytag2[4]) * newy2;//font size
                con2.Font = new Font(con2.Font.Name, currentSize2, con2.Font.Style, con2.Font.Unit);
                if (con2.Controls.Count > 0)
                {
                    setControls2(newx2, newy2, con2);
                }
            }
        }

        void DrawPoint()
        {
            OpenCvSharp.Mat tempCalib = Form_TIASAutomaticShootingSystem.m_CalibFrame.Clone();

            //とってくる点の描画
            //tempDrawに書き込んでいるので，degree.bmpは無事．とってくるのは
            //この画像のRGBにする．
            //tempDrawにとってくる点を全て描画し，pointcheck.bmpで確認できるようにする．
            //オッケーなら校正ボタンを再度押す感じで．

            //以降取ってきた点の描画．座標(実際値)
            //1
            Form_TIASAutomaticShootingSystem.getRGBpoint[0].X = Form_TIASAutomaticShootingSystem.pt[0].X - 700;
            Form_TIASAutomaticShootingSystem.getRGBpoint[0].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 50;
            //2
            Form_TIASAutomaticShootingSystem.getRGBpoint[1].X = Form_TIASAutomaticShootingSystem.pt[0].X - 700;
            Form_TIASAutomaticShootingSystem.getRGBpoint[1].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 250;
            //3
            Form_TIASAutomaticShootingSystem.getRGBpoint[2].X = Form_TIASAutomaticShootingSystem.pt[0].X - 700;
            Form_TIASAutomaticShootingSystem.getRGBpoint[2].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 450;
            //4
            Form_TIASAutomaticShootingSystem.getRGBpoint[3].X = Form_TIASAutomaticShootingSystem.pt[0].X - 700;
            Form_TIASAutomaticShootingSystem.getRGBpoint[3].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 650;
            //5
            Form_TIASAutomaticShootingSystem.getRGBpoint[4].X = Form_TIASAutomaticShootingSystem.pt[0].X - 700;
            Form_TIASAutomaticShootingSystem.getRGBpoint[4].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 850;
            //6
            Form_TIASAutomaticShootingSystem.getRGBpoint[5].X = Form_TIASAutomaticShootingSystem.pt[0].X - 700;
            Form_TIASAutomaticShootingSystem.getRGBpoint[5].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 1050;
            //7
            Form_TIASAutomaticShootingSystem.getRGBpoint[6].X = Form_TIASAutomaticShootingSystem.pt[0].X - 500;
            Form_TIASAutomaticShootingSystem.getRGBpoint[6].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 50;
            //8
            Form_TIASAutomaticShootingSystem.getRGBpoint[7].X = Form_TIASAutomaticShootingSystem.pt[0].X - 500;
            Form_TIASAutomaticShootingSystem.getRGBpoint[7].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 250;
            //9
            Form_TIASAutomaticShootingSystem.getRGBpoint[8].X = Form_TIASAutomaticShootingSystem.pt[0].X - 500;
            Form_TIASAutomaticShootingSystem.getRGBpoint[8].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 450;
            //10
            Form_TIASAutomaticShootingSystem.getRGBpoint[9].X = Form_TIASAutomaticShootingSystem.pt[0].X - 500;
            Form_TIASAutomaticShootingSystem.getRGBpoint[9].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 650;
            //11
            Form_TIASAutomaticShootingSystem.getRGBpoint[10].X = Form_TIASAutomaticShootingSystem.pt[0].X - 500;
            Form_TIASAutomaticShootingSystem.getRGBpoint[10].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 850;
            //12
            Form_TIASAutomaticShootingSystem.getRGBpoint[11].X = Form_TIASAutomaticShootingSystem.pt[0].X - 500;
            Form_TIASAutomaticShootingSystem.getRGBpoint[11].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 1050;
            //13
            Form_TIASAutomaticShootingSystem.getRGBpoint[12].X = Form_TIASAutomaticShootingSystem.pt[0].X - 300;
            Form_TIASAutomaticShootingSystem.getRGBpoint[12].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 50;
            //14
            Form_TIASAutomaticShootingSystem.getRGBpoint[13].X = Form_TIASAutomaticShootingSystem.pt[0].X - 300;
            Form_TIASAutomaticShootingSystem.getRGBpoint[13].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 250;
            //15
            Form_TIASAutomaticShootingSystem.getRGBpoint[14].X = Form_TIASAutomaticShootingSystem.pt[0].X - 300;
            Form_TIASAutomaticShootingSystem.getRGBpoint[14].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 450;
            //16
            Form_TIASAutomaticShootingSystem.getRGBpoint[15].X = Form_TIASAutomaticShootingSystem.pt[0].X - 300;
            Form_TIASAutomaticShootingSystem.getRGBpoint[15].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 650;
            //17
            Form_TIASAutomaticShootingSystem.getRGBpoint[16].X = Form_TIASAutomaticShootingSystem.pt[0].X - 300;
            Form_TIASAutomaticShootingSystem.getRGBpoint[16].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 850;
            //18
            Form_TIASAutomaticShootingSystem.getRGBpoint[17].X = Form_TIASAutomaticShootingSystem.pt[0].X - 300;
            Form_TIASAutomaticShootingSystem.getRGBpoint[17].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 1050;
            //19
            Form_TIASAutomaticShootingSystem.getRGBpoint[18].X = Form_TIASAutomaticShootingSystem.pt[0].X - 100;
            Form_TIASAutomaticShootingSystem.getRGBpoint[18].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 50;
            //20
            Form_TIASAutomaticShootingSystem.getRGBpoint[19].X = Form_TIASAutomaticShootingSystem.pt[0].X - 100;
            Form_TIASAutomaticShootingSystem.getRGBpoint[19].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 250;
            //21
            Form_TIASAutomaticShootingSystem.getRGBpoint[20].X = Form_TIASAutomaticShootingSystem.pt[0].X - 100;
            Form_TIASAutomaticShootingSystem.getRGBpoint[20].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 450;
            //22
            Form_TIASAutomaticShootingSystem.getRGBpoint[21].X = Form_TIASAutomaticShootingSystem.pt[0].X - 100;
            Form_TIASAutomaticShootingSystem.getRGBpoint[21].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 650;
            //23
            Form_TIASAutomaticShootingSystem.getRGBpoint[22].X = Form_TIASAutomaticShootingSystem.pt[0].X - 100;
            Form_TIASAutomaticShootingSystem.getRGBpoint[22].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 850;
            //24
            Form_TIASAutomaticShootingSystem.getRGBpoint[23].X = Form_TIASAutomaticShootingSystem.pt[0].X - 100;
            Form_TIASAutomaticShootingSystem.getRGBpoint[23].Y = Form_TIASAutomaticShootingSystem.pt[0].Y + 1050;

            for (int i = 0; i < 24; i++)
            {
                OpenCvSharp.Cv2.Circle(tempCalib, Form_TIASAutomaticShootingSystem.getRGBpoint[i].X, Form_TIASAutomaticShootingSystem.getRGBpoint[i].Y, 10, new OpenCvSharp.Scalar(0, 255, 0), -1, OpenCvSharp.LineTypes.Link8, 0);
            }

            Form_TIASAutomaticShootingSystem.bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(tempCalib);
            this.pictureBox1.Image = Form_TIASAutomaticShootingSystem.bitmap;
            this.pictureBox1.Refresh();
            Form_TIASAutomaticShootingSystem.m_getColor = true;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (Form_TIASAutomaticShootingSystem.m_bCalib)//キャリブレーション時しか必要ないので
            {
                Form_TIASAutomaticShootingSystem.pt[Form_TIASAutomaticShootingSystem.click].X = e.X;
                Form_TIASAutomaticShootingSystem.pt[Form_TIASAutomaticShootingSystem.click].Y = e.Y;

                //2点クリックすんでいたら
                if (Form_TIASAutomaticShootingSystem.pt[0].X != 0)
                {
                    if (Form_TIASAutomaticShootingSystem.pt[0].Y != 0)
                    {
                        Form_TIASAutomaticShootingSystem.wait3 = 0;
                        System.Threading.Thread.Sleep(500);
                        //描画用関数に飛ぶ
                        Form_TIASAutomaticShootingSystem.pt[0].X = Form_TIASAutomaticShootingSystem.pt[0].X * 2;
                        Form_TIASAutomaticShootingSystem.pt[0].Y = Form_TIASAutomaticShootingSystem.pt[0].Y * 2;
                        DrawPoint();
                        this.GuideBox.Text = "";
                        this.GuideBox.Text = "Please click Color Correction Button again！";
                        this.ShootingGuideBox.Text = "";
                        this.ShootingGuideBox.Text = "DON'T enlarge the window!";
                    }
                }
            }
        }
    }
}
