Example #1
0
    public void OnSceneGUI()
    {
        if (roadCreator == null)
        {
            roadCreator = (RoadCreator)target;
        }

        if (roadCreator.transform.hasChanged)
        {
            roadCreator.transform.rotation   = Quaternion.identity;
            roadCreator.transform.localScale = Vector3.one;
            roadCreator.CreateMesh();
            roadCreator.transform.hasChanged = false;
        }

        guiEvent = Event.current;

        Ray        ray = HandleUtility.GUIPointToWorldRay(guiEvent.mousePosition);
        RaycastHit raycastHit;

        if (Physics.Raycast(ray, out raycastHit, 100f, 1 << LayerMask.NameToLayer("Ignore Mouse Ray")))
        {
            if (guiEvent.type == EventType.MouseDown && guiEvent.button == 0)
            {
                roadCreator.DivideRoad(raycastHit);
            }
        }

        if (Physics.Raycast(ray, out raycastHit, 100f, ~(1 << LayerMask.NameToLayer("Ignore Mouse Ray"))))
        {
            hitPosition = raycastHit.point;

            if (guiEvent.control == true)
            {
                Vector3 nearestGuideline = Misc.GetNearestGuidelinePoint(hitPosition);
                if (nearestGuideline != Misc.MaxVector3)
                {
                    hitPosition = new Vector3(nearestGuideline.x, hitPosition.y, nearestGuideline.z);
                }
                else
                {
                    hitPosition = new Vector3(Mathf.Round(hitPosition.x), hitPosition.y, Mathf.Round(hitPosition.z));
                }
            }

            if (guiEvent.type == EventType.MouseDown)
            {
                if (guiEvent.button == 0)
                {
                    if (guiEvent.shift == true)
                    {
                        CreatePoints();
                    }
                }
                else if (guiEvent.button == 1 && guiEvent.shift == true)
                {
                    RemovePoints();
                }
            }
            else if (guiEvent.type == EventType.KeyDown)
            {
                if (guiEvent.keyCode == KeyCode.S)
                {
                    roadCreator.sDown = true;
                }
                else if (guiEvent.keyCode == KeyCode.P)
                {
                    roadCreator.pDown = true;
                }
                else if (guiEvent.keyCode == KeyCode.A)
                {
                    roadCreator.aDown = true;
                }
                else if (guiEvent.keyCode == KeyCode.D)
                {
                    roadCreator.dDown = true;
                }
            }
            else if (guiEvent.type == EventType.KeyUp)
            {
                if (guiEvent.keyCode == KeyCode.S)
                {
                    roadCreator.sDown = false;
                }
                else if (guiEvent.keyCode == KeyCode.P)
                {
                    roadCreator.pDown = false;
                }
                else if (guiEvent.keyCode == KeyCode.A)
                {
                    roadCreator.aDown = false;
                }
                else if (guiEvent.keyCode == KeyCode.D)
                {
                    roadCreator.dDown = false;
                }
            }

            if (roadCreator.transform.GetChild(0).childCount > 0 && roadCreator.transform.GetChild(0).GetChild(roadCreator.transform.GetChild(0).childCount - 1).GetChild(0).childCount == 2 && (guiEvent.type == EventType.MouseDrag || guiEvent.type == EventType.MouseMove || guiEvent.type == EventType.MouseDown))
            {
                points = CalculateTemporaryPoints(hitPosition);
            }

            if (roadCreator.transform.childCount > 0)
            {
                Draw(hitPosition);
            }
        }

        if (Physics.Raycast(ray, out raycastHit, 100f, ~(1 << LayerMask.NameToLayer("Road") | 1 << LayerMask.NameToLayer("Prefab Line"))))
        {
            hitPosition = raycastHit.point;

            if (guiEvent.control == true)
            {
                Vector3 nearestGuideline = Misc.GetNearestGuidelinePoint(hitPosition);
                if (nearestGuideline != Misc.MaxVector3)
                {
                    hitPosition = new Vector3(nearestGuideline.x, hitPosition.y, nearestGuideline.z);
                }
                else
                {
                    hitPosition = new Vector3(Mathf.Round(hitPosition.x), hitPosition.y, Mathf.Round(hitPosition.z));
                }
            }

            if (guiEvent.shift == false)
            {
                MovePoints(raycastHit);
            }
            else
            {
                hitPosition = Misc.MaxVector3;
                MovePoints(raycastHit);
            }
        }

        if (guiEvent.type == EventType.MouseDown && Physics.Raycast(ray, out raycastHit, 100f, (1 << LayerMask.NameToLayer("Road"))))
        {
            hitPosition = raycastHit.point;

            if (guiEvent.button == 2)
            {
                if (raycastHit.transform.parent.parent.GetComponent <RoadSegment>() != null)
                {
                    roadCreator.segmentToCopy = raycastHit.transform.parent.parent.GetComponent <RoadSegment>();
                }
            }
            else if (guiEvent.button == 0)
            {
                if (guiEvent.control == true)
                {
                    Vector3 nearestGuideline = Misc.GetNearestGuidelinePoint(hitPosition);
                    if (nearestGuideline != Misc.MaxVector3)
                    {
                        hitPosition = new Vector3(nearestGuideline.x, hitPosition.y, nearestGuideline.z);
                    }
                    else
                    {
                        hitPosition = new Vector3(Mathf.Round(hitPosition.x), hitPosition.y, Mathf.Round(hitPosition.z));
                    }
                }

                roadCreator.InsertSegment(hitPosition, raycastHit);
            }
        }

        roadCreator.transform.parent.GetComponent <RoadSystem>().ShowCreationButtons();
        SceneView.currentDrawingSceneView.Repaint();
    }