﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MouseDraw : MonoBehaviour
{
    private LineRenderer line;
    private bool mousePressed;
    public List<Vector3> pointsList;
    private Vector3 mousePosition;

    struct MyLine
    {
        public Vector3 StartPoint;
        public Vector3 EndPoint;
    }

    void Awake()
    {
        line = gameObject.AddComponent<LineRenderer>();
        line.material = new Material(Shader.Find("GUI/Text Shader"));

        line.SetVertexCount(0);
        line.SetWidth(0.1f, 0.1f);
        line.SetColors(Color.green, Color.green);

        line.useWorldSpace = true;
        mousePressed = false;
        pointsList = new List<Vector3>();
    }

    // Use this for initialization
    void Start ()
    {
	
	}
	
	// Update is called once per frame
	void Update ()
    {
        if (Input.GetMouseButtonDown(0))
        {
            mousePressed = true;
            line.SetVertexCount(0);
            pointsList.RemoveRange(0, pointsList.Count);
            line.SetColors(Color.blue, Color.blue);
        }
        if (Input.GetMouseButtonUp(0))
        {
            mousePressed = false;
        }

        if (mousePressed)
        {
            mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            mousePosition.z = -8;
            if (!pointsList.Contains(mousePosition))
            {
                pointsList.Add(mousePosition);
                line.SetVertexCount(pointsList.Count);
                line.SetPosition(pointsList.Count - 1, (Vector3)pointsList[pointsList.Count - 1]);
            }
        }
    }
}
