Exemple #1
0
    private void Update()
    {
        _doubleClicking = false;

        // push the button
        if (Input.GetButtonDown("Fire1"))
        {
            _firstPointOnScreen = Input.mousePosition;
            _firstLinePoint     = GetMousePositionOnPlane();
            //Instantiate(testPrefab, pos, Quaternion.identity);


            //// check if it is a double click
            //if(Time.time - _lastClickTime < timeForDoubleClick)
            //{
            //    _doubleClicking = true;
            //    // if double click we select/unselect group

            //    GameObject touchedObj = GetObjTouched();
            //    if (touchedObj != null && touchedObj.tag == "group" && !touchedObj.GetComponent<Groupe>().isSelected) // the touched object is a non-selected group
            //    {
            //        // get its controler
            //        Groupe controler = touchedObj.GetComponent<Groupe>();
            //        Groupe.Unselect(); // unselect already selected group
            //        controler.Select(); // select new one
            //    }
            //    else // touched object is not a group or is already selected
            //    {
            //        Groupe.Unselect(); // unselect already selected group
            //    }
            //}

            // update last click time
            _lastClickTime = Time.time;

            // we try to trace a line
            isTracingLine = true;
        }

        // release the left button
        if (Input.GetButtonUp("Fire1"))
        {
            // if we are tracing a line (it should be... )
            if (isTracingLine)
            {
                _secondPointOnScreen = Input.mousePosition;
                _secondLinePoint     = GetMousePositionOnPlane();


                isTracingLine = false;

                // check if minimum distance for slicing is reached -> slice
                if (CheckMinDistance()) // if we are slicing
                {
                    Debug.Log("over min distance, splitting objects");

                    Groupe slicedGroup = GetSlicedGroup();
                    if (slicedGroup != null)
                    {
                        Debug.Log("group found");

                        //ConstructLeftRightLists();
                        slicedGroup.SeparateGroup(_firstLinePoint, _secondLinePoint);
                    }
                    else
                    {
                        Debug.Log("no group found");
                    }
                    StartCoroutine(CreateTrail(_firstLinePoint, _secondLinePoint));
                }
                else // if slicing distance is not reach -> simple click
                {
                    // try to move selected group
                    GameObject touchedObj = GetObjTouched();
                    if (touchedObj != null)
                    {
                        Debug.Log("Objet non null");
                        Debug.Log("touched object: " + touchedObj.name);
                        if (touchedObj.tag == "group_2")
                        {
                            Debug.Log("Objet has tag group");

                            if (!touchedObj.GetComponent <Groupe>().isSelected)
                            {
                                Debug.Log("Objet is not selected");

// get its controler
                                Groupe controler = touchedObj.GetComponent <Groupe>();
                                Groupe.Unselect();  // unselect already selected group
                                controler.Select(); // select new one
                            }
                        }
                    }   // the touched object is a non-selected group
                    else // touched object is not a group or is already selected
                    {
                        Groupe.Unselect(); // unselect already selected group
                    }
                }
            }
        }

        // if pressing the right button
        if (Input.GetButtonUp("Fire2"))
        {
            GameObject objectUnderMouse = GetObjTouched();
            if (objectUnderMouse != null && objectUnderMouse.tag == "group_2" && Groupe.selectedObject != null)
            {
                // we hit a group, we try to merge the two groups

                Groupe clickedGroup = objectUnderMouse.GetComponent <Groupe>();
                if (clickedGroup == null)
                {
                    Debug.LogError("object with tag group has no groupe comp");
                }

                clickedGroup.MergeGroup(Groupe.selectedObject);
            }
            else  // if no group under mouse

            {
                Debug.Log("try to displace selected group");
                Vector3 newPos = GetMousePositionOnPlane();
                if (Groupe.selectedObject != null)
                {
                    Debug.Log("move selected group to " + newPos);
                    Groupe.ChangeSelectedPosition(newPos);
                }
                else
                {
                    Debug.Log("no group selected, no movement");
                }
            }
        }


        // TO SUPRESS
        if (Input.GetKeyDown(KeyCode.A))
        {
            Vector3    newPos = Input.mousePosition;
            GameObject cube   = GameObject.CreatePrimitive(PrimitiveType.Cube);
            cube.transform.position = newPos;
        }
    }