﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TP5
{
    class BiPoint
    {
        public Point debut;
        public Point fin;
        public BiPoint(Point debut, Point fin)
        {
            this.debut = debut;
            this.fin = fin;
        }
    }
    internal class ZoneDessin4 : Control
    {
        Point p_deb;
        Point p_fin;
        Point p_tmp;
        Pen tmpPen = new Pen(Color.LightGreen);
        Pen myPen = new Pen(Color.DarkBlue);

        private bool isDrawing = false;

        List<BiPoint> lignes = new List<BiPoint>();

        public ZoneDessin4() : base()
        {
            this.Location = new System.Drawing.Point(50, 50);
            this.Size = new System.Drawing.Size(400, 400);
            this.DoubleBuffered = true;//Pour éviter le scintillement
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            this.p_deb = new Point(e.Location.X, e.Location.Y);
            this.isDrawing = true;
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            this.p_fin = new Point(e.Location.X, e.Location.Y);
            BiPoint biPoint = new BiPoint(p_deb,p_fin);
            lignes.Add(biPoint);
            this.isDrawing = false;
            this.Invalidate();
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (this.isDrawing)
            {
                this.p_tmp = new Point(e.Location.X, e.Location.Y);
                this.Invalidate();
            }
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            //On commence par tout effacer
            e.Graphics.Clear(Color.White);

            //On dessine
            if (isDrawing)
            {

                e.Graphics.DrawLine(tmpPen, this.p_deb, this.p_tmp);

                foreach (BiPoint biPoint in lignes)
                {
                    e.Graphics.DrawLine(myPen, biPoint.debut, biPoint.fin);
                }
            }
            else
            {
                foreach (BiPoint biPoint in lignes)
                {
                    e.Graphics.DrawLine(myPen, biPoint.debut, biPoint.fin);
                }
            }
        }

    }
}
